This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
105 lines (90 loc) · 2.54 KB
/
engine.js
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
define(function(require) {
// TODO Adding board and brick to the game should be done from
// the GameSample module (or maybe a class that GameSample uses)
// But should definitely not be spesified in the engine
var Board = require('modules/board');
var actor_brick = require('modules/brick_actor');
var physics = require('modules/physics');
var Engine = function Engine(layers) {
var phys = new physics();
var redraw = function() {
brick.clearRect(0, 0, WIDTH, HEIGHT);
if (falling) board.draw(layers);
brick.draw();
};
this.run = function() {
// TODO: find a better way to find and handle layers and cxts
brick = new actor_brick(layers[2]);
// This is UGLY TODO Fix a better way to access underlying
// methods. Maybe create a style-object that we can pass
// along instead.
brick.topStyle = "#ff00c6";
brick.leftStyle = brick.topStyle;
brick.rightStyle = brick.leftStyle;
phys.register(brick, true, [physics.forces.gravity]);
board.draw(layers);
redraw();
window.setInterval(function() { next(); redraw(); },
100/12);
window.addEventListener('keydown', doKeyDown, true);
};
var falling = false;
var next = function() {
brick.update();
// TODO: This method should be a part of some collision
// detection mechanism
brick.onGround(board, phys);
phys.tick();
// TODO reintroduce falling in another way. The falling semantics
// changed when I introduced physics and forces
// setFalling();
}
WIDTH = 800;
HEIGHT = 600;
var speed = 5;
function doKeyDown(evt) {
if (falling) return;
switch (evt.keyCode) {
case 38:
brick.pos.y += speed;
brick.pos.x -= speed;
break;
case 40:
brick.pos.y -= speed;
brick.pos.x += speed;
break;
case 37:
brick.pos.x -= speed;
brick.pos.y -= speed;
break;
case 39:
brick.pos.x += speed;
brick.pos.y += speed;
break;
case 32:
// a more natural way of introducing jumping.
phys.get(brick).tempForces.push(function (object) {
object.speed.z += 1.5;
});
}
};
board = new Board();
// init the bottom floor for testing
for (var i = 0; i < board.DIMENSIONS.x; i++) {
for (var j = 0; j < board.DIMENSIONS.y; j++) {
if (i >= 10 && i <= 15
&& j >= 10 && j <= 15)
continue;
board.board[i][j][0] = 1;
};
};
this.setFalling = function() {
// falling = true;
brick.clearRect(0,0, WIDTH, HEIGHT);
brick.topLayer = layers[0];
brick.leftLayer = layers[0];
brick.rightLayer = layers[0];
};
};
return Engine;
});