Browse Source

Fixed Inertia

master
Ludovic 'Archivist' Lagouardette 1 month ago
parent
commit
a06766b72b
4 changed files with 14 additions and 23 deletions
  1. +4
    -3
      Expansion1/ExampleInputComponent.cs
  2. +1
    -1
      Expansion2/ExampleShootingInputComponent.cs
  3. +6
    -17
      Expansion2/InertiaComponent.cs
  4. +3
    -2
      Program.cs

+ 4
- 3
Expansion1/ExampleInputComponent.cs View File

@ -1,11 +1,12 @@
using Smoll;
using Raylib_cs;
using System.Numerics;
using Smoll.Ex2;
namespace Smoll.Ex1
{
class ExampleInputComponent : Component {
Transform2DComponent? transform;
InertiaComponent? transform;
public float speedPixelPerSecond;
public float angularSpeedTurnPerSecond;
@ -18,12 +19,12 @@ namespace Smoll.Ex1
public override void OnAttached()
{
base.OnAttached();
transform = owner.GetComponent<Transform2DComponent>();
transform = owner.GetComponent<InertiaComponent>();
}
public override void Update(float deltaTimeSeconds)
{
transform ??= owner.GetComponent<Transform2DComponent>();
transform ??= owner.GetComponent<InertiaComponent>();
if(transform == null) throw new Exception("Example Input used on immovable object");
transform.transform.position -= Raylib.IsKeyDown(KeyboardKey.Up) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne;
transform.transform.position += Raylib.IsKeyDown(KeyboardKey.Down) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne;

+ 1
- 1
Expansion2/ExampleShootingInputComponent.cs View File

@ -33,7 +33,7 @@ namespace Smoll.Ex2
shoot.Attach(new Transform2DComponent((float)details.Item1.Real, (float)details.Item1.Imaginary, selfTransform.angle, selfTransform.scale));
shoot.Attach(new TimeToLiveComponent(4f));
shoot.Attach(new SpriteComponent("Beam.png"));
shoot.Attach(new InertiaComponent(1.0f, m">0.001f*(float)details.Item2.Real, 0.001f*(float)details.Item2.Imaginary));
shoot.Attach(new InertiaComponent(1.0f, (float)details.Item2.Real, (float)details.Item2.Imaginary));
}
}
}

+ 6
- 17
Expansion2/InertiaComponent.cs View File

@ -4,7 +4,6 @@ namespace Smoll.Ex2 {
sealed class InertiaComponent : Component {
public Transform2D transform;
public float dampening;
public float minimalDampening = 2.0f;
private Transform2DComponent? ownerTransform;
public InertiaComponent(float dampening = 1)
@ -37,22 +36,12 @@ namespace Smoll.Ex2 {
{
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
ownerTransform ??= new Transform2DComponent();
ownerTransform.transform.position += transform.position;
ownerTransform.transform.angle += transform.angle;
if(dampening != 1) {
if(transform.position.Magnitude > minimalDampening) {
transform.position *= dampening;
} else {
transform.position *= 0;
}
if(transform.angle > minimalDampening) {
transform.angle *= dampening;
} else {
transform.angle *= 0;
}
}
ownerTransform.transform.position += transform.position * deltaTimeSeconds;
ownerTransform.transform.angle += transform.angle * deltaTimeSeconds;
transform.position *= dampening;
transform.angle *= dampening;
}
}

+ 3
- 2
Program.cs View File

@ -19,7 +19,8 @@ var player = new Entity(layer);
debugLayer.Attach(player);
player.Attach(new Transform2DComponent(128, 128, 0, 1));
player.Attach(new CrosshairGizmoComponent());
player.Attach(new ExampleInputComponent(100f, 0.25f));
player.Attach(new ExampleInputComponent(250f, 0.35f));
player.Attach(new InertiaComponent(0.999f));
var entity = new Entity(player);
entity.Attach(new Transform2DComponent(-16, -16, 0, 4));
@ -31,7 +32,7 @@ var entity2 = new Entity(entity);
entity2.Attach(new Transform2DComponent(5, -10, 0));
entity2.Attach(new SpriteComponent("Laser.png"));
var arrow = new ArrowComponent(2, 0);
arrow.rawValue = new System.Numerics.Complex(0, -12);
arrow.rawValue = new System.Numerics.Complex(0, -48);
entity2.Attach(arrow);
entity2.Attach(new ExampleShootingInputComponent());

Loading…
Cancel
Save