From 6ee812987dd1b5e42e23ae463838c4c15a2c3e86 Mon Sep 17 00:00:00 2001 From: JBM Date: Sun, 31 May 2020 13:08:49 +0200 Subject: [PATCH 1/1] Pants color selector --- config/statement_en.html | 2 +- .../java/com/codingame/game/PantsModule.java | 99 +++++++++++++++++++ src/main/java/com/codingame/game/View.java | 33 +++++-- src/main/resources/view/config.js | 16 ++- 4 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/codingame/game/PantsModule.java diff --git a/config/statement_en.html b/config/statement_en.html index ff44618..58f3d1c 100644 --- a/config/statement_en.html +++ b/config/statement_en.html @@ -140,7 +140,7 @@

This draft's last change is: - trolls can be of different races. + troll pants' color can be chosen.

diff --git a/src/main/java/com/codingame/game/PantsModule.java b/src/main/java/com/codingame/game/PantsModule.java new file mode 100644 index 0000000..7cf561b --- /dev/null +++ b/src/main/java/com/codingame/game/PantsModule.java @@ -0,0 +1,99 @@ +/* + * This file is almost CG's toggle module from the SDK. I'd have + * subclassed it if it were an option. + */ + +package com.codingame.game; + +import java.util.HashMap; +import java.util.Map; + +import com.codingame.gameengine.core.AbstractPlayer; +import com.codingame.gameengine.core.GameManager; +import com.codingame.gameengine.core.Module; +import com.codingame.gameengine.module.entities.Entity; +import com.codingame.gameengine.module.entities.GraphicEntityModule; +import com.google.inject.Inject; +import com.google.inject.Singleton; + +/** + * @author Jean Porée, JBM + * + * This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu. + * + */ +@Singleton +public class PantsModule implements Module { + + GameManager gameManager; + @Inject GraphicEntityModule entityModule; + Map registered, newRegistration; + + class Toggle { + public String name; + public boolean state = true; + + public Toggle(String name, boolean state) { + this.name = name; + this.state = state; + } + } + + class Pants { + String name = "pants"; + int state = 1; + Pants(int state) { this.state = state; } + } + + @Inject + PantsModule(GameManager gameManager) { + this.gameManager = gameManager; + gameManager.registerModule(this); + registered = new HashMap<>(); + newRegistration = new HashMap<>(); + } + + @Override + public void onGameInit() { + sendFrameData(); + } + + @Override + public void onAfterGameTurn() { + sendFrameData(); + } + + @Override + public void onAfterOnEnd() {} + + private void sendFrameData() { + Object[] data = { newRegistration }; + gameManager.setViewData("toggles", data); + + newRegistration.clear(); + } + /** + * Will display the entity only when the toggle state matches the state you set + * + * @param entity which will be displayed + * @param toggle the name of the toggle you want to use + * @param state the state of the toggle where the entity will be displayed at + */ + public void displayOnToggleState(Entity entity, String toggle, boolean state) { + int id = entity.getId(); + Toggle associatedToggle = new Toggle(toggle, state); + if (!associatedToggle.equals(registered.get(id))) { + newRegistration.put(id, associatedToggle); + registered.put(id, associatedToggle); + } + } + + public void displayOnPantsState(Entity entity, int state) { + int id = entity.getId(); + Pants associatedPants = new Pants(state); + if (!associatedPants.equals(registered.get(id))) { + newRegistration.put(id, associatedPants); + registered.put(id, associatedPants); + } + } +} diff --git a/src/main/java/com/codingame/game/View.java b/src/main/java/com/codingame/game/View.java index e8ae7d7..0820190 100644 --- a/src/main/java/com/codingame/game/View.java +++ b/src/main/java/com/codingame/game/View.java @@ -14,13 +14,12 @@ import com.codingame.gameengine.module.entities.Text; import com.codingame.gameengine.module.entities.TextBasedEntity; import com.codingame.gameengine.module.entities.Group; import com.codingame.gameengine.module.entities.Curve; -import com.codingame.gameengine.module.toggle.ToggleModule; import com.google.inject.Inject; class View { @Inject private MultiplayerGameManager gameManager; @Inject private GraphicEntityModule graphicEntityModule; - @Inject ToggleModule toggleModule; + @Inject PantsModule pantsModule; enum Dir { LEFT("walks left.", 0), @@ -134,7 +133,7 @@ class View { .setFillColor(0xff0080) .setAnchorX(p0 ? 0 : 1) .setAnchorY(0.5); - toggleModule.displayOnToggleState(stoneReminder, "debug", true); + pantsModule.displayOnToggleState(stoneReminder, "debug", true); } void updateStoneCounter() { @@ -406,11 +405,29 @@ class View { .setAnchorX(0.5) .setAnchorY(1) .setTint(trollRace.tint); - Sprite trollPants = graphicEntityModule.createSprite() + Sprite trollPantsRed = graphicEntityModule.createSprite() .setImage("pants_red.png") .setAnchorX(0.5) .setAnchorY(1); - troll = graphicEntityModule.createGroup(trollBody, trollPants) + pantsModule.displayOnPantsState(trollPantsRed, 1); + Sprite trollPantsGreen = graphicEntityModule.createSprite() + .setImage("pants_green.png") + .setAnchorX(0.5) + .setAnchorY(1); + pantsModule.displayOnPantsState(trollPantsGreen, 2); + Sprite trollPantsBlue = graphicEntityModule.createSprite() + .setImage("pants_blue.png") + .setAnchorX(0.5) + .setAnchorY(1); + pantsModule.displayOnPantsState(trollPantsBlue, 3); + Sprite trollPantsPerv = graphicEntityModule.createSprite() + .setImage("pants_perv.png") + .setAnchorX(0.5) + .setAnchorY(1); + pantsModule.displayOnPantsState(trollPantsPerv, 4); + troll = graphicEntityModule + .createGroup(trollBody, trollPantsRed, + trollPantsGreen, trollPantsBlue, trollPantsPerv) .setX(1920/2) .setY(880) .setScaleX(random.nextInt(2) == 0 ? 1 : -1) @@ -433,7 +450,7 @@ class View { .setStrokeColor(0xFFFF00) .setFillColor(0xFFFF00) .setFontSize(40); - toggleModule.displayOnToggleState(trollMessage, "verboseTrolling", true); + pantsModule.displayOnToggleState(trollMessage, "verboseTrolling", true); } private void moveTroll() { @@ -497,7 +514,7 @@ class View { .setY(60) .setAnchorX(0.5) .setLoop(true); - toggleModule.displayOnToggleState(debugMode, "debug", true); + pantsModule.displayOnToggleState(debugMode, "debug", true); turnCounter = graphicEntityModule.createText() .setAnchorX(0.5) @@ -509,7 +526,7 @@ class View { .setFontFamily("monospace") .setFontWeight(Text.FontWeight.BOLD) .setFontSize(100); - toggleModule.displayOnToggleState(turnCounter, "debug", true); + pantsModule.displayOnToggleState(turnCounter, "debug", true); animateTurnCounter(); } diff --git a/src/main/resources/view/config.js b/src/main/resources/view/config.js index 3ac3c9a..933976f 100644 --- a/src/main/resources/view/config.js +++ b/src/main/resources/view/config.js @@ -29,8 +29,8 @@ function trollToggle(toggle, title, def) { } obj['default'] = def; if (def == undefined) { - if (values.length > 2) { - obj['default'] = values.length * Math.floor(2 * Math.random()); + if (obj['values'].length > 2) { + obj['default'] = obj['values'].length * Math.floor(2 * Math.random()); } else { obj['default'] = Math.random() < 0.5; @@ -40,8 +40,20 @@ function trollToggle(toggle, title, def) { } export const options = [ + // TODO: detroll this a bit + trollToggle('konami', 'unused', undefined, + '←', 0, '↓', 1, '↑', 2, '→', 3, 'B', 4, 'A', 5), + trollToggle('pants', 'pants', 1 + Math.floor(3 * Math.random()), + 'red', 1, 'green', 2, 'blue', 3, 'you perv', 4), trollToggle('verboseTrolling', 'Verbose Trolling', true, 'on', true, 'off', false), trollToggle('debug', 'Detroll Mode', false, 'on', true, 'off', false) ]; + +for (var i = options.length-1; i > 1; i--) { + var s = Math.floor((i+1) * Math.random()); + var selected = options[s]; + options[s] = options[i]; + options[i] = selected; +} -- 2.30.2