This repository was archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.js
More file actions
199 lines (181 loc) · 5.46 KB
/
engine.js
File metadata and controls
199 lines (181 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const events = require("events");
const THREE = require("three");
const { blockFactory } = require("./lib/block");
const { boardFactory } = require("./lib/board");
const { configFactory } = require("./lib/config");
const { utils } = require("./lib/utils");
const { CONSTANTS } = require("./lib/constants");
/**
* Engine configuration
*/
function engineFactory() {
function gameStateFactory(timeStep = 1000, frameRate = 60) {
// time is in ms
return {
gameStepTime: timeStep,
frameTime: 0,
cumulatedFrameTime: 0,
_lastFrameTime: Date.now(), // timestamp assume it's 1 starting
gameOver: false,
staticBlocks: [],
frameRate: timeStep / frameRate
};
}
const gameState = gameStateFactory();
const config = configFactory();
const scene = new THREE.Scene();
const staticBlocks = gameState.staticBlocks;
const gestureQueue = [];
function addStaticBlock({ pos: { x, y, z } }) {
const { blockSize, boundingBoxConfig: { splitX, splitY, splitZ } } = config;
if (staticBlocks[x] === undefined) staticBlocks[x] = [];
if (staticBlocks[x][y] === undefined) staticBlocks[x][y] = [];
const mesh = THREE.SceneUtils.createMultiMaterialObject(
new THREE.CubeGeometry(blockSize, blockSize, blockSize),
[
new THREE.MeshBasicMaterial({
color: 0x000000,
flatShading: THREE.FlatShading,
wireframe: true,
transparent: true
})
]
);
mesh.position.x = (x - splitX / 2) * blockSize + blockSize / 2;
mesh.position.y = (y - splitY / 2) * blockSize + blockSize / 2;
mesh.position.z = (z - splitZ / 2) * blockSize + blockSize / 2;
scene.add(mesh);
staticBlocks[x][y][z] = mesh;
}
const board = boardFactory({
config,
utils,
scene,
staticBlocks,
addStaticBlock
});
const block = blockFactory({
config,
utils,
board,
gameState,
scene,
addStaticBlock
});
const { boundingBoxConfig: { splitX, splitY, splitZ } } = config;
board.init(splitX, splitY, splitZ);
block.generate();
function run(util) {
const time = Date.now();
gameState.frameTime = time - gameState._lastFrameTime; // 1 in the frame time (2-1)
gameState._lastFrameTime = time; // Last frame time is now 2
gameState.cumulatedFrameTime += gameState.frameTime; // 1 is the cummilated frameTime
while (gameState.cumulatedFrameTime > gameState.gameStepTime) {
processQueue({block, util});
clearQueue(gestureQueue);
gameState.cumulatedFrameTime -= gameState.gameStepTime;
block.move({
pos: { x: 0, y: 0, z: -1 },
callback: data => {
util.emit("newEngineState", data);
}
}); // Default move by 1 step down
}
if (gameState.gameOver) {
console.log("Game is over");
}
if (!gameState.gameOver) requestAnimationFrame(util);
}
function requestAnimationFrame(util) {
setTimeout(() => {
run(util);
}, gameState.frameRate);
}
/**
* Firebase helpers
*/
function processGestureObject(snapshot) {
// Processes the deep tree and turns it into a flat object
const result = [];
const objectToInsert = {};
objectToInsert["key"] = Object.keys(snapshot.val())[0];
// << in or of?
for (const gesture in snapshot.val()[objectToInsert.key].gestures) {
objectToInsert["gesture"] = gesture;
objectToInsert["action"] = snapshot.val()[objectToInsert.key].gestures[
objectToInsert.gesture
];
// Push copy of object into array
result.push(JSON.parse(JSON.stringify(objectToInsert)));
}
return result;
}
function registerGestureListner(session, gameSessionGesturePath) {
const gestureRef = firebase_admin.database().ref(gameSessionGesturePath);
gestureRef.on("value", snapshot => {
if (snapshot.val() != null) {
onGestureChange(session, snapshot);
}
});
session.gestureRef = gestureRef;
}
function onGestureChange(session, snapshot) {
const result = processGestureObject(snapshot);
result.forEach(element => {
session.gesture_queue.push(element);
});
console.log(JSON.stringify(session.gesture_queue));
}
function processQueue(obj) {
const { block, util } = obj;
gestureQueue.forEach(value => {
switch (value) {
case CONSTANTS().GESTURES.MOVE_LEFT:
block.move({
pos: { x: -1, y: 0, z: 0 },
callback: data => {
util.emit("newEngineState", data);
}
});
break;
case CONSTANTS().GESTURES.MOVE_RIGHT:
block.move({
pos: { x: 1, y: 0, z: 0 },
callback: data => {
util.emit("newEngineState", data);
}
});
break;
case CONSTANTS().GESTURES.MOVE_UP:
block.move({
pos: { x: 0, y: 1, z: 0 },
callback: data => {
util.emit("newEngineState", data);
}
});
break;
case CONSTANTS().GESTURES.MOVE_DOWN:
block.move({
pos: { x: 0, y: -1, z: 0 },
callback: data => {
util.emit("newEngineState", data);
}
});
break;
default:
break;
}
});
}
function clearQueue() {
while (gestureQueue.shift() !== undefined) {}
}
return {
run,
registerGestureListner,
gestureQueue,
onGestureChange,
util: new events.EventEmitter()
};
}
module.exports.engine = engineFactory;