Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added lives func #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void smokeTest() throws InterruptedException {
// we're close to monsters, this will get us killed.
move(game, Direction.WEST, 10);
move(game, Direction.EAST, 10);
assertThat(player.isAlive()).isFalse();
assertThat(player.getLives()).isLessThan(3);

game.stop();
assertThat(game.isInProgress()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ private CollisionInteractionMap defaultCollisions() {
collisionMap.onCollision(Player.class, Ghost.class,
(player, ghost) -> {
pointCalculator.collidedWithAGhost(player, ghost);
player.setAlive(false);
player.setKiller(ghost);
player.decrementLives();
if (!player.isAlive()) {
player.setKiller(ghost);
}
});

collisionMap.onCollision(Player.class, Pellet.class,
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/nl/tudelft/jpacman/level/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Player extends Unit {
* <code>true</code> iff this player is alive.
*/
private boolean alive;
public static final int INITIAL_LIVES = 3;
private int lives;

/**
* {@link Unit} iff this player died by collision, <code>null</code> otherwise.
Expand All @@ -49,7 +51,7 @@ public class Player extends Unit {
*/
protected Player(Map<Direction, Sprite> spriteMap, AnimatedSprite deathAnimation) {
this.score = 0;
this.alive = true;
this.lives = INITIAL_LIVES;
this.sprites = spriteMap;
this.deathSprite = deathAnimation;
deathSprite.setAnimating(false);
Expand All @@ -61,26 +63,26 @@ protected Player(Map<Direction, Sprite> spriteMap, AnimatedSprite deathAnimation
* @return <code>true</code> iff the player is alive.
*/
public boolean isAlive() {
return alive;
return lives > 0;
}

/**
* Sets whether this player is alive or not.
*
* If the player comes back alive, the {@link killer} will be reset.
*
* @param isAlive
* <code>true</code> iff this player is alive.
*/
public void setAlive(boolean isAlive) {
if (isAlive) {
public int getLives() {
return this.lives;
}

public void setLives(int lives) {
if (lives == 0) {
deathSprite.restart();
} else {
deathSprite.setAnimating(false);
this.killer = null;
}
if (!isAlive) {
deathSprite.restart();
}
this.alive = isAlive;

if (lives >= 0) this.lives = lives;
}

public void decrementLives() {
setLives((lives - 1));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/nl/tudelft/jpacman/level/PlayerCollisions.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ private void pelletColliding(Pellet pellet, Unit collidedOn) {
*/
public void playerVersusGhost(Player player, Ghost ghost) {
pointCalculator.collidedWithAGhost(player, ghost);
player.setAlive(false);
player.setKiller(ghost);
player.decrementLives();
if (!player.isAlive()) {
player.setKiller(ghost);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/nl/tudelft/jpacman/ui/ScorePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ScorePanel extends JPanel {
*/
private final Map<Player, JLabel> scoreLabels;

/**
* The map of players and the labels their lives are on.
*/
private final Map<Player, JLabel> livesLabels;

/**
* The default way in which the score is shown.
*/
Expand Down Expand Up @@ -61,6 +66,13 @@ public ScorePanel(List<Player> players) {
scoreLabels.put(player, scoreLabel);
add(scoreLabel);
}

livesLabels = new LinkedHashMap<>();
for (Player player : players) {
JLabel livesLabel = new JLabel(Integer.toString(player.getLives()), JLabel.CENTER);
livesLabels.put(player, livesLabel);
add(livesLabel);
}
}

/**
Expand All @@ -76,6 +88,12 @@ protected void refresh() {
score += scoreFormatter.format(player);
entry.getValue().setText(score);
}

for (Map.Entry<Player, JLabel> entry : livesLabels.entrySet()) {
Player player = entry.getKey();
String lives = "Lives : " + player.getLives();
entry.getValue().setText(lives);
}
}

/**
Expand Down