diff options
author | Pacien TRAN-GIRARD | 2016-05-05 12:52:18 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-05 12:52:18 +0200 |
commit | f3c51f3143679521462d6a25055ac6629a0d23d3 (patch) | |
tree | e883c0d1d332cd91f5d2395785dcac40f37ac147 /src/ch | |
parent | da02a1baebad3ec350694ee097d9fc25612d8bdc (diff) | |
download | xblast-f3c51f3143679521462d6a25055ac6629a0d23d3.tar.gz |
Refactor LifeState sequence building
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/server/Player.java | 44 |
1 files changed, 15 insertions, 29 deletions
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 { | |||
42 | public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { | 42 | public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { |
43 | this( | 43 | this( |
44 | id, | 44 | id, |
45 | Player.buildDefaultLifeStateSequence(lives), | 45 | Player.buildLifeStateSequence(lives), |
46 | Player.buildDefaultDirectedPositionSequence(position), | 46 | Player.buildDefaultDirectedPositionSequence(position), |
47 | maxBombs, | 47 | maxBombs, |
48 | bombRange | 48 | bombRange |
49 | ); | 49 | ); |
50 | } | 50 | } |
51 | |||
51 | /** | 52 | /** |
52 | * Instantiates a new Player. | 53 | * Instantiates a new Player. |
53 | * | 54 | * |
@@ -73,19 +74,16 @@ public final class Player { | |||
73 | * @param lives number of lives of the desired sequence | 74 | * @param lives number of lives of the desired sequence |
74 | * @return the sequence | 75 | * @return the sequence |
75 | */ | 76 | */ |
76 | private static Sq<LifeState> buildDefaultLifeStateSequence(int lives) { | 77 | private static Sq<LifeState> buildLifeStateSequence(int lives) { |
77 | int l = ArgumentChecker.requireNonNegative(lives); | 78 | if (ArgumentChecker.requireNonNegative(lives) <= 0) |
78 | 79 | return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); | |
79 | switch (l) { | ||
80 | case 0: | ||
81 | return Sq.constant(new LifeState(l, LifeState.State.DEAD)); | ||
82 | 80 | ||
83 | default: | 81 | LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); |
84 | LifeState invulnerability = new LifeState(l, LifeState.State.INVULNERABLE); | 82 | LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); |
85 | LifeState vulnerability = new LifeState(l, LifeState.State.VULNERABLE); | ||
86 | return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability).concat(Sq.constant(vulnerability)); | ||
87 | 83 | ||
88 | } | 84 | return Sq |
85 | .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) | ||
86 | .concat(Sq.constant(vulnerability)); | ||
89 | } | 87 | } |
90 | 88 | ||
91 | /** | 89 | /** |
@@ -97,8 +95,7 @@ public final class Player { | |||
97 | private static Sq<DirectedPosition> buildDefaultDirectedPositionSequence(Cell pos) { | 95 | private static Sq<DirectedPosition> buildDefaultDirectedPositionSequence(Cell pos) { |
98 | DirectedPosition dp = new DirectedPosition( | 96 | DirectedPosition dp = new DirectedPosition( |
99 | SubCell.centralSubCellOf(Objects.requireNonNull(pos)), | 97 | SubCell.centralSubCellOf(Objects.requireNonNull(pos)), |
100 | Player.DEFAULT_DIRECTION | 98 | Player.DEFAULT_DIRECTION); |
101 | ); | ||
102 | 99 | ||
103 | return DirectedPosition.stopped(dp); | 100 | return DirectedPosition.stopped(dp); |
104 | } | 101 | } |
@@ -110,22 +107,10 @@ public final class Player { | |||
110 | */ | 107 | */ |
111 | public Sq<LifeState> statesForNextLife() { | 108 | public Sq<LifeState> statesForNextLife() { |
112 | LifeState dying = new LifeState(lives(), LifeState.State.DYING); | 109 | LifeState dying = new LifeState(lives(), LifeState.State.DYING); |
113 | Sq<LifeState> nextLifeState = Sq.repeat(Ticks.PLAYER_DYING_TICKS, dying); | ||
114 | |||
115 | int newLives = lives() - 1; | ||
116 | 110 | ||
117 | if (newLives <= 0) { | 111 | return Sq |
118 | LifeState dead = new LifeState(newLives, LifeState.State.DEAD); | 112 | .repeat(Ticks.PLAYER_DYING_TICKS, dying) |
119 | nextLifeState = nextLifeState.concat(Sq.constant(dead)); | 113 | .concat(Player.buildLifeStateSequence(lives() - 1)); |
120 | } else { | ||
121 | LifeState invulnerable = new LifeState(newLives, LifeState.State.INVULNERABLE); | ||
122 | LifeState vulnerable = new LifeState(newLives, LifeState.State.VULNERABLE); | ||
123 | |||
124 | nextLifeState = nextLifeState.concat(Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerable)); | ||
125 | nextLifeState = nextLifeState.concat(Sq.constant(vulnerable)); | ||
126 | } | ||
127 | |||
128 | return nextLifeState; | ||
129 | } | 114 | } |
130 | 115 | ||
131 | /** | 116 | /** |
@@ -362,6 +347,7 @@ public final class Player { | |||
362 | 347 | ||
363 | /** | 348 | /** |
364 | * Builds and returns an infinite sequence of directed positions corresponding to a stopped player. | 349 | * Builds and returns an infinite sequence of directed positions corresponding to a stopped player. |
350 | * | ||
365 | * @param p the infinitely repeated element of the Sequence | 351 | * @param p the infinitely repeated element of the Sequence |
366 | * @return the sequence | 352 | * @return the sequence |
367 | */ | 353 | */ |