Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/hexgl/ShipEffects.js
4627 views
1
/*
2
* HexGL
3
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
4
* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.
5
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
6
*/
7
8
var bkcore = bkcore || {};
9
bkcore.hexgl = bkcore.hexgl || {};
10
11
bkcore.hexgl.ShipEffects = function(opts)
12
{
13
this.scene = opts.scene;
14
this.shipControls = opts.shipControls;
15
16
this.booster = opts.booster;
17
this.boosterLight = opts.boosterLight;
18
this.boosterSprite = opts.boosterSprite;
19
20
this.useParticles = opts.useParticles;
21
22
if(this.useParticles)
23
{
24
this.pVel = new THREE.Vector3(0.5,0,0);
25
this.pOffset = new THREE.Vector3(-3,-0.3,0);
26
this.pRad = new THREE.Vector3(0,0,1.5);
27
28
this.shipVelocity = new THREE.Vector3();
29
30
this.pVelS = this.pVel.length();
31
this.pOffsetS = this.pOffset.length();
32
this.pRadS = this.pRad.length();
33
34
this.pVel.normalize();
35
this.pOffset.normalize();
36
this.pRad.normalize();
37
38
this.particles = {
39
40
leftSparks: new bkcore.threejs.Particles(
41
{
42
randomness: new THREE.Vector3(0.4,0.4,0.4),
43
tint: 0xffffff,
44
color: 0xffc000,
45
color2: 0xffffff,
46
texture: opts.textureSpark,
47
size: 2,
48
life: 60,
49
max: 200
50
}),
51
52
leftClouds: new bkcore.threejs.Particles(
53
{
54
opacity: 0.8,
55
tint: 0xffffff,
56
color: 0x666666,
57
color2: 0xa4f1ff,
58
texture: opts.textureCloud,
59
size: 6,
60
blending: THREE.NormalBlending,
61
life: 60,
62
max: 200,
63
spawn: new THREE.Vector3(3,-0.3,0),
64
spawnRadius: new THREE.Vector3(1,1,2),
65
velocity: new THREE.Vector3(0,0,-0.4),
66
randomness: new THREE.Vector3(0.05,0.05,0.1)
67
}),
68
69
rightSparks: new bkcore.threejs.Particles(
70
{
71
randomness: new THREE.Vector3(0.4,0.4,0.4),
72
tint: 0xffffff,
73
color: 0xffc000,
74
color2: 0xffffff,
75
texture: opts.textureSpark,
76
size: 2,
77
life: 60,
78
max: 200
79
}),
80
81
rightClouds: new bkcore.threejs.Particles(
82
{
83
opacity: 0.8,
84
tint: 0xffffff,
85
color: 0x666666,
86
color2: 0xa4f1ff,
87
texture: opts.textureCloud,
88
size: 6,
89
blending: THREE.NormalBlending,
90
life: 60,
91
max: 200,
92
spawn: new THREE.Vector3(-3,-0.3,0),
93
spawnRadius: new THREE.Vector3(1,1,2),
94
velocity: new THREE.Vector3(0,0,-0.4),
95
randomness: new THREE.Vector3(0.05,0.05,0.1)
96
})
97
};
98
99
this.shipControls.mesh.add(this.particles.leftClouds.system);
100
this.shipControls.mesh.add(this.particles.rightClouds.system);
101
this.scene.add(this.particles.leftSparks.system);
102
this.scene.add(this.particles.rightSparks.system);
103
}
104
}
105
106
bkcore.hexgl.ShipEffects.prototype.update = function(dt)
107
{
108
var boostRatio, opacity, scale, intensity, random;
109
110
if(this.shipControls.destroyed)
111
{
112
opacity = 0;
113
scale = 0;
114
intensity = 0;
115
random = 0;
116
}
117
else
118
{
119
boostRatio = this.shipControls.getBoostRatio();
120
opacity = this.shipControls.key.forward ? 0.8 : 0.3 + boostRatio * 0.4;
121
scale = (this.shipControls.key.forward ? 1.0 : 0.8) + boostRatio * 0.5;
122
intensity = this.shipControls.key.forward ? 4.0 : 2.0;
123
random = Math.random()*0.2;
124
}
125
126
if(this.booster)
127
{
128
this.booster.rotation.z += 1;
129
this.booster.scale.set(scale, scale, scale);
130
this.booster.material.opacity = random+opacity;
131
this.boosterSprite.opacity = random+opacity;
132
this.boosterLight.intensity = intensity*(random+0.8);
133
}
134
135
// PARTICLES
136
if(this.useParticles)
137
{
138
this.shipVelocity.copy(this.shipControls.currentVelocity).multiplyScalar(0.7);
139
140
this.particles.rightSparks.velocity.copy(this.pVel);
141
this.particles.rightSparks.spawnRadius.copy(this.pRad);
142
this.particles.rightSparks.spawn.copy(this.pOffset);
143
144
this.particles.leftSparks.velocity.copy(this.pVel).x *= -1;
145
this.particles.leftSparks.spawn.copy(this.pOffset).x *= -1;
146
147
if(this.shipControls.mesh)
148
{
149
// RIGHT
150
this.shipControls.mesh.matrix.rotateAxis(this.particles.rightSparks.spawn);
151
this.particles.rightSparks.spawn.multiplyScalar(this.pOffsetS).addSelf(this.shipControls.dummy.position);
152
153
this.shipControls.mesh.matrix.rotateAxis(this.particles.rightSparks.velocity);
154
this.particles.rightSparks.velocity.multiplyScalar(this.pVelS).addSelf(this.shipVelocity);
155
156
this.shipControls.mesh.matrix.rotateAxis(this.particles.rightSparks.spawnRadius);
157
this.particles.rightSparks.spawnRadius.multiplyScalar(this.pRadS);
158
159
// LEFT
160
this.shipControls.mesh.matrix.rotateAxis(this.particles.leftSparks.spawn);
161
this.particles.leftSparks.spawn.multiplyScalar(this.pOffsetS).addSelf(this.shipControls.dummy.position);
162
163
this.shipControls.mesh.matrix.rotateAxis(this.particles.leftSparks.velocity);
164
this.particles.leftSparks.velocity.multiplyScalar(this.pVelS).addSelf(this.shipVelocity);
165
166
this.particles.leftSparks.spawnRadius.copy(this.particles.rightSparks.spawnRadius);
167
}
168
169
if(this.shipControls.collision.right)
170
{
171
this.particles.rightSparks.emit(10);
172
this.particles.rightClouds.emit(5);
173
}
174
175
if(this.shipControls.collision.left)
176
{
177
this.particles.leftSparks.emit(10);
178
this.particles.leftClouds.emit(5);
179
}
180
181
this.particles.rightSparks.update(dt);
182
this.particles.rightClouds.update(dt);
183
this.particles.leftSparks.update(dt);
184
this.particles.leftClouds.update(dt);
185
}
186
}
187
188