forked from Kaelinator/AGAD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.js
More file actions
58 lines (44 loc) · 1.11 KB
/
Copy pathShip.js
File metadata and controls
58 lines (44 loc) · 1.11 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
function Ship(fillColor, strokeColor) {
this.angle = 0; // theta
this.angleVelocity = 0; // theta velocity
this.fillColor = fillColor; // body color
this.strokeColor = strokeColor; // perimeter color
}
/**
* changes angle by angleVelocity
*/
Ship.prototype.update = function() {
this.angle += this.angleVelocity;
this.angleVelocity *= 0.7;
};
/**
* shoots a lazer
* pushes it to bullets array
*/
Ship.prototype.shoot = function(bullets) {
bullets.push(new Lazer(-this.angle + PI, 0, 5));
};
/**
* changes the angleVelocity based upon acceleration
*/
Ship.prototype.rotate = function(acceleration) {
this.angleVelocity += acceleration;
};
/**
* draws the ship
*/
Ship.prototype.draw = function() {
fill(this.fillColor);
strokeWeight(2);
stroke(this.strokeColor);
push(); // save translations & rotations
translate(width / 2, height / 2); // draw relative to the center
rotate(this.angle); // draw relative to the angle of the ship
/* draw triangle */
beginShape();
vertex(0, -30);
vertex(15, 15);
vertex(-15, 15);
endShape(CLOSE);
pop(); // revert translations & rotations
};