Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Hitable interface.<br>
- * Classes implementing this interface can be hit.
- */
- interface Hitable {
- void hit();
- }
- /**
- * Asteroid, destroyed when hit (invisible).
- */
- class Asteroid implements Hitable {
- boolean isVisible = true;
- @Override
- public void hit() {
- isVisible = false;
- }
- }
- /**
- * When hit, the health is reduced by 1.
- */
- class SpaceShip implements Hitable {
- int health = 100;
- @Override
- public void hit() {
- health--;
- }
- }
- /**
- * GameManager holds a list of all Entities in game.
- */
- class GameMamanger {
- List<Hitable> hitableEntities = new ArrayList<>();
- void addEntity(Hitable entity) {
- hitableEntities.add(entity);
- }
- // iterate over all known hitable entities and check for hits
- void checkForHits() {
- for (Hitable entity : hitableEntities) {
- // TODO Game Logic, check for hits between Asteroids and Spaceship
- if (true) {
- // an entity is hit (Asteroid or Spaceship)
- entity.hit();
- }
- }
- }
- }
- public class SimpleTest1 {
- public static void main(String[] args) {
- // create asteroid
- Asteroid a1 = new Asteroid();
- Asteroid a2 = new Asteroid();
- // create player
- SpaceShip player = new SpaceShip();
- // create gamemanager and add all entities
- GameMamanger manager = new GameMamanger();
- manager.addEntity(a1);
- manager.addEntity(a2);
- manager.addEntity(player);
- // TODO game logic
- // Game Manager checks if hitable entities have been hit
- manager.checkForHits();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement