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.

216 lines
6.3 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. public void onUp(KeyEvent kc, GameObject ego) {
  38. ego.setVX(0.0);
  39. ego.setVY(-SpaceInvadersLevel.EGOSPEED);
  40. }
  41. public void onDown(KeyEvent kc, GameObject ego) {
  42. ego.setVX(0.0);
  43. ego.setVY(SpaceInvadersLevel.EGOSPEED);
  44. }
  45. public void onLeft(KeyEvent kc, GameObject ego) {
  46. ego.setVY(0.0);
  47. ego.setVX(-SpaceInvadersLevel.EGOSPEED);
  48. }
  49. public void onRight(KeyEvent kc, GameObject ego) {
  50. ego.setVY(0.0);
  51. ego.setVX(SpaceInvadersLevel.EGOSPEED);
  52. }
  53. public void onStop(KeyEvent kc, GameObject ego) {
  54. ego.setVY(0.0);
  55. ego.setVX(0.0);
  56. ego.setComponentProperty("controller", "setDummy", "NEW");
  57. ego.setComponentProperty("controller", "setDummy2", "XXX");
  58. }
  59. /** checks the position and respects level boundaries and own radius or width/height set on constructor.
  60. *
  61. * @return true if the object reached the boundaries of the level, false otherwise
  62. */
  63. public boolean stopObject() {
  64. // check whether ego object is at level boundaries
  65. // can use radius (rad) and width or height in one check as either rad or width/height is zero.
  66. int pgSizeX = this.getPlayground().getSizeX();
  67. int pgSizeY = this.getPlayground().getSizeY();
  68. double ts = this.getTimestep();
  69. if (this.getX() + rad + (width/2d) + this.getVX() * ts >= pgSizeX
  70. || this.getX() - rad - (width/2d) + this.getVX() * ts < 0) {
  71. return true;
  72. }
  73. if (this.getY() + rad + (height/2d) + this.getVY() * ts >= pgSizeY
  74. || this.getY() - rad - (height/2d) + this.getVY() * ts < 0) {
  75. return true;
  76. }
  77. return false;
  78. }
  79. /** behavior for shooting on key space
  80. *
  81. * @param e KeyEvent of the space key
  82. * @param ego EgoObject instance (used to determine position of shot object's start)
  83. */
  84. public void onSpace(KeyEvent e, GameObject ego) {
  85. pressedKey = lastPressedKey;
  86. lastPressedKey = null;
  87. // create unique name for object
  88. // read Flag nextShot read (if not existing already it will be set)
  89. // it will be updated by 1 and saved
  90. Integer nextShot =
  91. (Integer) this.getPlayground().getOrCreateLevelFlag("nextShot", Integer.valueOf(0));
  92. String shotName = "simpleShot" + nextShot++;
  93. this.getPlayground().setLevelFlag("nextShot", nextShot);
  94. SimpleShotController simpleshot = new SimpleShotController();
  95. GameObject ss = new RectObject(shotName, this.getPlayground(), ego.getX(), ego.getY(), 0,
  96. -1. * SpaceInvadersLevel.SHOTSPEED, 4, 12, Color.CYAN).addController(simpleshot);
  97. ss.generateColliders();
  98. this.getPlayground().addObject(ss);
  99. }
  100. /**
  101. * updates position based on key events (mouse currently ignored)
  102. */
  103. public void updateObject() {
  104. logger.trace("Playground inst is"+this.getPlayground()) ;
  105. Stack<KeyEvent> keyEvents = this.getPlayground().getKeyEvents();
  106. GameObject ego = this.gameObject;
  107. while (!keyEvents.isEmpty()) {
  108. KeyEvent e = keyEvents.pop();
  109. boolean pressed = false;
  110. boolean released = true;
  111. int kc = e.getKeyCode();
  112. if (e.paramString().indexOf("PRESSED") >= 0) {
  113. pressed = true;
  114. released = false;
  115. }
  116. /**
  117. * Generelle Idee: Wenn eine Taste gedrückt wird wird sie gespeichert. wenn die zuvor
  118. * gespeicherte Taste wieder losgelassen wird stoppt das Ego-Objekt. Falls vor dem Loslassen
  119. * eine andere Taste gedrückt wird, wird diese gespeichert und die alte vergessen. Dh das
  120. * loslassen der alten Taste stoppt das Objekt nicht. Spezialfall: space, das loslassen von
  121. * space stoppt das Objekt nicht!
  122. */
  123. if (pressed == true) {
  124. lastPressedKey = pressedKey;
  125. pressedKey = kc;
  126. }
  127. /**
  128. * Nur eine losgelassene Taste die auch vorher gedrückt wurde stoppt das Objekt. Eine
  129. * losgelassene Taste die nicht vorher gedrückt wurde bzw vergessen wurde stoppt das Objekt
  130. * nicht
  131. */
  132. if (released == true) {
  133. if (pressedKey != null) {
  134. if (pressedKey.equals(kc)) {
  135. ego.setVX(0);
  136. ego.setVY(0);
  137. pressedKey = null;
  138. }
  139. }
  140. continue;
  141. }
  142. if (kc == KeyEvent.VK_LEFT) {
  143. this.onLeft(e, ego);
  144. }
  145. if (kc == KeyEvent.VK_RIGHT) {
  146. this.onRight(e, ego);
  147. }
  148. if (kc == KeyEvent.VK_UP) {
  149. this.onUp(e, ego);
  150. }
  151. if (kc == KeyEvent.VK_DOWN) {
  152. this.onDown(e, ego);
  153. }
  154. // stop
  155. if (kc == KeyEvent.VK_Z) {
  156. this.onStop(e, ego);
  157. }
  158. // shot
  159. if (kc == KeyEvent.VK_SPACE) {
  160. // space is not registered! Releasing space does not stop the egoobject
  161. this.onSpace(e, ego);
  162. }
  163. }
  164. boolean stop = this.stopObject();
  165. if (stop) {
  166. this.setVX(0);
  167. this.setVY(0);
  168. }
  169. // updateSpeed and position
  170. applySpeedVector();
  171. }
  172. }