Skip to content

Commit 2ea4aab

Browse files
author
Raphael Brand
committed
add onMove event listener
1 parent 0fec9f9 commit 2ea4aab

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

demo/app/js/acceleration.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ define('acceleration', () => {
3030
printMap();
3131
return true;
3232
};
33+
this.getPosition = function() {
34+
console.log(playerAt);
35+
return playerAt ? playerAt : playerPosition
36+
}
37+
this.onMove;
3338
var onKeyDown = function onKeyDown(e) {
3439
var code = codes[e.keyCode] ? codes[e.keyCode] : e.target.className;
3540

@@ -87,6 +92,7 @@ define('acceleration', () => {
8792
playerAt = playerPosition;
8893
//console.clear();
8994
console.log("Player has moved " + direction);
95+
onMove()
9096
}
9197
return hasMoved;
9298
};
@@ -145,6 +151,12 @@ define('acceleration', () => {
145151
},
146152
setLevel: (matrix) => {
147153
level = matrix;
154+
},
155+
getPosition: this.getPosition,
156+
onMove: callback => {
157+
if(typeof callback == 'function')
158+
onMove = callback;
159+
else log('no function')
148160
}
149161
}
150162
});

demo/app/js/main.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const log = console.log;
22

33
log('Hello Bootstrap');
4-
require(['test-module', 'acceleration'], function(test, acc) {
4+
require(['test-module', 'acceleration'], function (test, acc) {
55
test.test();
66
acc.setCanvas(document.querySelector('#canvas'));
77
acc.setLevel([
@@ -14,8 +14,11 @@ require(['test-module', 'acceleration'], function(test, acc) {
1414
[1, 0, 1, 1, 1, 1, 0, 1],
1515
[1, 0, 0, 0, 0, 0, 0, 1]
1616
]);
17-
acc.draw() && acc.printMap()
17+
acc.draw() && acc.printMap();
1818
});
19-
require(['three-scene'], (Scene) => {
20-
Scene.animate({rotation:{speed:{x:0.01, y:0.01}}})
19+
20+
require(['three-scene', 'acceleration'], (Scene, acc) => {
21+
Scene.animate({ rotation: { speed: { x: 0.01, y: 0 } } });
22+
Scene.animate({ position: { x: 100, y: 0 } })
23+
acc.onMove(acc.getPosition)
2124
})

demo/app/js/three-scene.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ define('three-scene', () => {
77

88
function init() {
99
scene = new THREE.Scene();
10-
camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
10+
camera = new THREE.PerspectiveCamera(100, 1, 0.1, 1000);
1111
container = document.createElement('div');
1212
document.getElementById('perspective').appendChild(container);
1313

@@ -18,7 +18,9 @@ define('three-scene', () => {
1818
renderer = new THREE.CanvasRenderer();
1919
console.log(renderer);
2020
renderer.setSize(w, h);
21-
camera.position.z = 200;
21+
camera.position.x = 50;
22+
camera.position.y = 50;
23+
camera.position.z = 300;
2224

2325
container.appendChild(renderer.domElement);
2426

@@ -28,11 +30,13 @@ define('three-scene', () => {
2830

2931
let currentFrame = 0;
3032
let animate;
31-
33+
let rotationSpeed = (rotation={speed:{x:0,y:0}}) => {
34+
cube.rotation.x += rotation.speed.x;
35+
cube.rotation.y += rotation.speed.y;
36+
}
3237
animate = (params) => {
3338
requestAnimationFrame(() => animate(params));
34-
cube.rotation.x += params.rotation.speed.x;
35-
cube.rotation.y += params.rotation.speed.y;
39+
if(params.rotation) rotationSpeed(params.rotation)
3640
//console.info(cube.rotation.y, scene, camera, cube);
3741
scene.add(cube);
3842
renderer.render(scene, camera);

0 commit comments

Comments
 (0)