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.

84 lines
1.8 KiB

  1. package controller;
  2. import playground.*;
  3. import gameobjects.*;
  4. import java.util.*;
  5. import java.awt.event.*;
  6. import java.io.File;
  7. /**
  8. * An EgoController which cannot move through obstacle objects (is collission aware). Only respects
  9. * GameObjects that have the String 'obstacle' in their name.
  10. *
  11. */
  12. public class CollisionAwareEgoController extends EgoController {
  13. double savex, savey, savevx, savevy;
  14. double lastSpaceAt = -1;
  15. private File shot = null;
  16. /**
  17. *
  18. * @param egoRad radius of ego object to be used.
  19. */
  20. public CollisionAwareEgoController(double egoRad) {
  21. super(egoRad);
  22. }
  23. /**
  24. *
  25. * @param egoRad radius of ego object to be used.
  26. * @param soundOnShot WAV file to be played on shot
  27. */
  28. public CollisionAwareEgoController(double egoRad, File soundOnShot) {
  29. super(egoRad);
  30. this.shot = soundOnShot;
  31. }
  32. public void saveDynamicState() {
  33. this.savex = this.getX();
  34. this.savey = this.getY();
  35. this.savevx = this.getVX();
  36. this.savevy = this.getVY();
  37. }
  38. public void restoreDynamicState() {
  39. this.setX(savex);
  40. this.setY(savey);
  41. this.setVX(savevx);
  42. this.setVY(savevy);
  43. }
  44. public boolean stopObject() {
  45. boolean s = super.stopObject();
  46. Playground pg = this.getPlayground();
  47. LinkedList<GameObject> obstacles = pg.collectObjects("obstacle", false);
  48. this.saveDynamicState();
  49. this.applySpeedVector();
  50. for (GameObject ob : obstacles) {
  51. if (ob.collisionDetection(this.gameObject)) {
  52. this.restoreDynamicState();
  53. return true;
  54. }
  55. }
  56. this.restoreDynamicState();
  57. return s;
  58. }
  59. public void onSpace(KeyEvent e, GameObject ego) {
  60. double cgt = ego.getGameTime();
  61. if ((cgt - this.lastSpaceAt) > 0.1) {
  62. super.onSpace(e, ego);
  63. Music.music(this.shot);
  64. }
  65. }
  66. }