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.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.SpriteAnimation;
12 import com.codingame.gameengine.module.entities.Text;
13 import com.codingame.gameengine.module.entities.Group;
14 import com.codingame.gameengine.module.entities.Curve;
15 import com.codingame.gameengine.module.toggle.ToggleModule;
16 import com.google.inject.Inject;
19 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
20 @Inject private GraphicEntityModule graphicEntityModule;
21 @Inject ToggleModule toggleModule;
39 void init(com.codingame.game.Player p) {
41 colorToken = p.getColorToken();
42 nicknameToken = p.getNicknameToken();
43 avatarToken = p.getAvatarToken();
45 boolean p0 = model.index == 0;
46 int x = p0 ? 280 : 1920 - 280;
49 Sprite frame = graphicEntityModule.createSprite()
50 .setImage("frame.png")
52 .setRotation(frameRot)
56 Sprite frameBg = graphicEntityModule.createSprite()
57 .setImage("frame_bg.png")
59 .setRotation(frameRot)
62 Sprite avatarSprite = graphicEntityModule.createSprite()
64 .setImage(avatarToken)
69 avatar = graphicEntityModule
70 .createGroup(frame, frameBg, avatarSprite)
73 Text text = graphicEntityModule.createText(nicknameToken)
78 .setFillColor(0x7f3f00)
81 stoneCounter = graphicEntityModule.createText()
86 .setFillColor(0x7f3f00)
90 message = graphicEntityModule.createText()
91 .setX(p0 ? 15 : 1920-15)
95 .setFillColor(0xffbf7f)
96 .setAnchorX(p0 ? 0 : 1)
99 castle = graphicEntityModule.createSprite()
100 .setImage("castle.png")
102 .setX(p0 ? 160 : 1920-160)
103 .setY(p0 ? 890 : 880)
107 .setScaleX(p0 ? 1 : -1);
109 stone = graphicEntityModule.createText()
112 .setFillColor(0x12322a)
116 stoneReminder = graphicEntityModule.createText()
117 .setX(p0 ? x + 100 : x - 100)
121 .setFillColor(0x3f3f3f)
122 .setAnchorX(p0 ? 0 : 1)
126 void updateStoneCounter() {
127 int stones = model.getStones();
129 stoneCounter.setText("Out of stones!");
130 stoneCounter.setFillColor(0xff7777);
132 else if (stones == 1) {
133 stoneCounter.setText("1 stone");
134 stoneCounter.setFillColor(0xffbb77);
137 stoneCounter.setText(stones + " stones");
141 void animateStones(int stones) {
142 String stonesString = new Integer(stones).toString();
143 stone.setX(castle.getX());
144 stone.setY(castle.getY() - 100);
145 stone.setText(stonesString);
147 graphicEntityModule.commitEntityState(0, stone);
149 int peakX = (castle.getX() + troll.getX()) / 2;
152 stone.setY(peakY, Curve.EASE_OUT);
153 graphicEntityModule.commitEntityState(0.25,
157 stone.setX(troll.getX());
158 stone.setY(troll.getY() - 50, Curve.EASE_IN);
159 stone.setAlpha(0, Curve.EASE_IN);
160 graphicEntityModule.commitEntityState(0.5, stone);
162 stoneReminder.setText(stonesString);
163 graphicEntityModule.commitEntityState(0.25, stoneReminder);
164 stoneReminder.setAlpha(1);
165 graphicEntityModule.commitEntityState(0.5, stoneReminder);
168 void displayMessage(String msg) {
169 message.setText(msg);
170 graphicEntityModule.commitEntityState(0, message);
174 graphicEntityModule.commitEntityState(0.5, avatar);
175 int dir = random.nextInt(2) == 1 ? 1 : -1;
176 avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
180 graphicEntityModule.commitEntityState(0.5, castle);
181 castle.setX(castle.getX(), Curve.ELASTIC);
182 castle.setScaleY(-0.2, Curve.EASE_IN);
186 stoneReminder.setAlpha(0);
187 graphicEntityModule.commitEntityState(0, stoneReminder);
192 Random random = new Random();
194 Text trollPositionGauge;
195 Player p0 = new Player(), p1 = new Player();
196 Text turnCounter; int _turns = 0;
203 * Random π/2-grained rotation of the avatar frames. Avoid
204 * having them π/2 apart, though, as one of them is likely
205 * going to end upside-down and the trick would be revealed.
206 * And I'd have to "draw" a new frame. Ewww.
208 p0.frameRot = random.nextInt(4) * Math.PI / 2;
209 p0.init(gameManager.getPlayer(0));
210 p1.frameRot = p1.frameRot +
211 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
212 p1.init(gameManager.getPlayer(1));
223 animateTurnCounter();
228 Pos(int _x, int _y) { x = _x; y = _y; }
231 private void drawBackground() {
232 graphicEntityModule.createSprite()
233 .setImage("background.png")
236 int numMountains = random.nextInt(5);
237 while (numMountains --> 0) {
238 final int pngWidth = 366;
239 double scale = 0.5 * (1 + random.nextDouble());
240 int x = random.nextInt(1920 + (int) (scale*pngWidth))
241 - (int) (scale*pngWidth/2);
242 int baseTint = 64 + random.nextInt(128);
243 Sprite mountain = graphicEntityModule.createSprite()
244 .setImage("mountain.png")
248 .setAnchorY(283.0 / 321.0)
249 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
250 .setScaleX(random.nextInt(2) == 0 ? scale : -scale)
251 .setScaleY(scale * (1 + (random.nextDouble() - 0.5) / 2))
252 .setSkewX((random.nextDouble() - 0.5) / 4)
253 .setSkewY((random.nextDouble() - 0.5) / 8)
254 .setTint((baseTint + random.nextInt(16) - 8) * 0x010000
255 + (baseTint + random.nextInt(16) - 8) * 0x0100
256 + (baseTint + random.nextInt(16) - 8) * 0x01);
257 graphicEntityModule.createSprite().setImage("mountaintop.png")
258 .setX(mountain.getX())
259 .setY(mountain.getY())
260 .setAnchorX(mountain.getAnchorX())
261 .setAnchorY(mountain.getAnchorY())
262 .setRotation(mountain.getRotation())
263 .setScaleX(mountain.getScaleX())
264 .setScaleY(mountain.getScaleY())
265 .setSkewX(mountain.getSkewX())
266 .setSkewY(mountain.getSkewY());
269 int numTrees = random.nextInt(21);
270 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
271 while (numTrees --> 0) {
274 x = random.nextInt(1920);
275 // yes, this biases randomness wrt perspective! :-(
276 y = 700 + random.nextInt(175);
277 } while (y > 880 && (x < 200 || x > 1720));
278 poss.add(new Pos(x, y));
280 poss.sort(new Comparator<Pos>() {
281 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
285 double scale = ( 90.0 / 433.0 // base height from PNG
286 * (p.y - 680) / (875 - 680) ); // perspective
287 graphicEntityModule.createSprite()
288 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
294 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
295 * (1 + (random.nextDouble() - 0.5) / 6))
296 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
297 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
298 .setSkewX((random.nextDouble() - 0.5) /4)
299 .setSkewY((random.nextDouble() - 0.5) /8);
303 private void drawTroll() {
304 troll = graphicEntityModule.createSprite()
305 .setImage("troll.png")
311 trollPositionGauge = graphicEntityModule.createText()
317 .setFillColor(0xffffff);
323 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
324 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
325 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
326 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
328 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
331 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
332 int distLeft = model.trollPosition;
333 int distRight = model.roadLength - model.trollPosition;
335 trollPositionGauge.setText("← " + distRight);
337 else if (distRight <= 0) {
338 trollPositionGauge.setText(distLeft + " →");
341 trollPositionGauge.setText(distLeft + " ↔ " + distRight);
343 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
344 trollPositionGauge.setX(troll.getX());
347 void animateTurnCounter() {
348 for (int i = 0; i < 10; i++) {
349 turnCounter.setText("T" + _turns + "." + i);
350 // The following line is likely not a bug.
351 graphicEntityModule.commitEntityState((double) i/9, turnCounter);
357 String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
358 .setSourceImage("debug.png")
367 SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
368 .setImages(debugModePngs)
373 toggleModule.displayOnToggleState(debugMode, "debug", true);
375 turnCounter = graphicEntityModule.createText()
380 .setStrokeColor(0xff0080)
381 .setFillColor(0xff0080)
382 .setFontFamily("monospace")
383 .setFontWeight(Text.FontWeight.BOLD)
385 toggleModule.displayOnToggleState(turnCounter, "debug", true);
386 animateTurnCounter();