A Smoll game engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

59 行
1.8 KiB

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