diff options
author | Timothée Floure | 2016-05-09 16:19:56 +0200 |
---|---|---|
committer | Timothée Floure | 2016-05-09 16:19:56 +0200 |
commit | 9850fe44401a1401aef5e6bbf683b507befa03dd (patch) | |
tree | 960d23a626cbc44113b7701eb0a04d71618897de | |
parent | c27e85228fe3c7de8465959940c1cde64bdb7887 (diff) | |
download | xblast-9850fe44401a1401aef5e6bbf683b507befa03dd.tar.gz |
Implementation of the XBlastComponent Class
-rw-r--r-- | src/ch/epfl/xblast/client/XBlastComponent.java | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/XBlastComponent.java b/src/ch/epfl/xblast/client/XBlastComponent.java new file mode 100644 index 0000000..ac9f223 --- /dev/null +++ b/src/ch/epfl/xblast/client/XBlastComponent.java | |||
@@ -0,0 +1,171 @@ | |||
1 | package ch.epfl.xblast.client; | ||
2 | |||
3 | import ch.epfl.xblast.PlayerID; | ||
4 | |||
5 | import javax.swing.*; | ||
6 | import java.awt.*; | ||
7 | import java.util.List; | ||
8 | |||
9 | /** | ||
10 | * @author Timothée FLOURE (257420) | ||
11 | */ | ||
12 | public final class XBlastComponent extends JComponent { | ||
13 | private static final int PREFERRED_WIDTH = 960; | ||
14 | private static final int PREFERRED_HEIGHT = 688; | ||
15 | private static final int BLOCK_WIDTH = 64; | ||
16 | private static final int BLOCK_HEIGHT = 48; | ||
17 | private static final int SCORE_IMAGE_WIDTH = 48; | ||
18 | private static final int SCORE_IMAGE_HEIGHT = 48; | ||
19 | private static final int TIME_LED_WIDTH = 16; | ||
20 | private static final int TIME_LED_HEIGHT = 16; | ||
21 | |||
22 | private static final Font font = new Font("Arial", Font.BOLD, 25); | ||
23 | private static final int SCORES_TEXT_VERTICAL_POSITION = 659; | ||
24 | private static final int SCORES_P1_TEXT_HORIZONTAL_POSITION = 96; | ||
25 | private static final int SCORES_P2_TEXT_HORIZONTAL_POSITION = 240; | ||
26 | private static final int SCORES_P3_TEXT_HORIZONTAL_POSITION = 768; | ||
27 | private static final int SCORES_P4_TEXT_HORIZONTAL_POSITION = 912; | ||
28 | |||
29 | /** | ||
30 | * Displayed gameState. | ||
31 | */ | ||
32 | private GameState gamestate; | ||
33 | |||
34 | /** | ||
35 | * Display a list of images on the given graphic context. | ||
36 | * | ||
37 | * @param g the graphic context | ||
38 | * @param images the given list of images, ordered in row-major order | ||
39 | */ | ||
40 | private void drawMap(Graphics2D g, List<Image> images) { | ||
41 | int x = 0; | ||
42 | int y = 0; | ||
43 | |||
44 | for (Image img : images) { | ||
45 | if (x + BLOCK_WIDTH > PREFERRED_WIDTH) { | ||
46 | y += BLOCK_HEIGHT; // Or img.getHeight(null) ? | ||
47 | x = 0; | ||
48 | } | ||
49 | |||
50 | if (img != null) | ||
51 | g.drawImage(img, x, y, null); | ||
52 | |||
53 | x += BLOCK_WIDTH; // Or img.getWidth(null) ? | ||
54 | } | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Compute the horizontal position of the player in px. | ||
59 | * | ||
60 | * @param player the given player | ||
61 | * @return the horizontal position in pixel | ||
62 | */ | ||
63 | private int playerXPosition(GameState.Player player) { return 4 * player.position().x() - 24; } | ||
64 | |||
65 | /** | ||
66 | * Compute the vertical position of the player in px. | ||
67 | * | ||
68 | * @param player the given player | ||
69 | * @return the vertical position in pixel | ||
70 | */ | ||
71 | private int playerYPosition(GameState.Player player) { return 3 * player.position().y() - 52; } | ||
72 | |||
73 | /** | ||
74 | * Draw the players on the graphic context. | ||
75 | * | ||
76 | * @param g the graphic context | ||
77 | * @param players the list of players to be displayed | ||
78 | */ | ||
79 | private void drawPlayers(Graphics2D g, List<GameState.Player> players) { | ||
80 | for (GameState.Player player : players) { | ||
81 | g.drawImage(player.image(), playerXPosition(player), playerYPosition(player), null); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Draw the scores on the graphic context. | ||
87 | * | ||
88 | * @param g the graphic context | ||
89 | * @param scores the list of images composing the scores to be displayed | ||
90 | */ | ||
91 | private void drawScores(Graphics2D g, List<Image> scores) { | ||
92 | int x = 0; | ||
93 | int y = PREFERRED_HEIGHT - SCORE_IMAGE_HEIGHT - TIME_LED_HEIGHT; | ||
94 | for (Image img : scores) { | ||
95 | g.drawImage(img, x, y, null); | ||
96 | x += SCORE_IMAGE_WIDTH; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Write the remaining lives of each player on the graphic context. | ||
102 | * | ||
103 | * @param g the graphic context | ||
104 | * @param players list of players | ||
105 | */ | ||
106 | private void writeScores(Graphics2D g,List<GameState.Player> players) { | ||
107 | g.setColor(Color.WHITE); | ||
108 | g.setFont(font); | ||
109 | |||
110 | // Ugly !!!!! | ||
111 | g.drawString(Integer.toString(players.get(0).lives()), SCORES_P1_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); | ||
112 | g.drawString(Integer.toString(players.get(1).lives()), SCORES_P2_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); | ||
113 | g.drawString(Integer.toString(players.get(2).lives()), SCORES_P3_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); | ||
114 | g.drawString(Integer.toString(players.get(3).lives()), SCORES_P4_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION); | ||
115 | |||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Draw the time "line" on the graphic context | ||
120 | * | ||
121 | * @param g the graphic context | ||
122 | * @param time the list of images composing the line | ||
123 | */ | ||
124 | private void drawTime(Graphics2D g, List<Image> time) { | ||
125 | int x = 0; | ||
126 | int y = PREFERRED_HEIGHT - TIME_LED_HEIGHT; | ||
127 | for (Image img : time) { | ||
128 | g.drawImage(img, x, y, null); | ||
129 | x += TIME_LED_WIDTH; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | /** | ||
134 | * Display the given GameState from the point of view of the given player. | ||
135 | * | ||
136 | * @param gameState GameState to be displayer | ||
137 | * @param player player related to the view | ||
138 | */ | ||
139 | public void setGameState(GameState gameState, PlayerID player) { | ||
140 | this.gamestate = gameState; | ||
141 | repaint(); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Returns the value of the preferredSize property. | ||
146 | * | ||
147 | * @return the value of the preferredSize property | ||
148 | */ | ||
149 | @Override | ||
150 | public Dimension getPreferredSize() { | ||
151 | return new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Draw the component. | ||
156 | * | ||
157 | * @param g0 the Graphics context | ||
158 | */ | ||
159 | @Override | ||
160 | protected void paintComponent(Graphics g0) { | ||
161 | if (this.gamestate != null) { | ||
162 | Graphics2D g = (Graphics2D) g0; | ||
163 | drawMap(g, this.gamestate.board()); | ||
164 | drawMap(g, this.gamestate.explosions()); | ||
165 | drawPlayers(g, this.gamestate.players()); | ||
166 | drawScores(g, this.gamestate.scores()); | ||
167 | writeScores(g, this.gamestate.players()); | ||
168 | drawTime(g, this.gamestate.ticks()); | ||
169 | }; | ||
170 | } | ||
171 | } | ||