-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
89 lines (67 loc) · 1.58 KB
/
game.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
// Copied from the talk
// TODO: support code so it can be tested
var GamePlayer = new Class({
Extends: GameObject,
initialize: function () {
this.x = 0;
this.y = -1;
this.yVelocity = 0;
this.jumping = false;
this.running = false;
this.facingLeft = false;
},
tick: function (dt, commands) {
var oldX = this.x;
if (commands.left) {
this.x -= 8 * dt;
}
if (commands.right) {
this.x += 8 * dt;
}
if (commands.up && !this.jumping) {
this.yVelocity = -25;
}
this.yVelocity += 100 * dt;
this.y += this.yVelocity * dt;
var hitInfo = this.hitTest();
this.jumping = (hitInfo.bumpedY >= this.y);
if (hitInfo.bumpedY != this.Y) {
this.yVelocity = 0;
}
this.x = hitInfo.bumpedX;
this.y = hitInfo.bumpedY;
this.running = (this.x != oldX) && !this.jumping;
this.facingLeft = (this.x < oldX) || (this.x == oldX && ...?)
if (hitInfo.hitObject) {
this.yVelocity = -24;
hitInfo.hitObject.stomp();
}
if (this.y > 45 || this.y < -40) {
this.initialize();
}
}
});
//=====================================================
//
// GameEnemy
var GameEnemy = new Class({
Extends: GameObject,
initialize: function () {
this.x = 5;
this.y = 14;
this.direction = -1;
this.stompTime = 0;
},
tick: function (dt, commands) {
var isStomped = (this.stompTime > 0);
if (isStomped) { this.stompTime -= dt; }
if (!isStomped) {
this.x += this.direction * dt * 0.9;
}
if (this.x < 4.0) { this.direction = 1; }
if (this.x > 5.0) { this.direction = -1; }
},
stomp: function () {
this.stompTime = 2.0;
}
});