2 * This file is almost CG's toggle module from the SDK. I'd have
3 * subclassed it if it were an option.
6 package com.codingame.game;
8 import java.util.HashMap;
11 import com.codingame.gameengine.core.AbstractPlayer;
12 import com.codingame.gameengine.core.GameManager;
13 import com.codingame.gameengine.core.Module;
14 import com.codingame.gameengine.module.entities.Entity;
15 import com.codingame.gameengine.module.entities.GraphicEntityModule;
16 import com.google.inject.Inject;
17 import com.google.inject.Singleton;
20 * @author Jean Porée, JBM
22 * This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu.
26 public class PantsModule implements Module {
28 GameManager<AbstractPlayer> gameManager;
29 @Inject GraphicEntityModule entityModule;
30 Map<Integer, Object> registered, newRegistration;
34 public boolean state = true;
36 public Toggle(String name, boolean state) {
43 String name = "pants";
45 Pants(int state) { this.state = state; }
49 PantsModule(GameManager<AbstractPlayer> gameManager) {
50 this.gameManager = gameManager;
51 gameManager.registerModule(this);
52 registered = new HashMap<>();
53 newRegistration = new HashMap<>();
57 public void onGameInit() {
62 public void onAfterGameTurn() {
67 public void onAfterOnEnd() {}
69 private void sendFrameData() {
70 Object[] data = { newRegistration };
71 gameManager.setViewData("toggles", data);
73 newRegistration.clear();
76 * Will display the entity only when the toggle state matches the state you set
78 * @param entity which will be displayed
79 * @param toggle the name of the toggle you want to use
80 * @param state the state of the toggle where the entity will be displayed at
82 public void displayOnToggleState(Entity<?> entity, String toggle, boolean state) {
83 int id = entity.getId();
84 Toggle associatedToggle = new Toggle(toggle, state);
85 if (!associatedToggle.equals(registered.get(id))) {
86 newRegistration.put(id, associatedToggle);
87 registered.put(id, associatedToggle);
91 public void displayOnPantsState(Entity<?> entity, int state) {
92 int id = entity.getId();
93 Pants associatedPants = new Pants(state);
94 if (!associatedPants.equals(registered.get(id))) {
95 newRegistration.put(id, associatedPants);
96 registered.put(id, associatedPants);