A Smoll game engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

8 months ago
  1. using System.Numerics;
  2. namespace Smoll.Ex3
  3. {
  4. sealed class InertiaComponent : Component
  5. {
  6. public Transform2D transform;
  7. public float dampening;
  8. private Transform2DComponent? ownerTransform;
  9. public InertiaComponent(float dampening = 1)
  10. : this(dampening, 0, 0, 0)
  11. { }
  12. public InertiaComponent(float dampening, float x, float y)
  13. : this(dampening, x, y, 0)
  14. { }
  15. public InertiaComponent(float dampening, float x, float y, float angle)
  16. {
  17. transform = new Transform2D();
  18. transform.position = y * Complex.ImaginaryOne + x;
  19. transform.angle = angle;
  20. transform.scale = 1;
  21. this.dampening = dampening;
  22. }
  23. public InertiaComponent(float dampening, Transform2D tr)
  24. {
  25. transform = tr;
  26. this.dampening = dampening;
  27. }
  28. public override void OnAttached()
  29. {
  30. ownerTransform = owner.GetComponent<Transform2DComponent>();
  31. }
  32. public override void Update(float deltaTimeSeconds)
  33. {
  34. ownerTransform ??= owner.GetComponent<Transform2DComponent>();
  35. ownerTransform ??= new Transform2DComponent();
  36. ownerTransform.transform.position += transform.position * deltaTimeSeconds;
  37. ownerTransform.transform.angle += transform.angle * deltaTimeSeconds;
  38. transform.position *= MathF.Pow(dampening, deltaTimeSeconds);
  39. transform.angle *= MathF.Pow(dampening, deltaTimeSeconds);
  40. }
  41. }
  42. }