Major model/view split. Compiles and runs, but viewer is sub-par.
[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             Rectangle border1 = graphicEntityModule
41                     .createRectangle()
42                     .setWidth(140)
43                     .setHeight(140)
44                     .setX(x - 70)
45                     .setY(y - 70)
46                     .setLineWidth(0)
47                     .setFillColor(colorToken);
48
49             Rectangle border2 = graphicEntityModule
50                     .createRectangle()
51                     .setWidth(120)
52                     .setHeight(120)
53                     .setX(x - 60)
54                     .setY(y - 60)
55                     .setLineWidth(0)
56                     .setFillColor(0xffffff);
57
58             Sprite avatarSprite = graphicEntityModule.createSprite()
59                     .setX(x)
60                     .setY(y)
61                     .setZIndex(20)
62                     .setImage(avatarToken)
63                     .setAnchor(0.5)
64                     .setBaseHeight(116)
65                     .setBaseWidth(116);
66
67             avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite);
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("No 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         void animateStones(int stones) {
128             stone.setX(castle.getX());
129             stone.setY(castle.getY() - 100);
130             stone.setText(new Integer(stones).toString());
131             stone.setAlpha(1);
132             graphicEntityModule.commitEntityState(0, stone);
133     
134             int peakX = (castle.getX() + troll.getX()) / 2;
135             int peakY = 540;
136             stone.setX(peakX);
137             stone.setY(peakY, Curve.EASE_OUT);
138             graphicEntityModule.commitEntityState(0.25,
139                                                   stone,
140                                                   stoneCounter);
141     
142             stone.setX(troll.getX());
143             stone.setY(troll.getY() - 50, Curve.EASE_IN);
144             stone.setAlpha(0, Curve.EASE_IN);
145             graphicEntityModule.commitEntityState(0.5, stone);
146         }
147         void displayMessage(String msg) {
148             message.setText(msg);
149             graphicEntityModule.commitEntityState(0, message);
150         }
151
152         void destroy() {
153         avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
154
155         graphicEntityModule.commitEntityState(0.5, castle);
156         castle.setX(castle.getX(), Curve.ELASTIC);
157         castle.setScaleY(-0.2, Curve.EASE_IN);
158     }
159     }
160
161     Model model;
162     Random random = new Random();
163     Sprite troll;
164     Text trollPositionGauge;
165     Player p0 = new Player(), p1 = new Player();
166
167     void init(Model m) {
168         model = m;
169         p0.init(gameManager.getPlayer(0));
170         p1.init(gameManager.getPlayer(1));
171
172         drawBackground();
173         drawTroll();
174     }
175
176     private void drawBackground() {
177         graphicEntityModule.createSprite()
178                 .setImage("background.png")
179                 .setAnchor(0);
180     }
181
182     private void drawTroll() {
183         troll = graphicEntityModule.createSprite()
184             .setImage("troll.png")
185             .setAnchorX(0.5)
186             .setAnchorY(1)
187             .setX(1920/2)
188             .setY(880)
189             .setZIndex(2);
190         trollPositionGauge = graphicEntityModule.createText()
191             .setZIndex(2)
192             .setAnchor(0.5)
193             .setFontSize(40)
194             .setX(1980/2)
195             .setY(980)
196             .setFillColor(0xffffff);
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 }