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();
202 * Random π/2-grained rotation of the avatar frames. Avoid
203 * having them π/2 apart, though, as one of them is likely
204 * going to end upside-down and the trick would be revealed.
205 * And I'd have to "draw" a new frame. Ewww.
207 p0.frameRot = random.nextInt(4) * Math.PI / 2;
208 p0.init(gameManager.getPlayer(0));
209 p1.frameRot = p1.frameRot +
210 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
211 p1.init(gameManager.getPlayer(1));
225 Pos(int _x, int _y) { x = _x; y = _y; }
228 private void drawBackground() {
229 graphicEntityModule.createSprite()
230 .setImage("background.png")
233 int numMountains = random.nextInt(5);
234 while (numMountains --> 0) {
235 final int pngWidth = 366;
236 double scale = 0.5 * (1 + random.nextDouble());
237 int x = random.nextInt(1920 + (int) (scale*pngWidth))
238 - (int) (scale*pngWidth/2);
239 int baseTint = 64 + random.nextInt(128);
240 Sprite mountain = graphicEntityModule.createSprite()
241 .setImage("mountain.png")
245 .setAnchorY(283.0 / 321.0)
246 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
247 .setScaleX(random.nextInt(2) == 0 ? scale : -scale)
248 .setScaleY(scale * (1 + (random.nextDouble() - 0.5) / 2))
249 .setSkewX((random.nextDouble() - 0.5) / 4)
250 .setSkewY((random.nextDouble() - 0.5) / 8)
251 .setTint((baseTint + random.nextInt(16) - 8) * 0x010000
252 + (baseTint + random.nextInt(16) - 8) * 0x0100
253 + (baseTint + random.nextInt(16) - 8) * 0x01);
254 graphicEntityModule.createSprite().setImage("mountaintop.png")
255 .setX(mountain.getX())
256 .setY(mountain.getY())
257 .setAnchorX(mountain.getAnchorX())
258 .setAnchorY(mountain.getAnchorY())
259 .setRotation(mountain.getRotation())
260 .setScaleX(mountain.getScaleX())
261 .setScaleY(mountain.getScaleY())
262 .setSkewX(mountain.getSkewX())
263 .setSkewY(mountain.getSkewY());
266 int numTrees = random.nextInt(21);
267 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
268 while (numTrees --> 0) {
271 x = random.nextInt(1920);
272 // yes, this biases randomness wrt perspective! :-(
273 y = 700 + random.nextInt(175);
274 } while (y > 880 && (x < 200 || x > 1720));
275 poss.add(new Pos(x, y));
277 poss.sort(new Comparator<Pos>() {
278 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
282 double scale = ( 90.0 / 433.0 // base height from PNG
283 * (p.y - 680) / (875 - 680) ); // perspective
284 graphicEntityModule.createSprite()
285 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
291 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
292 * (1 + (random.nextDouble() - 0.5) / 6))
293 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
294 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
295 .setSkewX((random.nextDouble() - 0.5) /4)
296 .setSkewY((random.nextDouble() - 0.5) /8);
300 private void drawTroll() {
301 troll = graphicEntityModule.createSprite()
302 .setImage("troll.png")
308 trollPositionGauge = graphicEntityModule.createText()
314 .setFillColor(0xffffff);
320 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
321 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
322 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
323 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
325 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
328 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
329 int distLeft = model.trollPosition;
330 int distRight = model.roadLength - model.trollPosition;
332 trollPositionGauge.setText("← " + distRight);
334 else if (distRight <= 0) {
335 trollPositionGauge.setText(distLeft + " →");
338 trollPositionGauge.setText(distLeft + " ↔ " + distRight);
340 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
341 trollPositionGauge.setX(troll.getX());
345 String[] debugModePngs = graphicEntityModule.createSpriteSheetSplitter()
346 .setSourceImage("debug.png")
355 SpriteAnimation debugMode = graphicEntityModule.createSpriteAnimation()
356 .setImages(debugModePngs)
361 toggleModule.displayOnToggleState(debugMode, "debug", true);