-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPlayer.js
125 lines (103 loc) · 3.22 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
export default class Player {
WALK_ANIMATION_TIMER = 200;
walkAnimationTimer = this.WALK_ANIMATION_TIMER;
dinoRunImages = [];
jumpPressed = false;
jumpInProgress = false;
falling = false;
JUMP_SPEED = 0.6;
GRAVITY = 0.4;
constructor(ctx, width, height, minJumpHeight, maxJumpHeight, scaleRatio) {
this.ctx = ctx;
this.canvas = ctx.canvas;
this.width = width;
this.height = height;
this.minJumpHeight = minJumpHeight;
this.maxJumpHeight = maxJumpHeight;
this.scaleRatio = scaleRatio;
this.x = 10 * scaleRatio;
this.y = this.canvas.height - this.height - 1.5 * scaleRatio;
this.yStandingPosition = this.y;
this.standingStillImage = new Image();
this.standingStillImage.src = "images/standing_still.png";
this.image = this.standingStillImage;
const dinoRunImage1 = new Image();
dinoRunImage1.src = "images/dino_run1.png";
const dinoRunImage2 = new Image();
dinoRunImage2.src = "images/dino_run2.png";
this.dinoRunImages.push(dinoRunImage1);
this.dinoRunImages.push(dinoRunImage2);
//keyboard
window.removeEventListener("keydown", this.keydown);
window.removeEventListener("keyup", this.keyup);
window.addEventListener("keydown", this.keydown);
window.addEventListener("keyup", this.keyup);
//touch
window.removeEventListener("touchstart", this.touchstart);
window.removeEventListener("touchend", this.touchend);
window.addEventListener("touchstart", this.touchstart);
window.addEventListener("touchend", this.touchend);
}
touchstart = () => {
this.jumpPressed = true;
};
touchend = () => {
this.jumpPressed = false;
};
keydown = (event) => {
if (event.code === "Space") {
this.jumpPressed = true;
}
};
keyup = (event) => {
if (event.code === "Space") {
this.jumpPressed = false;
}
};
update(gameSpeed, frameTimeDelta) {
this.run(gameSpeed, frameTimeDelta);
if (this.jumpInProgress) {
this.image = this.standingStillImage;
}
this.jump(frameTimeDelta);
}
jump(frameTimeDelta) {
if (this.jumpPressed) {
this.jumpInProgress = true;
}
if (this.jumpInProgress && !this.falling) {
if (
this.y > this.canvas.height - this.minJumpHeight ||
(this.y > this.canvas.height - this.maxJumpHeight && this.jumpPressed)
) {
this.y -= this.JUMP_SPEED * frameTimeDelta * this.scaleRatio;
} else {
this.falling = true;
}
} else {
if (this.y < this.yStandingPosition) {
this.y += this.GRAVITY * frameTimeDelta * this.scaleRatio;
if (this.y + this.height > this.canvas.height) {
this.y = this.yStandingPosition;
}
} else {
this.falling = false;
this.jumpInProgress = false;
}
}
}
run(gameSpeed, frameTimeDelta) {
if (this.walkAnimationTimer <= 0) {
if (this.image === this.dinoRunImages[0]) {
this.image = this.dinoRunImages[1];
} else {
this.image = this.dinoRunImages[0];
}
this.walkAnimationTimer = this.WALK_ANIMATION_TIMER;
}
this.walkAnimationTimer -= frameTimeDelta * gameSpeed;
}
draw() {
this.ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
}