Skip to content

Commit b04d7b8

Browse files
author
Valentin Vetter
committed
feat(viewport): add viewport module
1 parent ba985cf commit b04d7b8

File tree

4 files changed

+3483
-0
lines changed

4 files changed

+3483
-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.google.inject.Inject;
11+
import com.google.inject.Singleton;
12+
13+
/**
14+
* The ViewportModule allows you to create a zoomable/draggable container.
15+
*
16+
* @see https://davidfig.github.io/pixi-viewport/jsdoc/
17+
*
18+
*/
19+
@Singleton
20+
public class ViewportModule implements Module {
21+
22+
GameManager<AbstractPlayer> gameManager;
23+
@Inject GraphicEntityModule entityModule;
24+
Set<Integer> registered = new HashSet<>();
25+
Set<Integer> newEntityIds = new HashSet<>();
26+
27+
@Inject
28+
ViewportModule(GameManager<AbstractPlayer> gameManager) {
29+
this.gameManager = gameManager;
30+
gameManager.registerModule(this);
31+
}
32+
33+
@Override
34+
public void onGameInit() {
35+
sendFrameData();
36+
}
37+
38+
@Override
39+
public void onAfterGameTurn() {
40+
sendFrameData();
41+
}
42+
43+
@Override
44+
public void onAfterOnEnd() {
45+
}
46+
47+
private void sendFrameData() {
48+
Object[] data = { newEntityIds };
49+
50+
gameManager.setViewData("viewport", data);
51+
52+
newEntityIds.clear();
53+
}
54+
55+
/**
56+
* Makes the Entity with the given ID a Viewport
57+
*
58+
* @param entityId
59+
*/
60+
public void createViewport(int entityId) {
61+
if (!registered.contains(entityId)) {
62+
newEntityIds.add(entityId);
63+
registered.add(entityId);
64+
}
65+
}
66+
67+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
entity.container.removeChildren()
71+
viewport.addChild(entity.graphics)
72+
const parent = entity.container.parent
73+
if (parent) {
74+
parent.removeChild(entity.container)
75+
}
76+
entity.container = viewport
77+
if (parent) {
78+
parent.addChild(entity.container)
79+
}
80+
}
81+
82+
/**
83+
* Called when global data is received. Should only be called on init.
84+
* @param players information about players, such as avatar, name, color..
85+
* @param globalData data that has been sent from the Java module.
86+
*/
87+
handleGlobalData (players, globalData) {
88+
this.globalData = globalData
89+
}
90+
91+
/**
92+
* Called when the scene needs an update.
93+
* @param previousData data from the previous frame.
94+
* @param currentData data of the current frame.
95+
* @param progress progress of the frame. 0 <= progress <= 1
96+
* @param speed the speed of the viewer, setted up by the user.
97+
*/
98+
updateScene (previousData, currentData, progress, speed) {
99+
}
100+
101+
/**
102+
* Called every delta milliseconds.
103+
* @param delta time between current and last call. Aproximately 16ms by default.
104+
*/
105+
animateScene (delta) {
106+
}
107+
108+
}

0 commit comments

Comments
 (0)