diff options
-rw-r--r-- | src/ch/epfl/xblast/server/painter/BlockImage.java | 42 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/painter/BoardPainter.java | 40 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/painter/ExplosionPainter.java | 55 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/painter/PlayerPainter.java | 44 | ||||
-rw-r--r-- | test/ch/epfl/xblast/painter/BoardPainterTest.java | 57 | ||||
-rw-r--r-- | test/ch/epfl/xblast/painter/ExplosionPainterTest.java | 37 | ||||
-rw-r--r-- | test/ch/epfl/xblast/painter/PlayerPainterTest.java | 61 |
7 files changed, 336 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/server/painter/BlockImage.java b/src/ch/epfl/xblast/server/painter/BlockImage.java new file mode 100644 index 0000000..d5fa593 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/BlockImage.java | |||
@@ -0,0 +1,42 @@ | |||
1 | package ch.epfl.xblast.server.painter; | ||
2 | |||
3 | /** | ||
4 | * @author Timothée FLOURE (257420 | ||
5 | */ | ||
6 | public enum BlockImage { | ||
7 | |||
8 | /** | ||
9 | * Free block. | ||
10 | */ | ||
11 | IRON_FLOOR, | ||
12 | |||
13 | /** | ||
14 | * Shadowed Free block. | ||
15 | */ | ||
16 | IRON_FLOOR_S, | ||
17 | |||
18 | /** | ||
19 | * Dark Block (Indestructible wall). | ||
20 | */ | ||
21 | DARK_BLOCK, | ||
22 | |||
23 | /** | ||
24 | * Wall possibly containing a Bonus. | ||
25 | */ | ||
26 | EXTRA, | ||
27 | |||
28 | /** | ||
29 | * Crumbling Wall possibly containing a Bonus. | ||
30 | */ | ||
31 | EXTRA_O, | ||
32 | |||
33 | /** | ||
34 | * MaxBomb bonus. | ||
35 | */ | ||
36 | BONUS_BOMB, | ||
37 | |||
38 | /** | ||
39 | * Bomb Range Bonus. | ||
40 | */ | ||
41 | BONUS_RANGE | ||
42 | } | ||
diff --git a/src/ch/epfl/xblast/server/painter/BoardPainter.java b/src/ch/epfl/xblast/server/painter/BoardPainter.java new file mode 100644 index 0000000..69af129 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/BoardPainter.java | |||
@@ -0,0 +1,40 @@ | |||
1 | package ch.epfl.xblast.server.painter; | ||
2 | |||
3 | import ch.epfl.xblast.*; | ||
4 | import ch.epfl.xblast.server.*; | ||
5 | |||
6 | import java.util.Map; | ||
7 | |||
8 | /** | ||
9 | * @author Timothée FLOURE (257420) | ||
10 | */ | ||
11 | public final class BoardPainter { | ||
12 | |||
13 | private final Map<Block, BlockImage> blocksMap; | ||
14 | private final BlockImage shadowedFreeBlock; | ||
15 | |||
16 | /** | ||
17 | * Instantiates a new BoardPainter. | ||
18 | * | ||
19 | * @param blocksMap map linking block to the images | ||
20 | * @param shadowedFreeBlock a "shadowed" block image | ||
21 | */ | ||
22 | public BoardPainter(Map<Block, BlockImage> blocksMap, BlockImage shadowedFreeBlock ) { | ||
23 | this.blocksMap = blocksMap; | ||
24 | this.shadowedFreeBlock = shadowedFreeBlock; | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * Returns the bits sequence of the block related to the given Cell. | ||
29 | * | ||
30 | * @param board given board | ||
31 | * @param cell given cell | ||
32 | * @return the bits sequence related to the image of the given cell | ||
33 | */ | ||
34 | public byte byteForCell(Board board, Cell cell) { | ||
35 | if (board.blockAt(cell).isFree() && board.blockAt(cell.neighbor(Direction.W)).castsShadow()) | ||
36 | return (byte) shadowedFreeBlock.ordinal(); | ||
37 | |||
38 | return (byte) this.blocksMap.get(board.blockAt(cell)).ordinal(); | ||
39 | } | ||
40 | } | ||
diff --git a/src/ch/epfl/xblast/server/painter/ExplosionPainter.java b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java new file mode 100644 index 0000000..63181e9 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java | |||
@@ -0,0 +1,55 @@ | |||
1 | package ch.epfl.xblast.server.painter; | ||
2 | |||
3 | import ch.epfl.xblast.server.*; | ||
4 | |||
5 | /** | ||
6 | * @author Timothée FLOURE (257420) | ||
7 | */ | ||
8 | public final class ExplosionPainter { | ||
9 | private static final byte BLACK_BOMB_IMAGE = 20; | ||
10 | private static final byte WHITE_BOMB_IMAGE = 21; | ||
11 | |||
12 | private static final byte NORTH_EXPLOSION = 0b1000; | ||
13 | private static final byte EAST_EXPLOSION = 0b0100; | ||
14 | private static final byte SOUTH_EXPLOSION = 0b0010; | ||
15 | private static final byte WEST_EXPLOSION = 0b0001; | ||
16 | |||
17 | /** | ||
18 | * Return the image corresponding to a bomb depending on its fuse length. | ||
19 | * | ||
20 | * @param bomb given bomb | ||
21 | * @return the byte related to the image of the bomb (black or white) | ||
22 | */ | ||
23 | public static byte byteForBomb(Bomb bomb) | ||
24 | { | ||
25 | if (Integer.bitCount(bomb.fuseLength()) == 1 ) { | ||
26 | return WHITE_BOMB_IMAGE; | ||
27 | } else { | ||
28 | return BLACK_BOMB_IMAGE; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Return the image corresponding to the explosion on a cell given the explosions of its neighbors. | ||
34 | * | ||
35 | * @param north is an explosion on the north cell | ||
36 | * @param east is an explosion on the east cell | ||
37 | * @param south is an explosion on the south cell | ||
38 | * @param west is an explosion on the west cell | ||
39 | * @return the byte corresponding to the related image | ||
40 | */ | ||
41 | public static byte byteForBlast(boolean north, boolean east, boolean south, boolean west) { | ||
42 | byte correspondingByte = 0b0; | ||
43 | |||
44 | if (north) | ||
45 | correspondingByte += NORTH_EXPLOSION; | ||
46 | if (east) | ||
47 | correspondingByte += EAST_EXPLOSION; | ||
48 | if (south) | ||
49 | correspondingByte += SOUTH_EXPLOSION; | ||
50 | if (west) | ||
51 | correspondingByte += WEST_EXPLOSION; | ||
52 | |||
53 | return correspondingByte; | ||
54 | } | ||
55 | } | ||
diff --git a/src/ch/epfl/xblast/server/painter/PlayerPainter.java b/src/ch/epfl/xblast/server/painter/PlayerPainter.java new file mode 100644 index 0000000..4f28554 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/PlayerPainter.java | |||
@@ -0,0 +1,44 @@ | |||
1 | package ch.epfl.xblast.server.painter; | ||
2 | |||
3 | import ch.epfl.xblast.server.*; | ||
4 | |||
5 | /** | ||
6 | * @author Timothée FLOURE (257420) | ||
7 | */ | ||
8 | public final class PlayerPainter { | ||
9 | private static final byte DEAD_PLAYER = 16; | ||
10 | private static final byte DYING_IMAGE_ID = 12; | ||
11 | private static final byte LAST_DYING_IMAGE_ID = 13; | ||
12 | private static final byte PLAYER_MULTIPLIER = 20; | ||
13 | |||
14 | /** | ||
15 | * Returns the byte related to the image corresponding to the actual state of the given player. | ||
16 | * | ||
17 | * @param player the given player | ||
18 | * @param tick the actual tick of the game | ||
19 | * @return the byte related to the image of the the actual state of the player | ||
20 | */ | ||
21 | public static byte byteForPlayer(Player player, int tick) { | ||
22 | if (!player.isAlive()) | ||
23 | return DEAD_PLAYER; | ||
24 | |||
25 | byte correspondingByte = (byte) (player.id().ordinal() * PLAYER_MULTIPLIER); | ||
26 | |||
27 | if (player.lifeState().state() == Player.LifeState.State.DYING) | ||
28 | if (player.lives() > 0) | ||
29 | return (byte) (correspondingByte + DYING_IMAGE_ID); | ||
30 | else | ||
31 | return (byte) (correspondingByte + LAST_DYING_IMAGE_ID ); | ||
32 | |||
33 | correspondingByte += player.direction().ordinal() * 3; | ||
34 | |||
35 | switch (tick % 4) { // Magic Numbers !!! | ||
36 | case 1: | ||
37 | return (byte) (correspondingByte + 1); | ||
38 | case 3: | ||
39 | return (byte) (correspondingByte + 2); | ||
40 | default: | ||
41 | return (byte) (correspondingByte + 0); | ||
42 | } | ||
43 | } | ||
44 | } | ||
diff --git a/test/ch/epfl/xblast/painter/BoardPainterTest.java b/test/ch/epfl/xblast/painter/BoardPainterTest.java new file mode 100644 index 0000000..9ac8ad4 --- /dev/null +++ b/test/ch/epfl/xblast/painter/BoardPainterTest.java | |||
@@ -0,0 +1,57 @@ | |||
1 | package ch.epfl.xblast.painter; | ||
2 | |||
3 | import ch.epfl.xblast.*; | ||
4 | import ch.epfl.xblast.server.*; | ||
5 | import ch.epfl.xblast.server.painter.*; | ||
6 | |||
7 | import java.util.Arrays; | ||
8 | import java.util.HashMap; | ||
9 | |||
10 | import org.junit.Assert; | ||
11 | import org.junit.Test; | ||
12 | |||
13 | /** | ||
14 | * @author Timothée Floure (257420) | ||
15 | */ | ||
16 | public class BoardPainterTest { | ||
17 | |||
18 | public static Board createBoard() { | ||
19 | Block __ = Block.FREE; | ||
20 | Block XX = Block.INDESTRUCTIBLE_WALL; | ||
21 | Block xx = Block.DESTRUCTIBLE_WALL; | ||
22 | return Board.ofQuadrantNWBlocksWalled(Arrays.asList(Arrays.asList(__, __, __, __, __, xx, __), | ||
23 | Arrays.asList(__, XX, xx, XX, xx, XX, xx), Arrays.asList(__, xx, __, __, __, xx, __), | ||
24 | Arrays.asList(xx, XX, __, XX, XX, XX, XX), Arrays.asList(__, xx, __, xx, __, __, __), | ||
25 | Arrays.asList(xx, XX, xx, XX, xx, XX, __))); | ||
26 | } | ||
27 | |||
28 | @Test | ||