From f3c51f3143679521462d6a25055ac6629a0d23d3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 12:52:18 +0200 Subject: Refactor LifeState sequence building --- src/ch/epfl/xblast/server/Player.java | 44 ++++++++++++----------------------- 1 file changed, 15 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 2532773..ad1dfe8 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -42,12 +42,13 @@ public final class Player { public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { this( id, - Player.buildDefaultLifeStateSequence(lives), + Player.buildLifeStateSequence(lives), Player.buildDefaultDirectedPositionSequence(position), maxBombs, bombRange ); } + /** * Instantiates a new Player. * @@ -73,19 +74,16 @@ public final class Player { * @param lives number of lives of the desired sequence * @return the sequence */ - private static Sq buildDefaultLifeStateSequence(int lives) { - int l = ArgumentChecker.requireNonNegative(lives); - - switch (l) { - case 0: - return Sq.constant(new LifeState(l, LifeState.State.DEAD)); + private static Sq buildLifeStateSequence(int lives) { + if (ArgumentChecker.requireNonNegative(lives) <= 0) + return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); - default: - LifeState invulnerability = new LifeState(l, LifeState.State.INVULNERABLE); - LifeState vulnerability = new LifeState(l, LifeState.State.VULNERABLE); - return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability).concat(Sq.constant(vulnerability)); + LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); + LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); - } + return Sq + .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) + .concat(Sq.constant(vulnerability)); } /** @@ -97,8 +95,7 @@ public final class Player { private static Sq buildDefaultDirectedPositionSequence(Cell pos) { DirectedPosition dp = new DirectedPosition( SubCell.centralSubCellOf(Objects.requireNonNull(pos)), - Player.DEFAULT_DIRECTION - ); + Player.DEFAULT_DIRECTION); return DirectedPosition.stopped(dp); } @@ -110,22 +107,10 @@ public final class Player { */ public Sq statesForNextLife() { LifeState dying = new LifeState(lives(), LifeState.State.DYING); - Sq nextLifeState = Sq.repeat(Ticks.PLAYER_DYING_TICKS, dying); - - int newLives = lives() - 1; - if (newLives <= 0) { - LifeState dead = new LifeState(newLives, LifeState.State.DEAD); - nextLifeState = nextLifeState.concat(Sq.constant(dead)); - } else { - LifeState invulnerable = new LifeState(newLives, LifeState.State.INVULNERABLE); - LifeState vulnerable = new LifeState(newLives, LifeState.State.VULNERABLE); - - nextLifeState = nextLifeState.concat(Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerable)); - nextLifeState = nextLifeState.concat(Sq.constant(vulnerable)); - } - - return nextLifeState; + return Sq + .repeat(Ticks.PLAYER_DYING_TICKS, dying) + .concat(Player.buildLifeStateSequence(lives() - 1)); } /** @@ -362,6 +347,7 @@ public final class Player { /** * Builds and returns an infinite sequence of directed positions corresponding to a stopped player. + * * @param p the infinitely repeated element of the Sequence * @return the sequence */ -- cgit v1.2.3