Skip to content

Commit 9a539d1

Browse files
author
Julien Poulton
committed
Merge branch 'toggle-module' into 'master'
[FEAT][SDK] Toggle module See merge request codingame/game-engine!172
2 parents 1c4ffb5 + a335ca6 commit 9a539d1

File tree

10 files changed

+277
-6
lines changed

10 files changed

+277
-6
lines changed

engine/modules/entities/src/main/resources/view/entity-module/Entity.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export class Entity {
3030
}
3131
}
3232

33+
setHidden (hide) {
34+
this.hide = hide
35+
this.container.visible = this.currentState.visible && !this.hide
36+
}
37+
3338
addState (t, params, frame) {
3439
if (!this.states[frame]) {
3540
this.states[frame] = []
@@ -143,7 +148,7 @@ export class Entity {
143148
this.container.position.set(state.x * globalData.coeff, state.y * globalData.coeff)
144149
this.container.scale.set(state.scaleX || eps, state.scaleY || eps)
145150
this.container.rotation = state.rotation
146-
this.container._visible = state.visible
151+
this.container._visible = state.visible && !this.hide
147152
}
148153

149154
static createState (time = 1, values = {}, curve = {}) {

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 you to display or hide elements of the GraphicEntityModule using the viewer's options menu.</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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
* This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu.
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+
private 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+
72+
private void sendFrameData() {
73+
Object[] data = { newRegistration };
74+
gameManager.setViewData("toggles", data);
75+
76+
newRegistration.clear();
77+
}
78+
/**
79+
* Will display the entity only when the toggle state matches the state you set
80+
*
81+
* @param entity which will be displayed
82+
* @param toggle the name of the toggle you want to use
83+
* @param state the state of the toggle where the entity will be displayed at
84+
*/
85+
public void displayOnToggleState(Entity<?> entity, String toggle, boolean state) {
86+
int id = entity.getId();
87+
Toggle associatedToggle = new Toggle(toggle, state);
88+
if (!associatedToggle.equals(registered.get(id))) {
89+
newRegistration.put(id, associatedToggle);
90+
registered.put(id, associatedToggle);
91+
}
92+
}
93+
}
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 == null && !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+
}

playground/extensions/extensions-1-tools.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Although the CodinGame SDK already includes the [GraphicEntityModule](graphics-1-introduction.md), you can add modules to your game. Modules are extensions allowing you to directly interact with the viewer using [PixiJS](http://www.pixijs.com/), which gives you a lot more control on its behaviour.
44

5-
This section presents a few existing modules. You can find their source code on Github: [https://github.com/CodinGame/codingame-sdk-modules](https://github.com/CodinGame/codingame-sdk-modules)
6-
75
If you want to create your own module, see [How to get started](extensions-2-tutorial.md).
86

97
# Usage

playground/extensions/extensions-5-animmodule.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# AnimModule
2+
This module is not a maven module, you can find it's source code on Github: [https://github.com/CodinGame/codingame-sdk-modules/tree/master/AnimModule](https://github.com/CodinGame/codingame-sdk-modules/tree/master/AnimModule)
23

3-
Useful for games with a lot of AnimatedSprites overlayed over the rest of the game.
4+
This module is useful for games with a lot of AnimatedSprites overlayed over the rest of the game.
45

56
Instead of having the Referee output the entire sequence of images for each new animation, you may have it reference a pre-existing animation as defined in `AnimData.js` and configured in `AnimModule.js`.
67

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# ToggleModule
2+
3+
This module allows you to display or hide elements of the GraphicEntityModule using the viewer's options menu.
4+
This can help to create debug modes.
5+
6+
## Setup
7+
⚠ This module requires the [GraphicEntityModule](https://github.com/CodinGame/codingame-game-engine/tree/master/engine/modules/entities) to work.
8+
9+
Add the dependency in the `pom.xml` of your project.
10+
```xml
11+
<dependency>
12+
<groupId>com.codingame.gameengine</groupId>
13+
<artifactId>module-toggle</artifactId>
14+
<version>3.2.0</version>
15+
</dependency>
16+
```
17+
18+
And setup the module and the toggles in your `config.js`.
19+
20+
```javascript
21+
import { GraphicEntityModule } from './entity-module/GraphicEntityModule.js'
22+
import { ToggleModule } from './toggle-module/ToggleModule.js'
23+
24+
// List of viewer modules that you want to use in your game
25+
export const modules = [
26+
GraphicEntityModule,
27+
ToggleModule
28+
]
29+
30+
// The list of toggles displayed in the options of the viewer
31+
export const options = [
32+
{
33+
// The name displayed over the toggle
34+
title: 'MY TOGGLE',
35+
// Getters and setters for the on/off state of your toggle
36+
get: function () {
37+
return toggles.myToggle // replace "myToggle" by the name of the toggle you want to use
38+
},
39+
set: function (value) {
40+
toggles.myToggle = value // replace "myToggle" by the name of the toggle you want to use
41+
ToggleModule.refreshContent()
42+
},
43+
// The labels for the on/off states of your toggle
44+
values: {
45+
'TOGGLED ON': true,
46+
'TOGGLED OFF': false
47+
}
48+
},
49+
{
50+
title: 'MY OTHER TOGGLE',
51+
52+
}
53+
]
54+
55+
// The list of the toggles used by the ToggleModule
56+
// replace "myToggle" by the name of the toggle you want to use
57+
export const toggles = {
58+
myToggle: true, // default value of your toggle
59+
myOtherToggle: false
60+
}
61+
62+
```
63+
64+
## Usage
65+
66+
`Referee.java`
67+
```java
68+
@Inject ToggleModule toggleModule;
69+
70+
@Override
71+
public void init() {
72+
// Only display `myEntity` when the state of `myToggle` is `true`
73+
toggleModule.displayOnToggleState(myEntity, "myToggle", true);
74+
// My entity will only be displayed when `myToggle` is true
75+
// (on the `TOGGLED ON` position according to our previous setup)
76+
}
77+
```
78+

playground/misc/misc-3-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
77
### 🎁 New feature
88

99
- The [EndScreenModule](playground/extensions/extensions-4-endscreen.md) is now bundled with the sdk.
10+
- The [ToggleModule](playground/extensions/extensions-toggle.md) has been added.
1011

1112
### 🐞 Bug fix
1213

techio.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ plan:
4141
plan:
4242
- title: Introduction
4343
statement: playground/extensions/extensions-1-tools.md
44-
- title: Create your own modules
45-
statement: playground/extensions/extensions-2-tutorial.md
4644
- title: Tooltip Module
4745
statement: playground/extensions/extensions-3-tooltip.md
4846
- title: End Screen Module
4947
statement: playground/extensions/extensions-4-endscreen.md
48+
- title: Toggle Module
49+
statement: playground/extensions/extensions-toggle.md
5050
- title: Anim Module
5151
statement: playground/extensions/extensions-5-animmodule.md
52+
- title: Create your own modules
53+
statement: playground/extensions/extensions-2-tutorial.md
5254
- title: One more thing
5355
plan:
5456
- title: Guidelines

0 commit comments

Comments
 (0)