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.

48 lines
1.5 KiB

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