Space von Team 22 (Nico B. Benjamin F. Lea A.)
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.

232 lines
6.5 KiB

  1. package controller;
  2. import java.awt.Color;
  3. import java.awt.event.KeyEvent;
  4. import playground.*;
  5. import gameobjects.*;
  6. import java.util.*;
  7. import org.apache.logging.log4j.Logger;
  8. import org.apache.logging.log4j.LogManager;
  9. /**
  10. * Controller using key events for up, down, left, right and space (shooting) to control the ego
  11. * object behavior.
  12. */
  13. public class EgoController extends ObjectController {
  14. // either rad is zero or width/height is zero (guaranteed by constructors)
  15. private double rad = 0;
  16. private double width = 0;
  17. private double height = 0;
  18. private Integer pressedKey = null;
  19. private Integer lastPressedKey = null;
  20. private static Logger logger = LogManager.getLogger(EgoController.class);
  21. /**
  22. * constructor that gives the ego controller a radius to stop the ego object when it reaches the level boundaries.
  23. * @param egoRad radius to use as a boundary stop for level borders (usually use the same dimensions as your ego object)
  24. */
  25. public EgoController(double egoRad) {
  26. this.rad = egoRad;
  27. }
  28. /**
  29. * constructor that gives the ego controller a width and height to stop the ego object when it reaches the level boundaries.
  30. * @param width width to use as a boundary stop for level borders (usually use the same dimensions as your ego object)
  31. * @param height height to use as a boundary stop for level borders (usually use the same dimensions as your ego object)
  32. */
  33. public EgoController(double width, double height) {
  34. this.width = width;
  35. this.height = height;
  36. }
  37. /**
  38. * moves ego up by {@link SpaceInvadersLevel#EGOSPEED}.
  39. * @param kc KeyEvent to process (ignored)
  40. * @param ego the ego object
  41. */
  42. public void onUp(KeyEvent kc, GameObject ego) {
  43. ego.setVX(0.0);
  44. ego.setVY(-SpaceInvadersLevel.EGOSPEED);
  45. }
  46. /**
  47. * moves ego down by {@link SpaceInvadersLevel#EGOSPEED}.
  48. * @param kc KeyEvent to process (ignored)
  49. * @param ego the ego object
  50. */
  51. public void onDown(KeyEvent kc, GameObject ego) {
  52. ego.setVX(0.0);
  53. ego.setVY(SpaceInvadersLevel.EGOSPEED);
  54. }
  55. /**
  56. * moves ego left by {@link SpaceInvadersLevel#EGOSPEED}.
  57. * @param kc KeyEvent to process (ignored)
  58. * @param ego the ego object
  59. */
  60. public void onLeft(KeyEvent kc, GameObject ego) {
  61. ego.setVY(0.0);
  62. ego.setVX(-SpaceInvadersLevel.EGOSPEED);
  63. }
  64. /**
  65. * moves ego right by {@link SpaceInvadersLevel#EGOSPEED}.
  66. * @param kc KeyEvent to process (ignored)
  67. * @param ego the ego object
  68. */
  69. public void onRight(KeyEvent kc, GameObject ego) {
  70. ego.setVY(0.0);
  71. ego.setVX(SpaceInvadersLevel.EGOSPEED);
  72. }
  73. /**
  74. * sets speed to 0.0
  75. * @param kc KeyEvent to process (ignored)
  76. * @param ego the ego object
  77. */
  78. public void onStop(KeyEvent kc, GameObject ego) {
  79. ego.setVY(0.0);
  80. ego.setVX(0.0);
  81. ego.setComponentProperty("controller", "setDummy", "NEW");
  82. ego.setComponentProperty("controller", "setDummy2", "XXX");
  83. }
  84. /** checks the position and respects level boundaries and own radius or width/height set on constructor.
  85. *
  86. * @return true if the object reached the boundaries of the level, false otherwise.
  87. */
  88. public boolean stopObject() {
  89. // check whether ego object is at level boundaries
  90. // can use radius (rad) and width or height in one check as either rad or width/height is zero.
  91. int pgSizeX = this.getPlayground().getSizeX();
  92. int pgSizeY = this.getPlayground().getSizeY();
  93. double ts = this.getTimestep();
  94. if (this.getX() + rad + (width/2d) + this.getVX() * ts >= pgSizeX
  95. || this.getX() - rad - (width/2d) + this.getVX() * ts < 0) {
  96. return true;
  97. }
  98. if (this.getY() + rad + (height/2d) + this.getVY() * ts >= pgSizeY
  99. || this.getY() - rad - (height/2d) + this.getVY() * ts < 0) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /** behavior for shooting on key space. Creates a new shot using {#link SimpleShotController} with a {@link RectObject}.
  105. *
  106. * @param e KeyEvent of the space key
  107. * @param ego EgoObject instance (used to determine position of shot object's start)
  108. */
  109. public void onSpace(KeyEvent e, GameObject ego) {
  110. pressedKey = lastPressedKey;
  111. lastPressedKey = null;
  112. // create unique name for object
  113. // read Flag nextShot read (if not existing already it will be set)
  114. // it will be updated by 1 and saved
  115. Integer nextShot =
  116. (Integer) this.getPlayground().getOrCreateLevelFlag("nextShot", Integer.valueOf(0));
  117. String shotName = "simpleShot" + nextShot++;
  118. this.getPlayground().setLevelFlag("nextShot", nextShot);
  119. SimpleShotController simpleshot = new SimpleShotController();
  120. GameObject ss = new RectObject(shotName, this.getPlayground(), ego.getX(), ego.getY(), 0,
  121. -1. * SpaceInvadersLevel.SHOTSPEED, 4, 12, Color.CYAN).addController(simpleshot);
  122. ss.generateColliders();
  123. this.getPlayground().addObject(ss);
  124. }
  125. /**
  126. * updates position based on key events (mouse currently ignored).
  127. */
  128. public void updateObject() {
  129. logger.trace("Playground inst is"+this.getPlayground()) ;
  130. Stack<KeyEvent> keyEvents = this.getPlayground().getKeyEvents();
  131. GameObject ego = this.gameObject;
  132. while (!keyEvents.isEmpty()) {
  133. KeyEvent e = keyEvents.pop();
  134. boolean pressed = false;
  135. boolean released = true;
  136. int kc = e.getKeyCode();
  137. if (e.paramString().indexOf("PRESSED") >= 0) {
  138. pressed = true;
  139. released = false;
  140. }
  141. if (pressed == true) {
  142. lastPressedKey = pressedKey;
  143. pressedKey = kc;
  144. }
  145. /*
  146. * Only if the released key is the same as the before pressed one, it stops the ego object movement.
  147. */
  148. if (released == true) {
  149. if (pressedKey != null) {
  150. if (pressedKey.equals(kc)) {
  151. ego.setVX(0);
  152. ego.setVY(0);
  153. pressedKey = null;
  154. }
  155. }
  156. continue;
  157. }
  158. if (kc == KeyEvent.VK_LEFT) {
  159. this.onLeft(e, ego);
  160. }
  161. if (kc == KeyEvent.VK_RIGHT) {
  162. this.onRight(e, ego);
  163. }
  164. if (kc == KeyEvent.VK_UP) {
  165. this.onUp(e, ego);
  166. }
  167. if (kc == KeyEvent.VK_DOWN) {
  168. this.onDown(e, ego);
  169. }
  170. // stop
  171. if (kc == KeyEvent.VK_Z) {
  172. this.onStop(e, ego);
  173. }
  174. // shot
  175. if (kc == KeyEvent.VK_SPACE) {
  176. // space is not registered! Releasing space does not stop the ego object
  177. this.onSpace(e, ego);
  178. }
  179. }
  180. boolean stop = this.stopObject();
  181. if (stop) {
  182. this.setVX(0);
  183. this.setVY(0);
  184. }
  185. // updateSpeed and position
  186. applySpeedVector();
  187. }
  188. }