-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathShip.js
executable file
·118 lines (93 loc) · 2.3 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
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
(function (window) {
function Ship() {
this.Container_constructor();
this.shipFlame = new createjs.Shape();
this.shipBody = new createjs.Shape();
this.addChild(this.shipFlame);
this.addChild(this.shipBody);
this.makeShape();
this.timeout = 0;
this.thrust = 0;
this.vX = 0;
this.vY = 0;
}
var p = createjs.extend(Ship, createjs.Container);
// public properties:
Ship.TOGGLE = 60;
Ship.MAX_THRUST = 2;
Ship.MAX_VELOCITY = 5;
// public properties:
p.shipFlame;
p.shipBody;
p.timeout;
p.thrust;
p.vX;
p.vY;
p.bounds;
p.hit;
// public methods:
p.makeShape = function () {
//draw ship body
var g = this.shipBody.graphics;
g.clear();
g.beginStroke("#FFFFFF");
g.moveTo(0, 10); //nose
g.lineTo(5, -6); //rfin
g.lineTo(0, -2); //notch
g.lineTo(-5, -6); //lfin
g.closePath(); // nose
//draw ship flame
var o = this.shipFlame;
o.scale = 0.5;
o.y = -5;
g = o.graphics;
g.clear();
g.beginStroke("#FFFFFF");
g.moveTo(2, 0); //ship
g.lineTo(4, -3); //rpoint
g.lineTo(2, -2); //rnotch
g.lineTo(0, -5); //tip
g.lineTo(-2, -2); //lnotch
g.lineTo(-4, -3); //lpoint
g.lineTo(-2, -0); //ship
//furthest visual element
this.bounds = 10;
this.hit = this.bounds;
}
p.tick = function (event) {
//move by velocity
this.x += this.vX;
this.y += this.vY;
//with thrust flicker a flame every Ship.TOGGLE frames, attenuate thrust
if (this.thrust > 0) {
this.timeout++;
this.shipFlame.alpha = 1;
if (this.timeout > Ship.TOGGLE) {
this.timeout = 0;
if (this.shipFlame.scaleX == 1) {
this.shipFlame.scale = 0.5;
} else {
this.shipFlame.scale = 1;
}
}
this.thrust -= 0.5;
} else {
this.shipFlame.alpha = 0;
this.thrust = 0;
}
}
p.accelerate = function () {
//increase push ammount for acceleration
this.thrust += this.thrust + 0.6;
if (this.thrust >= Ship.MAX_THRUST) {
this.thrust = Ship.MAX_THRUST;
}
//accelerate
this.vX += Math.sin(this.rotation * (Math.PI / -180)) * this.thrust;
this.vY += Math.cos(this.rotation * (Math.PI / -180)) * this.thrust;
//cap max speeds
this.vX = Math.min(Ship.MAX_VELOCITY, Math.max(-Ship.MAX_VELOCITY, this.vX));
this.vY = Math.min(Ship.MAX_VELOCITY, Math.max(-Ship.MAX_VELOCITY, this.vY));
}
window.Ship = createjs.promote(Ship, "Container");
}(window));