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