Skip to content

Commit 5e682ae

Browse files
committed
Merge branch 'jpn/merge-butanium-inteactive-display-module' into 'master'
[PR] Merge community module See merge request codingame/game-engine!329
2 parents 80c3e2d + a24482e commit 5e682ae

File tree

6 files changed

+659
-0
lines changed

6 files changed

+659
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Interactive display module
2+
3+
Contributed by [Butanium](https://github.com/Butanium).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.codingame</groupId>
7+
<artifactId>gameengine</artifactId>
8+
<version>master-SNAPSHOT</version>
9+
<relativePath>../../../pom.xml</relativePath>
10+
</parent>
11+
12+
<groupId>com.codingame.gameengine</groupId>
13+
<artifactId>module-interactivedisplay</artifactId>
14+
<name>CodinGame Game Engine Interactive display module</name>
15+
<description>The InteractiveDisplayModule allows you to display entities when the mouse is over an entity or
16+
when the user clicks an entity.</description>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.codingame.gameengine</groupId>
21+
<artifactId>core</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.codingame.gameengine</groupId>
26+
<artifactId>module-entities</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.codingame.gameengine.module.interactivedisplay;
2+
3+
import com.codingame.gameengine.core.AbstractPlayer;
4+
import com.codingame.gameengine.core.GameManager;
5+
import com.codingame.gameengine.core.Module;
6+
import com.codingame.gameengine.module.entities.Entity;
7+
import com.google.inject.Inject;
8+
import com.google.inject.Singleton;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
/**
14+
* The InteractiveDisplayModule allows you to display entities when the mouse is over an entity or when an entity is
15+
* clicked.
16+
*/
17+
@Singleton
18+
public class InteractiveDisplayModule implements Module {
19+
public static final String BOTH = "B";
20+
public static final String HOVER_ONLY = "H";
21+
public static final String CLICK_ONLY = "C";
22+
23+
private static final String DISPLAY = "D";
24+
private static final String RESIZE = "R";
25+
26+
GameManager<AbstractPlayer> gameManager;
27+
Map<Integer, Map<Integer, String>> newRegistration;
28+
Map<Integer, Map<Integer, String>> registration;
29+
30+
31+
@Inject
32+
InteractiveDisplayModule(GameManager<AbstractPlayer> gameManager) {
33+
this.gameManager = gameManager;
34+
gameManager.registerModule(this);
35+
newRegistration = new HashMap<>();
36+
registration = new HashMap<>();
37+
}
38+
39+
@Override
40+
public void onGameInit() {
41+
sendFrameData();
42+
}
43+
44+
@Override
45+
public void onAfterGameTurn() {
46+
sendFrameData();
47+
}
48+
49+
@Override
50+
public void onAfterOnEnd() {
51+
}
52+
53+
private void sendFrameData() {
54+
if (!newRegistration.isEmpty()) {
55+
Object data = new HashMap[]{new HashMap<>(newRegistration)};
56+
newRegistration.clear();
57+
gameManager.setViewData("intDisplay", data);
58+
}
59+
}
60+
61+
62+
/**
63+
* Make <code>displayEntities</code> appear when the mouse is over <code>entity</code>.
64+
*
65+
* @param entity the entity to track
66+
* @param displayEntity the entity to display when the mouse is over <code>entity</code>
67+
* @param mode when the displayEntity has to be displayed (HOVER_ONLY, CLICK_ONLY or BOTH)
68+
*/
69+
public void addDisplay(Entity<?> entity, Entity<?> displayEntity, String mode) {
70+
Map<Integer, String> displays = registration.getOrDefault(entity.getId(), new HashMap<>());
71+
displays.put(displayEntity.getId(), DISPLAY + "," + mode);
72+
registration.put(entity.getId(), displays);
73+
newRegistration.put(entity.getId(), displays);
74+
}
75+
76+
/**
77+
* Make <code>displayEntities</code> appear when the mouse is over <code>entity</code>.
78+
*
79+
* @param entity the entity to track
80+
* @param displayEntity the entity to display when the mouse is over <code>entity</code>
81+
*/
82+
public void addDisplay(Entity<?> entity, Entity<?> displayEntity) {
83+
addDisplay(entity, displayEntity, BOTH);
84+
}
85+
86+
87+
/**
88+
* Stop displaying/resizing entities when <code>entity</code> is hovered/clicked
89+
*
90+
* @param entity the entity to stop tracking
91+
*/
92+
public void untrack(Entity<?> entity) {
93+
newRegistration.put(entity.getId(), new HashMap<>());
94+
}
95+
96+
/**
97+
* Stop transforming associatedEntity when entity is clicked/Hovered
98+
*
99+
* @param entity the interactive entity
100+
* @param associatedEntity the entity that won't be transformed anymore
101+
*/
102+
public void removeTransformation(Entity<?> entity, Entity<?> associatedEntity) {
103+
Map<Integer, String> displays = registration.getOrDefault(entity.getId(), new HashMap<>());
104+
if (displays.remove(associatedEntity.getId()) != null) {
105+
newRegistration.put(entity.getId(), displays);
106+
}
107+
}
108+
109+
110+
/**
111+
* Make <code>associatedEntity</code> bigger when <code>entity</code> is hovered/clicked depending on <code>mode</code>
112+
*
113+
* @param entity the entity to track
114+
* @param associatedEntity the entity to resize
115+
* @param factor the factor by which the associatedEntity has to be resized
116+
* @param mode when the associatedEntity has to be resized (HOVER_ONLY, CLICK_ONLY or BOTH)
117+
*/
118+
public void addResize(Entity<?> entity, Entity<?> associatedEntity, double factor, String mode) {
119+
Map<Integer, String> resizes = registration.getOrDefault(entity.getId(), new HashMap<>());
120+
resizes.put(associatedEntity.getId(), RESIZE + "," + mode + "," + factor);
121+
registration.put(entity.getId(), resizes);
122+
newRegistration.put(entity.getId(), resizes);
123+
}
124+
125+
/**
126+
* Make <code>associatedEntity</code> <code>factor</code> times bigger when <code>entity</code> is hovered or clicked
127+
*
128+
* @param entity the entity to track
129+
* @param associatedEntity the entity to resize
130+
* @param factor the factor by which the associatedEntities have to be resized
131+
*/
132+
public void addResize(Entity<?> entity, Entity<?> associatedEntity, double factor) {
133+
addResize(entity, associatedEntity, factor, BOTH);
134+
}
135+
136+
/**
137+
* Make <code>entity</code> <code>factor</code> times bigger when it is hovered or clicked depending on <code>mode</code>
138+
*
139+
* @param entity the entity to resize
140+
* @param mode when the entity has to be resized (HOVER_ONLY, CLICK_ONLY or BOTH)
141+
*/
142+
public void addResize(Entity<?> entity, double factor, String mode) {
143+
addResize(entity, entity, factor, mode);
144+
}
145+
146+
/**
147+
* Make <code>entity</code> <code>factor</code> times bigger when it is hovered or clicked
148+
*
149+
* @param entity the entity to resize
150+
*/
151+
public void addResize(Entity<?> entity, double factor) {
152+
addResize(entity, entity, factor, BOTH);
153+
}
154+
155+
156+
}

0 commit comments

Comments
 (0)