-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects-shipSink.js
executable file
·133 lines (105 loc) · 2.42 KB
/
objects-shipSink.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//classes for an astroparty clone
//bullets for causing damage
class Projectile {
constructor(x, y, dx, dy, parent) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
beDrawn() {
}
tick() {
}
}
//ship class - the main one for shared behaviors
class Ship {
constructor(x, y, color) {
this.color = color;
//physics things
this.x = x;
this.y = y;
this.rLong = 0.03;
this.rShort = 0.015;
this.dx = 0;
this.dy = 0;
this.dv = 0.0004;
this.dMax = 0.05;
this.av = 0;
this.friction = 0.99;
this.aFriction = 0.85;
this.a = 0;
this.da = 0;
this.aa = 0;
this.aSpeed = Math.PI / 256;
this.aMax = Math.PI / 32;
//misc
}
beDrawn() {
this.beDrawn_pos(this.x, this.y);
//if close to the edges make sure to mirror
if (!game_map.large) {
var mirrorX = (this.x < this.rLong || 1 - this.x < this.rLong);
var mirrorY = (this.y < this.rLong || 1 - this.y < this.rLong);
if (mirrorX) {
this.beDrawn_pos(this.x + boolToSigned(this.x < 0.5), this.y);
}
if (mirrorY) {
this.beDrawn_pos(this.x, this.y + boolToSigned(this.y < 0.5));
}
if (mirrorX && mirrorY) {
this.beDrawn_pos(this.x + boolToSigned(this.x < 0.5), this.y + boolToSigned(this.y < 0.5));
}
}
}
beDrawn_pos(x, y) {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.moveTo(canvas.width * x, canvas.height * y);
ctx.ellipse(canvas.width * x, canvas.height * y, canvas.width * this.rLong, canvas.width * this.rShort, this.a, Math.PI * -0.7, Math.PI * 0.7);
ctx.lineTo(canvas.width * x, canvas.height * y);
ctx.fill();
}
tick() {
//angular velocity
if (this.aa == 0) {
this.da *= this.aFriction;
}
this.da = clamp(this.da + this.aa * this.aSpeed, -this.aMax, this.aMax);
this.a += this.da;
//directional velocity
this.dx *= this.friction;
this.dy *= this.friction;
var additive = polToXY(0, 0, this.a, this.dv * this.av);
this.dx += additive[0];
this.dy += additive[1];
//speed cap
var dMag = Math.sqrt(this.dx ** 2 + this.dy ** 2);
if (dMag > this.dMax) {
this.dx /= dMag;
this.dy /= dMag;
this.dx *= this.dMax;
this.dy *= this.dMax;
}
//position
this.x += this.dx;
this.y += this.dy;
//large maps have no looping
if (game_map.large) {
return;
}
if (this.x < 0) {
this.x += 1;
}
if (this.x > 1) {
this.x -= 1;
}
if (this.y < 0) {
this.y += 1;
}
if (this.y > 1) {
this.y -= 1;
}
}
}
//here are special types of ship