FIX: IT belongs in a MODEL!
[troll.git] / src / main / java / com / codingame / game / PantsModule.java
1 /*
2  * This file is almost CG's toggle module from the SDK.  I'd have
3  * subclassed it if it were an option.
4  */
5
6 package com.codingame.game;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
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;
18
19 /**
20  * @author Jean PorĂ©e, JBM
21  * 
22  *         This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu.
23  * 
24  */
25 @Singleton
26 public class PantsModule implements Module {
27
28     GameManager<AbstractPlayer> gameManager;
29     @Inject GraphicEntityModule entityModule;
30     Map<Integer, Object> registered, newRegistration;
31
32     class Toggle {
33         public String name;
34         public boolean state = true;
35
36         public Toggle(String name, boolean state) {
37             this.name = name;
38             this.state = state;
39         }
40     }
41
42     class Pants {
43         String name = "pants";
44         int state = 1;
45         Pants(int state) { this.state = state; }
46     }
47
48     @Inject
49     PantsModule(GameManager<AbstractPlayer> gameManager) {
50         this.gameManager = gameManager;
51         gameManager.registerModule(this);
52         registered = new HashMap<>();
53         newRegistration = new HashMap<>();
54     }
55
56     @Override
57     public void onGameInit() {
58         sendFrameData();
59     }
60
61     @Override
62     public void onAfterGameTurn() {
63         sendFrameData();
64     }
65
66     @Override
67     public void onAfterOnEnd() {}
68
69     private void sendFrameData() {
70         Object[] data = { newRegistration };
71         gameManager.setViewData("toggles", data);
72
73         newRegistration.clear();
74     }
75     /**
76      * Will display the entity only when the toggle state matches the state you set
77      * 
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
81      */
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);
88         }
89     }
90
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);
97         }
98     }
99 }