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.

59 lines
1.8 KiB

  1. package controller;
  2. import gameobjects.GameObject;
  3. import org.apache.logging.log4j.Logger;
  4. import org.apache.logging.log4j.LogManager;
  5. /**
  6. * Stops GameObject movement at bottom of level and let it 'fly' in x-direction towards ego object.
  7. * If the object flies outside of the level (by x-coordinate) it is removed.
  8. *
  9. * The behavior looks like a sinking sea mine, which stays at a certain depth and moves left/right to catch the player.
  10. *
  11. *
  12. */
  13. public class MineController extends ObjectController {
  14. int rad = 3;
  15. double xSpeed = 0.;
  16. double lineSpeed = 0;
  17. private static Logger logger = LogManager.getLogger(MineController.class);
  18. public MineController(double lineSpeed) {
  19. this.lineSpeed = lineSpeed;
  20. }
  21. /**
  22. * Fetches ego object by name 'ego' from current level and determines it's x-position.
  23. * The controlled GameObject will move towards this position (in x-direction only).
  24. * y-direction speed is reduced to zero, if the objects reached lower bound of level (high y-values).
  25. * Only deletes the object if it flies out of visible game area.
  26. */
  27. @Override
  28. public void updateObject() {
  29. if (gameObject.getY() >= this.getPlayground().getSizeY() - 10) {
  30. this.gameObject.setVY(0);
  31. if (xSpeed == 0.) {
  32. GameObject ego = this.getPlayground().getObject("ego");
  33. double egoXPos = ego.getX();
  34. if (egoXPos > this.gameObject.getX()) {
  35. xSpeed = 50;
  36. } else {
  37. xSpeed = -50;
  38. }
  39. this.gameObject.setVX(xSpeed);
  40. }
  41. this.gameObject.setVX(xSpeed);
  42. }
  43. if (this.gameObject.getX() < 0 || (this.gameObject.getX() > this.getPlayground().getSizeX())) {
  44. logger.debug("deleting" + this.gameObject.getId());
  45. this.getPlayground().deleteObject(this.gameObject.getId());
  46. }
  47. this.applySpeedVector();
  48. }
  49. }