-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
130 lines (109 loc) · 3.03 KB
/
player.js
File metadata and controls
130 lines (109 loc) · 3.03 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
class Player {
constructor(x, y, color) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.width = 30;
this.height = 20;
this.maxClimb = 3; // Maximum height the player can climb
this.gravity = 0.5;
this.speed = 2;
this.onGround = false;
this.color = color
this.cannon_rotation = 0;
this.rotationVelocity = 0;
}
update() {
this.vel.y += this.gravity;
this.cannon_rotation += this.rotationVelocity;
fall:
for (let pixel = 0; pixel < this.vel.y; pixel++) {
for (let checkPos = 0; checkPos <= this.width; checkPos += resolution) {
if (isGround(this.pos.x + checkPos, this.pos.y + this.height + 1)) {
this.vel.y = 0;
break fall;
}
}
this.pos.y += 1;
}
shouldgo:
switch (this.vel.x) {
case (this.speed): {
let climb = stepsToAir(this.pos.x + this.width + this.vel.x, this.pos.y + this.height)
for (let checkPos = 0; checkPos <= this.height; checkPos++) {
let potential = stepsToAir(this.pos.x + this.width + this.vel.x, this.pos.y + this.height - checkPos)
if (potential > climb) {
console.log("wall");
break shouldgo;
}
}
if (climb <= this.maxClimb) {
this.pos.x += this.vel.x;
this.pos.y -= climb;
}
break;
}
case (-this.speed): { // going left
let climb = stepsToAir(this.pos.x + this.vel.x, this.pos.y + this.height)
for (let checkPos = 0; checkPos <= this.height; checkPos++) {
let potential = stepsToAir(this.pos.x + this.vel.x, this.pos.y + this.height - checkPos)
if (potential > climb) {
console.log("wall", potential, climb);
break shouldgo;
}
}
if (climb <= this.maxClimb) {
this.pos.x += this.vel.x;
this.pos.y -= climb;
}
break;
}
}
}
outOfBounds() {
return (
this.pos.x + this.width < 0 || // Left boundary
this.pos.x > width || // Right boundary
this.pos.y + this.height < 0 || // Top boundary
this.pos.y > height // Bottom boundary
);
}
rotateLeft() {
this.rotationVelocity = -2;
}
rotateRight() {
this.rotationVelocity = 2;
}
stopRotating() {
this.rotationVelocity = 0;
}
moveLeft() {
this.vel.x = -this.speed;
}
moveRight() {
this.vel.x = this.speed;
}
stop() {
this.vel.x = 0;
}
display() {
fill(this.color);
stroke(1);
rect(this.pos.x, this.pos.y, this.width, this.height);
push()
translate(this.pos.x + this.width / 2, this.pos.y + 3);
rotate(radians(this.cannon_rotation));
rectMode(CENTER);
rect(this.width / 2, 0, this.width, 10)
pop()
}
shoot(destructive = true) {
let tip = createVector(this.width, 0);
// Rotate the vector by the cannon's rotation
tip.rotate(radians(this.cannon_rotation));
// Add the translation offset to get the global position
let globalTip = createVector(this.pos.x + this.width / 2, this.pos.y + 3).add(tip);
let direction = createVector(100, 0).rotate(radians(this.cannon_rotation));
let directionTip = globalTip.copy().add(direction);
return new Projectile(globalTip.x, globalTip.y, directionTip.x, directionTip.y, destructive)
}
}