-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.js
More file actions
134 lines (113 loc) · 4.3 KB
/
Copy pathPlayer.js
File metadata and controls
134 lines (113 loc) · 4.3 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
132
133
134
class Player extends Sprite {
constructor({collisionBlocks = [], imageSrc, frameRate, animations, loop}) {
super({imageSrc,frameRate,animations,loop});
this.position = {
x: 200,
y: 200
};
this.velocity = {
x: 0,
y: 0
};
this.width = 25;
this.height = 25;
this.gravity = 1;
this.collisionBlocks = collisionBlocks
}
update() {
this.position.x += this.velocity.x;
this.updateHitbox();
this.checkHorizontalCollisions();
this.applyGravity();
this.updateHitbox();
// c.fillRect(this.hitbox.position.x, this.hitbox.position.y, this.hitbox.width, this.hitbox.height)
this.checkVerticalCollisions();
}
switchSprite(name) {
if(this.image === this.animations[name].image) {
return
}
this.currentFrame = 0;
this.image = this.animations[name].image
this.frameRate = this.animations[name].frameRate
this.frameBuffer = this.animations[name].frameBuffer
this.loop = this.animations[name].loop
this.currentAnimation = this.animations[name]
}
updateHitbox() {
this.hitbox = {
position: {
x: this.position.x + 55,
y: this.position.y + 34
},
width: 50,
height: 53
}
}
applyGravity() {
this.velocity.y += this.gravity;
this.position.y += this.velocity.y;
}
checkVerticalCollisions() {
for (let i = 0; i < this.collisionBlocks.length; i++) {
const collisionBlock = collisionBlocks[i]
if (this.hitbox.position.x <= collisionBlock.position.x + collisionBlock.width &&
this.hitbox.position.x + this.hitbox.width >= collisionBlock.position.x &&
this.hitbox.position.y + this.hitbox.height >= collisionBlock.position.y &&
this.hitbox.position.y <= collisionBlock.position.y + collisionBlock.height) {
if (this.velocity.y < 0) {
this.velocity.y = 0;
const offset = this.hitbox.position.y - this.position.y;
this.position.y = collisionBlock.position.y + collisionBlock.height - offset + 0.01
break
}
if (this.velocity.y > 0) {
this.velocity.y = 0;
const offset = this.hitbox.position.y - this.position.y + this.hitbox.height;
this.position.y = collisionBlock.position.y - offset - 0.01
break
}
}
}
}
checkHorizontalCollisions() {
for (let i = 0; i < this.collisionBlocks.length; i++) {
const collisionBlock = collisionBlocks[i]
if (this.hitbox.position.x <= collisionBlock.position.x + collisionBlock.width &&
this.hitbox.position.x + this.hitbox.width >= collisionBlock.position.x &&
this.hitbox.position.y + this.hitbox.height >= collisionBlock.position.y &&
this.hitbox.position.y <= collisionBlock.position.y + collisionBlock.height) {
if (this.velocity.x < -0) {
const offset = this.hitbox.position.x - this.position.x;
this.position.x = collisionBlock.position.x + collisionBlock.width - offset + 0.01
break
}
if (this.velocity.x > 0) {
const offset = this.hitbox.position.x - this.position.x + this.hitbox.width;
this.position.x = collisionBlock.position.x - offset - 0.01
break
}
}
}
}
handleInput(keys) {
if (this.preventInput) {
return
}
if (keys.d.pressed) {
this.switchSprite('runRight')
this.velocity.x = 4
this.lastDirection = 'right'
} else if (keys.a.pressed) {
this.switchSprite('runLeft')
this.velocity.x = -4
this.lastDirection = 'left'
} else {
if(this.lastDirection === 'left') {
this.switchSprite('idleLeft')
} else {
this.switchSprite('idleRight')
}
}
}
}