2023-02-17 11:06:01 +01:00

107 lines
3.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LifeUpdater = void 0;
const tsparticles_engine_1 = require("tsparticles-engine");
const Life_1 = require("./Options/Classes/Life");
class LifeUpdater {
constructor(container) {
this.container = container;
}
init(particle) {
const container = this.container, particlesOptions = particle.options, lifeOptions = particlesOptions.life;
if (!lifeOptions) {
return;
}
particle.life = {
delay: container.retina.reduceFactor
? (((0, tsparticles_engine_1.getRangeValue)(lifeOptions.delay.value) * (lifeOptions.delay.sync ? 1 : (0, tsparticles_engine_1.getRandom)())) /
container.retina.reduceFactor) *
1000
: 0,
delayTime: 0,
duration: container.retina.reduceFactor
? (((0, tsparticles_engine_1.getRangeValue)(lifeOptions.duration.value) * (lifeOptions.duration.sync ? 1 : (0, tsparticles_engine_1.getRandom)())) /
container.retina.reduceFactor) *
1000
: 0,
time: 0,
count: lifeOptions.count,
};
if (particle.life.duration <= 0) {
particle.life.duration = -1;
}
if (particle.life.count <= 0) {
particle.life.count = -1;
}
if (particle.life) {
particle.spawning = particle.life.delay > 0;
}
}
isEnabled(particle) {
return !particle.destroyed;
}
loadOptions(options, ...sources) {
if (!options.life) {
options.life = new Life_1.Life();
}
for (const source of sources) {
options.life.load(source === null || source === void 0 ? void 0 : source.life);
}
}
update(particle, delta) {
if (!this.isEnabled(particle) || !particle.life) {
return;
}
const life = particle.life;
let justSpawned = false;
if (particle.spawning) {
life.delayTime += delta.value;
if (life.delayTime >= particle.life.delay) {
justSpawned = true;
particle.spawning = false;
life.delayTime = 0;
life.time = 0;
}
else {
return;
}
}
if (life.duration === -1) {
return;
}
if (particle.spawning) {
return;
}
if (justSpawned) {
life.time = 0;
}
else {
life.time += delta.value;
}
if (life.time < life.duration) {
return;
}
life.time = 0;
if (particle.life.count > 0) {
particle.life.count--;
}
if (particle.life.count === 0) {
particle.destroy();
return;
}
const canvasSize = this.container.canvas.size, widthRange = (0, tsparticles_engine_1.setRangeValue)(0, canvasSize.width), heightRange = (0, tsparticles_engine_1.setRangeValue)(0, canvasSize.width);
particle.position.x = (0, tsparticles_engine_1.randomInRange)(widthRange);
particle.position.y = (0, tsparticles_engine_1.randomInRange)(heightRange);
particle.spawning = true;
life.delayTime = 0;
life.time = 0;
particle.reset();
const lifeOptions = particle.options.life;
if (lifeOptions) {
life.delay = (0, tsparticles_engine_1.getRangeValue)(lifeOptions.delay.value) * 1000;
life.duration = (0, tsparticles_engine_1.getRangeValue)(lifeOptions.duration.value) * 1000;
}
}
}
exports.LifeUpdater = LifeUpdater;