Skip to content

Commit 77bec42

Browse files
Merge branch 'viewport-module' into 'master'
feat(sdk): add viewport module See merge request codingame/game-engine!239
2 parents ba985cf + 11f35c3 commit 77bec42

File tree

7 files changed

+3523
-0
lines changed

7 files changed

+3523
-0
lines changed

engine/modules/viewport/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-viewport</artifactId>
14+
<name>CodinGame Game Engine Viewport Module</name>
15+
<description>A module to display a zoomable viewport for the CodinGame engine toolkit.</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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.codingame.gameengine.module.viewport;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
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.GraphicEntityModule;
10+
import com.codingame.gameengine.module.entities.Group;
11+
import com.google.inject.Inject;
12+
import com.google.inject.Singleton;
13+
14+
/**
15+
* The ViewportModule allows you to create a zoomable/draggable container.
16+
*
17+
* @see https://davidfig.github.io/pixi-viewport/jsdoc/
18+
*
19+
*/
20+
@Singleton
21+
public class ViewportModule implements Module {
22+
23+
GameManager<AbstractPlayer> gameManager;
24+
@Inject GraphicEntityModule entityModule;
25+
Set<Integer> registered = new HashSet<>();
26+
Set<Integer> newEntityIds = new HashSet<>();
27+
28+
@Inject
29+
ViewportModule(GameManager<AbstractPlayer> gameManager) {
30+
this.gameManager = gameManager;
31+
gameManager.registerModule(this);
32+
}
33+
34+
@Override
35+
public void onGameInit() {
36+
sendFrameData();
37+
}
38+
39+
@Override
40+
public void onAfterGameTurn() {
41+
sendFrameData();
42+
}
43+
44+
@Override
45+
public void onAfterOnEnd() {
46+
}
47+
48+
private void sendFrameData() {
49+
gameManager.setViewData("viewport", newEntityIds);
50+
51+
newEntityIds.clear();
52+
}
53+
54+
/**
55+
* Wraps the given Group in a Viewport.
56+
*
57+
* @param group A group to wrap into a Viewport
58+
*/
59+
public void createViewport(Group group) {
60+
int entityId = group.getId();
61+
if (!registered.contains(entityId)) {
62+
newEntityIds.add(entityId);
63+
registered.add(entityId);
64+
}
65+
}
66+
67+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { HEIGHT, WIDTH } from '../core/constants.js'
2+
import { getRenderer } from '../core/rendering.js'
3+
import { api as entityModule } from '../entity-module/GraphicEntityModule.js'
4+
import vp from './lib/viewport.es.js'
5+
6+
export class ViewportModule {
7+
8+
static get name() {
9+
return 'viewport'
10+
}
11+
12+
entityIds = []
13+
14+
/**
15+
* Called when data is received.
16+
* Handles data for the given frame. Returns data that will be sent as parameter to updateScene.
17+
* @param frameInfo information about the current frame.
18+
* @param frameData data that has been sent from the Java module.
19+
*/
20+
handleFrameData (frameInfo, frameData) {
21+
// Handle your data here
22+
const newEntityIds = frameData
23+
24+
this.entityIds = [...this.entityIds, ...newEntityIds]
25+
26+
// Return what is necessary to your module
27+
return { frameInfo, frameData }
28+
}
29+
30+
/**
31+
* Called when the viewer needs to be rerendered (init phase, resized viewer).
32+
* @param container a PIXI Container. Add your elements to this object.
33+
* @param canvasData canvas data containing width and height.
34+
*/
35+
reinitScene (container, canvasData) {
36+
if (this.vp == null) {
37+
this.vp = vp()
38+
}
39+
40+
this.entityIds.forEach(entityId => {
41+
this._initViewPort(entityId);
42+
})
43+
}
44+
45+
_initViewPort(entityId) {
46+
const entity = entityModule.entities.get(entityId)
47+
48+
const viewport = new this.vp.Viewport({
49+
screenWidth: WIDTH,
50+
screenHeight: HEIGHT,
51+
worldWidth: WIDTH,
52+
worldHeight: HEIGHT,
53+
54+
interaction: getRenderer().plugins.interaction, // the interaction module is important for wheel to work properly when renderer.view is placed or scaled,
55+
divWheel: getRenderer().view,
56+
passiveWheel: false,
57+
stopPropagation: true
58+
})
59+
60+
viewport
61+
.drag()
62+
.wheel()
63+
64+
viewport.fitWorld()
65+
viewport.interactiveChildren = false
66+
Object.defineProperty(viewport, 'cursor', {
67+
get: () => viewport.input.isMouseDown ? 'grabbing' : 'grab'
68+
})
69+
70+
// Switch the GraphicEntityModule's Container with our Viewport
71+
entity.container.removeChildren()
72+
viewport.addChild(entity.graphics)
73+
const parent = entity.container.parent
74+
if (parent) {
75+
parent.removeChild(entity.container)
76+
}
77+
entity.container = viewport
78+
if (parent) {
79+
parent.addChild(entity.container)
80+
}
81+
}
82+
83+
/**
84+
* Called when the scene needs an update.
85+
* @param previousData data from the previous frame.
86+
* @param currentData data of the current frame.
87+
* @param progress progress of the frame. 0 <= progress <= 1
88+
* @param speed the speed of the viewer, setted up by the user.
89+
*/
90+
updateScene (previousData, currentData, progress, speed) {
91+
}
92+
93+
}

0 commit comments

Comments
 (0)