/
player.js
67 lines (53 loc) · 1.5 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
"use strict";
class Player {
constructor (height, floorHeight) {
this.floorHeight = floorHeight;
this.height = height;
this.y = 0; // 0 relative to floorHeight
this.x = 40;
this.yspeed = 0;
this.yacc = -0.3;
this.color = color(255,0,0);
this.side = 20;
this.jumpState = 0;
}
show () {
noStroke();
fill(this.color);
var yRect = this.height - this.floorHeight - this.side - this.y;
var xRect = this.x;
rect(xRect, yRect, this.side, this.side);
}
update () {
this.yspeed = this.yspeed + this.yacc;
this.y = max (0, this.y + this.yspeed);
if (this.y == 0) {
//this.yspeed = 0; // normal force or something
this.yspeed *= -.4;
}
if (this.y < 10 && this.yspeed < 0) {
this.jumpState = 0;
}
}
isJumping () {
return (this.y > 15);
}
jump () {
if (this.jumpState == 1) {
this.yspeed += 2;
this.jumpState = 2;
}
else if (this.jumpState == 0) {
this.yspeed = 6;
this.jumpState = 1;
}
}
collided (obstacle) {
if (this.y <= obstacle.getY() + obstacle.getH() &&
((this.x >= obstacle.getX() && this.x <= obstacle.getX() + obstacle.getW()) ||
(obstacle.getX() >= this.x && obstacle.getX() <= this.x + this.side))) {
return true;
}
return false;
}
}