Skip to content

Commit

Permalink
add event listeners to particle system
Browse files Browse the repository at this point in the history
  • Loading branch information
Alchemist0823 committed Jul 28, 2024
1 parent 666d766 commit 65e3ccc
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/quarks.core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quarks.core",
"version": "0.15.2",
"version": "0.15.3",
"description": "Core library for Quarks VFX / Particle System Library",
"type": "module",
"types": "./dist/types/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/quarks.core/src/IParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export interface IEmitter {
matrixWorld: any; //type is annoying
}

export type ParticleSystemEventType = "emitEnd" | "destroy";
export interface ParticleSystemEvent {
type: ParticleSystemEventType;
particleSystem: IParticleSystem;
}

export interface IParticleSystem {

Expand Down Expand Up @@ -152,4 +157,7 @@ export interface IParticleSystem {

emit(delta: number, subEmissionState: EmissionState, matrix: Matrix4): void;

addEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent)=>void): void;
removeEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent)=>void): void;
removeAllEventListeners(event: ParticleSystemEventType): void;
}
2 changes: 1 addition & 1 deletion packages/quarks.nodes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "node based behavior and render system for quarks engine",
"type": "module",
"dependencies": {
"three.quarks": "^0.15.2"
"three.quarks": "^0.15.3"
},
"devDependencies": {
"@types/three": "^0.165.0",
Expand Down
38 changes: 37 additions & 1 deletion packages/quarks.nodes/src/nodes/NodeVFX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import {
EmissionState,
IEmitter,
} from 'quarks.core';
import {ParticleEmitter, RenderMode, BatchedRenderer, VFXBatchSettings} from 'three.quarks';
import {
ParticleEmitter,
RenderMode,
BatchedRenderer,
VFXBatchSettings,
ParticleSystemEventType,
ParticleSystemEvent,
} from 'three.quarks';
import {
BufferGeometry,
Layers,
Expand Down Expand Up @@ -353,6 +360,7 @@ export class NodeVFX implements IParticleSystem {
if (this.autoDestroy) {
this.markForDestroy = true;
}
this.fire({type: "emitEnd", particleSystem: this});
}

dispose() {
Expand All @@ -361,6 +369,7 @@ export class NodeVFX implements IParticleSystem {
emitter.dispose();
if (emitter.parent)
emitter.parent!.remove(emitter);
this.fire({type: "destroy", particleSystem: this});
}

restart() {
Expand Down Expand Up @@ -499,4 +508,31 @@ export class NodeVFX implements IParticleSystem {
}

speedFactor = 0;

private listeners: {[event: string]: Array<(event: ParticleSystemEvent)=>void>} = {};
addEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent) => void): void {
if (!this.listeners[event])
this.listeners[event] = [];
this.listeners[event].push(callback);
}

removeAllEventListeners(event: ParticleSystemEventType): void {
if (this.listeners[event])
this.listeners[event] = [];
}

removeEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent) => void): void {
if (this.listeners[event]) {
const index = this.listeners[event].indexOf(callback);
if (index !== -1) {
this.listeners[event].splice(index, 1);
}
}
}

private fire(event: ParticleSystemEvent) {
if (this.listeners[event.type]) {
this.listeners[event.type].forEach(callback => callback(event));
}
}
}
6 changes: 6 additions & 0 deletions packages/three.quarks/examples/explosionDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export class ExplosionDemo extends Demo {

this.batchRenderer = new BatchedParticleRenderer();
this.scene.add(this.batchRenderer);
function listener(event) {
console.log(event.type);
}

new QuarksLoader().load('ps.json', (obj) => {
QuarksUtil.addToBatchRenderer(obj, this.batchRenderer);
QuarksUtil.runOnAllParticleEmitters(obj, (emitter) => {
emitter.system.addEventListener("emitEnd", listener);
})
this.scene.add(obj);
this.groups.push(obj);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/three.quarks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "three.quarks",
"version": "0.15.2",
"version": "0.15.3",
"description": "A General-Purpose Particle System for three.js",
"type": "module",
"types": "./dist/types/index.d.ts",
Expand Down Expand Up @@ -60,7 +60,7 @@
"three": ">=0.165.0"
},
"dependencies": {
"quarks.core": "^0.15.2"
"quarks.core": "^0.15.3"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand Down
31 changes: 30 additions & 1 deletion packages/three.quarks/src/ParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
Matrix3,
Matrix4,
Quaternion,
Vector3Generator,
Vector3Generator, ParticleSystemEvent, ParticleSystemEventType,
} from 'quarks.core';
import {MetaData, ParticleEmitter} from './ParticleEmitter';
import {
Expand Down Expand Up @@ -455,6 +455,7 @@ export class ParticleSystem implements IParticleSystem {

private normalMatrix: Matrix3 = new Matrix3();
private memory: GeneratorMemory = [];
private listeners: {[event: string]: Array<(event: ParticleSystemEvent)=>void>} = {};
/** @internal **/
_renderer?: BatchedRenderer;

Expand Down Expand Up @@ -920,6 +921,7 @@ export class ParticleSystem implements IParticleSystem {
if (this.autoDestroy) {
this.markForDestroy = true;
}
this.fire({type: "emitEnd", particleSystem: this});
}

/**
Expand All @@ -929,6 +931,7 @@ export class ParticleSystem implements IParticleSystem {
if (this._renderer) this._renderer.deleteSystem(this);
this.emitter.dispose();
if (this.emitter.parent) this.emitter.parent.remove(this.emitter);
this.fire({type: "destroy", particleSystem: this});
}

/**
Expand Down Expand Up @@ -1353,6 +1356,32 @@ export class ParticleSystem implements IParticleSystem {
return this.rendererSettings;
}

addEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent) => void): void {
if (!this.listeners[event])
this.listeners[event] = [];
this.listeners[event].push(callback);
}

removeAllEventListeners(event: ParticleSystemEventType): void {
if (this.listeners[event])
this.listeners[event] = [];
}

removeEventListener(event: ParticleSystemEventType, callback: (event: ParticleSystemEvent) => void): void {
if (this.listeners[event]) {
const index = this.listeners[event].indexOf(callback);
if (index !== -1) {
this.listeners[event].splice(index, 1);
}
}
}

private fire(event: ParticleSystemEvent) {
if (this.listeners[event.type]) {
this.listeners[event.type].forEach(callback => callback(event));
}
}

/**
* Clone the particle system
*/
Expand Down

0 comments on commit 65e3ccc

Please sign in to comment.