diff options
Diffstat (limited to 'src/ch/epfl/xblast/server/Player.java')
-rw-r--r-- | src/ch/epfl/xblast/server/Player.java | 95 |
1 files changed, 61 insertions, 34 deletions
diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index ad1dfe8..46e254d 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java | |||
@@ -3,7 +3,11 @@ package ch.epfl.xblast.server; | |||
3 | import ch.epfl.cs108.Sq; | 3 | import ch.epfl.cs108.Sq; |
4 | import ch.epfl.xblast.*; | 4 | import ch.epfl.xblast.*; |
5 | 5 | ||
6 | import java.util.Arrays; | ||
7 | import java.util.Collections; | ||
8 | import java.util.List; | ||
6 | import java.util.Objects; | 9 | import java.util.Objects; |
10 | import java.util.stream.Collectors; | ||
7 | 11 | ||
8 | /** | 12 | /** |
9 | * A Player. | 13 | * A Player. |
@@ -13,10 +17,65 @@ import java.util.Objects; | |||
13 | */ | 17 | */ |
14 | public final class Player { | 18 | public final class Player { |
15 | 19 | ||
20 | private static final int INITIAL_LIVES = 3; | ||
21 | private static final int INITIAL_MAX_BOMB = 5; | ||
22 | private static final int INITIAL_BOMB_RANGE = 5; | ||
23 | private static final Direction INITIAL_DIRECTION = Direction.S; | ||
24 | |||
25 | static final List<Player> DEFAULT_PLAYERS = buildDefaultPlayers(); | ||
26 | |||
27 | /** | ||
28 | * Builds the default players of the games. | ||
29 | * | ||
30 | * @return a list of the 4 players built using the default parameters. | ||
31 | */ | ||
32 | private static List<Player> buildDefaultPlayers() { | ||
33 | return Collections.unmodifiableList(Arrays.stream(PlayerID.values()) | ||
34 | .map(Player::buildDefaultPlayer) | ||
35 | .collect(Collectors.toList())); | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Builds a player with default initial characteristics. | ||
40 | * | ||
41 | * @param id the player id | ||
42 | * @return the player | ||
43 | */ | ||
44 | private static Player buildDefaultPlayer(PlayerID id) { | ||
45 | return new Player(id, INITIAL_LIVES, id.initialPosition(), INITIAL_MAX_BOMB, INITIAL_BOMB_RANGE); | ||
46 | } | ||
47 | |||
16 | /** | 48 | /** |
17 | * The default Direction of a new Player. | 49 | * Builds a default LifeState sequence with the given number of lives. |
50 | * | ||
51 | * @param lives number of lives of the desired sequence | ||
52 | * @return the sequence | ||
18 | */ | 53 | */ |
19 | private static final Direction DEFAULT_DIRECTION = Direction.S; | 54 | private static Sq<LifeState> buildLifeStateSequence(int lives) { |
55 | if (ArgumentChecker.requireNonNegative(lives) <= 0) | ||
56 | return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); | ||
57 | |||
58 | LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); | ||
59 | LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); | ||
60 | |||
61 | return Sq | ||
62 | .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) | ||
63 | .concat(Sq.constant(vulnerability)); | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * Builds a default DirectedPosition sequence at the given position. | ||
68 | * | ||
69 | * @param pos the position | ||
70 | * @return the sequence | ||
71 | */ | ||
72 | private static Sq<DirectedPosition> buildDefaultDirectedPositionSequence(Cell pos) { | ||
73 | DirectedPosition dp = new DirectedPosition( | ||
74 | SubCell.centralSubCellOf(Objects.requireNonNull(pos)), | ||
75 | Player.INITIAL_DIRECTION); | ||
76 | |||
77 | return DirectedPosition.stopped(dp); | ||
78 | } | ||
20 | 79 | ||
21 | private final PlayerID id; | 80 | private final PlayerID id; |
22 | private final Sq<LifeState> lifeStates; | 81 | private final Sq<LifeState> lifeStates; |
@@ -69,38 +128,6 @@ public final class Player { | |||
69 | } | 128 | } |
70 | 129 | ||
71 | /** | 130 | /** |
72 | * Builds a default LifeState sequence with the given number of lives. | ||
73 | * | ||
74 | * @param lives number of lives of the desired sequence | ||
75 | * @return the sequence | ||
76 | */ | ||
77 | private static Sq<LifeState> buildLifeStateSequence(int lives) { | ||
78 | if (ArgumentChecker.requireNonNegative(lives) <= 0) | ||
79 | return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); | ||
80 | |||
81 | LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); | ||
82 | LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); | ||
83 | |||
84 | return Sq | ||
85 | .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) | ||
86 | .concat(Sq.constant(vulnerability)); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Builds a default DirectedPosition sequence at the given position. | ||
91 | * | ||
92 | * @param pos the position | ||
93 | * @return the sequence | ||
94 | */ | ||
95 | private static Sq<DirectedPosition> buildDefaultDirectedPositionSequence(Cell pos) { | ||
96 | DirectedPosition dp = new DirectedPosition( | ||
97 | SubCell.centralSubCellOf(Objects.requireNonNull(pos)), | ||
98 | Player.DEFAULT_DIRECTION); | ||
99 | |||
100 | return DirectedPosition.stopped(dp); | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Builds and returns the Player's next life LifeState sequence. | 131 | * Builds and returns the Player's next life LifeState sequence. |
105 | * | 132 | * |
106 | * @return the sequence related to the Player's next life | 133 | * @return the sequence related to the Player's next life |