Skip to content

Commit ed634cc

Browse files
NaejDoreeJean Poree
authored andcommitted
feat(sdk): adding toggle module
1 parent 1e06742 commit ed634cc

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

engine/modules/toggle/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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-toggle</artifactId>
14+
<name>CodinGame Game Engine Toggle Module</name>
15+
<description>This module allows to display or hide elements from the graphicEntityModule using the cg-player option toggles.</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.codingame.gameengine</groupId>
20+
<artifactId>core</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.codingame.gameengine</groupId>
25+
<artifactId>module-entities</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.codingame.gameengine.module.toggle;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.codingame.gameengine.core.AbstractPlayer;
7+
import com.codingame.gameengine.core.GameManager;
8+
import com.codingame.gameengine.core.Module;
9+
import com.codingame.gameengine.module.entities.Entity;
10+
import com.codingame.gameengine.module.entities.GraphicEntityModule;
11+
import com.google.inject.Inject;
12+
import com.google.inject.Singleton;
13+
14+
/**
15+
* @author Jean Porée
16+
*
17+
* The toggle module allows to display or hide elements from the graphicEntityModule using the cg-player option toggles.
18+
*
19+
*/
20+
@Singleton
21+
public class ToggleModule implements Module {
22+
23+
GameManager<AbstractPlayer> gameManager;
24+
@Inject GraphicEntityModule entityModule;
25+
Map<Integer, Toggle> registered, newRegistration;
26+
27+
class Toggle {
28+
public String name;
29+
public Boolean state = true;
30+
31+
public Toggle(String name, Boolean state) {
32+
this.name = name;
33+
this.state = state;
34+
}
35+
36+
public Boolean equals(Toggle other) {
37+
return other != null && this.state == other.state && stringEquals(this.name, other.name);
38+
}
39+
40+
boolean stringEquals(String a, String b) {
41+
if (a == b) {
42+
return true;
43+
} else if (a != null && a.equals(b)) {
44+
return true;
45+
} else {
46+
return false;
47+
}
48+
}
49+
}
50+
51+
@Inject
52+
ToggleModule(GameManager<AbstractPlayer> gameManager) {
53+
this.gameManager = gameManager;
54+
gameManager.registerModule(this);
55+
registered = new HashMap<>();
56+
newRegistration = new HashMap<>();
57+
}
58+
59+
@Override
60+
public void onGameInit() {
61+
sendFrameData();
62+
}
63+
64+
@Override
65+
public void onAfterGameTurn() {
66+
sendFrameData();
67+
}
68+
69+
@Override
70+
public void onAfterOnEnd() {
71+
sendFrameData();
72+
}
73+
74+
private void sendFrameData() {
75+
Object[] data = { newRegistration };
76+
gameManager.setViewData("toggles", data);
77+
78+
newRegistration.clear();
79+
}
80+
/**
81+
* Will display the entity only when the toggle state matches the state you set
82+
*
83+
* @param entity which will be displayed
84+
* @param toggle the name of the toggle you want to use
85+
* @param state the state of the toggle where the entity will be displayed at
86+
*/
87+
public void displayOnToggleState(Entity<?> entity, String toggle, Boolean state) {
88+
int id = entity.getId();
89+
Toggle associatedToggle = new Toggle(toggle, state);
90+
if (!associatedToggle.equals(registered.get(id))) {
91+
newRegistration.put(id, associatedToggle);
92+
registered.put(id, associatedToggle);
93+
}
94+
}
95+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { api as entityModule } from '../../entity-module/GraphicEntityModule.js'
2+
import { toggles } from '../../config.js'
3+
import {ErrorLog} from '../core/ErrorLog.js'
4+
import {MissingToggleError} from './errors/MissingToggleError.js'
5+
6+
export class ToggleModule {
7+
constructor (assets) {
8+
this.previousFrame = {}
9+
this.missingToggles = {}
10+
ToggleModule.refreshContent = () => {
11+
if (!this.currentFrame) {
12+
return
13+
}
14+
for (const registeredEntity in this.currentFrame.registered) {
15+
const entity = entityModule.entities.get(parseInt(registeredEntity))
16+
const toggleInfo = this.currentFrame.registered[registeredEntity]
17+
const toggleState = toggles[toggleInfo.name]
18+
19+
if (toggleState === undefined && !this.missingToggles[toggleInfo.name]) {
20+
ErrorLog.push(new MissingToggleError(toggleInfo.name))
21+
this.missingToggles[toggleInfo.name] = true
22+
}
23+
entity.setHidden(
24+
toggleState !== toggleInfo.state
25+
)
26+
}
27+
}
28+
}
29+
static refreshContent () {}
30+
31+
static get name () {
32+
return 'toggles'
33+
}
34+
35+
updateScene (previousData, currentData, progress) {
36+
this.currentFrame = currentData
37+
this.currentProgress = progress
38+
ToggleModule.refreshContent()
39+
}
40+
41+
handleFrameData (frameInfo, data) {
42+
if (!data) {
43+
return
44+
}
45+
const newRegistration = data[0]
46+
const registered = { ...this.previousFrame.registered, ...newRegistration }
47+
const frame = { registered, number: frameInfo.number }
48+
this.previousFrame = frame
49+
return frame
50+
}
51+
52+
reinitScene (container, canvasData) {
53+
ToggleModule.refreshContent()
54+
}
55+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class MissingToggleError extends Error {
2+
constructor (toggle, cause) {
3+
super('Could not find toggle: "' + toggle + '". Make sure it is set up correctly in your config.js.')
4+
this.toggle = toggle
5+
this.cause = cause
6+
this.name = 'MissingToggleError'
7+
}
8+
}

0 commit comments

Comments
 (0)