Time rationalization
authorJBM <jbm@codingame.com>
Sun, 7 Jun 2020 13:06:05 +0000 (15:06 +0200)
committerJBM <jbm@codingame.com>
Sun, 7 Jun 2020 13:06:05 +0000 (15:06 +0200)
PLAN.org
src/main/java/com/codingame/game/View.java

index 0d5dc40..b8d16aa 100644 (file)
--- a/PLAN.org
+++ b/PLAN.org
@@ -50,7 +50,7 @@
 ** TODO Leagues (need multiround)
 ** TODO Multiround (need early termination)
 ** TODO Early termination (need time rationalization)
-** TODO Time rationalization (need code reorg)
+** DONE Time rationalization (need code reorg)
 ** TODO Code cleanup
 
 That one's probably never going to be DONE ^^'
index 9d5cd80..52cfa64 100644 (file)
@@ -21,6 +21,31 @@ class View {
     @Inject private GraphicEntityModule graphicEntityModule;
     @Inject PantsModule pantsModule;
 
+    /*
+     * Frame timings, for a base frame length of 2s:
+     *   - first half: stone throw
+     *   - second half: troll move
+     * The troll message is anchored around the troll move.
+     *
+     * The castle destruction is currently ad hoc simultaneously with
+     * the troll move, but this ought to change if I get some
+     * lengthened frame system ready.
+     *
+     * The endgame message is completely ad hoc, and really ought to
+     * improve.
+     */
+    private final double AVATAR_ANIMATION_START = 0.5;
+    private final double STONE_THROW_START = 0.0;
+    private final double STONE_THROW_PEAK = 0.25;
+    private final double STONE_THROW_END = 0.5;
+    private final double CASTLE_DESTRUCTION_START = 0.5;
+    private final double CASTLE_DESTRUCTION_END = 1.0;
+    private final double TROLL_MOVE_START = 0.5;
+    private final double TROLL_MOVE_END = 1.0;
+    private final double TROLL_MESSAGE_START = 0.5;
+    private final double TROLL_MESSAGE_END = 1.0;
+    private final double ENDGAME_MESSAGE_START = 0.25;
+
     class Player {
         Model.Player model;
 
@@ -128,7 +153,7 @@ class View {
         }
 
         void startTurn() {
-            graphicEntityModule.commitEntityState(0, stoneReminder);
+            graphicEntityModule.commitEntityState(0.0, stoneReminder);
         }
 
         void victory() {
@@ -156,7 +181,7 @@ class View {
         }
 
         void markWinner() {
-            graphicEntityModule.commitEntityState(0.5, avatar);
+            graphicEntityModule.commitEntityState(AVATAR_ANIMATION_START, avatar);
             avatar.setScaleX(1.5, Curve.EASE_OUT);
             avatar.setScaleY(1.5, Curve.EASE_OUT);
             avatar.setRotation((random.nextDouble() - 0.5) * Math.PI / 18,
@@ -164,7 +189,7 @@ class View {
         }
 
         void markLoser() {
-            graphicEntityModule.commitEntityState(0.5, avatar);
+            graphicEntityModule.commitEntityState(AVATAR_ANIMATION_START, avatar);
             int dir = random.nextInt(2) == 1 ? 1 : -1;
             avatar.setRotation(dir * 170 * Math.PI / 180, Curve.ELASTIC);
         }
@@ -204,36 +229,37 @@ class View {
             stone.setY(castle.getY() - 100);
             stone.setText(stonesString);
             stone.setAlpha(1);
-            graphicEntityModule.commitEntityState(0, stone);
+            graphicEntityModule.commitEntityState(STONE_THROW_START, stone);
     
             int peakX = (castle.getX() + troll.getX()) / 2;
             int peakY = 540;
             stone.setX(peakX);
             stone.setY(peakY, Curve.EASE_OUT);
-            graphicEntityModule.commitEntityState(0.25,
+            graphicEntityModule.commitEntityState(STONE_THROW_PEAK,
                                                   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);
+            graphicEntityModule.commitEntityState(STONE_THROW_END, stone);
 
             stoneReminder.setText(stonesString);
-            graphicEntityModule.commitEntityState(0, stoneReminder);
+            graphicEntityModule.commitEntityState(0.0, stoneReminder);
         }
 
         // ========== Player/castle
 
         void displayMessage(String msg) {
             message.setText(msg);
-            graphicEntityModule.commitEntityState(0, message);
+            graphicEntityModule.commitEntityState(0.0, message);
         }
 
         void destroyCastle() {
-            graphicEntityModule.commitEntityState(0.5, castle);
+            graphicEntityModule.commitEntityState(CASTLE_DESTRUCTION_START, castle);
             castle.setX(castle.getX(), Curve.ELASTIC);
             castle.setScaleY(-0.2, Curve.EASE_IN);
+            graphicEntityModule.commitEntityState(CASTLE_DESTRUCTION_END, castle);
         }
     } // class Player
 
@@ -488,7 +514,7 @@ class View {
     }
 
     private void moveTroll() {
-        graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
+        graphicEntityModule.commitEntityState(TROLL_MOVE_START, 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,
@@ -508,7 +534,8 @@ class View {
         else {
             trollPositionGauge.setText(distLeft + " ↔ " + distRight);
         }
-        graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
+        final double moveMid = (TROLL_MOVE_START + TROLL_MOVE_END) / 2;
+        graphicEntityModule.commitEntityState(moveMid, trollPositionGauge);
         trollPositionGauge.setX(troll.getX());
     }
 
@@ -526,9 +553,9 @@ class View {
         gameManager.addToGameSummary(trollRace.starter + " " + d.movement);
 
         trollMessage.setText(selectTrollMessage(d)).setAlpha(1, Curve.NONE);
-        graphicEntityModule.commitEntityState(0.5, trollMessage);
+        graphicEntityModule.commitEntityState(TROLL_MESSAGE_START, trollMessage);
         trollMessage.setAlpha(0, Curve.EASE_IN);
-        graphicEntityModule.commitEntityState(1, trollMessage);
+        graphicEntityModule.commitEntityState(TROLL_MESSAGE_END, trollMessage);
     }
 
     String selectTrollMessage(Dir d) {
@@ -614,7 +641,7 @@ class View {
             .setFillColor(0xff7f7f)
             .setFontWeight(Text.FontWeight.BOLD)
             .setTextAlign(TextBasedEntity.TextAlign.CENTER);
-        graphicEntityModule.commitEntityState(0.25, msg);
+        graphicEntityModule.commitEntityState(ENDGAME_MESSAGE_START, msg);
         Curve curve = Curve.ELASTIC;
         msg.setX(x, Curve.EASE_OUT)
             .setY(y, Curve.ELASTIC)