+ enum Dir {
+ LEFT("walks left."),
+ STILL("stands still."),
+ RIGHT("walks right.");
+
+ String movement;
+ Dir(String mvt) { movement = mvt; }
+ }
+
+ class Player {
+ Model.Player model;
+
+ int colorToken;
+ String nicknameToken;
+ String avatarToken;
+
+ double frameRot;
+
+ Group avatar;
+ Text stoneCounter;
+ Text message;
+ Sprite castle;
+ Text stone;
+ Text stoneReminder;
+
+ 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 frame = graphicEntityModule.createSprite()
+ .setImage("frame.png")
+ .setAnchor(0.5)
+ .setRotation(frameRot)
+ .setZIndex(22)
+ .setTint(colorToken);
+
+ Sprite frameBg = graphicEntityModule.createSprite()
+ .setImage("frame_bg.png")
+ .setAnchor(0.5)
+ .setRotation(frameRot)
+ .setZIndex(20);
+
+ Sprite avatarSprite = graphicEntityModule.createSprite()
+ .setZIndex(21)
+ .setImage(avatarToken)
+ .setAnchor(0.5)
+ .setBaseHeight(116)
+ .setBaseWidth(116);
+
+ avatar = graphicEntityModule
+ .createGroup(frame, frameBg, 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()
+ .setX(x)
+ .setY(y+200)
+ .setZIndex(20)
+ .setFontSize(40)
+ .setFillColor(0x7f3f00)
+ .setAnchor(0.5);
+ updateStoneCounter();
+
+ message = graphicEntityModule.createText()
+ .setX(p0 ? 15 : 1920-15)
+ .setY(680)
+ .setZIndex(1)
+ .setFontSize(40)
+ .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);
+
+ stoneReminder = graphicEntityModule.createText()
+ .setX(p0 ? x + 100 : x - 100)
+ .setY(y)
+ .setZIndex(20)
+ .setFontSize(80)
+ .setFontFamily("monospace")
+ .setStrokeColor(0xff0080)
+ .setFillColor(0xff0080)
+ .setAnchorX(p0 ? 0 : 1)
+ .setAnchorY(0.5);
+ toggleModule.displayOnToggleState(stoneReminder, "debug", true);
+ }
+
+ void updateStoneCounter() {
+ 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) {
+ String stonesString = Integer.valueOf(stones).toString();
+ stone.setX(castle.getX());
+ stone.setY(castle.getY() - 100);
+ stone.setText(stonesString);
+ 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);
+
+ stoneReminder.setText(stonesString);
+ graphicEntityModule.commitEntityState(0, stoneReminder);
+ }
+
+ void displayMessage(String msg) {
+ message.setText(msg);
+ graphicEntityModule.commitEntityState(0, message);
+ }
+
+ void markLoser() {
+ graphicEntityModule.commitEntityState(0.5, avatar);
+ int dir = random.nextInt(2) == 1 ? 1 : -1;
+ avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
+ }
+
+ void destroy() {
+ gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + nicknameToken + "."));
+ graphicEntityModule.commitEntityState(0.5, castle);
+ castle.setX(castle.getX(), Curve.ELASTIC);
+ castle.setScaleY(-0.2, Curve.EASE_IN);
+ }
+
+ void startTurn() {
+ graphicEntityModule.commitEntityState(0, stoneReminder);
+ }
+
+ void victory() {
+ gameManager.addToGameSummary(GameManager.formatSuccessMessage(nicknameToken + " wins."));
+ graphicEntityModule.commitEntityState(0.5, avatar);
+ avatar.setScaleX(1.5, Curve.EASE_OUT);
+ avatar.setScaleY(1.5, Curve.EASE_OUT);
+ avatar.setRotation((random.nextDouble() - 0.5) * Math.PI / 18,
+ Curve.ELASTIC);
+ }
+
+ void throwStones(int stones) {
+ gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", nicknameToken, stones, stones == 1 ? "" : "s"));
+ }
+
+ void threwMoreStonesThanHad() {
+ gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried to throw more stones than they had. I'll let it slide for this time. (But not let them throw that much!)"));
+ }
+
+ void failedToThrowStonesAndShouldHave() {
+ gameManager.addToGameSummary(GameManager.formatErrorMessage(nicknameToken + " tried not throwing any stones. Fixing that for them because I'm in a good mood today."));
+ }
+
+ void markTimeout() {
+ animateLoss(avatar.getX(), avatar.getY(), 100, "SLOW\nPOKE");
+ }
+
+ void markIllegal() {
+ animateLoss(avatar.getX(), avatar.getY(), 100, "STUPID");
+ }
+
+ void markCheat() {
+ animateLoss(avatar.getX(), avatar.getY(), 100, "CHEATER");
+ }
+ }
+
+ Model model;