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.TextBasedEntity;
15 import com.codingame.gameengine.module.entities.Group;
16 import com.codingame.gameengine.module.entities.Curve;
17 import com.google.inject.Inject;
20 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
21 @Inject private GraphicEntityModule graphicEntityModule;
22 @Inject PantsModule pantsModule;
40 void init(com.codingame.game.Player p) {
42 colorToken = p.getColorToken();
43 nicknameToken = p.getNicknameToken();
44 avatarToken = p.getAvatarToken();
46 boolean p0 = model.index == 0;
47 int x = p0 ? 280 : 1920 - 280;
50 Sprite frame = graphicEntityModule.createSprite()
51 .setImage("frame.png")
53 .setRotation(frameRot)
57 Sprite frameBg = graphicEntityModule.createSprite()
58 .setImage("frame_bg.png")
60 .setRotation(frameRot)
63 Sprite avatarSprite = graphicEntityModule.createSprite()
65 .setImage(avatarToken)
70 avatar = graphicEntityModule
71 .createGroup(frame, frameBg, avatarSprite)
74 Text text = graphicEntityModule.createText(nicknameToken)
79 .setFillColor(0x7f3f00)
82 stoneCounter = graphicEntityModule.createText()
87 .setFillColor(0x7f3f00)
91 message = graphicEntityModule.createText()
92 .setX(p0 ? 15 : 1920-15)
96 .setFillColor(0xffbf7f)
97 .setAnchorX(p0 ? 0 : 1)
100 castle = graphicEntityModule.createSprite()
101 .setImage("castle.png")
103 .setX(p0 ? 160 : 1920-160)
104 .setY(p0 ? 890 : 880)
108 .setScaleX(p0 ? 1 : -1);
110 stone = graphicEntityModule.createText()
113 .setFillColor(0x12322a)
117 stoneReminder = graphicEntityModule.createText()
118 .setX(p0 ? x + 100 : x - 100)
122 .setFontFamily("monospace")
123 .setStrokeColor(0xff0080)
124 .setFillColor(0xff0080)
125 .setAnchorX(p0 ? 0 : 1)
127 pantsModule.displayOnToggleState(stoneReminder, "debug", true);
131 graphicEntityModule.commitEntityState(0, stoneReminder);
135 gameManager.addToGameSummary(GameManager.formatSuccessMessage(nicknameToken + " wins."));
140 gameManager.addToGameSummary(GameManager.formatErrorMessage(trollRace.starter + " destroys " + nicknameToken + "."));
144 // ========== Player/avatar markings
147 animateLoss(avatar.getX(), avatar.getY(), 100, "SLOW\nPOKE");
151 animateLoss(avatar.getX(), avatar.getY(), 100, "STUPID");
155 animateLoss(avatar.getX(), avatar.getY(), 100, "CHEATER");
159 graphicEntityModule.commitEntityState(0.5, avatar);
160 avatar.setScaleX(1.5, Curve.EASE_OUT);
161 avatar.setScaleY(1.5, Curve.EASE_OUT);
162 avatar.setRotation((random.nextDouble() - 0.5) * Math.PI / 18,
167 graphicEntityModule.commitEntityState(0.5, avatar);
168 int dir = random.nextInt(2) == 1 ? 1 : -1;
169 avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
172 // ==========Player/stones
174 void throwStones(int stones) {
175 gameManager.addToGameSummary(String.format("%s throws %d stone%s at " + trollRace.nonStarter(), nicknameToken, stones, stones == 1 ? "" : "s"));
178 void threwMoreStonesThanHad() {
179 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!)"));
182 void failedToThrowStonesAndShouldHave() {
183 gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried not throwing any stones. Fixing that for them because I'm in a good mood today."));
186 void updateStoneCounter() {
187 int stones = model.getStones();
189 stoneCounter.setText("Out of stones!");
190 stoneCounter.setFillColor(0xff7777);
192 else if (stones == 1) {
193 stoneCounter.setText("1 stone");
194 stoneCounter.setFillColor(0xffbb77);
197 stoneCounter.setText(stones + " stones");
201 void animateStones(int stones) {
202 String stonesString = Integer.valueOf(stones).toString();
203 stone.setX(castle.getX());
204 stone.setY(castle.getY() - 100);
205 stone.setText(stonesString);
207 graphicEntityModule.commitEntityState(0, stone);
209 int peakX = (castle.getX() + troll.getX()) / 2;
212 stone.setY(peakY, Curve.EASE_OUT);
213 graphicEntityModule.commitEntityState(0.25,
217 stone.setX(troll.getX());
218 stone.setY(troll.getY() - 50, Curve.EASE_IN);
219 stone.setAlpha(0, Curve.EASE_IN);
220 graphicEntityModule.commitEntityState(0.5, stone);
222 stoneReminder.setText(stonesString);
223 graphicEntityModule.commitEntityState(0, stoneReminder);
226 // ========== Player/castle
228 void displayMessage(String msg) {
229 message.setText(msg);
230 graphicEntityModule.commitEntityState(0, message);
233 void destroyCastle() {
234 graphicEntityModule.commitEntityState(0.5, castle);
235 castle.setX(castle.getX(), Curve.ELASTIC);
236 castle.setScaleY(-0.2, Curve.EASE_IN);
241 Random random = new Random();
244 Text trollPositionGauge;
245 Player p0 = new Player(), p1 = new Player();
246 Text turnCounter; int _turns = 0;
248 // ==================== Referee interface
255 * Random π/2-grained rotation of the avatar frames. Avoid
256 * having them π/2 apart, though, as one of them is likely
257 * going to end upside-down and the trick would be revealed.
258 * And I'd have to "draw" a new frame. Ewww.
260 p0.frameRot = random.nextInt(4) * Math.PI / 2;
261 p0.init(gameManager.getPlayer(0));
262 p1.frameRot = p1.frameRot +
263 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
264 p1.init(gameManager.getPlayer(1));
275 trollMessage.setX(troll.getX());
277 animateTurnCounter();
280 void doubleDefeat() {
281 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
282 animateLoss(1920/2, 680, 150, "L0SERZ!");
286 gameManager.addToGameSummary("Draw.");
287 animateLoss(1920/2, 680, 200, "DRAW");
290 // drawBackground() helper class
291 // @Java nerds: is this avoidable?
294 Pos(int _x, int _y) { x = _x; y = _y; }
297 private void drawBackground() {
298 graphicEntityModule.createSprite()
299 .setImage("background.png")
302 int numMountains = random.nextInt(5);
303 while (numMountains --> 0) {
304 final int pngWidth = 366;
305 double scale = 0.5 * (1 + random.nextDouble());
306 int x = random.nextInt(1920 + (int) (scale*pngWidth))
307 - (int) (scale*pngWidth/2);
308 int baseTint = 64 + random.nextInt(128);
309 Sprite mountain = graphicEntityModule.createSprite()
310 .setImage("mountain.png")
314 .setAnchorY(283.0 / 321.0)
315 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
316 .setScaleX(random.nextInt(2) == 0 ? scale : -scale)
317 .setScaleY(scale * (1 + (random.nextDouble() - 0.5) / 2))
318 .setSkewX((random.nextDouble() - 0.5) / 4)
319 .setSkewY((random.nextDouble() - 0.5) / 8)
320 .setTint((baseTint + random.nextInt(16) - 8) * 0x010000
321 + (baseTint + random.nextInt(16) - 8) * 0x0100
322 + (baseTint + random.nextInt(16) - 8) * 0x01);
323 graphicEntityModule.createSprite().setImage("mountaintop.png")
324 .setX(mountain.getX())
325 .setY(mountain.getY())
326 .setAnchorX(mountain.getAnchorX())
327 .setAnchorY(mountain.getAnchorY())
328 .setRotation(mountain.getRotation())
329 .setScaleX(mountain.getScaleX())
330 .setScaleY(mountain.getScaleY())
331 .setSkewX(mountain.getSkewX())
332 .setSkewY(mountain.getSkewY());
335 int numTrees = random.nextInt(21);
336 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
337 while (numTrees --> 0) {
340 x = random.nextInt(1920);
341 // yes, this biases randomness wrt perspective! :-(
342 y = 700 + random.nextInt(175);
343 } while (y > 880 && (x < 200 || x > 1720));
344 poss.add(new Pos(x, y));
346 poss.sort(new Comparator<Pos>() {
347 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
351 double scale = ( 90.0 / 433.0 // base height from PNG
352 * (p.y - 680) / (875 - 680) ); // perspective
353 graphicEntityModule.createSprite()
354 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
360 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
361 * (1 + (random.nextDouble() - 0.5) / 6))
362 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
363 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
364 .setSkewX((random.nextDouble() - 0.5) /4)
365 .setSkewY((random.nextDouble() - 0.5) /8);
369 Sprite f7u12 = graphicEntityModule.createSprite()
370 .setImage("f7u12.png")
375 .setBaseWidth(514*1080/387)
378 pantsModule.displayOnToggleState(f7u12, "troll", true);
381 // ==================== Troll
384 Troll("The troll", 0xfac200, "bland"),
385 IceTroll("The ice troll", 0x59a2a2, "ice"),
386 RockTroll("The rock troll", 0x78877f, "rock"),
387 WaterTroll("The water troll", 0x2b2fc6, "water"),
388 OlogHai("The Olog-Hai", 0x5b2e7d, "ologhai");
389 String starter, parser; int tint;
390 TrollRace(String s, int t, String p) {
395 String nonStarter() {
396 return Character.toLowerCase(starter.charAt(0))
397 + starter.substring(1);
402 private void drawTroll() {
403 int r, league = gameManager.getLeagueLevel();
404 if (league <= 1) r = 4;
405 else if (league <= 2) r = 8;
408 r = random.nextInt(r);
409 if (r < 4) trollRace = TrollRace.Troll;
410 else if (r < 6) trollRace = TrollRace.IceTroll;
411 else if (r < 8) trollRace = TrollRace.RockTroll;
412 else if (r < 9) trollRace = TrollRace.WaterTroll;
413 else if (r < 10) trollRace = TrollRace.OlogHai;
414 else throw new RuntimeException("Internal error: unknown troll race " + r);
416 // We read it for debugging purposes, but don't echo it back
417 // to the IDE. It is, after all, *not* a map parameter!
418 String buf = gameManager.getGameParameters().getProperty("ehtnicity");
421 for (char c : buf.toCharArray())
422 if (Character.isLetter(c))
423 key += Character.toLowerCase(c);
425 for (TrollRace race : TrollRace.values()) {
426 if (key.equals(race.parser)) {
428 break/*ing news: */ iHateJava;
431 gameManager.addToGameSummary("Ignoring unknown troll race: " + buf);
434 photoFinish: ; // The race is through, but Java has no goto :-(
436 Sprite trollBody = graphicEntityModule.createSprite()
437 .setImage("troll_body.png")
440 .setTint(trollRace.tint);
441 Sprite trollPantsRed = graphicEntityModule.createSprite()
442 .setImage("pants_red.png")
445 pantsModule.displayOnPantsState(trollPantsRed, 1);
446 Sprite trollPantsGreen = graphicEntityModule.createSprite()
447 .setImage("pants_green.png")
450 pantsModule.displayOnPantsState(trollPantsGreen, 2);
451 Sprite trollPantsBlue = graphicEntityModule.createSprite()
452 .setImage("pants_blue.png")
455 pantsModule.displayOnPantsState(trollPantsBlue, 3);
456 Sprite trollPantsPerv = graphicEntityModule.createSprite()
457 .setImage("pants_perv.png")
460 pantsModule.displayOnPantsState(trollPantsPerv, 4);
461 troll = graphicEntityModule
462 .createGroup(trollBody, trollPantsRed,
463 trollPantsGreen, trollPantsBlue, trollPantsPerv)
466 .setScaleX(random.nextInt(2) == 0 ? 1 : -1)
468 trollPositionGauge = graphicEntityModule.createText()
474 .setFillColor(0xffffff);
477 trollMessage = graphicEntityModule.createText()
483 .setTextAlign(TextBasedEntity.TextAlign.CENTER)
484 .setStrokeColor(0xFFFF00)
485 .setFillColor(0xFFFF00)
487 pantsModule.displayOnToggleState(trollMessage, "verboseTrolling", true);
490 private void moveTroll() {
491 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
492 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
493 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
494 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
496 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
499 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
500 int distLeft = model.trollPosition;
501 int distRight = model.roadLength - model.trollPosition;
503 trollPositionGauge.setText("← " + distRight);
505 else if (distRight <= 0) {
506 trollPositionGauge.setText(distLeft + " →");
509 trollPositionGauge.setText(distLeft + " ↔ " + distRight);
511 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
512 trollPositionGauge.setX(troll.getX());
516 LEFT("walks left.", 0),
517 STILL("stands still.", 1),
518 RIGHT("walks right.", 2);
520 String movement; int index;
521 Dir(String mvt, int i) { movement = mvt; index = i; }
524 void moveTroll(Dir d) {
526 gameManager.addToGameSummary(trollRace.starter + " " + d.movement);
528 trollMessage.setText(selectTrollMessage(d)).setAlpha(1, Curve.NONE);
529 graphicEntityModule.commitEntityState(0.5, trollMessage);
530 trollMessage.setAlpha(0, Curve.EASE_IN);
531 graphicEntityModule.commitEntityState(1, trollMessage);
534 String selectTrollMessage(Dir d) {
535 if (random.nextInt(10000) == 0) {
536 return TrollText.specials[random.nextInt(TrollText.specials.length)];
540 int i = random.nextInt(TrollText.directed.length + TrollText.isotropic.length / 3);
541 if (i < TrollText.directed.length) {
542 return TrollText.directed[i][d.index];
545 return TrollText.isotropic[random.nextInt(TrollText.isotropic.length)];
549 // ==================== Debug information
551 void animateTurnCounter() {
552 for (int i = 0; i < 10; i++) {
553 turnCounter.setText("T" + _turns + "." + i);
554 // The following line is likely not a bug.
555 graphicEntityModule.commitEntityState((double) i/9, turnCounter);
561 String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
562 .setSourceImage("debug.png")
571 SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
572 .setImages(debugModePngs)
577 pantsModule.displayOnToggleState(debugMode, "debug", true);
579 turnCounter = graphicEntityModule.createText()
584 .setStrokeColor(0xff0080)
585 .setFillColor(0xff0080)
586 .setFontFamily("monospace")
587 .setFontWeight(Text.FontWeight.BOLD)
589 pantsModule.displayOnToggleState(turnCounter, "debug", true);
590 animateTurnCounter();
593 // ==================== Endgame status
595 void animateLoss(int x, int y, int size, String message) {
597 if (x < 1920/2) { startX = 1920; }
598 else if (x > 1920/2) { startX = 1920; } // XXX
599 else { startX = 1920 * random.nextInt(2); }
601 Text msg = graphicEntityModule.createText(message)
606 .setScaleX(3*random.nextDouble() - 1)
607 .setScaleY(3*random.nextDouble() - 1)
608 .setSkewX(2*random.nextDouble() - 1)
609 .setSkewY(2*random.nextDouble() - 1)
610 .setRotation(4*Math.PI * (1 + random.nextDouble())
611 * (random.nextInt(2) == 0 ? 1 : -1))
613 .setStrokeColor(0xff7f7f)
614 .setFillColor(0xff7f7f)
615 .setFontWeight(Text.FontWeight.BOLD)
616 .setTextAlign(TextBasedEntity.TextAlign.CENTER);
617 graphicEntityModule.commitEntityState(0.25, msg);
618 Curve curve = Curve.ELASTIC;
619 msg.setX(x, Curve.EASE_OUT)
620 .setY(y, Curve.ELASTIC)
625 .setRotation(2*Math.PI * (random.nextDouble() - 0.5), Curve.LINEAR)
626 .setFontSize(size, curve);