Restore cheat throw visuals
[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.GameManager;
8 import com.codingame.gameengine.core.MultiplayerGameManager;
9 import com.codingame.gameengine.module.entities.GraphicEntityModule;
10 import com.codingame.gameengine.module.entities.Rectangle;
11 import com.codingame.gameengine.module.entities.Sprite;
12 import com.codingame.gameengine.module.entities.SpriteAnimation;
13 import com.codingame.gameengine.module.entities.Text;
14 import com.codingame.gameengine.module.entities.Group;
15 import com.codingame.gameengine.module.entities.Curve;
16 import com.codingame.gameengine.module.toggle.ToggleModule;
17 import com.google.inject.Inject;
18
19 class View {
20     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
21     @Inject private GraphicEntityModule graphicEntityModule;
22     @Inject ToggleModule toggleModule;
23
24     enum Dir {
25         LEFT("walks left."),
26         STILL("stands still."),
27         RIGHT("walks right.");
28
29         String movement;
30         Dir(String mvt) { movement = mvt; }
31     }
32
33     class Player {
34         Model.Player model;
35
36         int colorToken;
37         String nicknameToken;
38         String avatarToken;
39
40         double frameRot;
41
42         Group avatar;
43         Text stoneCounter;
44         Text message;
45         Sprite castle;
46         Text stone;
47         Text stoneReminder;
48
49         void init(com.codingame.game.Player p) {
50             model = p.model;
51             colorToken = p.getColorToken();
52             nicknameToken = p.getNicknameToken();
53             avatarToken = p.getAvatarToken();
54
55             boolean p0 = model.index == 0;
56             int x = p0 ? 280 : 1920 - 280;
57             int y = 220;
58
59             Sprite frame = graphicEntityModule.createSprite()
60                 .setImage("frame.png")
61                 .setAnchor(0.5)
62                 .setRotation(frameRot)
63                 .setZIndex(22)
64                 .setTint(colorToken);
65
66             Sprite frameBg = graphicEntityModule.createSprite()
67                 .setImage("frame_bg.png")
68                 .setAnchor(0.5)
69                 .setRotation(frameRot)
70                 .setZIndex(20);
71
72             Sprite avatarSprite = graphicEntityModule.createSprite()
73                 .setZIndex(21)
74                 .setImage(avatarToken)
75                 .setAnchor(0.5)
76                 .setBaseHeight(116)
77                 .setBaseWidth(116);
78
79             avatar = graphicEntityModule
80                 .createGroup(frame, frameBg, avatarSprite)
81                 .setX(x).setY(y);
82
83             Text text = graphicEntityModule.createText(nicknameToken)
84                     .setX(x)
85                     .setY(y + 120)
86                     .setZIndex(20)
87                     .setFontSize(40)
88                     .setFillColor(0x7f3f00)
89                     .setAnchor(0.5);
90
91             stoneCounter = graphicEntityModule.createText()
92                 .setX(x)
93                 .setY(y+200)
94                 .setZIndex(20)
95                 .setFontSize(40)
96                 .setFillColor(0x7f3f00)
97                 .setAnchor(0.5);
98             updateStoneCounter();
99
100             message = graphicEntityModule.createText()
101                 .setX(p0 ? 15 : 1920-15)
102                 .setY(680)
103                 .setZIndex(1)
104                 .setFontSize(40)
105                 .setFillColor(0xffbf7f)
106                 .setAnchorX(p0 ? 0 : 1)
107                 .setAnchorY(1);
108
109             castle = graphicEntityModule.createSprite()
110                 .setImage("castle.png")
111                 .setTint(colorToken)
112                 .setX(p0 ? 160 : 1920-160)
113                 .setY(p0 ? 890 : 880)
114                 .setZIndex(1)
115                 .setAnchorX(0.5)
116                 .setAnchorY(1)
117                 .setScaleX(p0 ? 1 : -1);
118
119             stone = graphicEntityModule.createText()
120                 .setZIndex(3)
121                 .setFontSize(150)
122                 .setFillColor(0x12322a)
123                 .setAnchor(0.5)
124                 .setAlpha(0);
125
126             stoneReminder = graphicEntityModule.createText()
127                 .setX(p0 ? x + 100 : x - 100)
128                 .setY(y)
129                 .setZIndex(20)
130                 .setFontSize(75)
131                 .setFillColor(0x3f3f3f)
132                 .setAnchorX(p0 ? 0 : 1)
133                 .setAnchorY(0.5);
134         }
135
136         void updateStoneCounter() {
137             int stones = model.getStones();
138             if (stones <= 0) {
139                 stoneCounter.setText("Out of stones!");
140                 stoneCounter.setFillColor(0xff7777);
141             }
142             else if (stones == 1) {
143                 stoneCounter.setText("1 stone");
144                 stoneCounter.setFillColor(0xffbb77);
145             }
146             else {
147                 stoneCounter.setText(stones + " stones");
148             }
149         }
150
151         void animateStones(int stones) {
152             String stonesString = Integer.valueOf(stones).toString();
153             stone.setX(castle.getX());
154             stone.setY(castle.getY() - 100);
155             stone.setText(stonesString);
156             stone.setAlpha(1);
157             graphicEntityModule.commitEntityState(0, stone);
158     
159             int peakX = (castle.getX() + troll.getX()) / 2;
160             int peakY = 540;
161             stone.setX(peakX);
162             stone.setY(peakY, Curve.EASE_OUT);
163             graphicEntityModule.commitEntityState(0.25,
164                                                   stone,
165                                                   stoneCounter);
166     
167             stone.setX(troll.getX());
168             stone.setY(troll.getY() - 50, Curve.EASE_IN);
169             stone.setAlpha(0, Curve.EASE_IN);
170             graphicEntityModule.commitEntityState(0.5, stone);
171
172             stoneReminder.setText(stonesString);
173             graphicEntityModule.commitEntityState(0.25, stoneReminder);
174             stoneReminder.setAlpha(1);
175             graphicEntityModule.commitEntityState(0.5, stoneReminder);
176         }
177
178         void displayMessage(String msg) {
179             message.setText(msg);
180             graphicEntityModule.commitEntityState(0, message);
181         }
182
183         void markLoser() {
184             graphicEntityModule.commitEntityState(0.5, avatar);
185             int dir = random.nextInt(2) == 1 ? 1 : -1;
186             avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
187         }
188
189         void destroy() {
190             gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + nicknameToken + "."));
191             graphicEntityModule.commitEntityState(0.5, castle);
192             castle.setX(castle.getX(), Curve.ELASTIC);
193             castle.setScaleY(-0.2, Curve.EASE_IN);
194         }
195
196         void startTurn() {
197             stoneReminder.setAlpha(0);
198             graphicEntityModule.commitEntityState(0, stoneReminder);
199         }
200
201         void victory() {
202             gameManager.addToGameSummary(GameManager.formatSuccessMessage(nicknameToken + " wins."));
203         }
204
205         void throwStones(int stones) {
206             gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", nicknameToken, stones, stones == 1 ? "" : "s"));
207         }
208
209         void threwMoreStonesThanHad() {
210             gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried to throw more stones than they had.  I'll let it slide for this time.  (But not let them throw that much!)"));
211         }
212
213         void failedToThrowStonesAndShouldHave() {
214             gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried not throwing any stones.  Fixing that for them because I'm in a good mood today."));
215         }
216     }
217
218     Model model;
219     Random random = new Random();
220     Sprite troll;
221     Text trollPositionGauge;
222     Player p0 = new Player(), p1 = new Player();
223     Text turnCounter; int _turns = 0;
224
225     void init(Model m) {
226         model = m;
227         drawBackground();
228
229         /*
230          * Random π/2-grained rotation of the avatar frames.  Avoid
231          * having them π/2 apart, though, as one of them is likely
232          * going to end upside-down and the trick would be revealed.
233          * And I'd have to "draw" a new frame. Ewww.
234          */
235         p0.frameRot = random.nextInt(4) * Math.PI / 2;
236         p0.init(gameManager.getPlayer(0));
237         p1.frameRot = p1.frameRot +
238             (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
239         p1.init(gameManager.getPlayer(1));
240
241         drawTroll();
242
243         drawDebug();
244     }
245
246     void startTurn() {
247         p0.startTurn();
248         p1.startTurn();
249
250         animateTurnCounter();
251     }
252
253     private class Pos {
254         int x, y;
255         Pos(int _x, int _y) { x = _x; y = _y; }
256     }
257
258     private void drawBackground() {
259         graphicEntityModule.createSprite()
260                 .setImage("background.png")
261                 .setAnchor(0);
262
263         int numMountains = random.nextInt(5);
264         while (numMountains --> 0) {
265             final int pngWidth = 366;
266             double scale = 0.5 * (1 + random.nextDouble());
267             int x = random.nextInt(1920 + (int) (scale*pngWidth))
268                 - (int) (scale*pngWidth/2);
269             int baseTint = 64 + random.nextInt(128);
270             Sprite mountain = graphicEntityModule.createSprite()
271                 .setImage("mountain.png")
272                 .setX(x)
273                 .setY(680)
274                 .setAnchorX(0.5)
275                 .setAnchorY(283.0 / 321.0)
276                 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
277                 .setScaleX(random.nextInt(2) == 0 ? scale : -scale)
278                 .setScaleY(scale * (1 + (random.nextDouble() - 0.5) / 2))
279                 .setSkewX((random.nextDouble() - 0.5) / 4)
280                 .setSkewY((random.nextDouble() - 0.5) / 8)
281                 .setTint((baseTint + random.nextInt(16) - 8) * 0x010000
282                          + (baseTint + random.nextInt(16) - 8) * 0x0100
283                          + (baseTint + random.nextInt(16) - 8) * 0x01);
284             graphicEntityModule.createSprite().setImage("mountaintop.png")
285                 .setX(mountain.getX())
286                 .setY(mountain.getY())
287                 .setAnchorX(mountain.getAnchorX())
288                 .setAnchorY(mountain.getAnchorY())
289                 .setRotation(mountain.getRotation())
290                 .setScaleX(mountain.getScaleX())
291                 .setScaleY(mountain.getScaleY())
292                 .setSkewX(mountain.getSkewX())
293                 .setSkewY(mountain.getSkewY());
294         }
295
296         int numTrees = random.nextInt(21);
297         ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
298         while (numTrees --> 0) {
299             int x, y;
300             do {
301                 x = random.nextInt(1920);
302                 // yes, this biases randomness wrt perspective! :-(
303                 y = 700 + random.nextInt(175);
304             } while (y > 880 && (x < 200 || x > 1720));
305             poss.add(new Pos(x, y));
306         }
307         poss.sort(new Comparator<Pos>() {
308                 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
309             });
310
311         for (Pos p : poss) {
312             double scale = ( 90.0 / 433.0           // base height from PNG
313                              * (p.y - 680) / (875 - 680) ); // perspective
314             graphicEntityModule.createSprite()
315                 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
316                                                  : "Alshockv2.png")
317                 .setAnchorX(0.5)
318                 .setAnchorY(1)
319                 .setX(p.x)
320                 .setY(p.y)
321                 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
322                            * (1 + (random.nextDouble() - 0.5) / 6))
323                 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
324                 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
325                 .setSkewX((random.nextDouble() - 0.5) /4)
326                 .setSkewY((random.nextDouble() - 0.5) /8);
327         }
328     }
329
330     private void drawTroll() {
331         troll = graphicEntityModule.createSprite()
332             .setImage("troll.png")
333             .setAnchorX(0.5)
334             .setAnchorY(1)
335             .setX(1920/2)
336             .setY(880)
337             .setZIndex(2);
338         trollPositionGauge = graphicEntityModule.createText()
339             .setZIndex(2)
340             .setAnchor(0.5)
341             .setFontSize(40)
342             .setX(1980/2)
343             .setY(980)
344             .setFillColor(0xffffff);
345
346         moveTroll();
347     }
348
349     private void moveTroll() {
350         graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
351         int x0 = p0.castle.getX(), x1 = p1.castle.getX();
352         int y0 = p0.castle.getY(), y1 = p1.castle.getY();
353         troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
354                    Curve.ELASTIC);
355         troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
356                    Curve.ELASTIC);
357
358         trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
359         int distLeft = model.trollPosition;
360         int distRight = model.roadLength - model.trollPosition;
361         if (distLeft <= 0) {
362             trollPositionGauge.setText("← " + distRight);
363         }
364         else if (distRight <= 0) {
365             trollPositionGauge.setText(distLeft + " →");
366         }
367         else {
368             trollPositionGauge.setText(distLeft + " ↔ " + distRight);
369         }
370         graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
371         trollPositionGauge.setX(troll.getX());
372     }
373
374     void moveTroll(Dir d) {
375         moveTroll();
376         gameManager.addToGameSummary("Troll " + d.movement);
377     }
378
379     void animateTurnCounter() {
380         for (int i = 0; i < 10; i++) {
381             turnCounter.setText("T" + _turns + "." + i);
382             // The following line is likely not a bug.
383             graphicEntityModule.commitEntityState((double) i/9, turnCounter);
384         }
385         _turns++;
386     }
387
388     void drawDebug() {
389         String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
390             .setSourceImage("debug.png")
391             .setImageCount(2)
392             .setWidth(900)
393             .setHeight(150)
394             .setOrigRow(0)
395             .setOrigCol(0)
396             .setImagesPerRow(1)
397             .setName("debug")
398             .split();
399         SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
400             .setImages(debugModePngs)
401             .setX(1920 / 2)
402             .setY(80)
403             .setAnchorX(0.5)
404             .setLoop(true);
405         toggleModule.displayOnToggleState(debugMode, "debug", true);
406
407         turnCounter = graphicEntityModule.createText()
408             .setAnchorX(0.5)
409             .setAnchorY(0)
410             .setX(1920 / 2)
411             .setY(280)
412             .setStrokeColor(0xff0080)
413             .setFillColor(0xff0080)
414             .setFontFamily("monospace")
415             .setFontWeight(Text.FontWeight.BOLD)
416             .setFontSize(100);
417         toggleModule.displayOnToggleState(turnCounter, "debug", true);
418         animateTurnCounter();
419     }
420
421     void doubleDefeat() {
422         gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
423     }
424
425     void draw() {
426         gameManager.addToGameSummary("Draw.");
427     }
428 }