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;
30 void init(com.codingame.game.Player p) {
32 colorToken = p.getColorToken();
33 nicknameToken = p.getNicknameToken();
34 avatarToken = p.getAvatarToken();
36 boolean p0 = model.index == 0;
37 int x = p0 ? 280 : 1920 - 280;
40 Sprite border1 = graphicEntityModule.createSprite()
41 .setImage("frame.png")
43 .setScale(p0 ? -1 : 1)
47 Sprite border2 = graphicEntityModule.createSprite()
48 .setImage("frame_bg.png")
52 Sprite avatarSprite = graphicEntityModule.createSprite()
54 .setImage(avatarToken)
59 avatar = graphicEntityModule
60 .createGroup(border1, border2, avatarSprite)
63 Text text = graphicEntityModule.createText(nicknameToken)
68 .setFillColor(0x7f3f00)
71 stoneCounter = graphicEntityModule.createText()
76 .setFillColor(0x7f3f00)
80 message = graphicEntityModule.createText()
81 .setX(p0 ? 15 : 1920-15)
85 .setStrokeColor(0x000000)
86 .setFillColor(0xffbf7f)
87 .setAnchorX(p0 ? 0 : 1)
90 castle = graphicEntityModule.createSprite()
91 .setImage("castle.png")
93 .setX(p0 ? 160 : 1920-160)
98 .setScaleX(p0 ? 1 : -1);
100 stone = graphicEntityModule.createText()
103 .setFillColor(0x12322a)
108 void updateStoneCounter() {
109 int stones = model.getStones();
111 stoneCounter.setText("Out of stones!");
112 stoneCounter.setFillColor(0xff7777);
114 else if (stones == 1) {
115 stoneCounter.setText("1 stone");
116 stoneCounter.setFillColor(0xffbb77);
119 stoneCounter.setText(stones + " stones");
123 void animateStones(int stones) {
124 stone.setX(castle.getX());
125 stone.setY(castle.getY() - 100);
126 stone.setText(new Integer(stones).toString());
128 graphicEntityModule.commitEntityState(0, stone);
130 int peakX = (castle.getX() + troll.getX()) / 2;
133 stone.setY(peakY, Curve.EASE_OUT);
134 graphicEntityModule.commitEntityState(0.25,
138 stone.setX(troll.getX());
139 stone.setY(troll.getY() - 50, Curve.EASE_IN);
140 stone.setAlpha(0, Curve.EASE_IN);
141 graphicEntityModule.commitEntityState(0.5, stone);
144 void displayMessage(String msg) {
145 message.setText(msg);
146 graphicEntityModule.commitEntityState(0, message);
150 avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
152 graphicEntityModule.commitEntityState(0.5, castle);
153 castle.setX(castle.getX(), Curve.ELASTIC);
154 castle.setScaleY(-0.2, Curve.EASE_IN);
159 Random random = new Random();
161 Text trollPositionGauge;
162 Player p0 = new Player(), p1 = new Player();
168 p0.init(gameManager.getPlayer(0));
169 p1.init(gameManager.getPlayer(1));
174 private void drawBackground() {
175 graphicEntityModule.createSprite()
176 .setImage("background.png")
180 private void drawTroll() {
181 troll = graphicEntityModule.createSprite()
182 .setImage("troll.png")
188 trollPositionGauge = graphicEntityModule.createText()
194 .setFillColor(0xffffff);
200 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
201 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
202 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
203 troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
205 troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
208 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
209 int delta = model.trollPosition - model.roadLength / 2;
211 trollPositionGauge.setText("← " + Math.abs(delta));
213 else if (delta > 0) {
214 trollPositionGauge.setText(Math.abs(delta) + " →");
217 trollPositionGauge.setText("↔");
219 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
220 trollPositionGauge.setX(troll.getX());