Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. /**
  5.  * Hitable interface.<br>
  6.  * Classes implementing this interface can be hit.
  7.  */
  8. interface Hitable {
  9.     void hit();
  10. }
  11.  
  12. /**
  13.  * Asteroid, destroyed when hit (invisible).
  14.  */
  15. class Asteroid implements Hitable {
  16.     boolean isVisible = true;
  17.  
  18.     @Override
  19.     public void hit() {
  20.         isVisible = false;
  21.     }
  22.  
  23. }
  24.  
  25. /**
  26.  * When hit, the health is reduced by 1.
  27.  */
  28. class SpaceShip implements Hitable {
  29.     int health = 100;
  30.  
  31.     @Override
  32.     public void hit() {
  33.         health--;
  34.     }
  35. }
  36.  
  37. /**
  38.  * GameManager holds a list of all Entities in game.
  39.  */
  40. class GameMamanger {
  41.     List<Hitable> hitableEntities = new ArrayList<>();
  42.  
  43.     void addEntity(Hitable entity) {
  44.         hitableEntities.add(entity);
  45.     }
  46.  
  47.     // iterate over all known hitable entities and check for hits
  48.     void checkForHits() {
  49.         for (Hitable entity : hitableEntities) {
  50.  
  51.             // TODO Game Logic, check for hits between Asteroids and Spaceship
  52.             if (true) {
  53.                 // an entity is hit (Asteroid or Spaceship)
  54.                 entity.hit();
  55.             }
  56.         }
  57.     }
  58.  
  59. }
  60.  
  61. public class SimpleTest1 {
  62.  
  63.     public static void main(String[] args) {
  64.         // create asteroid
  65.         Asteroid a1 = new Asteroid();
  66.         Asteroid a2 = new Asteroid();
  67.  
  68.         // create player
  69.         SpaceShip player = new SpaceShip();
  70.  
  71.         // create gamemanager and add all entities
  72.         GameMamanger manager = new GameMamanger();
  73.         manager.addEntity(a1);
  74.         manager.addEntity(a2);
  75.         manager.addEntity(player);
  76.  
  77.         // TODO game logic
  78.         // Game Manager checks if hitable entities have been hit
  79.         manager.checkForHits();
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement