-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
85 lines (73 loc) · 2.1 KB
/
main.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
(function(){
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
var LEFT_KEY = 37;
var UP_KEY = 38;
var RIGHT_KEY = 39;
function keyDownTextField (e) {
var keyCode = e.keyCode;
// console.log(keyCode);
if(keyCode == UP_KEY) {
p1.jump();
}
if(keyCode == LEFT_KEY) {
if(p1.canMoveLeft(scene)){
p1.moveLeft();
}
if(p1.canPushLeft(scene)){
p1.pushLeft(scene);
}
}
if(keyCode == RIGHT_KEY) {
if(p1.canMoveRight(scene)) {
p1.moveRight();
}
if(p1.canPushRight(scene)) {
p1.pushRight(scene);
}
}
}
document.addEventListener("keydown", keyDownTextField, false);
var canvas = new Canvas(1920, 1080);
var scene = new Scene();
var p1 = new Player(7, 1);
canvas.add(scene);
// for(var i=0; i<7; i++) {
// scene.add(new Box(i, 3));
// }
scene.add(new Box(2,0));
scene.add(new Box(4,1));
scene.add(new Box(4,0));
scene.add(new Box(1,4));
scene.add(new Box(6,0));
scene.add(p1);
window.log = [];
var log = document.getElementById("log");
function printLog() {
log.innerHTML = objToTable(window.log);
}
function objToTable(obj) {
var res = "";
for(var i in obj) {
if(typeof obj[i] == "object") {
res += "<tr><td colspan=2 align='center' ><b>" + i + "</b></td></tr>";
res += objToTable(obj[i]);
} else {
res += "<tr><td>" + i + "</td><td>" + obj[i] + "</td></tr>";
}
}
return res;
}
(function animloop() {
requestAnimFrame(animloop);
canvas.clear();
canvas.render();
printLog();
})();
})();