d3d51d1265864b5df8c53e1fc44621e93cbcd0ca
[troll.git] / src / main / java / com / codingame / game / View.java
1 package com.codingame.game;
2
3 import java.util.Random;
4 import java.util.ArrayList;
5 import java.util.Comparator;
6
7 import com.codingame.gameengine.core.MultiplayerGameManager;
8 import com.codingame.gameengine.module.entities.GraphicEntityModule;
9 import com.codingame.gameengine.module.entities.Rectangle;
10 import com.codingame.gameengine.module.entities.Sprite;
11 import com.codingame.gameengine.module.entities.Text;
12 import com.codingame.gameengine.module.entities.Group;
13 import com.codingame.gameengine.module.entities.Curve;
14 import com.google.inject.Inject;
15
16 class View {
17     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
18     @Inject private GraphicEntityModule graphicEntityModule;
19
20     class Player {
21         Model.Player model;
22
23         int colorToken;
24         String nicknameToken;
25         String avatarToken;
26
27         double frameRot;
28
29         Group avatar;
30         Text stoneCounter;
31         Text message;
32         Sprite castle;
33         Text stone;
34         Text stoneReminder;
35
36         void init(com.codingame.game.Player p) {
37             model = p.model;
38             colorToken = p.getColorToken();
39             nicknameToken = p.getNicknameToken();
40             avatarToken = p.getAvatarToken();
41
42             boolean p0 = model.index == 0;
43             int x = p0 ? 280 : 1920 - 280;
44             int y = 220;
45
46             Sprite frame = graphicEntityModule.createSprite()
47                 .setImage("frame.png")
48                 .setAnchor(0.5)
49                 .setRotation(frameRot)
50                 .setZIndex(22)
51                 .setTint(colorToken);
52
53             Sprite frameBg = graphicEntityModule.createSprite()
54                 .setImage("frame_bg.png")
55                 .setAnchor(0.5)
56                 .setRotation(frameRot)
57                 .setZIndex(20);
58
59             Sprite avatarSprite = graphicEntityModule.createSprite()
60                 .setZIndex(21)
61                 .setImage(avatarToken)
62                 .setAnchor(0.5)
63                 .setBaseHeight(116)
64                 .setBaseWidth(116);
65
66             avatar = graphicEntityModule
67                 .createGroup(frame, frameBg, avatarSprite)
68                 .setX(x).setY(y);
69
70             Text text = graphicEntityModule.createText(nicknameToken)
71                     .setX(x)
72                     .setY(y + 120)
73                     .setZIndex(20)
74                     .setFontSize(40)
75                     .setFillColor(0x7f3f00)
76                     .setAnchor(0.5);
77
78             stoneCounter = graphicEntityModule.createText()
79                 .setX(x)
80                 .setY(y+200)
81                 .setZIndex(20)
82                 .setFontSize(40)
83                 .setFillColor(0x7f3f00)
84                 .setAnchor(0.5);
85             updateStoneCounter();
86
87             message = graphicEntityModule.createText()
88                 .setX(p0 ? 15 : 1920-15)
89                 .setY(680)
90                 .setZIndex(1)
91                 .setFontSize(40)
92                 .setFillColor(0xffbf7f)
93                 .setAnchorX(p0 ? 0 : 1)
94                 .setAnchorY(1);
95
96             castle = graphicEntityModule.createSprite()
97                 .setImage("castle.png")
98                 .setTint(colorToken)
99                 .setX(p0 ? 160 : 1920-160)
100                 .setY(p0 ? 890 : 880)
101                 .setZIndex(1)
102                 .setAnchorX(0.5)
103                 .setAnchorY(1)
104                 .setScaleX(p0 ? 1 : -1);
105
106             stone = graphicEntityModule.createText()
107                 .setZIndex(3)
108                 .setFontSize(150)
109                 .setFillColor(0x12322a)
110                 .setAnchor(0.5)
111                 .setAlpha(0);
112
113             stoneReminder = graphicEntityModule.createText()
114                 .setX(p0 ? x + 100 : x - 100)
115                 .setY(y)
116                 .setZIndex(20)
117                 .setFontSize(75)
118                 .setFillColor(0x3f3f3f)
119                 .setAnchorX(p0 ? 0 : 1)
120                 .setAnchorY(0.5);
121         }
122
123         void updateStoneCounter() {
124             int stones = model.getStones();
125             if (stones <= 0) {
126                 stoneCounter.setText("Out of stones!");
127                 stoneCounter.setFillColor(0xff7777);
128             }
129             else if (stones == 1) {
130                 stoneCounter.setText("1 stone");
131                 stoneCounter.setFillColor(0xffbb77);
132             }
133             else {
134                 stoneCounter.setText(stones + " stones");
135             }
136         }
137
138         void animateStones(int stones) {
139             String stonesString = new Integer(stones).toString();
140             stone.setX(castle.getX());
141             stone.setY(castle.getY() - 100);
142             stone.setText(stonesString);
143             stone.setAlpha(1);
144             graphicEntityModule.commitEntityState(0, stone);
145     
146             int peakX = (castle.getX() + troll.getX()) / 2;
147             int peakY = 540;
148             stone.setX(peakX);
149             stone.setY(peakY, Curve.EASE_OUT);
150             graphicEntityModule.commitEntityState(0.25,
151                                                   stone,
152                                                   stoneCounter);
153     
154             stone.setX(troll.getX());
155             stone.setY(troll.getY() - 50, Curve.EASE_IN);
156             stone.setAlpha(0, Curve.EASE_IN);
157             graphicEntityModule.commitEntityState(0.5, stone);
158
159             stoneReminder.setText(stonesString);
160             graphicEntityModule.commitEntityState(0.25, stoneReminder);
161             stoneReminder.setAlpha(1);
162             graphicEntityModule.commitEntityState(0.5, stoneReminder);
163         }
164
165         void displayMessage(String msg) {
166             message.setText(msg);
167             graphicEntityModule.commitEntityState(0, message);
168         }
169
170         void markLoser() {
171             graphicEntityModule.commitEntityState(0.5, avatar);
172             int dir = random.nextInt(2) == 1 ? 1 : -1;
173             avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
174         }
175
176         void destroy() {
177             graphicEntityModule.commitEntityState(0.5, castle);
178             castle.setX(castle.getX(), Curve.ELASTIC);
179             castle.setScaleY(-0.2, Curve.EASE_IN);
180         }
181
182         void startTurn() {
183             stoneReminder.setAlpha(0);
184             graphicEntityModule.commitEntityState(0, stoneReminder);
185         }
186     }
187
188     Model model;
189     Random random = new Random();
190     Sprite troll;
191     Text trollPositionGauge;
192     Player p0 = new Player(), p1 = new Player();
193
194     void init(Model m) {
195         model = m;
196         drawBackground();
197
198         /*
199          * Random π/2-grained rotation of the avatar frames.  Avoid
200          * having them π/2 apart, though, as one of them is likely
201          * going to end upside-down and the trick would be revealed.
202          * And I'd have to "draw" a new frame. Ewww.
203          */
204         p0.frameRot = random.nextInt(4) * Math.PI / 2;
205         p0.init(gameManager.getPlayer(0));
206         p1.frameRot = p1.frameRot +
207             (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
208         p1.init(gameManager.getPlayer(1));
209
210         drawTroll();
211     }
212
213     void startTurn() {
214         p0.startTurn();
215         p1.startTurn();
216     }
217
218     private class Pos {
219         int x, y;
220         Pos(int _x, int _y) { x = _x; y = _y; }
221     }
222
223     private void drawBackground() {
224         graphicEntityModule.createSprite()
225                 .setImage("background.png")
226                 .setAnchor(0);
227
228         int numTrees = random.nextInt(21);
229         ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
230         while (numTrees --> 0) {
231             int x, y;
232             do {
233                 x = random.nextInt(1920);
234                 // yes, this biases randomness wrt perspective! :-(
235                 y = 700 + random.nextInt(175);
236             } while (y > 880 && (x < 200 || x > 1720));
237             poss.add(new Pos(x, y));
238         }
239         poss.sort(new Comparator<Pos>() {
240                 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
241             });
242
243         for (Pos p : poss) {
244             double scale = ( 90.0 / 433.0           // base height from PNG
245                              * (p.y - 680) / (875 - 680) ); // perspective
246             graphicEntityModule.createSprite()
247                 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
248                                                  : "Alshockv2.png")
249                 .setAnchorX(0.5)
250                 .setAnchorY(1)
251                 .setX(p.x)
252                 .setY(p.y)
253                 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
254                            * (1 + (random.nextDouble() - 0.5) / 6))
255                 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
256                 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
257                 .setSkewX((random.nextDouble() - 0.5) /4)
258                 .setSkewY((random.nextDouble() - 0.5) /8);
259         }
260     }
261
262     private void drawTroll() {
263         troll = graphicEntityModule.createSprite()
264             .setImage("troll.png")
265             .setAnchorX(0.5)
266             .setAnchorY(1)
267             .setX(1920/2)
268             .setY(880)
269             .setZIndex(2);
270         trollPositionGauge = graphicEntityModule.createText()
271             .setZIndex(2)
272             .setAnchor(0.5)
273             .setFontSize(40)
274             .setX(1980/2)
275             .setY(980)
276             .setFillColor(0xffffff);
277
278         moveTroll();
279     }
280
281     void moveTroll() {
282         graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
283         int x0 = p0.castle.getX(), x1 = p1.castle.getX();
284         int y0 = p0.castle.getY(), y1 = p1.castle.getY();
285         troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
286                    Curve.ELASTIC);
287         troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
288                    Curve.ELASTIC);
289
290         trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
291         int delta = model.trollPosition - model.roadLength / 2;
292         if (delta < 0) {
293             trollPositionGauge.setText("← " + Math.abs(delta));
294         }
295         else if (delta > 0) {
296             trollPositionGauge.setText(Math.abs(delta) + " →");
297         }
298         else {
299             trollPositionGauge.setText("↔");
300         }
301         graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
302         trollPositionGauge.setX(troll.getX());
303     }
304
305 }