-
Notifications
You must be signed in to change notification settings - Fork 1
/
actors.js
336 lines (245 loc) · 10.8 KB
/
actors.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
class Enemy{
constructor(mesh,planet,target,enemyType,DEBUG=true,scene){
this.mesh=mesh
this.enemy=null
this.pivot=null
this.planet=planet
//move toward target
this.target=target
this.waitSpawningTime=new Date().getTime()+spawnDurationTime;
//movement
this.nextEnemyMoveTime=new Date().getTime();
this.nextUpdateTargetTime=new Date().getTime();
//distance to move before stopping
this.maxdistanceMoved=Math.PI/4
//how much to move per frame
this.velocity=Math.PI/200
//how much to wait before moving again, if 0 continue movement
this.moveInterval=5 //ms
//how much to wait before updating the target to another point near player
this.updateTargetInterval=500
this.accuracy=0
//distance traveled in this step
this.distanceStepMoved=0
this.direction=1
// how many times it was hit by a bullet
this.life = enemyLife;
this.healthBar = null;
this.dying=false
this.enemyType=enemyType
this.DEBUG=DEBUG
}
spawn(position=new BABYLON.Vector3(0,0,0)){
//time used for naming
const currentTime = new Date().getTime();
spawningAnimation(position)
this.enemy=this.mesh.createInstance("enemy")
this.enemy.position=position;
if(DEBUG) this.enemyPivot = BABYLON.Mesh.CreateCapsule(`enemyPivot`, { radiusTop: 0.05 }, scene);
else this.enemyPivot=new BABYLON.TransformNode(`${currentTime}enemyPivot`)
//rotate pivot towards player w.r.t enemy position
var newTarget=getPointNearPosition(this.target.getAbsolutePosition(),0)
rotateTowards(this.enemyPivot,this.enemy.getAbsolutePosition(),newTarget)
//orient enemy so that it's on surface along normal
orientSurface(this.enemy,position,this.planet)
this.enemy.setParent(this.enemyPivot)
this.enemyPivot.setParent(this.planet)
if(position.x>0) this.direction=-1
this.enemy.checkCollisions = true;
//different stats for different types of enemy
if(this.enemyType==enemyFastType) {
this.velocity*=2
this.moveInterval=300
this.life=enemyLife/2
}
else if(this.enemyType==enemyTankType) {
this.velocity*=0.3
this.moveInterval=5
this.life=enemyLife*3
}
this.healthBar = new HealthBar(this.enemy, this.scene,this.life);
}
moveStep(){
//move of some distance and wait MoveInterval
//if moveInterval is set to 0 or almost 0, the movement is continous
const currentTime = new Date().getTime();
//compute how to rotate pivot
var forward = new BABYLON.Vector3(0, 0, 1);
var direction = this.enemyPivot.getDirection(forward);
direction.normalize();
//1 or -1
var dir=this.direction
//rotation while moving
this.enemy.addRotation(0,0.05,0)
if(currentTime>this.waitSpawningTime)
{
if (this.moveInterval<50 || (this.distanceStepMoved<this.maxdistanceMoved && currentTime>this.nextEnemyMoveTime)) {
this.enemyPivot.rotate(direction,this.velocity*dir, BABYLON.Space.WORLD);
this.distanceStepMoved+=this.velocity
}
else if(this.distanceStepMoved>0){
//stop moving and wait
this.nextEnemyMoveTime = new Date().getTime() + this.moveInterval;
this.distanceStepMoved=0
}
}
}
updatePosition(){
this.accuracy=0.5*remainingEnemies
if(this.enemyType==enemyFastType) this.accuracy=(this.accuracy)*2.5;
if(this.enemyType==enemyTankType) this.accuracy=0.1;
//new target to choose every x seconds
const currentTime = new Date().getTime();
//update target position
if(currentTime>this.nextUpdateTargetTime) {
this.enemyPivot.setParent(null)
this.enemy.setParent(null)
this.enemyPivot.rotation = BABYLON.Vector3.Zero()
//new target
var newTarget=getPointNearPosition(this.target.getAbsolutePosition(),this.accuracy)
rotateTowards(this.enemyPivot,this.enemy.getAbsolutePosition(),newTarget)
//perform shortest path
if(this.enemy.getAbsolutePosition().x>0) this.direction=-1
else this.direction=1
this.enemy.setParent(this.enemyPivot)
this.enemyPivot.setParent(this.planet)
this.nextUpdateTargetTime = new Date().getTime() + this.updateTargetInterval;
}
}
whenHit(damage) {
this.life-=damage
this.healthBar.whenHit(damage)
if (this.life <= 0) this.dying=true
else this.mesh.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
}
clean() {
this.enemy.dispose()
this.enemyPivot.dispose()
}
}
class Bullet{
constructor(mesh,shooter=null,planet,scene){
this.mesh=mesh
this.planet=planet
this.bullet=null
this.pivot=null
//movement
this.axis=null
this.direction=null
this.shooter=shooter
//stats
this.bulletAngleOffset = pi/12;
this.bulletHorizOffset = 0.5;
this.bulletSpeed=bulletSpeed
this.bulletHeight = 0.3;
this.bulletRange = 1000;
this.damage=1;
this.bulletSize=0
}
//get range value from number of revolutions needed
getRangeFromNTurns(N) {
var s=this.bulletSpeed
var hi=this.bulletHeight
var range= (-hi*s)/ (2*Math.PI *N)
return range
}
//thruster partciles for rockets
createParticles() {
var particles= new BABYLON.ParticleSystem("particles", 500);
particles.particleTexture = new BABYLON.Texture("texture/flare.png");
particles.emitter= this.bullet
var direction1=new BABYLON.Vector3(0, -10,0)
var direction2=new BABYLON.Vector3(0, -5, 0)
var maxEmitBox=new BABYLON.Vector3(0.1, 0.1, 0)
var minEmitBox=new BABYLON.Vector3(-0.1, -0.1, 0)
particles.createBoxEmitter(direction1,direction2,minEmitBox,maxEmitBox)
particles.color1=new BABYLON.Color3(1,0,0)
particles.color2=new BABYLON.Color3(0.5,0.5,0)
particles.colorDead=new BABYLON.Color3(0.1,0.1,0.1)
particles.minLifeTime = 0.05;
particles.maxLifeTime = 0.1;
particles.minSize=0.05
particles.maxSize=0.2
//optimization if many bullets
var emitRate= 300/( (bulletArcCount+bulletParallelCount));
//minimum of 20
if(emitRate<20) particles.emitRate = 20
particles.emitRate =emitRate
//particles must start from base, not center
particles.startPositionFunction = (worldMatrix, positionToUpdate, particle, isLocal) => {
var randX = BABYLON.Scalar.RandomRange(minEmitBox.x, maxEmitBox.x);
var randY = BABYLON.Scalar.RandomRange(minEmitBox.y, maxEmitBox.y);
randY-=this.bulletSize.y
var randZ = BABYLON.Scalar.RandomRange(minEmitBox.z, maxEmitBox.z);
BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
};
particles.start();
}
spawn(dir){
const currentTime = new Date().getTime();
var mesh=this.mesh
//get size of bullet
this.bulletSize=mesh.getBoundingInfo().boundingBox.extendSize
bulletHorizOffset = this.bulletSize.x;
var dir;
if(DEBUG) this.pivot = BABYLON.Mesh.CreateCapsule(`${currentTime}pivot$`, { radiusTop: 0.05 }, scene); // capsule is visible for debug
else this.pivot = new BABYLON.TransformNode(`${currentTime}pivot$`); // transformNode is invisible
//get instance from pre-loaded model
this.bullet = mesh.createInstance("bullet ");
var shooterPos = this.shooter.getAbsolutePosition();
this.bullet.position = shooterPos;
// dir is the direction of the cannon basically
this.bullet.rotation.z = dir;
this.pivot.rotation.z = dir;
this.bullet.setParent(this.pivot);
this.pivot.setParent(this.planet);
this.axis = new BABYLON.Vector3(dir, 0, 0);
this.direction = dir > 0 ? 1 : -1;
//slightly higher in order to not collide with ground immediately
this.bullet.locallyTranslate(new BABYLON.Vector3(0, 0, -this.bulletHeight));
//little forward w.r.t shooter
this.bullet.locallyTranslate(new BABYLON.Vector3(0, 0.5, 0));
//this.bullet.material = new BABYLON.StandardMaterial("bulletmat", scene);
this.bullet.checkCollisions = true;
this.bulletRange=this.getRangeFromNTurns(1.25)
this.createParticles()
}
move() {
this.bullet.rotation.y+=0.1
this.bullet.position.z-=this.bulletRange
this.pivot.rotate(this.axis, this.bulletSpeed * this.direction, BABYLON.Space.LOCAL);
}
}
class HealthBar {
constructor(enemy, scene, life) {
this.enemy = enemy;
this.mesh = BABYLON.MeshBuilder.CreateCylinder("healthbar",
{height: 0.8, diameter: 0.15}, scene);
this.mesh.parent = this.enemy;
this.startLife=life
this.remainingLife=life
this.greenThreshold=0.8
this.yellowThreshold=0.5
this.redThreshold=0.3
this.mesh.position.y += this.enemy.getBoundingInfo().boundingBox.extendSize.y*2+0.3
this.mesh.rotation.z = pi/2;
this.mesh.material = new BABYLON.StandardMaterial("healthbar", scene);
this.mesh.material.emissiveColor = new BABYLON.Color3(0, 1, 0);
for(var l=0;l<lights.length;l++) lights[l].excludedMeshes.push(this.mesh);
glowLayer.addIncludedOnlyMesh(this.mesh);
}
whenHit(damage) {
this.remainingLife-=damage
//compute scaling of health bar w.r.t remaining health
//remaining:full health = scaling : 1 (full bar)
//scaling=remaining/full Health
this.mesh.scaling.y = this.remainingLife/this.startLife;
if (this.mesh.scaling.y>this.greenThreshold)
this.mesh.material.emissiveColor = new BABYLON.Color3(0, 0.8, 0);
else if (this.mesh.scaling.y>this.yellowThreshold)
this.mesh.material.emissiveColor = new BABYLON.Color3(1, 0.8, 0);
else if (this.mesh.scaling.y > this.redThreshold)
this.mesh.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
else if (this.mesh.scaling.y <= 0) this.mesh.dispose(false, true);
}
}