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
2.0 KiB

  1. using System.Numerics;
  2. using Raylib_cs;
  3. namespace Smoll.Ex2 {
  4. class ArrowComponent : Smoll.Component {
  5. Complex offset;
  6. public Complex rawValue;
  7. Transform2DComponent? ownerTransform;
  8. public ArrowComponent(int offset_x = 0, int offset_y = 0)
  9. {
  10. this.offset = offset_x + Complex.ImaginaryOne*offset_y;
  11. }
  12. public override void OnAttached() {
  13. ownerTransform = owner.GetComponent<Transform2DComponent>();
  14. }
  15. public Complex Origin() {
  16. ownerTransform ??= owner.GetComponent<Transform2DComponent>();
  17. ownerTransform ??= new Transform2DComponent();
  18. var tr = ownerTransform.AbsoluteTransform();
  19. return tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale;
  20. }
  21. public Complex Vector() {
  22. ownerTransform ??= owner.GetComponent<Transform2DComponent>();
  23. ownerTransform ??= new Transform2DComponent();
  24. var tr = ownerTransform.AbsoluteTransform();
  25. return rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale;
  26. }
  27. public Tuple<Complex, Complex> OriginAndVector() {
  28. ownerTransform ??= owner.GetComponent<Transform2DComponent>();
  29. ownerTransform ??= new Transform2DComponent();
  30. var tr = ownerTransform.AbsoluteTransform();
  31. return Tuple.Create(tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale, rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale);
  32. }
  33. public override void Draw(Smoll.Layer.DrawMode drawMode) {
  34. if(drawMode == Layer.DrawMode.Debug) {
  35. var data = OriginAndVector();
  36. Complex start = data.Item1;
  37. Complex end = data.Item1 + data.Item2;
  38. Raylib.DrawLineEx(new Vector2((float)start.Real, (float)start.Imaginary),new Vector2((float)end.Real, (float)end.Imaginary),2f, Color.Red);
  39. }
  40. }
  41. }
  42. }