Average Density: 0.02
  1 package nl.tudelft.jpacman.level;
  2 
  3 import java.util.Map;
  4 
  5 import nl.tudelft.jpacman.board.Direction;
  6 import nl.tudelft.jpacman.board.Unit;
  7 import nl.tudelft.jpacman.sprite.AnimatedSprite;
  8 import nl.tudelft.jpacman.sprite.Sprite;
  9 
 10 /**
 11  * A player operated unit in our game.
 12  *
 13  * @author Jeroen Roosen 
 14  */
 15 public class Player extends Unit {
 16 
 17     /**
 18      * The amount of points accumulated by this player.
 19      */
 20     private int score;
 21 
 22     /**
 23      * The animations for every direction.
 24      */
 25     private final Map<Direction, Sprite> sprites;
 26 
 27     /**
 28      * The animation that is to be played when Pac-Man dies.
 29      */
 30     private final AnimatedSprite deathSprite;
 31 
 32     /**
 33      * <code>true</code> iff this player is alive.
 34      */
 35     private boolean alive;
 36 
 37     /**
 38      * {@link Unit} iff this player died by collision, <code>null</code> otherwise.
 39      */
 40     private Unit killer;
 41 
 42     /**
 43      * Creates a new player with a score of 0 points.
 44      *
 45      * @param spriteMap
 46      *            A map containing a sprite for this player for every direction.
 47      * @param deathAnimation
 48      *            The sprite to be shown when this player dies.
 49      */
 50     protected Player(Map<Direction, Sprite> spriteMap, AnimatedSprite deathAnimation) {
 51         this.score = 0;
 52         this.alive = true;
 53         this.sprites = spriteMap;
 54         this.deathSprite = deathAnimation;
 55         deathSprite.setAnimating(false);
 56     }
 57 
 58     /**
 59      * Returns whether this player is alive or not.
 60      *
 61      * @return <code>true</code> iff the player is alive.
 62      */
 63     public boolean isAlive() {
 64         return alive;
 65     }
 66 
 67     /**
 68      * Sets whether this player is alive or not.
 69      *
 70      * If the player comes back alive, the {@link killer} will be reset.
 71      *
 72      * @param isAlive
 73      *            <code>true</code> iff this player is alive.
 74      */
 75     public void setAlive(boolean isAlive) {
 76         if (isAlive) {
 77             deathSprite.setAnimating(false);
 78             this.killer = null;
 79         }
 80         if (!isAlive) {
 81             deathSprite.restart();
 82         }
 83         this.alive = isAlive;
 84     }
 85 
 86     /**
 87      * Returns the unit that caused the death of Pac-Man.
 88      *
 89      * @return <code>Unit</code> iff the player died by collision, otherwise <code>null</code>.
 90      */
 91     public Unit getKiller() {
 92         return killer;
 93     }
 94 
 95     /**
 96      * Sets the cause of death.
 97      *
 98      * @param killer is set if collision with ghost happens.
 99      */
100     public void setKiller(Unit killer) {
101         this.killer =  killer;
102     }
103 
104     /**
105      * Returns the amount of points accumulated by this player.
106      *
107      * @return The amount of points accumulated by this player.
108      */
109     public int getScore() {
110         return score;
111     }
112 
113     @Override
114     public Sprite getSprite() {
115         if (isAlive()) {
116             return sprites.get(getDirection());
117         }
118         return deathSprite;
119     }
120 
121     /**
122      * Adds points to the score of this player.
123      *
124      * @param points
125      *            The amount of points to add to the points this player already
126      *            has.
127      */
128     public void addPoints(int points) {
129         score += points;
130     }
131 }