Incluyendo Trasnporte Curve y la biblioteca CommunityToolkit.Mvvm

This commit is contained in:
Miguel 2024-05-21 12:52:44 +02:00
parent 334b1a2fd8
commit f090722de0
18 changed files with 289 additions and 144 deletions

View File

@ -12,13 +12,22 @@
<Compile Remove="ObjetosSim\ucBasicExample.xaml.cs" /> <Compile Remove="ObjetosSim\ucBasicExample.xaml.cs" />
<Compile Remove="ObjetosSim\ucTransporteCurva.xaml.cs" /> <Compile Remove="ObjetosSim\ucTransporteCurva.xaml.cs" />
<Compile Remove="ObjetosSim\UserControls\ucBasicExample.xaml.cs" /> <Compile Remove="ObjetosSim\UserControls\ucBasicExample.xaml.cs" />
<Compile Remove="ObjetosSim\UserControls\ucTransporteCurva.xaml.cs" />
<Compile Remove="Simulacion\GeometrySimulator.cs" /> <Compile Remove="Simulacion\GeometrySimulator.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Remove="app2.png" />
<None Remove="CtrEditorE.png" />
<None Remove="Icons\app.256x256.ico" />
<None Remove="Icons\app.png" />
<None Remove="Icons\app2.128x128.ico" />
<None Remove="Icons\app2.256x256.ico" />
<None Remove="Icons\app2.png" />
<None Remove="Icons\borrar.png" /> <None Remove="Icons\borrar.png" />
<None Remove="Icons\connect.png" /> <None Remove="Icons\connect.png" />
<None Remove="Icons\CtrEditorA.png" />
<None Remove="Icons\CtrEditorC.png" />
<None Remove="Icons\CtrEditorE.png" />
<None Remove="Icons\disconnect.png" /> <None Remove="Icons\disconnect.png" />
<None Remove="Icons\duplicate.png" /> <None Remove="Icons\duplicate.png" />
<None Remove="Icons\fotocelula.png" /> <None Remove="Icons\fotocelula.png" />
@ -36,18 +45,16 @@
<Page Remove="ObjetosSim\ucBasicExample.xaml" /> <Page Remove="ObjetosSim\ucBasicExample.xaml" />
<Page Remove="ObjetosSim\ucTransporteCurva.xaml" /> <Page Remove="ObjetosSim\ucTransporteCurva.xaml" />
<Page Remove="ObjetosSim\UserControls\ucBasicExample.xaml" /> <Page Remove="ObjetosSim\UserControls\ucBasicExample.xaml" />
<Page Remove="ObjetosSim\UserControls\ucTransporteCurva.xaml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="ObjetosSim\UserControls\ucBasicExample.xaml" /> <None Include="ObjetosSim\UserControls\ucBasicExample.xaml" />
<None Include="ObjetosSim\UserControls\ucBasicExample.xaml.cs" /> <None Include="ObjetosSim\UserControls\ucBasicExample.xaml.cs" />
<None Include="ObjetosSim\UserControls\ucTransporteCurva.xaml" />
<None Include="ObjetosSim\UserControls\ucTransporteCurva.xaml.cs" />
<None Include="Simulacion\GeometrySimulator.cs" /> <None Include="Simulacion\GeometrySimulator.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="FarseerPhysics" Version="3.5.0" /> <PackageReference Include="FarseerPhysics" Version="3.5.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
@ -62,8 +69,15 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Resource Include="app2.png" />
<Resource Include="CtrEditorE.png" />
<Resource Include="Icons\app.png" />
<Resource Include="Icons\app2.png" />
<Resource Include="Icons\borrar.png" /> <Resource Include="Icons\borrar.png" />
<Resource Include="Icons\connect.png" /> <Resource Include="Icons\connect.png" />
<Resource Include="Icons\CtrEditorA.png" />
<Resource Include="Icons\CtrEditorC.png" />
<Resource Include="Icons\CtrEditorE.png" />
<Resource Include="Icons\disconnect.png" /> <Resource Include="Icons\disconnect.png" />
<Resource Include="Icons\duplicate.png" /> <Resource Include="Icons\duplicate.png" />
<Resource Include="Icons\fotocelula.png" /> <Resource Include="Icons\fotocelula.png" />

BIN
CtrEditorE.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 655 KiB

BIN
Icons/CtrEditorA.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 KiB

BIN
Icons/CtrEditorC.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

BIN
Icons/CtrEditorE.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 KiB

BIN
Icons/app.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
Icons/app2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

View File

@ -348,6 +348,8 @@ namespace CtrEditor
if (NuevoObjetoDuplicado != null) if (NuevoObjetoDuplicado != null)
{ {
NuevoObjetoDuplicado.Nombre += "_Duplicado"; NuevoObjetoDuplicado.Nombre += "_Duplicado";
NuevoObjetoDuplicado.Left += 0.5f;
NuevoObjetoDuplicado.Top += 0.5f;
ObjetosSimulables.Add(NuevoObjetoDuplicado); ObjetosSimulables.Add(NuevoObjetoDuplicado);
CrearUserControlDesdeObjetoSimulable(NuevoObjetoDuplicado); CrearUserControlDesdeObjetoSimulable(NuevoObjetoDuplicado);
} }

View File

@ -7,7 +7,7 @@
xmlns:convert="clr-namespace:CtrEditor.Convertidores" xmlns:convert="clr-namespace:CtrEditor.Convertidores"
xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim" x:Class="CtrEditor.MainWindow" xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim" x:Class="CtrEditor.MainWindow"
Height="900" Width="1600" WindowState="Maximized" Height="900" Width="1600" WindowState="Maximized"
ResizeMode="CanResize" Title="{Binding directorioTrabajo}"> ResizeMode="CanResize" Title="{Binding directorioTrabajo}" Icon="/app2.png">
<Window.DataContext> <Window.DataContext>
<ctreditor:MainViewModel/> <ctreditor:MainViewModel/>

View File

@ -1,19 +1,9 @@
using CtrEditor.Convertidores; using CtrEditor.Convertidores;
using CtrEditor.Siemens; using CtrEditor.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {

View File

@ -40,7 +40,7 @@ namespace CtrEditor.ObjetosSim
private float TiempoRestante; private float TiempoRestante;
private float _leftSalida; private float _leftSalida;
private float _topSalida; private float _topSalida;
private List<osBotella> Botellas = new List<osBotella>(); private osBotella UltimaBotella;
public static string NombreClase() public static string NombreClase()
{ {
@ -210,14 +210,16 @@ namespace CtrEditor.ObjetosSim
var X = Left + OffsetLeftSalida; var X = Left + OffsetLeftSalida;
var Y = Top + OffsetTopSalida; var Y = Top + OffsetTopSalida;
var UltimaBotella = GetLastElement<osBotella>(Botellas); if (UltimaBotella != null && UltimaBotella.RemoverDesdeSimulacion)
UltimaBotella = null;
if (UltimaBotella == null) if (UltimaBotella == null)
{ {
// No hay botellas, se puede crear una nueva directamente // No hay botellas, se puede crear una nueva directamente
var nuevaBotella = _mainViewModel.CrearObjetoSimulable(typeof(osBotella), X, Y); var nuevaBotella = _mainViewModel.CrearObjetoSimulable(typeof(osBotella), X, Y);
((osBotella)nuevaBotella).Diametro = Diametro_botella; ((osBotella)nuevaBotella).Diametro = Diametro_botella;
nuevaBotella.AutoCreated = true; nuevaBotella.AutoCreated = true;
Botellas.Add((osBotella)nuevaBotella); UltimaBotella = (osBotella)nuevaBotella;
} }
else else
{ {
@ -230,7 +232,7 @@ namespace CtrEditor.ObjetosSim
var nuevaBotella = _mainViewModel.CrearObjetoSimulable(typeof(osBotella), X, Y); var nuevaBotella = _mainViewModel.CrearObjetoSimulable(typeof(osBotella), X, Y);
((osBotella)nuevaBotella).Diametro = Diametro_botella; ((osBotella)nuevaBotella).Diametro = Diametro_botella;
nuevaBotella.AutoCreated = true; nuevaBotella.AutoCreated = true;
Botellas.Add((osBotella)nuevaBotella); UltimaBotella = (osBotella)nuevaBotella;
} }
} }
} }

View File

@ -9,36 +9,42 @@
<UserControl.Resources> <UserControl.Resources>
<convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/> <convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/>
<convert:HalfWidthConverter x:Key="HalfWidthConverter"/>
</UserControl.Resources> </UserControl.Resources>
<Canvas> <Grid>
<!-- Contenedor para la imagen y el rectángulo que permite rotación conjunta --> <Grid.ColumnDefinitions>
<Canvas x:Name="RotatingContainer"> <ColumnDefinition Width="Auto"/>
<Image Source="/Icons/fotocelula.png" <!-- Columna para el Label -->
<ColumnDefinition Width="*"/>
<!-- Columna para la Image -->
<ColumnDefinition Width="*"/>
<!-- Columna para el Rectangle -->
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<RotateTransform Angle="{Binding Angulo}" CenterX="0" CenterY="0"/>
</Grid.RenderTransform>
<!-- Label en la primera columna -->
<Label Content="{Binding Nombre}" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0"/>
<Image Source="/Icons/fotocelula.png"
Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=3}" Width="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=3}"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=3}" Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=3}"
Canvas.Top="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=-1}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,0,0,0" Grid.Column="1"/>
Canvas.Left="{Binding Alto, Converter={StaticResource MeterToPixelConverter}, ConverterParameter=-3}"/>
<Rectangle x:Name="Photocell" Width="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}" Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}"> <Rectangle x:Name="Photocell" Grid.Column="2" Width="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}" Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}">
<Rectangle.Fill> <Rectangle.Fill>
<VisualBrush x:Name="MovingPattern" TileMode="Tile" Viewport="0,0,3,3" ViewportUnits="Absolute" Viewbox="0,0,3,3" ViewboxUnits="Absolute"> <VisualBrush x:Name="MovingPattern" TileMode="Tile" Viewport="0,0,3,3" ViewportUnits="Absolute" Viewbox="0,0,3,3" ViewboxUnits="Absolute">
<VisualBrush.Visual> <VisualBrush.Visual>
<Canvas> <Canvas>
<Ellipse Width="2" Height="2" Fill="Green"/> <Ellipse Width="2" Height="2" Fill="{Binding Color}"/>
</Canvas> </Canvas>
</VisualBrush.Visual> </VisualBrush.Visual>
</VisualBrush> </VisualBrush>
</Rectangle.Fill> </Rectangle.Fill>
<!-- No se aplica la transformación aquí --> <!-- No se aplica la transformación aquí -->
</Rectangle> </Rectangle>
</Canvas> </Grid>
<!-- Transformación de rotación aplicada al contenedor --> </UserControl>
<Canvas.RenderTransform>
<RotateTransform Angle="{Binding Angulo}" CenterX="0" CenterY="0"/>
</Canvas.RenderTransform>
</Canvas>
</UserControl>

View File

@ -1,20 +1,10 @@
using CtrEditor.Convertidores; using CtrEditor.Convertidores;
using CtrEditor.Siemens;
using CtrEditor.Simulacion; using CtrEditor.Simulacion;
using System; using CtrEditor.Siemens;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CtrEditor.ObjetosSim.UserControls namespace CtrEditor.ObjetosSim.UserControls
{ {
@ -32,6 +22,7 @@ namespace CtrEditor.ObjetosSim.UserControls
private bool _luzCortada; private bool _luzCortada;
private string _tagPhotocellOUT; private string _tagPhotocellOUT;
private bool _tipoNC; private bool _tipoNC;
private Brush _color;
private simBarrera Simulation_Photocell; private simBarrera Simulation_Photocell;
@ -40,6 +31,15 @@ namespace CtrEditor.ObjetosSim.UserControls
return "Photocell"; return "Photocell";
} }
public Brush Color
{
get => _color;
set
{
_color = value;
OnPropertyChanged(nameof(Color));
}
}
public bool LuzCortada public bool LuzCortada
{ {
get => _luzCortada; get => _luzCortada;
@ -48,6 +48,10 @@ namespace CtrEditor.ObjetosSim.UserControls
if (_luzCortada != value) if (_luzCortada != value)
{ {
_luzCortada = value; _luzCortada = value;
if (_luzCortada)
Color = Brushes.Blue;
else
Color = Brushes.Green;
OnPropertyChanged(nameof(LuzCortada)); OnPropertyChanged(nameof(LuzCortada));
} }
} }
@ -236,7 +240,7 @@ namespace CtrEditor.ObjetosSim.UserControls
public void Highlight(bool State) { } public void Highlight(bool State) { }
public int ZIndex() public int ZIndex()
{ {
return 1; return 16;
} }
} }

View File

@ -10,7 +10,8 @@
<convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/> <convert:MeterToPixelConverter x:Key="MeterToPixelConverter"/>
</UserControl.Resources> </UserControl.Resources>
<Canvas> <Canvas>
<localuc:CircularSegment Angle="{Binding Angulo}" OuterRadius="{Binding RadioExterno, Converter={StaticResource MeterToPixelConverter}}" InnerRadius="{Binding RadioInterno, Converter={StaticResource MeterToPixelConverter}}"
<localuc:CircularSegment x:Name="Transporte" Angle="{Binding Angulo}" OuterRadius="{Binding RadioExterno, Converter={StaticResource MeterToPixelConverter}}" InnerRadius="{Binding RadioInterno, Converter={StaticResource MeterToPixelConverter}}"
StartAngle="0" EndAngle="90" /> StartAngle="0" EndAngle="90" />
</Canvas> </Canvas>
</UserControl> </UserControl>

View File

@ -1,106 +1,107 @@
using System; using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using CommunityToolkit.Mvvm.ComponentModel;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CtrEditor.Convertidores; using CtrEditor.Convertidores;
using CtrEditor.Siemens; using CtrEditor.Siemens;
using CtrEditor.Simulacion;
namespace CtrEditor.ObjetosSim.UserControls namespace CtrEditor.ObjetosSim
{ {
/// <summary> /// <summary>
/// Interaction logic for ucTransporteCurva.xaml /// Interaction logic for ucTransporteCurva.xaml
/// </summary> /// </summary>
public class osTransporteCurva : osBase, IosBase public partial class osTransporteCurva : osBase, IosBase
{ {
private string _nombre = "Transporte Curva";
private float frictionCoefficient; private float frictionCoefficient;
private float velMax50hz; // en metros por minuto private float velMax50hz; // en metros por minuto
private float tiempoRampa; private float tiempoRampa;
private bool esMarcha; private bool esMarcha;
private Rectangle Geometria = new Rectangle(); private float _velocidadActual;
private osBase _osMotor = null;
private string _motor;
public override float Left private simTransporte Simulation_Transporte;
{
get => Geometria.Left;
set
{
Geometria.Left = value;
CanvasSetLeftinMeter(value);
OnPropertyChanged(nameof(Left));
}
}
public override float Top
{
get => Geometria.Top;
set
{
Geometria.Top = value;
CanvasSetTopinMeter(value);
OnPropertyChanged(nameof(Top));
}
}
public float RadioExterno public static string NombreClase()
{ {
get => Geometria.Length; return "Transporte Curva 90";
set
{
Geometria.Length = value;
OnPropertyChanged(nameof(RadioExterno));
}
} }
public float RadioInterno
{ private float angulo;
get => Geometria.Width;
set
{
Geometria.Width = value;
OnPropertyChanged(nameof(RadioInterno));
}
}
public float Angulo public float Angulo
{ {
get => Geometria.Angle; get => angulo;
set set
{ {
Geometria.Angle = value; ActualizarGeometrias();
OnPropertyChanged(nameof(Angulo)); SetProperty(ref angulo, value);
} }
} }
public float VelocidadActual
[ObservableProperty]
private float radioExterno;
[ObservableProperty]
private float radioInterno;
[ObservableProperty]
private string motor;
private float left;
public override float Left
{ {
get => Geometria.Speed; get => left;
set set
{ {
Geometria.Speed = value; CanvasSetLeftinMeter(value);
SetProperty(ref left, value);
}
}
private float top;
public override float Top
{
get => top;
set
{
CanvasSetTopinMeter(value);
SetProperty(ref top, value);
}
}
[ObservableProperty]
public float ancho;
[ObservableProperty]
public float alto;
public float VelocidadActual
{
get => _velocidadActual;
set
{
_velocidadActual = value;
Simulation_Transporte?.SetSpeed(value);
OnPropertyChanged(nameof(VelocidadActual)); OnPropertyChanged(nameof(VelocidadActual));
} }
} }
private string nombre = "Transporte Curva 90";
public override string Nombre public override string Nombre
{ {
get => _nombre; get => nombre;
set set => SetProperty(ref nombre, value);
}
private void ActualizarGeometrias()
{
if (_visualRepresentation is ucTransporteCurva uc)
{ {
if (_nombre != value) //UpdateRectangle(Simulation_Transporte, uc.Transporte, Alto, Ancho, Angulo);
{ Simulation_Transporte.Speed = VelocidadActual;
_nombre = value;
OnPropertyChanged(nameof(Nombre));
}
} }
} }
@ -111,26 +112,49 @@ namespace CtrEditor.ObjetosSim.UserControls
public osTransporteCurva() public osTransporteCurva()
{ {
Ancho = 1;
Alto = 0.10f;
RadioExterno = 2; RadioExterno = 2;
RadioInterno = 1; RadioInterno = 1;
} }
public override void ConnectSimManager(SimulationManager simulationManager) public override void UpdateGeometryStart()
{
simulationManager.rectangles.Add(Geometria);
}
public override void UpdateGeometry()
{ {
// Se llama antes de la simulacion // Se llama antes de la simulacion
ActualizarGeometrias();
} }
public override void UpdateGeometryStep()
public override void UpdateControl()
{ {
} }
public override void UpdatePLC(PLCModel plc) { } public override void UpdatePLC(PLCModel plc, int elapsedMilliseconds)
{
if (_osMotor != null)
{
if (_osMotor is osVMmotorSim motor)
VelocidadActual = motor.Velocidad;
}
else
_osMotor = ObtenerLink(_motor, typeof(osVMmotorSim));
}
public override void UpdateControl(int elapsedMilliseconds)
{
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
ActualizarLeftTop();
//if (_visualRepresentation is ucTransporteCurva uc)
// Simulation_Transporte = AddRectangle(simulationManager, uc.Transporte, Alto, Ancho, Angulo);
}
public override void ucUnLoaded()
{
// El UserControl se esta eliminando
// eliminar el objeto de simulacion
simulationManager.Remove(Simulation_Transporte);
}
} }
@ -141,11 +165,21 @@ namespace CtrEditor.ObjetosSim.UserControls
public ucTransporteCurva() public ucTransporteCurva()
{ {
InitializeComponent(); InitializeComponent();
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Datos?.ucLoaded();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
Datos?.ucUnLoaded();
} }
public void Resize(float width, float height) public void Resize(float width, float height)
{ {
if (Datos is osTransporteCurva datos) if (Datos is osTransporteCurva datos)
datos.RadioExterno = PixelToMeter.Instance.calc.PixelsToMeters(width); datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
} }
public void Move(float LeftPixels, float TopPixels) public void Move(float LeftPixels, float TopPixels)
{ {
@ -167,7 +201,6 @@ namespace CtrEditor.ObjetosSim.UserControls
return 1; return 1;
} }
} }
} }

View File

@ -20,6 +20,7 @@ using FarseerPhysics.Dynamics;
using Siemens.Simatic.Simulation.Runtime; using Siemens.Simatic.Simulation.Runtime;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Input; using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
namespace CtrEditor.ObjetosSim namespace CtrEditor.ObjetosSim
{ {
@ -64,7 +65,7 @@ namespace CtrEditor.ObjetosSim
} }
public abstract class osBase : INotifyPropertyChanged public abstract class osBase : ObservableObject
{ {
public virtual string Nombre { get; set; } = "osBase"; public virtual string Nombre { get; set; } = "osBase";

View File

@ -12,9 +12,8 @@ using CtrEditor.Convertidores;
using FarseerPhysics.Common; using FarseerPhysics.Common;
using System.Windows; using System.Windows;
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Documents;
using CtrEditor.ObjetosSim; using FarseerPhysics.Dynamics.Joints;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CtrEditor.Simulacion namespace CtrEditor.Simulacion
{ {
@ -37,6 +36,90 @@ namespace CtrEditor.Simulacion
} }
public class simCurve : simBase
{
private float _innerRadius;
private float _outerRadius;
private float _startAngle;
private float _endAngle;
public simCurve(World world, float innerRadius, float outerRadius, float startAngle, float endAngle, Vector2 position)
{
_world = world;
_innerRadius = innerRadius;
_outerRadius = outerRadius;
_startAngle = MathHelper.ToRadians(startAngle);
_endAngle = MathHelper.ToRadians(endAngle);
Create(position);
}
private void Create(Vector2 position)
{
if (Body != null)
{
_world.RemoveBody(Body);
}
// Crear la geometría del sensor de curva
List<Vertices> segments = CreateCurveVertices(_innerRadius, _outerRadius, _startAngle, _endAngle);
Body = new Body(_world);
foreach (var segment in segments)
{
var shape = new PolygonShape(segment, 1f);
var fixture = Body.CreateFixture(shape);
fixture.IsSensor = true;
}
Body.Position = position;
Body.BodyType = BodyType.Static;
Body.UserData = this;
}
private List<Vertices> CreateCurveVertices(float innerRadius, float outerRadius, float startAngle, float endAngle)
{
List<Vertices> verticesList = new List<Vertices>();
int segments = 32;
float angleStep = (endAngle - startAngle) / segments;
Vertices innerVertices = new Vertices();
Vertices outerVertices = new Vertices();
for (int i = 0; i <= segments; i++)
{
float angle = startAngle + i * angleStep;
innerVertices.Add(new Vector2(innerRadius * (float)Math.Cos(angle), innerRadius * (float)Math.Sin(angle)));
outerVertices.Add(new Vector2(outerRadius * (float)Math.Cos(angle), outerRadius * (float)Math.Sin(angle)));
}
outerVertices.Reverse();
innerVertices.AddRange(outerVertices);
verticesList.Add(innerVertices);
return verticesList;
}
public void ApplyCurveEffect(simBotella bottle)
{
// Crear un joint para mantener la botella en la curva
RevoluteJoint joint = new RevoluteJoint(bottle.Body, Body, Body.Position, true)
{
CollideConnected = false
};
_world.AddJoint(joint);
bottle.CurrentJoint = joint;
}
public void RemoveCurveEffect(simBotella bottle)
{
if (bottle.CurrentJoint != null)
{
_world.RemoveJoint(bottle.CurrentJoint);
bottle.CurrentJoint = null;
}
}
}
public class simDescarte : simBase public class simDescarte : simBase
{ {
private float _radius; private float _radius;
@ -178,6 +261,7 @@ namespace CtrEditor.Simulacion
private float _radius; private float _radius;
private float _mass; private float _mass;
public bool Descartar = false; public bool Descartar = false;
public RevoluteJoint CurrentJoint { get; set; }
public simBotella(World world, float diameter, Vector2 position, float mass) public simBotella(World world, float diameter, Vector2 position, float mass)
{ {
@ -255,6 +339,11 @@ namespace CtrEditor.Simulacion
Sensor.LuzCortada = true; Sensor.LuzCortada = true;
return true; return true;
} }
else if (fixtureB.Body.UserData is simCurve curve)
{
curve.ApplyCurveEffect(this);
return true; // No aplicar respuestas físicas
}
else if (fixtureB.Body.UserData is simDescarte) else if (fixtureB.Body.UserData is simDescarte)
{ {
Descartar = true; Descartar = true;
@ -346,8 +435,11 @@ namespace CtrEditor.Simulacion
public void Remove(simBase Objeto) public void Remove(simBase Objeto)
{ {
Objeto.RemoverBody(); if (Objeto != null)
Cuerpos.Remove(Objeto); {
Objeto.RemoverBody();
Cuerpos.Remove(Objeto);
}
} }
public simBotella AddCircle(float diameter, Vector2 position, float mass) public simBotella AddCircle(float diameter, Vector2 position, float mass)

BIN
app2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB