-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
81 lines (75 loc) · 1.97 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
import {canvas} from "./index.js"
import {c} from "./index.js"
import {Area} from "./Area.js"
import {Collidable} from "./Collidable.js"
export class Player extends Collidable{
constructor() {
super()
this.velocity = {
x: 0,
y: 0
}
this.rotation = 0
this.width = 50
this.height = 50
this.position = {
x: canvas.width / 2 - this.width / 2,
y: canvas.height - this.height - 20
}
const scale = .15
const image = new Image()
image.src = './assets/spaceship.png'
image.onload = () => {
this.image = image
this.width = this.image.width * scale
this.height = this.image.height * scale
this.position = {
x: canvas.width / 2 - this.width / 2,
y: canvas.height - this.height - 20
}
}
this.collided = false
this.type = 0
}
draw() {
c.save()
c.translate(
this.position.x + this.width / 2,
this.position.y + this.height / 2
)
c.rotate(this.rotation)
c.translate(
-this.position.x - this.width / 2,
-this.position.y - this.height / 2
)
c.drawImage(
this.image,
this.position.x,
this.position.y,
this.width,
this.height
)
c.restore()
}
update() {
if (this.image) {
this.position.x += this.velocity.x
this.position.y += this.velocity.y
this.draw()
}
}
get nozzlePosition() {
return {
x: this.position.x + this.width / 2,
y: this.position.y
}
}
get area() {
const top = this.position
const bottom = {
x: this.position.x + this.width,
y: this.position.y + this.height
}
return new Area({top, bottom})
}
}