Repository navigation
Expand file tree
/
Copy pathmoduletraits.js
More file actions
75 lines (63 loc) · 1.89 KB
/
Copy pathmoduletraits.js
File metadata and controls
75 lines (63 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* global input, game */
var moduleTraits =
{
// Modules that can be activated through player input. Activation can be either periodic or
// continuous.
// Input: [activationPeriod, manualActivationKey]
// Output: lastActivationTime
ActiveModule:
{
manualActivationKey: null,
init: function()
{
this.lastActivationTime = -1e99;
},
step: function(t, dt)
{
if ((!this.activationPeriod || t > this.lastActivationTime + this.activationPeriod) && this.isActive(t)) {
var cost = this.activationCost * (this.activationPeriod ? 1 : dt);
if (cost <= this.ship.energy) {
this.ship.energy -= cost;
this.activate(t, dt);
this.lastActivationTime = t;
}
}
},
isActive: function(t)
{
return !this.manualActivationKey || input.keyDown(this.manualActivationKey);
},
isReady: function(t)
{
return !this.activationPeriod || t > this.lastActivationTime + this.activationPeriod;
},
},
// Activation method for ordinary projectile weapons.
// Input: projectileSpeed, projectileExpire, [spreadAngle, spread]
ProjectileWeapon:
{
spreadAngle: 0,
spread: 0,
proxyAttributeCategory: "projectile",
activate: function(t)
{
var p = this.ship.relativePos(this.relativePos);
var targetDir = this.ship.getModuleTargetPos(this).sub(p);
if (targetDir.len() < 0.001)
targetDir = new V(0, 1);
targetDir.rot_(-this.spreadAngle / 2);
var projectileCount = this.projectileCount || 1;
var sideDir = targetDir.rot90left().setLen_(1);
var sideDistance = -this.spread / 2;
for (var i = 0; i < Math.round(projectileCount); ++i) {
var v = targetDir.setLen(this.projectileSpeed);
game.addEntity(this.projectileClass({
p: p.addMul(sideDir, sideDistance), v: v,
expire: t + this.projectileExpire,
faction: this.ship.faction, bonuses: this.totalBonuses}));
targetDir.rot_(this.spreadAngle / (projectileCount - 1));
sideDistance += this.spread / (projectileCount - 1);
}
}
}
};