Repository navigation
Expand file tree
/
Copy pathPlayer.js
More file actions
131 lines (110 loc) · 3.5 KB
/
Copy pathPlayer.js
File metadata and controls
131 lines (110 loc) · 3.5 KB
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
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;
MUSIC_JUMP = new Audio("./sounds/jump.mp3");
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 dinoRunImages1 = new Image();
dinoRunImages1.src = "./images/dino_run1.png";
const dinoRunImages2 = new Image();
dinoRunImages2.src = "./images/dino_run2.png";
this.dinoRunImages.push(dinoRunImages1);
this.dinoRunImages.push(dinoRunImages2);
// 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);
}
// Mobile touch event
touchstart = () => {
this.jumpPressed = true;
};
touchend = () => {
this.jumpPressed = false;
};
// Keyboard type event
keydown = (e) => {
if (e.code === "Space") {
this.jumpPressed = true;
}
};
keyup = (e) => {
if (e.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.MUSIC_JUMP.currentTime = 0;
this.MUSIC_JUMP.play();
this.jumpInProgress = true;
}
// jump is pregressing and not falling
if (this.jumpInProgress && !this.falling) {
if (
// check at least jumping to min Jump Height
this.y > this.canvas.height - this.minJumpHeight ||
// continuing to jump till hit the max height
(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) {
this.image =
this.image === this.dinoRunImages[0]
? this.dinoRunImages[1]
: 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);
}
}