-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmarine.js
183 lines (160 loc) · 6.71 KB
/
submarine.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
pc.script.attribute('diveThrust', 'number', 200, {
displayName: 'Dive Thrust'
});
pc.script.attribute('linearThrust', 'number', 500, {
displayName: 'Linear Thrust'
});
pc.script.attribute('turnThrust', 'number', 100, {
displayName: 'Turn Thrust'
});
pc.script.attribute('grappleDistance', 'number', 3, {
displayName: 'Grapple Distance'
});
pc.script.create('submarine', function (context) {
// Creates a new Submarine instance
var Submarine = function (entity) {
this.entity = entity;
this.fuel = 100;
this.refuelling = false;
this.diveForce = new pc.Vec3();
this.leftForce = new pc.Vec3();
this.rightForce = new pc.Vec3();
this.relPos = new pc.Vec3();
this.initialPos = this.entity.getPosition().clone();
this.initialRot = this.entity.getRotation().clone();
};
Submarine.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.leftEngine = this.entity.findByName('Left Engine');
this.rightEngine = this.entity.findByName('Right Engine');
this.grapple = this.entity.findByName('Grapple');
this.pipes = context.root.findByName('Pipes');
this.game = context.root.findByName('Pipe Wars');
this.grappledPipe = null;
var fuelUI = context.root.findByName('UI').findByName('Fuel');
setInterval(function () {
if (this.fuel > 0 && !this.refuelling) {
this.fuel -= 1;
fuelUI.script.font_renderer.text = 'Fuel: ' + this.fuel + '%';
}
}.bind(this), 1000);
setInterval(function () {
if (this.fuel < 100 && this.refuelling) {
this.fuel += 1;
fuelUI.script.font_renderer.text = 'Fuel: ' + this.fuel + '%';
}
}.bind(this), 50);
},
reset: function () {
this.fuel = 100;
this.refuelling = false;
this.grappleDisable();
this.entity.setPosition(this.initialPos);
this.entity.setRotation(this.initialRot);
this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.syncEntityToBody();
},
grappleDisable: function () {
if (this.grappledPipe) {
if (this.grappledPipe.ballsocketjoint) {
context.systems.ballsocketjoint.removeComponent(this.grappledPipe);
}
this.grappledPipe = null;
}
},
getClosestPipe: function () {
var closestPipe = null;
var closestDist = this.grappleDistance;
var grappleToPipe = new pc.Vec3();
var pipes = this.pipes.getChildren();
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
var dist = grappleToPipe.sub2(pipe.getPosition(), this.grapple.getPosition()).length();
if (dist < this.grappleDistance && dist < closestDist) {
closestPipe = pipe;
}
}
return closestPipe;
},
grappleEnable: function () {
if (this.grappledPipe === null) {
var pipe = this.getClosestPipe();
if (pipe) {
context.systems.ballsocketjoint.addComponent(pipe, {
pivot: new pc.Vec3(0.5, 0, 0)
});
this.grappledPipe = pipe;
}
}
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
if (this.game.script.game.state !== 'game') return;
var leftEngine = 0;
var rightEngine = 0;
var dive = 0;
if (context.keyboard.isPressed(pc.input.KEY_A)) {
dive += this.diveThrust;
}
if (context.keyboard.isPressed(pc.input.KEY_Z)) {
dive -= this.diveThrust;
}
if (context.keyboard.isPressed(pc.input.KEY_UP)) {
leftEngine += this.linearThrust;
rightEngine += this.linearThrust;
}
if (context.keyboard.isPressed(pc.input.KEY_DOWN)) {
leftEngine -= this.linearThrust;
rightEngine -= this.linearThrust;
}
if (context.keyboard.isPressed(pc.input.KEY_LEFT)) {
rightEngine += this.turnThrust;
}
if (context.keyboard.isPressed(pc.input.KEY_RIGHT)) {
leftEngine += this.turnThrust;
}
if (dive !== 0) {
this.diveForce.copy(this.entity.up).scale(dive);
this.entity.rigidbody.applyForce(this.diveForce);
}
if (leftEngine < 0) {
this.leftEngine.script.propeller.speed = -20;
} else if (leftEngine === 0) {
this.leftEngine.script.propeller.speed = 0;
} else if (leftEngine > 0) {
this.leftEngine.script.propeller.speed = 20;
}
if (rightEngine < 0) {
this.rightEngine.script.propeller.speed = -20;
} else if (rightEngine === 0) {
this.rightEngine.script.propeller.speed = 0;
} else if (rightEngine > 0) {
this.rightEngine.script.propeller.speed = 20;
}
if (leftEngine !== 0) {
this.leftForce.copy(this.entity.forward).scale(leftEngine);
this.relPos.sub2(this.leftEngine.getPosition(), this.entity.getPosition());
this.entity.rigidbody.applyForce(this.leftForce, this.relPos);
}
if (rightEngine !== 0) {
this.rightForce.copy(this.entity.forward).scale(rightEngine);
this.relPos.sub2(this.rightEngine.getPosition(), this.entity.getPosition());
this.entity.rigidbody.applyForce(this.rightForce, this.relPos);
}
if (context.keyboard.wasPressed(pc.input.KEY_SPACE)) {
if (this.grappledPipe) {
this.grappleDisable();
} else {
this.grappleEnable();
}
}
if (this.grappledPipe && this.grappledPipe.ballsocketjoint) {
this.grappledPipe.rigidbody.activate();
this.grappledPipe.ballsocketjoint.position = this.grapple.getPosition();
}
}
};
return Submarine;
});