Creada la clase simBase

This commit is contained in:
Miguel 2024-05-18 17:42:27 +02:00
parent 2e8c3b7d83
commit f458a031c5
2 changed files with 98 additions and 95 deletions

View File

@ -213,9 +213,9 @@ namespace CtrEditor.ObjetosSim
// crear el objeto de simulacion
ActualizarLeftTop();
simulationManager.rectangles.Add(TransporteCentral);
simulationManager.lines.Add(Guia_Superior);
simulationManager.lines.Add(Guia_Inferior);
//simulationManager.rectangles.Add(TransporteCentral);
//simulationManager.lines.Add(Guia_Superior);
//simulationManager.lines.Add(Guia_Inferior);
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion

View File

@ -18,13 +18,29 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CtrEditor.Simulacion
{
public class simDescarte
public class simBase
{
public Body Body { get; private set; }
private float _radius;
public Body Body { get; protected set; }
public World _world;
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
}
}
public class simDescarte : simBase
{
private float _radius;
public simDescarte(World world, float diameter, Vector2 position)
{
_world = world;
@ -32,23 +48,12 @@ namespace CtrEditor.Simulacion
Create(position);
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
}
public void SetDiameter(float diameter)
{
_radius = diameter / 2;
Create(Body.Position); // Recrear el círculo con el nuevo tamaño
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(Vector2 position)
{
RemoverBody();
@ -60,11 +65,10 @@ namespace CtrEditor.Simulacion
}
}
public class simTransporte
public class simTransporte : simBase
{
public Body Body { get; private set; }
public float Speed { get; set; } // Velocidad para efectos de cinta transportadora
public World _world;
public simTransporte(World world, float width, float height, Vector2 position, float angle = 0)
{
@ -78,7 +82,7 @@ namespace CtrEditor.Simulacion
set { Body.Rotation = MathHelper.ToRadians(value); }
}
public void SetPosition(float x, float y)
public new void SetPosition(float x, float y)
{
Body.Position = new Vector2(x, y);
}
@ -95,13 +99,6 @@ namespace CtrEditor.Simulacion
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
Body.CreateFixture(newShape);
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(float width, float height, Vector2 position, float angle = 0)
{
RemoverBody();
@ -113,23 +110,55 @@ namespace CtrEditor.Simulacion
}
}
public class simGuia
public class simBarrera : simBase
{
public Body Body { get; private set; }
public World _world;
public bool LuzCortada = false;
public simBarrera(World world, float width, float height, Vector2 position, float angle = 0)
{
_world = world;
Create(width, height, position, angle);
}
public float Angle
{
get { return MathHelper.ToDegrees(Body.Rotation); }
set { Body.Rotation = MathHelper.ToRadians(value); }
}
public new void SetPosition(float x, float y)
{
Body.Position = new Vector2(x, y);
}
public void SetDimensions(float width, float height)
{
Body.DestroyFixture(Body.FixtureList[0]);
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
Body.CreateFixture(newShape);
}
public void Create(float width, float height, Vector2 position, float angle = 0)
{
RemoverBody();
Body = BodyFactory.CreateRectangle(_world, width, height, 1f, position);
Body.FixtureList[0].IsSensor = true;
Body.BodyType = BodyType.Static;
Body.Rotation = MathHelper.ToRadians(angle);
Body.UserData = this; // Importante para la identificación durante la colisión
LuzCortada = false;
}
}
public class simGuia : simBase
{
public simGuia(World world, Vector2 start, Vector2 end)
{
_world = world;
Create(start, end);
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
public void Create(Vector2 start, Vector2 end)
{
RemoverBody();
@ -144,10 +173,8 @@ namespace CtrEditor.Simulacion
}
}
public class simBotella
public class simBotella : simBase
{
public Body Body { get; private set; }
public World _world;
private float _radius;
private float _mass;
public bool Descartar = false;
@ -187,13 +214,7 @@ namespace CtrEditor.Simulacion
}
set { _mass = value; }
}
public void RemoverBody()
{
if (Body != null)
{
_world.RemoveBody(Body);
}
}
private void Create(Vector2 position)
{
RemoverBody();
@ -202,7 +223,7 @@ namespace CtrEditor.Simulacion
// Restablecer manejador de eventos de colisión
Body.OnCollision += HandleCollision;
//Body.OnSeparation += HandleOnSeparation;
Body.OnSeparation += HandleOnSeparation;
Body.UserData = this; // Importante para la identificación durante la colisión
@ -216,11 +237,6 @@ namespace CtrEditor.Simulacion
Body.IsBullet = true;
}
public void SetPosition(float x, float y)
{
Body.SetTransform(new Vector2(x, y), Body.Rotation);
}
public void SetDiameter(float diameter)
{
_radius = diameter / 2;
@ -234,7 +250,12 @@ namespace CtrEditor.Simulacion
private bool HandleCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
if (fixtureB.Body.UserData is simDescarte)
if (fixtureB.Body.UserData is simBarrera Sensor)
{
Sensor.LuzCortada = true;
return true;
}
else if (fixtureB.Body.UserData is simDescarte)
{
Descartar = true;
return true;
@ -274,7 +295,8 @@ namespace CtrEditor.Simulacion
private void HandleOnSeparation(Fixture fixtureA, Fixture fixtureB)
{
// Aquí puedes restablecer cualquier estado si es necesario al separarse de un simRectangle
if (fixtureB.Body.UserData is simBarrera Sensor)
Sensor.LuzCortada = false;
}
private void ApplyConveyorEffect(simTransporte conveyor, Fixture circleFixture, float porcentajeCompartido)
@ -289,10 +311,8 @@ namespace CtrEditor.Simulacion
{
private World world;
private Canvas simulationCanvas;
public List<simBotella> circles;
public List<simTransporte> rectangles;
public List<simGuia> lines;
public List<simDescarte> descartes;
public List<simBase> Cuerpos;
public Stopwatch stopwatch;
public Canvas DebugCanvas { get => simulationCanvas; set => simulationCanvas = value; }
@ -300,18 +320,13 @@ namespace CtrEditor.Simulacion
public SimulationManagerFP()
{
world = new World(new Vector2(0, 0)); // Vector2.Zero
circles = new List<simBotella>();
rectangles = new List<simTransporte>();
lines = new List<simGuia>();
descartes = new List<simDescarte>();
Cuerpos = new List<simBase>();
stopwatch = new Stopwatch();
}
public void Clear()
{
circles.Clear();
rectangles.Clear();
lines.Clear();
Cuerpos.Clear();
world.Clear();
}
@ -328,56 +343,44 @@ namespace CtrEditor.Simulacion
world.Step(elapsedMilliseconds / 1000.0f);
}
public void Remove(object Objeto)
public void Remove(simBase Objeto)
{
switch (Objeto)
{
case simBotella obj:
obj.RemoverBody();
circles.Remove(obj);
break;
case simTransporte obj:
obj.RemoverBody();
rectangles.Remove(obj);
break;
case simGuia obj:
obj.RemoverBody();
lines.Remove(obj);
break;
case simDescarte obj:
obj.RemoverBody();
descartes.Remove(obj);
break;
default:
throw new InvalidOperationException("Tipo no soportado");
}
Objeto.RemoverBody();
Cuerpos.Remove(Objeto);
}
public simBotella AddCircle(float diameter, Vector2 position, float mass)
{
simBotella circle = new simBotella(world, diameter, position, mass);
circles.Add(circle);
Cuerpos.Add(circle);
return circle;
}
public simTransporte AddRectangle(float width, float height, Vector2 position, float angle)
{
simTransporte rectangle = new simTransporte(world, width, height, position, angle);
rectangles.Add(rectangle);
Cuerpos.Add(rectangle);
return rectangle;
}
public simTransporte AddBarrera(float width, float height, Vector2 position, float angle)
{
simTransporte rectangle = new simTransporte(world, width, height, position, angle);
Cuerpos.Add(rectangle);
return rectangle;
}
public simGuia AddLine(Vector2 start, Vector2 end)
{
simGuia line = new simGuia(world, start, end);
lines.Add(line);
Cuerpos.Add(line);
return line;
}
public simDescarte AddDescarte(float diameter, Vector2 position)
{
simDescarte descarte = new simDescarte(world, diameter, position);
descartes.Add(descarte);
Cuerpos.Add(descarte);
return descarte;
}