-
Notifications
You must be signed in to change notification settings - Fork 1
/
engines.js
295 lines (268 loc) · 7.23 KB
/
engines.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* engines.js - simulate ion pulse engines. */
/**
*
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* Copyright (C) 2014 Alan Manuel K. Gloria
*
*
* The JavaScript code in this page is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in this page.
*
*/
define(['field', 'vars', 'signal'], function (field, vars, signal) {
"use strict";
/*
engines.initialize()
- initialize the engines.
engines.damage()
- damage the engines (stutter).
engines.destroy()
- destroy the engines (almost 1 speed).
engines.fix()
- cancel engine damage or destruction.
engines.colorState()
- string 'red' for destroyed, 'yellow' for
damaged, 'green' for OK.
engines.setSpeed(n)
- 0 <= n <= 9
- Sets the target speed according to the
player's requested speed. If fixed,
engines will attempt to move to that
speed, if damaged, engines will stutter
to that speed, if destroyed, engines
will saturate to 1.
- cancels hyperwarp mode.
engines.getSetSpeed()
- returns the value given to setSpeed().
engines.atTargetSpeed()
- return true if running at the speed
targeted by setSpeed().
engines.hyperwarp(fun)
- fun is a function to be called when
speed reaches 100.
- cancels normal speed.
- ignores engine destruction.
engines.isHyperwarp()
- return true if currently in hyperwarp,
false otherwise.
engines.toString()
- Returns a two-digit string indicating
the Math.floor of the current speed,
or ∞ if 100 or more.
engines.update(seconds)
- Updates the field speed.
*/
var FIXED = 0;
var DAMAGED = 1;
var DESTROYED = 2;
var speeds = [
0.0,
0.390625,
0.78125,
1.5625,
3.125,
6.25,
12.5,
25.0,
37.5,
43.75
];
var rates = [
0.0,
1.0,
1.5,
2.0,
2.5,
3.0,
3.5,
7.5,
11.25,
15.0
];
var hyperwarprate = 15.5;
var acceleration = 15.5;
function nullFun() { }
function Engines() {
this.initialize();
signal('update', this.update.bind(this));
signal('mainMenu', this._onMainMenu.bind(this));
signal('newGame', this._onNewGame.bind(this));
signal('enterNormal', this._onEnterNormal.bind(this));
signal('enterHyperspace', this._onEnterHyperspace.bind(this));
signal('fix', this.fix.bind(this));
}
Engines.prototype.initialize = function () {
this._state = FIXED;
this._warping = false;
this._inhyperspace = false;
this._warpcb = nullFun;
this._warpspeed = 0.0;
this._speedsetting = 0;
this._curspeed = 0.0;
this._actualspeed = 0.0;
this._targetspeed = 0.0;
this._enerate = 0.0;
return this;
};
Engines.prototype.damage = function () {
this._state = DAMAGED;
return this;
};
Engines.prototype.destroy = function () {
this._state = DESTROYED;
return this;
};
Engines.prototype.fix = function () {
this._state = FIXED;
return this;
};
Engines.prototype.colorState = function () {
if (this._state === FIXED) {
return 'green';
} else if (this._state === DAMAGED) {
return 'yellow';
} else {
return 'red';
}
};
Engines.prototype.setSpeed = function (n) {
if (this._inhyperspace) {
// ignore player attempts to change speed.
return this;
}
this._targetspeed = speeds[n];
this._enerate = rates[n];
this._warping = false;
this._warpcb = nullFun;
this._speedsetting = n;
return this;
};
Engines.prototype.getSetSpeed = function () {
return this._speedsetting;
};
Engines.prototype.atTargetSpeed = function () {
return this._targetspeed === this._actualspeed;
};
Engines.prototype.hyperwarp = function (f) {
if (this._inhyperspace) {
// ignore player attempts to change speed.
return this;
}
this._enerate = hyperwarprate;
this._warping = true;
this._warpcb = f;
this._speedsetting = -1;
return this;
};
Engines.prototype.isHyperwarp = function () {
return this._warping;
};
Engines.prototype.toString = function () {
var rv = Math.floor(this._actualspeed);
if (rv < 10) {
return '0' + rv;
} else if (rv >= 100) {
return '∞';
} else {
return '' + rv;
}
};
Engines.prototype.update = function (seconds) {
var step = acceleration * seconds;
var warpcb = nullFun;
if (this._inhyperspace) {
// Don't update in hyperspace
return this;
}
/* Update normal engines. */
if (this._curspeed < this._targetspeed) {
this._curspeed += step;
if (this._curspeed > this._targetspeed) {
this._curspeed = this._targetspeed;
}
} else if (this._curspeed > this._targetspeed) {
this._curspeed -= step;
if (this._curspeed < this._targetspeed) {
this._curspeed = this._targetspeed;
}
}
/* Handle engine damage. */
if (this._state === FIXED) {
this._actualspeed = this._curspeed;
} else if (this._state === DAMAGED) {
this._actualspeed = this._curspeed * Math.random();
} else {
this._actualspeed = this._curspeed * 0.1;
}
/* Update warp engines. */
if (this._warping) {
if (this._warpspeed < 100.0) {
this._warpspeed += step;
if (this._warpspeed > 100.0) {
this._warpspeed = 100.0;
}
}
} else {
if (this._warpspeed > 0.0) {
this._warpspeed -= step;
if (this._warpspeed < 0.0) {
this._warpspeed = 0.0;
}
}
}
/* Get the higher of actual and warp speed*/
if (this._warpspeed > this._actualspeed) {
this._actualspeed = this._warpspeed;
}
field.speed = this._actualspeed;
vars.energy.consume(this._enerate * seconds);
/* On hyperwarp entry. */
if (this._warping && this._warpspeed >= 99.999) {
warpcb = this._warpcb;
this.setSpeed(0);
warpcb.call(warpcb);
}
return this;
};
Engines.prototype._onMainMenu = function () {
this._curspeed = 0.0;
this._targetspeed = speeds[6];
this._enerate = 0.0;
this._warping = false;
this._inhyperspace = false;
this._warpspeed = 0.0;
this._warpcb = nullFun;
this._state = FIXED;
this._speedsetting = 6;
return this;
};
Engines.prototype._onNewGame = function () {
this._enerate = rates[6];
return this;
};
Engines.prototype._onEnterNormal = function () {
this._inhyperspace = false;
return this;
};
Engines.prototype._onEnterHyperspace = function () {
this._inhyperspace = true;
return this;
};
return new Engines();
});