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.

40 line
1.6 KiB

8 月之前
  1. using Smoll;
  2. using Raylib_cs;
  3. using System.Numerics;
  4. using Smoll.Ex1;
  5. using Smoll.Ex3;
  6. namespace Smoll.Ex2
  7. {
  8. class ExampleShootingInputComponent : Component {
  9. Transform2DComponent? transform;
  10. ArrowComponent? arrow;
  11. public ExampleShootingInputComponent() {
  12. }
  13. public override void OnAttached()
  14. {
  15. base.OnAttached();
  16. transform = owner.GetComponent<Transform2DComponent>();
  17. arrow = owner.GetComponent<ArrowComponent>();
  18. }
  19. public override void Update(float deltaTimeSeconds)
  20. {
  21. transform ??= owner.GetComponent<Transform2DComponent>();
  22. if(transform == null) throw new Exception("Example Input used on immovable object");
  23. arrow ??= owner.GetComponent<ArrowComponent>();
  24. if(arrow == null) throw new Exception("Example Shooter used on arrowless object");
  25. if(Raylib.IsKeyPressed(KeyboardKey.Space)) {
  26. var shoot = new Entity(owner.layers.First());
  27. shoot.zOrder = -5;
  28. var details = arrow.OriginAndVector();
  29. var selfTransform = transform.AbsoluteTransform();
  30. shoot.Attach(new Transform2DComponent((float)details.Item1.Real, (float)details.Item1.Imaginary, selfTransform.angle, selfTransform.scale));
  31. shoot.Attach(new TimeToLiveComponent(4f));
  32. shoot.Attach(new SpriteComponent("Beam.png"));
  33. shoot.Attach(new InertiaComponent(1.0f, (float)details.Item2.Real, (float)details.Item2.Imaginary));
  34. }
  35. }
  36. }
  37. }