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.

98 lines
2.6 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. /**
  33. * Copies current values of x,y position and speed vx,vy into attributes. These can be restored by call to {@link #restoreDynamicState()}.
  34. */
  35. public void saveDynamicState() {
  36. this.savex = this.getX();
  37. this.savey = this.getY();
  38. this.savevx = this.getVX();
  39. this.savevy = this.getVY();
  40. }
  41. /**
  42. * Restores formally saved values of x,y position and speed vx,vy from attributes back to the ego object.
  43. * These values should have been stored before by a call to {@link #saveDynamicState()}, otherwise all values will be 0.00.
  44. */
  45. public void restoreDynamicState() {
  46. this.setX(savex);
  47. this.setY(savey);
  48. this.setVX(savevx);
  49. this.setVY(savevy);
  50. }
  51. /**
  52. * extends parent class implementation by a check whether or not the ego object collides with any other "obstacle" object.
  53. * If yes, the position stays fixed (by using {@link #saveDynamicState()} and {@link #restoreDynamicState()}.
  54. */
  55. public boolean stopObject() {
  56. boolean s = super.stopObject();
  57. Playground pg = this.getPlayground();
  58. LinkedList<GameObject> obstacles = pg.collectObjects("obstacle", false);
  59. this.saveDynamicState();
  60. this.applySpeedVector();
  61. for (GameObject ob : obstacles) {
  62. if (ob.collisionDetection(this.gameObject)) {
  63. this.restoreDynamicState();
  64. return true;
  65. }
  66. }
  67. this.restoreDynamicState();
  68. return s;
  69. }
  70. /**
  71. * calls superclass {@link EgoController#onSpace(KeyEvent, GameObject)} only, if the time elapsed since last pressing of space is above 0.1 ms.
  72. */
  73. public void onSpace(KeyEvent e, GameObject ego) {
  74. double cgt = ego.getGameTime();
  75. if ((cgt - this.lastSpaceAt) > 0.1) {
  76. super.onSpace(e, ego);
  77. Music.music(this.shot);
  78. }
  79. }
  80. }