-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship.js
76 lines (63 loc) · 1.44 KB
/
ship.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
function Ship() {
this.pos = createVector(width / 2, height / 2);
this.r = 20;
this.angle = 0;
this.rotation = 0;
this.vel = createVector(0, 0);
this.isBoosting = false;
this.invincible = false;
this.boosting = function(b) {
this.isBoosting = b;
}
this.update = function() {
this.pos.add(this.vel);
this.vel.mult(.99);
if (this.isBoosting) {
ship.boost();
}
}
this.boost = function() {
let force = p5.Vector.fromAngle(this.angle);
force.mult(0.5);
this.vel.add(force);
}
this.setRotation = function(angle) {
this.rotation = angle;
}
this.edges = function() {
if (this.pos.x > width + this.r) {
this.pos.x = -this.r;
} else if (this.pos.x < -this.r) {
this.pos.x = width + this.r;
}
if (this.pos.y > height + this.r) {
this.pos.y = -this.r;
} else if (this.pos.y < -this.r) {
this.pos.y = height + this.r;
}
}
this.turn = function() {
this.angle += this.rotation;
}
this.render = function() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.angle + PI / 2);
fill(0);
stroke(255);
triangle(-this.r, this.r, this.r, this.r, 0, -this.r - 2);
pop();
}
this.hits = function(asteroid) {
let d = dist(this.pos.x, this.pos.y, asteroid.pos.x, asteroid.pos.y);
if (d < this.r + asteroid.r) {
return true;
} else {
return false;
}
}
this.hyperSpace = function() {
this.pos.x += 300 * cos(this.angle);
this.pos.y += 300 * sin(this.angle);
}
}