-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
77 lines (71 loc) · 1.62 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
class Player {
// length array (with previous positions)
size
pos
constructor() {
this.size = createVector(20, 20)
this.pos = createVector(canvas.x / 2, canvas.y / 2)
}
update = (key) => {
switch (key) {
case "ArrowUp":
case "w":
this.pos.y -= this.size.y
break
case "ArrowLeft":
case "a":
this.pos.x -= this.size.x
break
case "ArrowDown":
case "s":
this.pos.y += this.size.y
break
case "ArrowRight":
case "d":
this.pos.x += this.size.x
break
}
}
/**
* checks if player is still in range
*/
outOfBounds = () => {
switch (this.pos.x) {
case this.pos.x < 0 || this.pos.x + this.size.x > canvas.x:
return false
case this.pos.y < 0 || this.pos.y + this.size.y > canvas.y:
return false
default:
return true
}
}
outOfBoundsSetPosition = () => {
console.log(this.pos.x)
switch (this.pos) {
// x is to small
case this.pos.x < 0:
this.pos.x = canvas.x - this.size.x
console.log("setting x")
break
// x is to big
case this.pos.x > canvas.x - this.size.x:
console.log("setting x")
this.pos.x = 0
break
case this.pos.y < 0:
console.log("setting y")
this.pos.y = canvas.y - this.size.y
break
// x is to big
case this.pos.y > canvas.y - this.size.y:
console.log("setting y")
this.pos.y = 0
break
default:
break
}
}
render = () => {
rect(this.pos.x, this.pos.y, this.size.x, this.size.y)
}
}