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.

60 lines
1.4 KiB

  1. package playground;
  2. import gameobjects.GameObject;
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. /**
  6. * extends {@link SpaceInvadersLevel} with 10 enemies that need two shots each to be destroyed.
  7. *
  8. */
  9. public class LevelHitTwice extends SpaceInvadersLevel {
  10. /** constant defining the number of shots needed to destroy an enemy */
  11. public static final int MAX_HITS = 2;
  12. private static Logger logger = LogManager.getLogger(LevelHitTwice.class);
  13. /** constructor setting internal name to 'hitTwice' */
  14. public LevelHitTwice() {
  15. super();
  16. this.level = "hitTwice";
  17. }
  18. @Override
  19. protected String getStartupMessage() {
  20. return "2 shots at alien required!!!";
  21. }
  22. @Override
  23. protected int calcNrEnemies() {
  24. return 10;
  25. }
  26. @Override
  27. void actionIfEnemyIsHit(GameObject e, GameObject shot) {
  28. Object counterFlag = e.getOrCreateObjectFlag("counter", Integer.valueOf(1));
  29. int counter = (Integer) counterFlag;
  30. if (counter >= MAX_HITS) {
  31. logger.trace("enemy was hit before for " + counter + " times, which is above "
  32. + LevelHitTwice.MAX_HITS);
  33. super.actionIfEnemyIsHit(e, shot);
  34. } else {
  35. logger.trace("enemy was hit before for "+counter+" times, which is below "+LevelHitTwice.MAX_HITS);
  36. e.setObjectFlag("counter", Integer.valueOf(counter + 1));
  37. }
  38. deleteObject(shot.getId());
  39. }
  40. }