emeraldengine 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +968 -0
- package/dist/types/imports/BitmapText.d.ts +79 -0
- package/dist/types/imports/Color.d.ts +17 -0
- package/dist/types/imports/Drawable.d.ts +157 -0
- package/dist/types/imports/Emerald.d.ts +73 -0
- package/dist/types/imports/FPSCounter.d.ts +19 -0
- package/dist/types/imports/GLUtils.d.ts +6 -0
- package/dist/types/imports/Instance.d.ts +87 -0
- package/dist/types/imports/InstancedTexture.d.ts +230 -0
- package/dist/types/imports/Physics.d.ts +139 -0
- package/dist/types/imports/Scene.d.ts +39 -0
- package/dist/types/imports/Shaders.d.ts +4 -0
- package/dist/types/imports/Shapes.d.ts +25 -0
- package/dist/types/imports/Storage.d.ts +46 -0
- package/dist/types/imports/Texture.d.ts +19 -0
- package/dist/types/imports/Time.d.ts +22 -0
- package/dist/types/imports/Transform.d.ts +41 -0
- package/dist/types/imports/components/BoxCollider.d.ts +41 -0
- package/dist/types/imports/components/BoxColliderDebug.d.ts +19 -0
- package/dist/types/imports/components/CircleCollider.d.ts +39 -0
- package/dist/types/imports/components/CircleColliderDebug.d.ts +19 -0
- package/dist/types/imports/components/Collider.d.ts +53 -0
- package/dist/types/imports/components/GameObject.d.ts +72 -0
- package/dist/types/imports/components/RigidBody.d.ts +104 -0
- package/dist/types/imports/lights/DirectionalLight.d.ts +26 -0
- package/dist/types/imports/lights/PointLight.d.ts +18 -0
- package/dist/types/imports/managers/AudioManager.d.ts +60 -0
- package/dist/types/imports/managers/CameraManager.d.ts +35 -0
- package/dist/types/imports/managers/EventManager.d.ts +187 -0
- package/dist/types/imports/managers/GLManager.d.ts +47 -0
- package/dist/types/imports/managers/IDManager.d.ts +21 -0
- package/dist/types/imports/managers/SceneManager.d.ts +22 -0
- package/dist/types/imports/particlesystem/Particle.d.ts +42 -0
- package/dist/types/imports/particlesystem/ParticleSettings.d.ts +49 -0
- package/dist/types/imports/particlesystem/Particles.d.ts +71 -0
- package/dist/types/index.d.ts +32 -0
- package/imports/BitmapText.js +245 -0
- package/imports/Color.js +18 -0
- package/imports/Drawable.js +550 -0
- package/imports/Emerald.js +459 -0
- package/imports/FPSCounter.js +43 -0
- package/imports/GLUtils.js +67 -0
- package/imports/Instance.js +187 -0
- package/imports/InstancedTexture.js +856 -0
- package/imports/Physics.js +242 -0
- package/imports/Scene.js +79 -0
- package/imports/Shaders.js +165 -0
- package/imports/Shapes.js +129 -0
- package/imports/Storage.js +63 -0
- package/imports/Texture.js +67 -0
- package/imports/Time.js +28 -0
- package/imports/Transform.js +59 -0
- package/imports/components/BoxCollider.js +67 -0
- package/imports/components/BoxColliderDebug.js +38 -0
- package/imports/components/CircleCollider.js +67 -0
- package/imports/components/CircleColliderDebug.js +36 -0
- package/imports/components/Collider.js +51 -0
- package/imports/components/GameObject.js +160 -0
- package/imports/components/RigidBody.js +169 -0
- package/imports/lights/DirectionalLight.js +68 -0
- package/imports/lights/PointLight.js +17 -0
- package/imports/managers/AudioManager.js +146 -0
- package/imports/managers/CameraManager.js +60 -0
- package/imports/managers/EventManager.js +479 -0
- package/imports/managers/GLManager.js +65 -0
- package/imports/managers/IDManager.js +32 -0
- package/imports/managers/SceneManager.js +27 -0
- package/imports/managers/ShaderManager.js +133 -0
- package/imports/particlesystem/Particle.js +75 -0
- package/imports/particlesystem/ParticleSettings.js +49 -0
- package/imports/particlesystem/Particles.js +226 -0
- package/index.js +67 -0
- package/package.json +38 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class ShaderManager
|
|
3
|
+
* @description Manages the shaders for the game
|
|
4
|
+
*/
|
|
5
|
+
export class ShaderManager {
|
|
6
|
+
constructor(gl) {
|
|
7
|
+
this.gl = gl;
|
|
8
|
+
this.shaders = new Map();
|
|
9
|
+
this.activeShader = null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @method createShader
|
|
14
|
+
* @description Creates a shader
|
|
15
|
+
* @param {string} name - The name of the shader
|
|
16
|
+
* @param {string} vertexSource - The vertex shader source
|
|
17
|
+
* @param {string} fragmentSource - The fragment shader source
|
|
18
|
+
*/
|
|
19
|
+
createShader(name, vertexSource, fragmentSource) {
|
|
20
|
+
const vertexShader = this.compileShader(
|
|
21
|
+
vertexSource,
|
|
22
|
+
this.gl.VERTEX_SHADER
|
|
23
|
+
);
|
|
24
|
+
const fragmentShader = this.compileShader(
|
|
25
|
+
fragmentSource,
|
|
26
|
+
this.gl.FRAGMENT_SHADER
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const shaderProgram = this.gl.createProgram();
|
|
30
|
+
this.gl.attachShader(shaderProgram, vertexShader);
|
|
31
|
+
this.gl.attachShader(shaderProgram, fragmentShader);
|
|
32
|
+
this.gl.linkProgram(shaderProgram);
|
|
33
|
+
if (!this.gl.getProgramParameter(shaderProgram, this.gl.LINK_STATUS)) {
|
|
34
|
+
console.error(
|
|
35
|
+
"Error linking shader program:",
|
|
36
|
+
this.gl.getProgramInfoLog(shaderProgram)
|
|
37
|
+
);
|
|
38
|
+
this.gl.deleteProgram(shaderProgram);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const programInfo = {
|
|
43
|
+
program: shaderProgram,
|
|
44
|
+
attribLocations: this.getAttributeLocations(shaderProgram),
|
|
45
|
+
uniformLocations: this.getUniformLocations(shaderProgram),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
this.shaders.set(name, programInfo);
|
|
49
|
+
|
|
50
|
+
return programInfo;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @method compileShader
|
|
55
|
+
* @description Compiles a shader
|
|
56
|
+
* @param {string} source - The shader source
|
|
57
|
+
* @param {number} type - The type of shader
|
|
58
|
+
*/
|
|
59
|
+
compileShader(source, type) {
|
|
60
|
+
const shader = this.gl.createShader(type);
|
|
61
|
+
this.gl.shaderSource(shader, source);
|
|
62
|
+
this.gl.compileShader(shader);
|
|
63
|
+
|
|
64
|
+
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
|
|
65
|
+
console.error(
|
|
66
|
+
"Shader compilation error:",
|
|
67
|
+
this.gl.getShaderInfoLog(shader)
|
|
68
|
+
);
|
|
69
|
+
this.gl.deleteShader(shader);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return shader;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @method useShader
|
|
78
|
+
* @description Uses a shader
|
|
79
|
+
* @param {string} name - The name of the shader
|
|
80
|
+
*/
|
|
81
|
+
useShader(name) {
|
|
82
|
+
const shader = this.shaders.get(name);
|
|
83
|
+
if (shader) {
|
|
84
|
+
this.gl.useProgram(shader.program);
|
|
85
|
+
this.activeShader = shader;
|
|
86
|
+
} else {
|
|
87
|
+
console.warn(`Shader ${name} not found.`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return shader;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @method getActiveShader
|
|
95
|
+
* @description Returns the active shader
|
|
96
|
+
* @returns {Object} - The active shader
|
|
97
|
+
*/
|
|
98
|
+
getActiveShader() {
|
|
99
|
+
return this.activeShader;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @method getAttributeLocations
|
|
104
|
+
* @description Returns the attribute locations for a shader program
|
|
105
|
+
* @param {WebGLProgram} program - The shader program
|
|
106
|
+
* @returns {Object} - The attribute locations
|
|
107
|
+
*/
|
|
108
|
+
getAttributeLocations(program) {
|
|
109
|
+
return {
|
|
110
|
+
vertexPosition: this.gl.getAttribLocation(program, "aVertexPosition"),
|
|
111
|
+
textureCoord: this.gl.getAttribLocation(program, "aTextureCoord"),
|
|
112
|
+
// Add other attributes as needed
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @method getUniformLocations
|
|
118
|
+
* @description Returns the uniform locations for a shader program
|
|
119
|
+
* @param {WebGLProgram} program - The shader program
|
|
120
|
+
* @returns {Object} - The uniform locations
|
|
121
|
+
*/
|
|
122
|
+
getUniformLocations(program) {
|
|
123
|
+
return {
|
|
124
|
+
projectionMatrix: this.gl.getUniformLocation(
|
|
125
|
+
program,
|
|
126
|
+
"uProjectionMatrix"
|
|
127
|
+
),
|
|
128
|
+
modelViewMatrix: this.gl.getUniformLocation(program, "uModelViewMatrix"),
|
|
129
|
+
sampler: this.gl.getUniformLocation(program, "uSampler"),
|
|
130
|
+
// Add other uniforms as needed
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import Instance from "../Instance.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @class Particle
|
|
5
|
+
* @description Represents a particle in the particle system
|
|
6
|
+
* @param {InstancedTexture} instancedTexture - The instanced texture
|
|
7
|
+
* @param {Vector3} position - The position of the particle
|
|
8
|
+
* @param {number} rotation - The rotation of the particle
|
|
9
|
+
* @param {Vector2} scale - The scale of the particle
|
|
10
|
+
* @param {ParticleSettings} settings - The settings for the particle
|
|
11
|
+
*/
|
|
12
|
+
class Particle {
|
|
13
|
+
constructor(instancedTexture, position, rotation, scale, settings) {
|
|
14
|
+
this.settings = settings;
|
|
15
|
+
this.lifetime = settings.lifetime;
|
|
16
|
+
this.age = 0;
|
|
17
|
+
this.velocity = { ...settings.velocity };
|
|
18
|
+
this.gravity = { ...settings.gravity };
|
|
19
|
+
this.instance = new Instance(
|
|
20
|
+
`Particle at ${position.x}, ${position.y}`,
|
|
21
|
+
position,
|
|
22
|
+
scale,
|
|
23
|
+
rotation,
|
|
24
|
+
settings.frame
|
|
25
|
+
);
|
|
26
|
+
this.instancedTexture = instancedTexture;
|
|
27
|
+
if(this.settings.animation) {
|
|
28
|
+
this.instance.playAnimation(
|
|
29
|
+
this.settings.animation.frames,
|
|
30
|
+
this.settings.animation.speed
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
instancedTexture.addInstance(this.instance);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @method update
|
|
38
|
+
* @description Updates the particle
|
|
39
|
+
* @param {number} dt - The delta time
|
|
40
|
+
*/
|
|
41
|
+
update(dt) {
|
|
42
|
+
this.age += dt;
|
|
43
|
+
// Apply gravity
|
|
44
|
+
this.velocity.x += this.gravity.x * dt;
|
|
45
|
+
this.velocity.y += this.gravity.y * dt;
|
|
46
|
+
// Update position
|
|
47
|
+
this.instance.transform.position.x += this.velocity.x * dt;
|
|
48
|
+
this.instance.transform.position.y += this.velocity.y * dt;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @method isAlive
|
|
53
|
+
* @description Checks if the particle is alive
|
|
54
|
+
* @returns {boolean} - True if the particle is alive
|
|
55
|
+
*/
|
|
56
|
+
isAlive() {
|
|
57
|
+
return this.age < this.lifetime;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @method destroy
|
|
62
|
+
* @description Destroys the particle
|
|
63
|
+
*/
|
|
64
|
+
destroy() {
|
|
65
|
+
if (this.instancedTexture && this.instance) {
|
|
66
|
+
if (this.instancedTexture.instances.length > 0) {
|
|
67
|
+
if(this.instancedTexture.getInstanceWithId(this.instance.id)) {
|
|
68
|
+
this.instancedTexture.removeInstance(this.instance.id);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default Particle;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Vector2 } from "../Physics.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @class ParticleSettings
|
|
5
|
+
* @description Represents the settings for a particle system
|
|
6
|
+
* @param {number} lifetime - The lifetime of the particle
|
|
7
|
+
* @param {Vector2} velocity - The velocity of the particle
|
|
8
|
+
* @param {Vector2} gravity - The gravity of the particle
|
|
9
|
+
* @param {number} amount - The amount of particles
|
|
10
|
+
* @param {Vector2} direction - The direction of the particle
|
|
11
|
+
* @param {number} spread - The spread of the particle
|
|
12
|
+
* @param {number} emissionRate - The emission rate of the particle
|
|
13
|
+
* @param {number} frame - The frame of the particle
|
|
14
|
+
* @param {number} offset - The offset of the particle
|
|
15
|
+
* @param {number} rotation - The rotation of the particle
|
|
16
|
+
* @param {Vector2} scale - The scale of the particle
|
|
17
|
+
* @param {Array} animation - The animation of the particle
|
|
18
|
+
*/
|
|
19
|
+
class ParticleSettings {
|
|
20
|
+
constructor({
|
|
21
|
+
lifetime = 1.0,
|
|
22
|
+
velocity = new Vector2(0, 0),
|
|
23
|
+
gravity = new Vector2(0, 0),
|
|
24
|
+
amount = 10,
|
|
25
|
+
direction = new Vector2(1, 0),
|
|
26
|
+
spread = Math.PI / 4,
|
|
27
|
+
emissionRate = Infinity, // particles per second
|
|
28
|
+
frame,
|
|
29
|
+
offset,
|
|
30
|
+
rotation,
|
|
31
|
+
scale,
|
|
32
|
+
animation
|
|
33
|
+
} = {}) {
|
|
34
|
+
this.lifetime = lifetime;
|
|
35
|
+
this.velocity = velocity;
|
|
36
|
+
this.gravity = gravity;
|
|
37
|
+
this.amount = amount;
|
|
38
|
+
this.direction = direction;
|
|
39
|
+
this.spread = spread;
|
|
40
|
+
this.emissionRate = emissionRate;
|
|
41
|
+
this.frame = frame;
|
|
42
|
+
this.offset = offset;
|
|
43
|
+
this.rotation = rotation;
|
|
44
|
+
this.scale = scale;
|
|
45
|
+
this.animation = animation;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default ParticleSettings;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import GameObject from "../components/GameObject.js";
|
|
2
|
+
import InstancedTexture from "../InstancedTexture.js";
|
|
3
|
+
import { Vector3 } from "../Physics.js";
|
|
4
|
+
import Particle from "./Particle.js";
|
|
5
|
+
import ParticleSettings from "./ParticleSettings.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @class Particles
|
|
9
|
+
* @description Represents a particle system
|
|
10
|
+
* @param {string} name - The name of the particle system
|
|
11
|
+
* @param {string} texturePath - The path to the texture
|
|
12
|
+
* @param {number} frameWidth - The width of the frame
|
|
13
|
+
* @param {number} frameHeight - The height of the frame
|
|
14
|
+
* @param {number} framesPerRow - The number of frames per row
|
|
15
|
+
* @param {number} totalFrames - The total number of frames
|
|
16
|
+
* @param {number} duration - The duration of the particle system
|
|
17
|
+
* @param {ParticleSettings} settings - The settings for the particle system
|
|
18
|
+
*/
|
|
19
|
+
export default class Particles {
|
|
20
|
+
constructor(
|
|
21
|
+
name,
|
|
22
|
+
texturePath,
|
|
23
|
+
frameWidth,
|
|
24
|
+
frameHeight,
|
|
25
|
+
framesPerRow,
|
|
26
|
+
totalFrames,
|
|
27
|
+
duration = 0.2,
|
|
28
|
+
settings = null
|
|
29
|
+
) {
|
|
30
|
+
this.settings = settings
|
|
31
|
+
? new ParticleSettings(settings)
|
|
32
|
+
: new ParticleSettings();
|
|
33
|
+
this.position = new Vector3(0, 0, 0);
|
|
34
|
+
this.rotation = this.settings.rotation;
|
|
35
|
+
this.scale = this.settings.scale;
|
|
36
|
+
this.particles = [];
|
|
37
|
+
this.duration = duration; // How long the effect lasts in seconds
|
|
38
|
+
this.offset = this.settings.offset; // Distance from center
|
|
39
|
+
this.elapsed = 0;
|
|
40
|
+
this.active = false;
|
|
41
|
+
this.instanceCount = this.settings.amount;
|
|
42
|
+
this.emissionTimer = 0;
|
|
43
|
+
this.emittedParticles = 0;
|
|
44
|
+
this.stopped = false;
|
|
45
|
+
this.gameObject = new GameObject(
|
|
46
|
+
name,
|
|
47
|
+
this.position,
|
|
48
|
+
this.settings.rotation,
|
|
49
|
+
this.settings.scale
|
|
50
|
+
);
|
|
51
|
+
this.instancedTexture = new InstancedTexture(
|
|
52
|
+
texturePath,
|
|
53
|
+
this.instanceCount,
|
|
54
|
+
frameWidth,
|
|
55
|
+
frameHeight,
|
|
56
|
+
framesPerRow,
|
|
57
|
+
totalFrames,
|
|
58
|
+
this.settings.animation ? this.settings.animation.speed : 0,
|
|
59
|
+
this.settings.animation ? true : false,
|
|
60
|
+
);
|
|
61
|
+
this.instancedTexture.clearInstances(); // Ensure clean state on creation
|
|
62
|
+
this.gameObject.addComponent(this.instancedTexture);
|
|
63
|
+
|
|
64
|
+
// Initialize clean state
|
|
65
|
+
this.particles = [];
|
|
66
|
+
this.active = false;
|
|
67
|
+
this.elapsed = 0;
|
|
68
|
+
this.emissionTimer = 0;
|
|
69
|
+
this.emittedParticles = 0;
|
|
70
|
+
this.stopped = false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @method play
|
|
75
|
+
* @description Plays the particle system
|
|
76
|
+
* @param {Vector3} newPosition - The position to play the particle system at
|
|
77
|
+
*/
|
|
78
|
+
play(newPosition) {
|
|
79
|
+
this.instancedTexture.clearInstances();
|
|
80
|
+
this.particles = [];
|
|
81
|
+
this.elapsed = 0;
|
|
82
|
+
this.active = true;
|
|
83
|
+
this.emissionTimer = 0;
|
|
84
|
+
this.emittedParticles = 0;
|
|
85
|
+
this.stopped = false;
|
|
86
|
+
this.lastEmitPosition = newPosition;
|
|
87
|
+
if (this.settings.emissionRate === Infinity) {
|
|
88
|
+
// One-shot: emit all at once
|
|
89
|
+
this.emitParticles(this.settings.amount, newPosition);
|
|
90
|
+
}
|
|
91
|
+
// For continuous, particles will be emitted in update()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @method emitParticles
|
|
96
|
+
* @description Emits particles
|
|
97
|
+
* @param {number} count - The number of particles to emit
|
|
98
|
+
* @param {Vector3} position - The position to emit the particles at
|
|
99
|
+
*/
|
|
100
|
+
emitParticles(count, position) {
|
|
101
|
+
const numberOfParticles = count;
|
|
102
|
+
const baseDirection = this.settings.direction;
|
|
103
|
+
const baseVelocity = this.settings.velocity;
|
|
104
|
+
const speed = Math.sqrt(
|
|
105
|
+
baseVelocity.x * baseVelocity.x + baseVelocity.y * baseVelocity.y
|
|
106
|
+
);
|
|
107
|
+
for (let i = 0; i < numberOfParticles; i++) {
|
|
108
|
+
let angle = Math.atan2(baseDirection.y, baseDirection.x);
|
|
109
|
+
let spread = this.settings.spread;
|
|
110
|
+
const angleVariation = (Math.random() - 0.5) * spread;
|
|
111
|
+
const finalAngle = angle + angleVariation;
|
|
112
|
+
const velocity = {
|
|
113
|
+
x: Math.cos(finalAngle) * speed,
|
|
114
|
+
y: Math.sin(finalAngle) * speed,
|
|
115
|
+
};
|
|
116
|
+
const offsetVariation = (Math.random() - 0.5) * this.offset * 0.5;
|
|
117
|
+
const pos = new Vector3(
|
|
118
|
+
position.x + Math.cos(finalAngle) * (this.offset + offsetVariation),
|
|
119
|
+
position.y + Math.sin(finalAngle) * (this.offset + offsetVariation),
|
|
120
|
+
position.z
|
|
121
|
+
);
|
|
122
|
+
const particleSettings = new ParticleSettings({
|
|
123
|
+
...this.settings,
|
|
124
|
+
velocity,
|
|
125
|
+
});
|
|
126
|
+
if(this.particles.length < this.instanceCount) {
|
|
127
|
+
let particle = new Particle(
|
|
128
|
+
this.instancedTexture,
|
|
129
|
+
pos,
|
|
130
|
+
this.rotation,
|
|
131
|
+
this.scale,
|
|
132
|
+
particleSettings
|
|
133
|
+
);
|
|
134
|
+
this.particles.push(particle);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @method update
|
|
142
|
+
* @description Updates the particle system
|
|
143
|
+
* @param {number} deltaTime - The delta time
|
|
144
|
+
*/
|
|
145
|
+
update(deltaTime) {
|
|
146
|
+
if (!this.active || this.stopped) return;
|
|
147
|
+
this.elapsed += deltaTime;
|
|
148
|
+
// Continuous emission
|
|
149
|
+
if (
|
|
150
|
+
this.settings.emissionRate !== Infinity &&
|
|
151
|
+
this.elapsed <= this.duration
|
|
152
|
+
) {
|
|
153
|
+
this.emissionTimer += deltaTime;
|
|
154
|
+
const particlesPerSecond = this.settings.emissionRate;
|
|
155
|
+
let toEmit = 0;
|
|
156
|
+
if (particlesPerSecond > 0) {
|
|
157
|
+
const expectedTotal = Math.floor(
|
|
158
|
+
this.emissionTimer * particlesPerSecond
|
|
159
|
+
);
|
|
160
|
+
toEmit = expectedTotal - this.emittedParticles;
|
|
161
|
+
}
|
|
162
|
+
if (toEmit > 0) {
|
|
163
|
+
this.emitParticles(toEmit, this.lastEmitPosition);
|
|
164
|
+
this.emittedParticles += toEmit;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Update all particles and remove dead ones
|
|
168
|
+
this.particles = this.particles.filter((particle) => {
|
|
169
|
+
particle.update(deltaTime);
|
|
170
|
+
if (!particle.isAlive()) {
|
|
171
|
+
particle.destroy();
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
});
|
|
176
|
+
if (
|
|
177
|
+
this.elapsed >= this.duration &&
|
|
178
|
+
this.settings.emissionRate !== Infinity
|
|
179
|
+
) {
|
|
180
|
+
this.stopped = true;
|
|
181
|
+
}
|
|
182
|
+
if (this.stopped && this.particles.length === 0) {
|
|
183
|
+
this.instancedTexture.clearInstances();
|
|
184
|
+
this.particles = [];
|
|
185
|
+
this.active = false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @method stop
|
|
191
|
+
* @description Stops the particle system
|
|
192
|
+
*/
|
|
193
|
+
stop() {
|
|
194
|
+
this.stopped = true;
|
|
195
|
+
this.instancedTexture.clearInstances();
|
|
196
|
+
this.particles = [];
|
|
197
|
+
this.active = false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @method destroy
|
|
202
|
+
* @description Destroys the particle system
|
|
203
|
+
*/
|
|
204
|
+
destroy() {
|
|
205
|
+
this.stop();
|
|
206
|
+
if (this.instancedTexture) {
|
|
207
|
+
this.instancedTexture.clearInstances();
|
|
208
|
+
}
|
|
209
|
+
this.particles = [];
|
|
210
|
+
this.active = false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @method reset
|
|
215
|
+
* @description Resets the particle system
|
|
216
|
+
*/
|
|
217
|
+
reset() {
|
|
218
|
+
this.stop();
|
|
219
|
+
this.particles = [];
|
|
220
|
+
this.active = false;
|
|
221
|
+
this.elapsed = 0;
|
|
222
|
+
this.emissionTimer = 0;
|
|
223
|
+
this.emittedParticles = 0;
|
|
224
|
+
this.stopped = false;
|
|
225
|
+
}
|
|
226
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import Emerald from "./imports/Emerald.js";
|
|
2
|
+
import BitmapText from "./imports/BitmapText.js";
|
|
3
|
+
import Color from "./imports/Color.js";
|
|
4
|
+
import Drawable from "./imports/Drawable.js";
|
|
5
|
+
import FPSCounter from "./imports/FPSCounter.js";
|
|
6
|
+
import Instance from "./imports/Instance.js";
|
|
7
|
+
import InstancedTexture from "./imports/InstancedTexture.js";
|
|
8
|
+
import { Physics, Vector2, Vector3 } from "./imports/Physics.js";
|
|
9
|
+
import Scene from "./imports/Scene.js";
|
|
10
|
+
import { Square2D, Triangle2D, Circle2D } from "./imports/Shapes.js";
|
|
11
|
+
import Storage from "./imports/Storage.js";
|
|
12
|
+
import Texture from "./imports/Texture.js";
|
|
13
|
+
import Time from "./imports/Time.js";
|
|
14
|
+
import Transform from "./imports/Transform.js";
|
|
15
|
+
import Particle from "./imports/particlesystem/Particle.js";
|
|
16
|
+
import Particles from "./imports/particlesystem/Particles.js";
|
|
17
|
+
import ParticleSettings from "./imports/particlesystem/ParticleSettings.js";
|
|
18
|
+
import AudioManager from "./imports/managers/AudioManager.js";
|
|
19
|
+
import CameraManager from "./imports/managers/CameraManager.js";
|
|
20
|
+
import EventManager from "./imports/managers/EventManager.js";
|
|
21
|
+
import SceneManager from "./imports/managers/SceneManager.js";
|
|
22
|
+
import DirectionalLight from "./imports/lights/DirectionalLight.js";
|
|
23
|
+
import PointLight from "./imports/lights/PointLight.js";
|
|
24
|
+
import BoxCollider from "./imports/components/BoxCollider.js";
|
|
25
|
+
import BoxColliderDebug from "./imports/components/BoxColliderDebug.js";
|
|
26
|
+
import CircleCollider from "./imports/components/CircleCollider.js";
|
|
27
|
+
import CircleColliderDebug from "./imports/components/CircleColliderDebug.js";
|
|
28
|
+
import Collider from "./imports/components/Collider.js";
|
|
29
|
+
import GameObject from "./imports/components/GameObject.js";
|
|
30
|
+
import RigidBody from "./imports/components/RigidBody.js";
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
Emerald,
|
|
34
|
+
BitmapText,
|
|
35
|
+
Color,
|
|
36
|
+
Drawable,
|
|
37
|
+
FPSCounter,
|
|
38
|
+
Instance,
|
|
39
|
+
InstancedTexture,
|
|
40
|
+
Physics,
|
|
41
|
+
Vector2,
|
|
42
|
+
Vector3,
|
|
43
|
+
Scene,
|
|
44
|
+
Square2D,
|
|
45
|
+
Triangle2D,
|
|
46
|
+
Circle2D,
|
|
47
|
+
Storage,
|
|
48
|
+
Texture,
|
|
49
|
+
Time,
|
|
50
|
+
Transform,
|
|
51
|
+
Particle,
|
|
52
|
+
Particles,
|
|
53
|
+
ParticleSettings,
|
|
54
|
+
AudioManager,
|
|
55
|
+
CameraManager,
|
|
56
|
+
EventManager,
|
|
57
|
+
SceneManager,
|
|
58
|
+
DirectionalLight,
|
|
59
|
+
PointLight,
|
|
60
|
+
BoxCollider,
|
|
61
|
+
BoxColliderDebug,
|
|
62
|
+
CircleCollider,
|
|
63
|
+
CircleColliderDebug,
|
|
64
|
+
Collider,
|
|
65
|
+
GameObject,
|
|
66
|
+
RigidBody,
|
|
67
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "emeraldengine",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "2D graphics library for Web",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "index.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./index.js",
|
|
13
|
+
"require": "./index.js",
|
|
14
|
+
"default": "./index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"index.js",
|
|
19
|
+
"imports",
|
|
20
|
+
"dist/types"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"2D",
|
|
24
|
+
"graphics",
|
|
25
|
+
"game",
|
|
26
|
+
"engine",
|
|
27
|
+
"webgl",
|
|
28
|
+
"canvas"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"gl-matrix": "^3.4.3",
|
|
32
|
+
"planck": "^1.4.2",
|
|
33
|
+
"colyseus.js": "^0.16.16"
|
|
34
|
+
},
|
|
35
|
+
"author": "vahan-gev",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"devDependencies": {}
|
|
38
|
+
}
|