-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship.js
87 lines (71 loc) · 2.14 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
77
78
79
80
81
82
83
84
85
86
87
function Ship(color){
this.pos = createVector(width/2, height/2);
this.r = 20;
this.heading = PI/2;
this.rotation = 0;
this.vel = createVector(0,0);
this.isBoosting = false;
this.color = color;
this.lifebar = 100;
this.boosting = function(b){
this.isBoosting = b;
}
this.applyForce = function(force) {
this.vel.add(force);
}
//tried adding gravity
// this.checkLanding = function(){
// if ((this.pos-this.r) > (height - this.r)) {
// this.vel.y *= -0.9;
//
// }
//}
this.update = function() {
if (this.isBoosting){
this.boost();
}
this.pos.add(this.vel);
this.vel.mult(0.98);
}
this.boost = function(){
var force = p5.Vector.fromAngle(this.heading);
force.mult(0.3);
this.vel.add(force);
}
this.render = function() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.heading + PI / 2);
fill(this.color);
stroke(255);
triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
line(this.r-20,this.r-1,this.r-20,this.r-30);
if(this.isBoosting){
line(this.r-(this.r-3),this.r+5,this.r-(this.r),this.r);
line(this.r-(this.r+3),this.r+5,this.r-(this.r),this.r);
}
stroke(255);
strokeWeight(1);
fill('red');
rect(10, 25, 10+this.lifebar/10, 3);
pop();
}
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.setRotation = function(a){
this.rotation = a;
}
this.turn = function(){
this.heading += this.rotation;
}
}