1 package com.codingame.game;
3 import java.util.Random;
4 import java.util.ArrayList;
5 import java.util.Comparator;
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;
20 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
21 @Inject private GraphicEntityModule graphicEntityModule;
22 @Inject ToggleModule toggleModule;
26 STILL("stands still."),
27 RIGHT("walks right.");
30 Dir(String mvt) { movement = mvt; }
49 void init(com.codingame.game.Player p) {
51 colorToken = p.getColorToken();
52 nicknameToken = p.getNicknameToken();
53 avatarToken = p.getAvatarToken();
55 boolean p0 = model.index == 0;
56 int x = p0 ? 280 : 1920 - 280;
59 Sprite frame = graphicEntityModule.createSprite()
60 .setImage("frame.png")
62 .setRotation(frameRot)
66 Sprite frameBg = graphicEntityModule.createSprite()
67 .setImage("frame_bg.png")
69 .setRotation(frameRot)
72 Sprite avatarSprite = graphicEntityModule.createSprite()
74 .setImage(avatarToken)
79 avatar = graphicEntityModule
80 .createGroup(frame, frameBg, avatarSprite)
83 Text text = graphicEntityModule.createText(nicknameToken)
88 .setFillColor(0x7f3f00)
91 stoneCounter = graphicEntityModule.createText()
96 .setFillColor(0x7f3f00)
100 message = graphicEntityModule.createText()
101 .setX(p0 ? 15 : 1920-15)
105 .setFillColor(0xffbf7f)
106 .setAnchorX(p0 ? 0 : 1)
109 castle = graphicEntityModule.createSprite()
110 .setImage("castle.png")
112 .setX(p0 ? 160 : 1920-160)
113 .setY(p0 ? 890 : 880)
117 .setScaleX(p0 ? 1 : -1);
119 stone = graphicEntityModule.createText()
122 .setFillColor(0x12322a)
126 stoneReminder = graphicEntityModule.createText()
127 .setX(p0 ? x + 100 : x - 100)
131 .setFillColor(0x3f3f3f)
132 .setAnchorX(p0 ? 0 : 1)
136 void updateStoneCounter() {
137 int stones = model.getStones();
139 stoneCounter.setText("Out of stones!");
140 stoneCounter.setFillColor(0xff7777);
142 else if (stones == 1) {
143 stoneCounter.setText("1 stone");
144 stoneCounter.setFillColor(0xffbb77);
147 stoneCounter.setText(stones + " stones");
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);
157 graphicEntityModule.commitEntityState(0, stone);
159 int peakX = (castle.getX() + troll.getX()) / 2;
162 stone.setY(peakY, Curve.EASE_OUT);
163 graphicEntityModule.commitEntityState(0.25,
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);
172 stoneReminder.setText(stonesString);
173 graphicEntityModule.commitEntityState(0.25, stoneReminder);
174 stoneReminder.setAlpha(1);
175 graphicEntityModule.commitEntityState(0.5, stoneReminder);
178 void displayMessage(String msg) {
179 message.setText(msg);
180 graphicEntityModule.commitEntityState(0, message);
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);
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);
197 stoneReminder.setAlpha(0);
198 graphicEntityModule.commitEntityState(0, stoneReminder);
202 gameManager.addToGameSummary(GameManager.formatSuccessMessage(nicknameToken + " wins."));
205 void throwStones(int stones) {
206 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", nicknameToken, stones, stones == 1 ? "" : "s"));
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!)"));
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."));
219 Random random = new Random();
221 Text trollPositionGauge;
222 Player p0 = new Player(), p1 = new Player();
223 Text turnCounter; int _turns = 0;
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.
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));
250 animateTurnCounter();
255 Pos(int _x, int _y) { x = _x; y = _y; }
258 private void drawBackground() {
259 graphicEntityModule.createSprite()
260 .setImage("background.png")
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")
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());
296 int numTrees = random.nextInt(21);
297 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
298 while (numTrees --> 0) {
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));
307 poss.sort(new Comparator<Pos>() {
308 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
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"
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);
330 private void drawTroll() {
331 troll = graphicEntityModule.createSprite()
332 .setImage("troll.png")
338 trollPositionGauge = graphicEntityModule.createText()
344 .setFillColor(0xffffff);
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,
355 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
358 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
359 int distLeft = model.trollPosition;
360 int distRight = model.roadLength - model.trollPosition;
362 trollPositionGauge.setText("← " + distRight);
364 else if (distRight <= 0) {
365 trollPositionGauge.setText(distLeft + " →");
368 trollPositionGauge.setText(distLeft + " ↔ " + distRight);
370 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
371 trollPositionGauge.setX(troll.getX());
374 void moveTroll(Dir d) {
376 gameManager.addToGameSummary("Troll " + d.movement);
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);
389 String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
390 .setSourceImage("debug.png")
399 SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
400 .setImages(debugModePngs)
405 toggleModule.displayOnToggleState(debugMode, "debug", true);
407 turnCounter = graphicEntityModule.createText()
412 .setStrokeColor(0xff0080)
413 .setFillColor(0xff0080)
414 .setFontFamily("monospace")
415 .setFontWeight(Text.FontWeight.BOLD)
417 toggleModule.displayOnToggleState(turnCounter, "debug", true);
418 animateTurnCounter();
421 void doubleDefeat() {
422 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
426 gameManager.addToGameSummary("Draw.");