1 package com.codingame.game;
3 import java.util.Random;
5 import com.codingame.gameengine.core.MultiplayerGameManager;
6 import com.codingame.gameengine.module.entities.GraphicEntityModule;
7 import com.codingame.gameengine.module.entities.Rectangle;
8 import com.codingame.gameengine.module.entities.Sprite;
9 import com.codingame.gameengine.module.entities.Text;
10 import com.codingame.gameengine.module.entities.Group;
11 import com.codingame.gameengine.module.entities.Curve;
12 import com.google.inject.Inject;
15 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
16 @Inject private GraphicEntityModule graphicEntityModule;
34 void init(com.codingame.game.Player p) {
36 colorToken = p.getColorToken();
37 nicknameToken = p.getNicknameToken();
38 avatarToken = p.getAvatarToken();
40 boolean p0 = model.index == 0;
41 int x = p0 ? 280 : 1920 - 280;
44 Sprite frame = graphicEntityModule.createSprite()
45 .setImage("frame.png")
47 .setRotation(frameRot)
51 Sprite frameBg = graphicEntityModule.createSprite()
52 .setImage("frame_bg.png")
54 .setRotation(frameRot)
57 Sprite avatarSprite = graphicEntityModule.createSprite()
59 .setImage(avatarToken)
64 avatar = graphicEntityModule
65 .createGroup(frame, frameBg, avatarSprite)
68 Text text = graphicEntityModule.createText(nicknameToken)
73 .setFillColor(0x7f3f00)
76 stoneCounter = graphicEntityModule.createText()
81 .setFillColor(0x7f3f00)
85 message = graphicEntityModule.createText()
86 .setX(p0 ? 15 : 1920-15)
90 .setFillColor(0xffbf7f)
91 .setAnchorX(p0 ? 0 : 1)
94 castle = graphicEntityModule.createSprite()
95 .setImage("castle.png")
97 .setX(p0 ? 160 : 1920-160)
102 .setScaleX(p0 ? 1 : -1);
104 stone = graphicEntityModule.createText()
107 .setFillColor(0x12322a)
111 stoneReminder = graphicEntityModule.createText()
112 .setX(p0 ? x + 100 : x - 100)
116 .setFillColor(0x3f3f3f)
117 .setAnchorX(p0 ? 0 : 1)
121 void updateStoneCounter() {
122 int stones = model.getStones();
124 stoneCounter.setText("Out of stones!");
125 stoneCounter.setFillColor(0xff7777);
127 else if (stones == 1) {
128 stoneCounter.setText("1 stone");
129 stoneCounter.setFillColor(0xffbb77);
132 stoneCounter.setText(stones + " stones");
136 void animateStones(int stones) {
137 String stonesString = new Integer(stones).toString();
138 stone.setX(castle.getX());
139 stone.setY(castle.getY() - 100);
140 stone.setText(stonesString);
142 graphicEntityModule.commitEntityState(0, stone);
144 int peakX = (castle.getX() + troll.getX()) / 2;
147 stone.setY(peakY, Curve.EASE_OUT);
148 graphicEntityModule.commitEntityState(0.25,
152 stone.setX(troll.getX());
153 stone.setY(troll.getY() - 50, Curve.EASE_IN);
154 stone.setAlpha(0, Curve.EASE_IN);
155 graphicEntityModule.commitEntityState(0.5, stone);
157 stoneReminder.setText(stonesString);
158 graphicEntityModule.commitEntityState(0.25, stoneReminder);
159 stoneReminder.setAlpha(1);
160 graphicEntityModule.commitEntityState(0.5, stoneReminder);
163 void displayMessage(String msg) {
164 message.setText(msg);
165 graphicEntityModule.commitEntityState(0, message);
169 graphicEntityModule.commitEntityState(0.5, avatar);
170 int dir = random.nextInt(2) == 1 ? 1 : -1;
171 avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
175 graphicEntityModule.commitEntityState(0.5, castle);
176 castle.setX(castle.getX(), Curve.ELASTIC);
177 castle.setScaleY(-0.2, Curve.EASE_IN);
181 stoneReminder.setAlpha(0);
182 graphicEntityModule.commitEntityState(0, stoneReminder);
187 Random random = new Random();
189 Text trollPositionGauge;
190 Player p0 = new Player(), p1 = new Player();
197 * Random π/2-grained rotation of the avatar frames. Avoid
198 * having them π/2 apart, though, as one of them is likely
199 * going to end upside-down and the trick would be revealed.
200 * And I'd have to "draw" a new frame. Ewww.
202 p0.frameRot = random.nextInt(4) * Math.PI / 2;
203 p0.init(gameManager.getPlayer(0));
204 p1.frameRot = p1.frameRot +
205 (random.nextInt(2) == 1 ? 1 : -1) * Math.PI / 2;
206 p1.init(gameManager.getPlayer(1));
216 private void drawBackground() {
217 graphicEntityModule.createSprite()
218 .setImage("background.png")
221 int numTrees = random.nextInt(101);
222 while (numTrees --> 0) {
225 x = random.nextInt(1920);
226 // yes, this biases randomness wrt perspective! :-(
227 y = 700 + random.nextInt(175);
228 } while (y > 880 && (x < 200 || x > 1720));
229 double scale = ( 100.0 / 433.0 // base height from PNG
230 * (y - 680) / (875 - 680) ); // perspective
231 graphicEntityModule.createSprite()
232 .setImage("Alshock.png")
237 .setScaleX(scale * (random.nextInt(2) == 0 ? -1 : 1))
239 .setRotation((random.nextDouble() - 0.5) * Math.PI / 1800)
240 .setSkewX((random.nextDouble() - 0.5) /4)
241 .setSkewY((random.nextDouble() - 0.5) /8);
245 private void drawTroll() {
246 troll = graphicEntityModule.createSprite()
247 .setImage("troll.png")
253 trollPositionGauge = graphicEntityModule.createText()
259 .setFillColor(0xffffff);
265 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
266 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
267 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
268 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
270 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
273 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
274 int delta = model.trollPosition - model.roadLength / 2;
276 trollPositionGauge.setText("← " + Math.abs(delta));
278 else if (delta > 0) {
279 trollPositionGauge.setText(Math.abs(delta) + " →");
282 trollPositionGauge.setText("↔");
284 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
285 trollPositionGauge.setX(troll.getX());