PlayerShipControlSystem.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using Artemis;
  3. using StarWarrior.Components;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using System.Collections.Generic;
  7. namespace StarWarrior.Systems
  8. {
  9. public class PlayerShipControlSystem : TagSystem {
  10. private SpriteBatch spriteBatch;
  11. private bool moveRight;
  12. private bool moveLeft;
  13. private bool shoot;
  14. private ComponentMapper<Transform> transformMapper;
  15. private KeyboardState oldState;
  16. public PlayerShipControlSystem(SpriteBatch spriteBatch) : base("PLAYER") {
  17. this.spriteBatch = spriteBatch;
  18. }
  19. public override void Initialize() {
  20. transformMapper = new ComponentMapper<Transform>(world);
  21. oldState = Keyboard.GetState();
  22. }
  23. public override void Process(Entity e)
  24. {
  25. Transform transform = transformMapper.Get(e);
  26. UpdateInput();
  27. if (moveLeft)
  28. {
  29. transform.AddX(world.GetDelta() * -0.3f);
  30. }
  31. if (moveRight)
  32. {
  33. transform.AddX(world.GetDelta() * 0.3f);
  34. }
  35. if (shoot)
  36. {
  37. Entity missile = EntityFactory.CreateMissile(world);
  38. missile.GetComponent<Transform>().SetLocation(transform.GetX()+30, transform.GetY() - 20);
  39. missile.GetComponent<Velocity>().SetVelocity(-0.5f);
  40. missile.GetComponent<Velocity>().SetAngle(90);
  41. missile.Refresh();
  42. shoot = false;
  43. }
  44. }
  45. public void UpdateInput() {
  46. KeyboardState ks = Keyboard.GetState();
  47. if (ks.IsKeyDown(Keys.A)) {
  48. moveLeft = true;
  49. moveRight = false;
  50. }
  51. else if (oldState.IsKeyDown(Keys.A)) {
  52. moveLeft = false;
  53. }
  54. if (ks.IsKeyDown(Keys.D)) {
  55. moveRight = true;
  56. moveLeft = false;
  57. }
  58. else if (oldState.IsKeyDown(Keys.D))
  59. {
  60. moveRight = false;
  61. }
  62. if (ks.IsKeyDown(Keys.Space) == true && oldState.IsKeyDown(Keys.Space) == false)
  63. {
  64. shoot = true;
  65. }
  66. else if (oldState.IsKeyDown(Keys.Space))
  67. {
  68. shoot = false;
  69. }
  70. oldState = ks;
  71. }
  72. }
  73. }