Break stone counter. FIX AVATAR DEFEAT ANIMATION AT LAST!!!cos0!!
[troll.git] / src / main / java / com / codingame / game / View.java
index 34894ac..dd350fe 100644 (file)
@@ -2,11 +2,225 @@ package com.codingame.game;
 
 import java.util.Random;
 
+import com.codingame.gameengine.core.MultiplayerGameManager;
+import com.codingame.gameengine.module.entities.GraphicEntityModule;
+import com.codingame.gameengine.module.entities.Rectangle;
 import com.codingame.gameengine.module.entities.Sprite;
 import com.codingame.gameengine.module.entities.Text;
+import com.codingame.gameengine.module.entities.Group;
+import com.codingame.gameengine.module.entities.Curve;
+import com.google.inject.Inject;
 
 class View {
+    @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
+    @Inject private GraphicEntityModule graphicEntityModule;
+
+    class Player {
+        Model.Player model;
+        int colorToken;
+        String nicknameToken;
+        String avatarToken;
+
+        Group avatar;
+        Text stoneCounter;
+        Text message;
+        Sprite castle;
+        Text stone;
+
+        void init(com.codingame.game.Player p) {
+            model = p.model;
+            colorToken = p.getColorToken();
+            nicknameToken = p.getNicknameToken();
+            avatarToken = p.getAvatarToken();
+
+            boolean p0 = model.index == 0;
+            int x = p0 ? 280 : 1920 - 280;
+            int y = 220;
+
+            Sprite border1 = graphicEntityModule.createSprite()
+            //    .setX(x)
+            //    .setY(y)
+                .setImage("frame.png")
+                .setAnchor(0.5)
+                .setScale(p0 ? -1 : 1)
+                .setZIndex(22)
+                .setTint(colorToken);
+
+            Sprite border2 = graphicEntityModule.createSprite()
+            //    .setX(x)
+            //    .setY(y)
+                .setImage("frame_bg.png")
+                .setAnchor(0.5)
+                .setZIndex(20);
+
+            Sprite avatarSprite = graphicEntityModule.createSprite()
+            //    .setX(x)
+            //    .setY(y)
+                .setZIndex(21)
+                .setImage(avatarToken)
+                .setAnchor(0.5)
+                .setBaseHeight(116)
+                .setBaseWidth(116);
+
+            avatar = graphicEntityModule
+                .createGroup(border1, border2, avatarSprite)
+                .setX(x).setY(y);
+
+            Text text = graphicEntityModule.createText(nicknameToken)
+                    .setX(x)
+                    .setY(y + 120)
+                    .setZIndex(20)
+                    .setFontSize(40)
+                    .setFillColor(0x7f3f00)
+                    .setAnchor(0.5);
+
+            stoneCounter = graphicEntityModule.createText("S")
+                .setX(x)
+                .setY(y+200)
+                .setZIndex(20)
+                .setFontSize(40)
+                .setFillColor(0x7f3f00)
+                .setAnchor(0.5);
+
+            message = graphicEntityModule.createText()
+                .setX(p0 ? 15 : 1920-15)
+                .setY(680)
+                .setZIndex(1)
+                .setFontSize(40)
+                .setStrokeColor(0x000000)
+                .setFillColor(0xffbf7f)
+                .setAnchorX(p0 ? 0 : 1)
+                .setAnchorY(1);
+
+            castle = graphicEntityModule.createSprite()
+                .setImage("castle.png")
+                .setTint(colorToken)
+                .setX(p0 ? 160 : 1920-160)
+                .setY(p0 ? 890 : 880)
+                .setZIndex(1)
+                .setAnchorX(0.5)
+                .setAnchorY(1)
+                .setScaleX(p0 ? 1 : -1);
+
+            stone = graphicEntityModule.createText()
+                .setZIndex(3)
+                .setFontSize(150)
+                .setFillColor(0x12322a)
+                .setAnchor(0.5)
+                .setAlpha(0);
+        }
+
+        void setStone() {
+            int stones = model.getStones();
+            if (stones <= 0) {
+                stoneCounter.setText("Out of stones!");
+                stoneCounter.setFillColor(0xff7777);
+            }
+            else if (stones == 1) {
+                stoneCounter.setText("1 stone");
+                stoneCounter.setFillColor(0xffbb77);
+            }
+            else {
+                stoneCounter.setText(stones + " stones");
+            }
+        }
+
+        void animateStones(int stones) {
+            stone.setX(castle.getX());
+            stone.setY(castle.getY() - 100);
+            stone.setText(new Integer(stones).toString());
+            stone.setAlpha(1);
+            graphicEntityModule.commitEntityState(0, stone);
+    
+            int peakX = (castle.getX() + troll.getX()) / 2;
+            int peakY = 540;
+            stone.setX(peakX);
+            stone.setY(peakY, Curve.EASE_OUT);
+            graphicEntityModule.commitEntityState(0.25,
+                                                  stone,
+                                                  stoneCounter);
+    
+            stone.setX(troll.getX());
+            stone.setY(troll.getY() - 50, Curve.EASE_IN);
+            stone.setAlpha(0, Curve.EASE_IN);
+            graphicEntityModule.commitEntityState(0.5, stone);
+        }
+
+        void displayMessage(String msg) {
+            message.setText(msg);
+            graphicEntityModule.commitEntityState(0, message);
+        }
+
+        void destroy() {
+            avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
+
+            graphicEntityModule.commitEntityState(0.5, castle);
+            castle.setX(castle.getX(), Curve.ELASTIC);
+            castle.setScaleY(-0.2, Curve.EASE_IN);
+        }
+    }
+
+    Model model;
     Random random = new Random();
     Sprite troll;
     Text trollPositionGauge;
+    Player p0 = new Player(), p1 = new Player();
+
+    void init(Model m) {
+        model = m;
+        drawBackground();
+
+        p0.init(gameManager.getPlayer(0));
+        p1.init(gameManager.getPlayer(1));
+
+        drawTroll();
+    }
+
+    private void drawBackground() {
+        graphicEntityModule.createSprite()
+                .setImage("background.png")
+                .setAnchor(0);
+    }
+
+    private void drawTroll() {
+        troll = graphicEntityModule.createSprite()
+            .setImage("troll.png")
+            .setAnchorX(0.5)
+            .setAnchorY(1)
+            .setX(1920/2)
+            .setY(880)
+            .setZIndex(2);
+        trollPositionGauge = graphicEntityModule.createText()
+            .setZIndex(2)
+            .setAnchor(0.5)
+            .setFontSize(40)
+            .setX(1980/2)
+            .setY(980)
+            .setFillColor(0xffffff);
+    }
+
+    void moveTroll() {
+        graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
+        int x0 = p0.castle.getX(), x1 = p1.castle.getX();
+        int y0 = p0.castle.getY(), y1 = p1.castle.getY();
+        troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
+                   Curve.ELASTIC);
+        troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
+                   Curve.ELASTIC);
+
+        trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
+        int delta = model.trollPosition - model.roadLength / 2;
+        if (delta < 0) {
+            trollPositionGauge.setText("← " + Math.abs(delta));
+        }
+        else if (delta > 0) {
+            trollPositionGauge.setText(Math.abs(delta) + " →");
+        }
+        else {
+            trollPositionGauge.setText("↔");
+        }
+        graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
+        trollPositionGauge.setX(troll.getX());
+    }
+
 }