Average Density: 0.02
  1 package nl.tudelft.jpacman.level;
  2 
  3 import java.util.List;
  4 import java.util.Map;
  5 import java.util.Optional;
  6 
  7 import nl.tudelft.jpacman.board.Board;
  8 import nl.tudelft.jpacman.board.Direction;
  9 import nl.tudelft.jpacman.board.Square;
 10 import nl.tudelft.jpacman.npc.Ghost;
 11 import nl.tudelft.jpacman.npc.ghost.GhostColor;
 12 import nl.tudelft.jpacman.npc.ghost.GhostFactory;
 13 import nl.tudelft.jpacman.points.PointCalculator;
 14 import nl.tudelft.jpacman.sprite.PacManSprites;
 15 import nl.tudelft.jpacman.sprite.Sprite;
 16 
 17 /**
 18  * Factory that creates levels and units.
 19  *
 20  * @author Jeroen Roosen 
 21  */
 22 public class LevelFactory {
 23 
 24     private static final int GHOSTS = 4;
 25     private static final int BLINKY = 0;
 26     private static final int INKY = 1;
 27     private static final int PINKY = 2;
 28     private static final int CLYDE = 3;
 29 
 30     /**
 31      * The default value of a pellet.
 32      */
 33     private static final int PELLET_VALUE = 10;
 34 
 35     /**
 36      * The sprite store that provides sprites for units.
 37      */
 38     private final PacManSprites sprites;
 39 
 40     /**
 41      * Used to cycle through the various ghost types.
 42      */
 43     private int ghostIndex;
 44 
 45     /**
 46      * The factory providing ghosts.
 47      */
 48     private final GhostFactory ghostFact;
 49 
 50     /**
 51      * The way to calculate points upon collisions.
 52      */
 53     private final PointCalculator pointCalculator;
 54 
 55     /**
 56      * Creates a new level factory.
 57      *
 58      * @param spriteStore
 59      *            The sprite store providing the sprites for units.
 60      * @param ghostFactory
 61      *            The factory providing ghosts.
 62      * @param pointCalculator
 63      *            The algorithm to calculate the points.
 64      */
 65     public LevelFactory(PacManSprites spriteStore,
 66                         GhostFactory ghostFactory,
 67                         PointCalculator pointCalculator) {
 68         this.sprites = spriteStore;
 69         this.ghostIndex = -1;
 70         this.ghostFact = ghostFactory;
 71         this.pointCalculator = pointCalculator;
 72     }
 73 
 74     /**
 75      * Creates a new level from the provided data.
 76      *
 77      * @param board
 78      *            The board with all ghosts and pellets occupying their squares.
 79      * @param ghosts
 80      *            A list of all ghosts on the board.
 81      * @param startPositions
 82      *            A list of squares from which players may start the game.
 83      * @return A new level for the board.
 84      */
 85     public Level createLevel(Board board, List<Ghost> ghosts, List<Square> startPositions) {
 86 
 87         // We'll adopt the simple collision map for now.
 88         CollisionMap collisionMap = new PlayerCollisions(pointCalculator);
 89 
 90         return new Level(board, ghosts, startPositions, collisionMap);
 91     }
 92 
 93     /**
 94      * Creates a new ghost.
 95      *
 96      * @return The new ghost.
 97      */
 98     Ghost createGhost() {
 99         ghostIndex++;
100         ghostIndex %= GHOSTS;
101         switch (ghostIndex) {
102             case BLINKY:
103                 return ghostFact.createBlinky();
104             case INKY:
105                 return ghostFact.createInky();
106             case PINKY:
107                 return ghostFact.createPinky();
108             case CLYDE:
109                 return ghostFact.createClyde();
110             default:
111                 return new RandomGhost(sprites.getGhostSprite(GhostColor.RED));
112         }
113     }
114 
115     /**
116      * Creates a new pellet.
117      *
118      * @return The new pellet.
119      */
120     public Pellet createPellet() {
121         return new Pellet(PELLET_VALUE, sprites.getPelletSprite());
122     }
123 
124     /**
125      * Implementation of an NPC that wanders around randomly.
126      *
127      * @author Jeroen Roosen
128      */
129     private static final class RandomGhost extends Ghost {
130 
131         /**
132          * The suggested delay between moves.
133          */
134         private static final long DELAY = 175L;
135 
136         /**
137          * Creates a new random ghost.
138          *
139          * @param ghostSprite
140          *            The sprite for the ghost.
141          */
142         RandomGhost(Map<Direction, Sprite> ghostSprite) {
143             super(ghostSprite, (int) DELAY, 0);
144         }
145 
146         @Override
147         public Optional<Direction> nextAiMove() {
148             return Optional.empty();
149         }
150     }
151 }