-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
173 lines (157 loc) · 4.21 KB
/
player.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
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
function Player(xMax, yMax, platforms) {
this.xMax = xMax;
this.yMax = yMax;
this.width = 10;
this.height = 20;
this.lineWidth = 1;
this.color = "#003030";
this.ySpeed = 8;
this.yJumpSpeed = -12;
this.xSpeed = 0;
this.xSpeedMax = 7;
this.platforms = platforms;
this.flight = true;
this.platform = null;
this.running = false;
this.initPos = function() {
this.x = 35;
this.y = 35;
this.old_x = 35;
this.old_y = 35;
};
this.initPos();
this.handleCollision = function(p) {
console.log("collision: old: " + this.old_y + " new: " + this.y +
" height " + this.height + " with old: " + p.old_y +
" new: " + p.y);
if(this.old_y + this.height <= p.old_y) { // was above
console.log('land');
this.y = p.y - this.height;
return true;
} else if(this.old_y > p.old_y + p.height) { // was below
console.log("hit head");
this.y = p.y + p.height;
} else { // from a side?
this.xSpeed = -this.xSpeed;
if(this.old_x + this.width < p.old_x) {
console.log("from left");
this.x = p.x - this.width - this.xSpeedMax;
} else if(this.old_x > p.old_x + p.width) {
console.log("from right");
this.x = p.x + p.width + this.xSpeedMax;
} else {
console.log("unhandled");
}
}
return false;
};
this.toggleFlight = function() {
this.flight = !this.flight;
};
this.updateX = function() {
this.x += this.xSpeed;
if(this.platform) {
this.x += this.platform.xSpeed;
}
var oldXSpeed = this.xSpeed;
if(Math.abs(this.xSpeed) > this.xSpeedMax) {
this.xSpeed = Math.abs(this.xSpeed) / this.xSpeed * this.xSpeedMax;
}
if(this.jumping) {
if(this.xSpeed > 0) {
this.xSpeed--;
} else if(this.xSpeed < 0) {
this.xSpeed++;
}
}
if( (oldXSpeed < 0 && this.xSpeed > 0) ||
(oldXSpeed > 0 && this.xSpeed < 0) ||
Math.abs(this.xSpeed) < 0.1) {
this.xSpeed = 0;
}
if(this.x < 0) {
this.x = 0;
this.xSpeed = -this.xSpeed;
} else if(this.x + this.width > this.xMax) {
this.x = this.xMax - this.width;
this.xSpeed = -this.xSpeed;
}
};
this.update = function() {
this.updateX();
this.y += this.ySpeed;
if(this.ySpeed < 6) {
this.ySpeed++;
}
this.platform = null;
for(var i=0; i<this.platforms.length; i++) {
var p = this.platforms[i];
if(this.collidesWith(p)) {
if(p.danger) {
this.initPos();
return;
}
if(this.handleCollision(p)) {
this.platform = this.platforms[i];
break;
}
} else if(this.y + this.height === p.y &&
((this.x > p.x && this.x < p.x + p.width) ||
(this.x + this.width < p.x + p.width &&
this.x + this.width > p.x))) {
if(!p.danger) {
this.platform = p;
}
break;
}
}
this.old_x = this.x;
this.old_y = this.y;
};
this.collidesWith = function(object) {
var tx2 = this.x + this.width;
var ox2 = object.x + object.width;
var ty2 = this.y + this.height;
var oy2 = object.y + object.height;
var xc = ((this.x > object.x && this.x < ox2) ||
(tx2 > object.x && tx2 < ox2));
var yc = ((this.y > object.y && this.y < oy2) ||
(ty2 > object.y && ty2 < oy2));
return xc && yc;
};
this.draw = function(camera) {
camera.rect(this.color, this.x, this.y, this.width, this.height);
};
this.jump = function() {
if(this.isFalling() || this.isJumping()) {
if(this.flight) {
this.ySpeed = this.yJumpSpeed / 3;
}
} else {
this.ySpeed = this.yJumpSpeed;
}
};
this.run = function(factor) {
this.running = true;
if(this.isJumping() || this.isFalling()) {
this.xSpeed += factor/2;
} else {
this.xSpeed = factor;
}
};
this.runLeft = function() {
this.run(-this.xSpeedMax);
};
this.runRight = function() {
this.run(this.xSpeedMax);
};
this.stopRunning = function() {
this.running = false;
};
this.isJumping = function() {
return this.ySpeed < 0;
};
this.isFalling = function() {
return this.ySpeed > 0;
};
}