-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
148 lines (138 loc) · 3.86 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
import { CollisionAnimation } from "./collisionAnimation.js";
import { FloatingMessage } from "./floatingMessage.js";
import {
Sitting,
Running,
Jumping,
Falling,
Rolling,
Diving,
Dizzy,
} from "./states.js";
export class Player {
constructor(game) {
this.game = game;
this.width = 100;
this.height = 91.3;
this.image = player;
this.x = 0;
this.y = this.game.height - this.height - this.game.gameMargin;
this.frameX = 0;
this.frameY = 0;
this.maxFrames = 0;
this.speed = 0;
this.maxSpeed = 10;
this.vy = 0;
this.weight = 1;
this.states = [
new Sitting(this.game),
new Running(this.game),
new Jumping(this.game),
new Falling(this.game),
new Rolling(this.game),
new Diving(this.game),
new Dizzy(this.game),
];
this.fps = 20;
this.frameInterval = 1000 / this.fps;
this.frameTimer = 0;
}
update(input, deltaTime) {
// check for collisions
this.checkCollisions();
this.currentState.handleInput(input);
// movements for x
if (input.includes("ArrowRight") && this.currentState !== this.states[6])
this.speed = this.maxSpeed;
else if (
input.includes("ArrowLeft") &&
this.currentState !== this.states[6]
)
this.speed = -this.maxSpeed;
else this.speed = 0;
this.x += this.speed;
// boundaries for x movement
if (this.x < 0) this.x = 0;
if (this.x > this.game.width - this.width)
this.x = this.game.width - this.width;
// movement for y
this.y += this.vy;
if (!this.onGround()) this.vy += this.weight;
else this.vy = 0;
// boundary for y
if (this.y > this.game.height - this.game.gameMargin - this.height)
this.y = this.game.height - this.game.gameMargin - this.height;
// sprite animations
if (this.frameTimer > this.frameInterval) {
this.frameTimer = 0;
if (this.frameX >= this.maxFrames) this.frameX = 0;
else this.frameX += 1;
} else this.frameTimer += deltaTime;
}
draw(context) {
if (this.game.debug)
context.strokeRect(this.x, this.y, this.width, this.height);
context.drawImage(
this.image,
this.frameX * this.width,
this.frameY * this.height,
this.width,
this.height,
this.x,
this.y,
this.width,
this.height
);
}
onGround() {
return this.y >= this.game.height - this.height - this.game.gameMargin;
}
setState(stateIndex, speed) {
this.currentState = this.states[stateIndex];
this.frameX = 0;
this.game.speed = this.game.maxSpeed * speed;
this.currentState.enter();
}
checkCollisions() {
this.game.enemies.forEach((enemy) => {
if (
this.x < enemy.x + enemy.width &&
this.x + this.width > enemy.x &&
this.y < enemy.y + enemy.height &&
this.y + this.height > enemy.y
) {
// collision
// remove enemy player collided with
enemy.markedForRemoval = true;
this.game.collisions.unshift(
new CollisionAnimation(
this.game,
enemy.x + enemy.width * 0.5,
enemy.y + enemy.height * 0.5
)
);
// if player was in scoring state
// increase score
if (
this.currentState == this.states[4] ||
this.currentState == this.states[5]
) {
this.game.score += 1;
this.game.floatingMessages.unshift(
new FloatingMessage("+1 🦴", enemy.x, enemy.y, 0, 0)
);
}
// player collided but not in a scoring state
// decrese player life
else {
this.setState(6, 0);
this.game.lives -= 1;
if (this.game.lives <= 0) this.game.gameOver = true;
this.game.floatingMessages.unshift(
new FloatingMessage("-1 ❤", enemy.x, enemy.y, 0, 0)
);
}
}
});
}
}