X-Git-Url: https://troll.desast.re/troll.git/blobdiff_plain/076ea5bbdda8751cd178b657157d3a08458b7c7b..20c63dac92d830a4c61f6d17ecfb8e7e0d04e58d:/src/main/java/com/codingame/game/Referee.java?ds=sidebyside diff --git a/src/main/java/com/codingame/game/Referee.java b/src/main/java/com/codingame/game/Referee.java index 6866b9b..f087503 100644 --- a/src/main/java/com/codingame/game/Referee.java +++ b/src/main/java/com/codingame/game/Referee.java @@ -21,55 +21,57 @@ public class Referee extends AbstractReferee { @Inject private MultiplayerGameManager gameManager; @Inject private GraphicEntityModule graphicEntityModule; - int roadLength; - int initialStones; - int trollPosition; + Model model = new Model(); + + // Hybrid :-( Player p0, p1; - Sprite troll; - Text trollPositionGauge; + + // View + View view = new View(); @Override public void init() { - switch (new Random(gameManager.getSeed()).nextInt(4)) { + model.random = new Random(gameManager.getSeed()); + switch (model.random.nextInt(4)) { case 0: - roadLength = 7; - initialStones = 15; + model.roadLength = 6; + model.initialStones = 15; break; case 1: - roadLength = 7; - initialStones = 30; + model.roadLength = 6; + model.initialStones = 30; break; case 2: - roadLength = 15; - initialStones = 30; + model.roadLength = 14; + model.initialStones = 30; break; case 3: - roadLength = 15; - initialStones = 50; + model.roadLength = 14; + model.initialStones = 50; break; } - trollPosition = (roadLength - 1) / 2; + model.trollPosition = model.roadLength / 2; p0 = gameManager.getPlayer(0); p0.setCastlePosition(0); p0.setMultiplier(1); - p0.adjustScore(trollPosition); - p0.sendInputLine(String.format("%d %d", roadLength - 1, initialStones)); + p0.adjustScore(model.trollPosition); + p0.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones)); p1 = gameManager.getPlayer(1); - p1.setCastlePosition(roadLength-1); + p1.setCastlePosition(model.roadLength); p1.setMultiplier(-1); - p1.adjustScore(trollPosition); - p1.sendInputLine(String.format("%d %d", roadLength - 1, initialStones)); + p1.adjustScore(model.trollPosition); + p1.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones)); drawBackground(); drawPlayer(); drawTroll(); // result in text display, so do last: - p0.setStones(initialStones); - p1.setStones(initialStones); + p0.setStones(model.initialStones); + p1.setStones(model.initialStones); gameManager.setFrameDuration(2000); } @@ -104,15 +106,7 @@ public class Referee extends AbstractReferee { .setLineWidth(0) .setFillColor(0xffffff); - Text text = graphicEntityModule.createText(player.getNicknameToken()) - .setX(x) - .setY(y + 120) - .setZIndex(20) - .setFontSize(40) - .setFillColor(0x7f3f00) - .setAnchor(0.5); - - Sprite avatar = graphicEntityModule.createSprite() + Sprite avatarSprite = graphicEntityModule.createSprite() .setX(x) .setY(y) .setZIndex(20) @@ -121,7 +115,17 @@ public class Referee extends AbstractReferee { .setBaseHeight(116) .setBaseWidth(116); - Text stoneCounter = graphicEntityModule.createText("S") + player.avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite); + + Text text = graphicEntityModule.createText(player.getNicknameToken()) + .setX(x) + .setY(y + 120) + .setZIndex(20) + .setFontSize(40) + .setFillColor(0x7f3f00) + .setAnchor(0.5); + + player.stoneCounter = graphicEntityModule.createText("S") .setX(x) .setY(y+200) .setZIndex(20) @@ -129,13 +133,25 @@ public class Referee extends AbstractReferee { .setFillColor(0x7f3f00) .setAnchor(0.5); + player.message = graphicEntityModule.createText() + .setX(p0 ? 15 : 1920-15) + .setY(680) + .setZIndex(1) + .setFontSize(40) + .setStrokeColor(0x000000) + .setFillColor(0xffbf7f) + .setAnchorX(p0 ? 0 : 1) + .setAnchorY(1); + player.castle = graphicEntityModule.createSprite() .setImage("castle.png") + .setTint(player.getColorToken()) .setX(p0 ? 160 : 1920-160) .setY(p0 ? 890 : 880) .setZIndex(1) .setAnchorX(0.5) - .setAnchorY(1); + .setAnchorY(1) + .setScaleX(p0 ? 1 : -1); player.stone = graphicEntityModule.createText() .setZIndex(3) @@ -143,21 +159,18 @@ public class Referee extends AbstractReferee { .setFillColor(0x12322a) .setAnchor(0.5) .setAlpha(0); - - player.hud = graphicEntityModule.createGroup(border1, border2, text, avatar, stoneCounter, player.castle); - player.stoneCounter = stoneCounter; } } private void drawTroll() { - troll = graphicEntityModule.createSprite() + view.troll = graphicEntityModule.createSprite() .setImage("troll.png") .setAnchorX(0.5) .setAnchorY(1) .setX(1920/2) .setY(880) .setZIndex(2); - trollPositionGauge = graphicEntityModule.createText() + view.trollPositionGauge = graphicEntityModule.createText() .setZIndex(2) .setAnchor(0.5) .setFontSize(40) @@ -167,27 +180,27 @@ public class Referee extends AbstractReferee { } private void moveTroll() { - graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge); + graphicEntityModule.commitEntityState(0.5, view.troll, view.trollPositionGauge); int x0 = p0.castle.getX(), x1 = p1.castle.getX(); int y0 = p0.castle.getY(), y1 = p1.castle.getY(); - troll.setX(x0 + trollPosition * (x1-x0) / (roadLength - 1), + view.troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength, Curve.ELASTIC); - troll.setY(y0 + trollPosition * (y1-y0) / (roadLength - 1), + view.troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength, Curve.ELASTIC); - trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2); - int delta = trollPosition - (roadLength - 1) / 2; + view.trollPositionGauge.setX((view.trollPositionGauge.getX() + view.troll.getX()) / 2); + int delta = model.trollPosition - model.roadLength / 2; if (delta < 0) { - trollPositionGauge.setText("← " + Math.abs(delta)); + view.trollPositionGauge.setText("← " + Math.abs(delta)); } else if (delta > 0) { - trollPositionGauge.setText(Math.abs(delta) + " →"); + view.trollPositionGauge.setText(Math.abs(delta) + " →"); } else { - trollPositionGauge.setText("↔"); + view.trollPositionGauge.setText("↔"); } - graphicEntityModule.commitEntityState(0.75, trollPositionGauge); - trollPositionGauge.setX(troll.getX()); + graphicEntityModule.commitEntityState(0.75, view.trollPositionGauge); + view.trollPositionGauge.setX(view.troll.getX()); } @Override @@ -203,27 +216,42 @@ public class Referee extends AbstractReferee { int delta = 0; for (Player player : gameManager.getActivePlayers()) { try { - final int stones = player.getAction(); + int stones = player.getAction(); + if (stones == 0 && player.getStones() > 0) { + if (view.random.nextInt(10) > 0) { + gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today.")); + stones = 1; + } + else { + throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue."); + } + } player.consumeStones(stones); gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s")); delta += player.getMultiplier() * stones; - if (stones > 0) { + if (stones < 0) { + player.deactivate(player.getNicknameToken() + " CHEAT"); + gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account.")); + player.setScore(-1); + endGame(); + } + else if (stones > 0) { player.stone.setX(player.castle.getX()); player.stone.setY(player.castle.getY() - 100); player.stone.setText(new Integer(stones).toString()); player.stone.setAlpha(1); graphicEntityModule.commitEntityState(0, player.stone); - int peakX = (player.castle.getX() + troll.getX()) / 2; + int peakX = (player.castle.getX() + view.troll.getX()) / 2; int peakY = 540; player.stone.setX(peakX); player.stone.setY(peakY, Curve.EASE_OUT); graphicEntityModule.commitEntityState(0.25, player.stone, player.stoneCounter); - player.stone.setX(troll.getX()); - player.stone.setY(troll.getY() - 50, Curve.EASE_IN); + player.stone.setX(view.troll.getX()); + player.stone.setY(view.troll.getY() - 50, Curve.EASE_IN); player.stone.setAlpha(0, Curve.EASE_IN); graphicEntityModule.commitEntityState(0.5, player.stone); } @@ -246,16 +274,21 @@ public class Referee extends AbstractReferee { player.setScore(-1); endGame(); } + + player.message + .setText(player.getMessageString()); + // .setAnchorX(/*player == p0 ? 0 : */ 1); + graphicEntityModule.commitEntityState(0, player.message); } if (delta > 0) { gameManager.addToGameSummary("Troll walks right."); - trollPosition++; + model.trollPosition++; moveTroll(); } else if (delta < 0) { gameManager.addToGameSummary("Troll walks left."); - trollPosition--; + model.trollPosition--; moveTroll(); } else { @@ -263,8 +296,8 @@ public class Referee extends AbstractReferee { } for (Player player : gameManager.getActivePlayers()) { - player.adjustScore(trollPosition); - if (trollPosition == player.getCastlePosition()) { + player.adjustScore(model.trollPosition); + if (model.trollPosition == player.getCastlePosition()) { gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken())); endGame(); } @@ -275,21 +308,34 @@ public class Referee extends AbstractReferee { } } + private void destroyPlayer(Player player) { + player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC); + + graphicEntityModule.commitEntityState(0.5, player.castle); + player.castle.setX(player.castle.getX(), Curve.ELASTIC); + player.castle.setScaleY(-0.2, Curve.EASE_IN); + } + private void endGame() { - gameManager.endGame(); + if (! gameManager.isGameEnd()) { + gameManager.endGame(); - if (p0.getScore() > p1.getScore()) { - gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins")); - p1.hud.setAlpha(0.3); - } - else if (p0.getScore() < p1.getScore()) { - gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins")); - p0.hud.setAlpha(0.3); - } - else { - gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!")); - p0.hud.setAlpha(0.3); - p1.hud.setAlpha(0.3); + if (p0.getScore() > p1.getScore()) { + gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins")); + destroyPlayer(p1); + } + else if (p0.getScore() < p1.getScore()) { + gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins")); + destroyPlayer(p0); + } + else if (p0.getScore() < 0) { + gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!")); + destroyPlayer(p0); + destroyPlayer(p1); + } + else { + gameManager.addToGameSummary("Draw."); + } } } }