Repair gauges
[troll.git] / src / main / java / com / codingame / game / View.java
1 package com.codingame.game;
2
3 import java.util.Random;
4
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;
13
14 class View {
15     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
16     @Inject private GraphicEntityModule graphicEntityModule;
17
18     class Player {
19         Model.Player model;
20         int colorToken;
21         String nicknameToken;
22         String avatarToken;
23
24         Group avatar;
25         Text stoneCounter;
26         Text message;
27         Sprite castle;
28         Text stone;
29
30         void init(com.codingame.game.Player p) {
31             model = p.model;
32             colorToken = p.getColorToken();
33             nicknameToken = p.getNicknameToken();
34             avatarToken = p.getAvatarToken();
35
36             boolean p0 = model.index == 0;
37             int x = p0 ? 280 : 1920 - 280;
38             int y = 220;
39
40             Sprite border1 = graphicEntityModule.createSprite()
41                 .setImage("frame.png")
42                 .setAnchor(0.5)
43                 .setScale(p0 ? -1 : 1)
44                 .setZIndex(22)
45                 .setTint(colorToken);
46
47             Sprite border2 = graphicEntityModule.createSprite()
48                 .setImage("frame_bg.png")
49                 .setAnchor(0.5)
50                 .setZIndex(20);
51
52             Sprite avatarSprite = graphicEntityModule.createSprite()
53                 .setZIndex(21)
54                 .setImage(avatarToken)
55                 .setAnchor(0.5)
56                 .setBaseHeight(116)
57                 .setBaseWidth(116);
58
59             avatar = graphicEntityModule
60                 .createGroup(border1, border2, avatarSprite)
61                 .setX(x).setY(y);
62
63             Text text = graphicEntityModule.createText(nicknameToken)
64                     .setX(x)
65                     .setY(y + 120)
66                     .setZIndex(20)
67                     .setFontSize(40)
68                     .setFillColor(0x7f3f00)
69                     .setAnchor(0.5);
70
71             stoneCounter = graphicEntityModule.createText()
72                 .setX(x)
73                 .setY(y+200)
74                 .setZIndex(20)
75                 .setFontSize(40)
76                 .setFillColor(0x7f3f00)
77                 .setAnchor(0.5);
78             updateStoneCounter();
79
80             message = graphicEntityModule.createText()
81                 .setX(p0 ? 15 : 1920-15)
82                 .setY(680)
83                 .setZIndex(1)
84                 .setFontSize(40)
85                 .setStrokeColor(0x000000)
86                 .setFillColor(0xffbf7f)
87                 .setAnchorX(p0 ? 0 : 1)
88                 .setAnchorY(1);
89
90             castle = graphicEntityModule.createSprite()
91                 .setImage("castle.png")
92                 .setTint(colorToken)
93                 .setX(p0 ? 160 : 1920-160)
94                 .setY(p0 ? 890 : 880)
95                 .setZIndex(1)
96                 .setAnchorX(0.5)
97                 .setAnchorY(1)
98                 .setScaleX(p0 ? 1 : -1);
99
100             stone = graphicEntityModule.createText()
101                 .setZIndex(3)
102                 .setFontSize(150)
103                 .setFillColor(0x12322a)
104                 .setAnchor(0.5)
105                 .setAlpha(0);
106         }
107
108         void updateStoneCounter() {
109             int stones = model.getStones();
110             if (stones <= 0) {
111                 stoneCounter.setText("Out of stones!");
112                 stoneCounter.setFillColor(0xff7777);
113             }
114             else if (stones == 1) {
115                 stoneCounter.setText("1 stone");
116                 stoneCounter.setFillColor(0xffbb77);
117             }
118             else {
119                 stoneCounter.setText(stones + " stones");
120             }
121         }
122
123         void animateStones(int stones) {
124             stone.setX(castle.getX());
125             stone.setY(castle.getY() - 100);
126             stone.setText(new Integer(stones).toString());
127             stone.setAlpha(1);
128             graphicEntityModule.commitEntityState(0, stone);
129     
130             int peakX = (castle.getX() + troll.getX()) / 2;
131             int peakY = 540;
132             stone.setX(peakX);
133             stone.setY(peakY, Curve.EASE_OUT);
134             graphicEntityModule.commitEntityState(0.25,
135                                                   stone,
136                                                   stoneCounter);
137     
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);
142         }
143
144         void displayMessage(String msg) {
145             message.setText(msg);
146             graphicEntityModule.commitEntityState(0, message);
147         }
148
149         void destroy() {
150             avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
151
152             graphicEntityModule.commitEntityState(0.5, castle);
153             castle.setX(castle.getX(), Curve.ELASTIC);
154             castle.setScaleY(-0.2, Curve.EASE_IN);
155         }
156     }
157
158     Model model;
159     Random random = new Random();
160     Sprite troll;
161     Text trollPositionGauge;
162     Player p0 = new Player(), p1 = new Player();
163
164     void init(Model m) {
165         model = m;
166         drawBackground();
167
168         p0.init(gameManager.getPlayer(0));
169         p1.init(gameManager.getPlayer(1));
170
171         drawTroll();
172     }
173
174     private void drawBackground() {
175         graphicEntityModule.createSprite()
176                 .setImage("background.png")
177                 .setAnchor(0);
178     }
179
180     private void drawTroll() {
181         troll = graphicEntityModule.createSprite()
182             .setImage("troll.png")
183             .setAnchorX(0.5)
184             .setAnchorY(1)
185             .setX(1920/2)
186             .setY(880)
187             .setZIndex(2);
188         trollPositionGauge = graphicEntityModule.createText()
189             .setZIndex(2)
190             .setAnchor(0.5)
191             .setFontSize(40)
192             .setX(1980/2)
193             .setY(980)
194             .setFillColor(0xffffff);
195
196         moveTroll();
197     }
198
199     void moveTroll() {
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,
204                    Curve.ELASTIC);
205         troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
206                    Curve.ELASTIC);
207
208         trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
209         int delta = model.trollPosition - model.roadLength / 2;
210         if (delta < 0) {
211             trollPositionGauge.setText("← " + Math.abs(delta));
212         }
213         else if (delta > 0) {
214             trollPositionGauge.setText(Math.abs(delta) + " →");
215         }
216         else {
217             trollPositionGauge.setText("↔");
218         }
219         graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
220         trollPositionGauge.setX(troll.getX());
221     }
222
223 }