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.Text;
12 import com.codingame.gameengine.module.entities.Group;
13 import com.codingame.gameengine.module.entities.Curve;
14 import com.google.inject.Inject;
17 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
18 @Inject private GraphicEntityModule graphicEntityModule;
36 void init(com.codingame.game.Player p) {
38 colorToken = p.getColorToken();
39 nicknameToken = p.getNicknameToken();
40 avatarToken = p.getAvatarToken();
42 boolean p0 = model.index == 0;
43 int x = p0 ? 280 : 1920 - 280;
46 Sprite frame = graphicEntityModule.createSprite()
47 .setImage("frame.png")
49 .setRotation(frameRot)
53 Sprite frameBg = graphicEntityModule.createSprite()
54 .setImage("frame_bg.png")
56 .setRotation(frameRot)
59 Sprite avatarSprite = graphicEntityModule.createSprite()
61 .setImage(avatarToken)
66 avatar = graphicEntityModule
67 .createGroup(frame, frameBg, avatarSprite)
70 Text text = graphicEntityModule.createText(nicknameToken)
75 .setFillColor(0x7f3f00)
78 stoneCounter = graphicEntityModule.createText()
83 .setFillColor(0x7f3f00)
87 message = graphicEntityModule.createText()
88 .setX(p0 ? 15 : 1920-15)
92 .setFillColor(0xffbf7f)
93 .setAnchorX(p0 ? 0 : 1)
96 castle = graphicEntityModule.createSprite()
97 .setImage("castle.png")
99 .setX(p0 ? 160 : 1920-160)
100 .setY(p0 ? 890 : 880)
104 .setScaleX(p0 ? 1 : -1);
106 stone = graphicEntityModule.createText()
109 .setFillColor(0x12322a)
113 stoneReminder = graphicEntityModule.createText()
114 .setX(p0 ? x + 100 : x - 100)
118 .setFillColor(0x3f3f3f)
119 .setAnchorX(p0 ? 0 : 1)
123 void updateStoneCounter() {
124 int stones = model.getStones();
126 stoneCounter.setText("Out of stones!");
127 stoneCounter.setFillColor(0xff7777);
129 else if (stones == 1) {
130 stoneCounter.setText("1 stone");
131 stoneCounter.setFillColor(0xffbb77);
134 stoneCounter.setText(stones + " stones");
138 void animateStones(int stones) {
139 String stonesString = new Integer(stones).toString();
140 stone.setX(castle.getX());
141 stone.setY(castle.getY() - 100);
142 stone.setText(stonesString);
144 graphicEntityModule.commitEntityState(0, stone);
146 int peakX = (castle.getX() + troll.getX()) / 2;
149 stone.setY(peakY, Curve.EASE_OUT);
150 graphicEntityModule.commitEntityState(0.25,
154 stone.setX(troll.getX());
155 stone.setY(troll.getY() - 50, Curve.EASE_IN);
156 stone.setAlpha(0, Curve.EASE_IN);
157 graphicEntityModule.commitEntityState(0.5, stone);
159 stoneReminder.setText(stonesString);
160 graphicEntityModule.commitEntityState(0.25, stoneReminder);
161 stoneReminder.setAlpha(1);
162 graphicEntityModule.commitEntityState(0.5, stoneReminder);
165 void displayMessage(String msg) {
166 message.setText(msg);
167 graphicEntityModule.commitEntityState(0, message);
171 graphicEntityModule.commitEntityState(0.5, avatar);
172 int dir = random.nextInt(2) == 1 ? 1 : -1;
173 avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
177 graphicEntityModule.commitEntityState(0.5, castle);
178 castle.setX(castle.getX(), Curve.ELASTIC);
179 castle.setScaleY(-0.2, Curve.EASE_IN);
183 stoneReminder.setAlpha(0);
184 graphicEntityModule.commitEntityState(0, stoneReminder);
189 Random random = new Random();
191 Text trollPositionGauge;
192 Player p0 = new Player(), p1 = new Player();
199 * Random π/2-grained rotation of the avatar frames. Avoid
200 * having them π/2 apart, though, as one of them is likely
201 * going to end upside-down and the trick would be revealed.
202 * And I'd have to "draw" a new frame. Ewww.
204 p0.frameRot = random.nextInt(4) * Math.PI / 2;
205 p0.init(gameManager.getPlayer(0));
206 p1.frameRot = p1.frameRot +
207 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
208 p1.init(gameManager.getPlayer(1));
220 Pos(int _x, int _y) { x = _x; y = _y; }
223 private void drawBackground() {
224 graphicEntityModule.createSprite()
225 .setImage("background.png")
228 int numTrees = random.nextInt(21);
229 ArrayList<Pos> poss = new ArrayList<Pos>(numTrees);
230 while (numTrees --> 0) {
233 x = random.nextInt(1920);
234 // yes, this biases randomness wrt perspective! :-(
235 y = 700 + random.nextInt(175);
236 } while (y > 880 && (x < 200 || x > 1720));
237 poss.add(new Pos(x, y));
239 poss.sort(new Comparator<Pos>() {
240 public int compare(Pos a, Pos b) { return a.y < b.y ? -1 : 1; }
244 double scale = ( 90.0 / 433.0 // base height from PNG
245 * (p.y - 680) / (875 - 680) ); // perspective
246 graphicEntityModule.createSprite()
247 .setImage(random.nextInt(2) == 0 ? "Alshockv1.png"
253 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1)
254 * (1 + (random.nextDouble() - 0.5) / 6))
255 .setScaleY(scale * (1 + (random.nextDouble() -0.5) / 6))
256 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
257 .setSkewX((random.nextDouble() - 0.5) /4)
258 .setSkewY((random.nextDouble() - 0.5) /8);
262 private void drawTroll() {
263 troll = graphicEntityModule.createSprite()
264 .setImage("troll.png")
270 trollPositionGauge = graphicEntityModule.createText()
276 .setFillColor(0xffffff);
282 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
283 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
284 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
285 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
287 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
290 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
291 int delta = model.trollPosition - model.roadLength / 2;
293 trollPositionGauge.setText("← " + Math.abs(delta));
295 else if (delta > 0) {
296 trollPositionGauge.setText(Math.abs(delta) + " →");
299 trollPositionGauge.setText("↔");
301 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
302 trollPositionGauge.setX(troll.getX());