- 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
import pool from "./../system/pooling.js";
import timer from "./../system/timer.js";
import { randomFloat, clamp } from "./../math/math.js";
import Renderable from "./../renderable/renderable.js";
/**
* @import ParticleEmitter from "./emitter.js";
*/
/**
* @classdesc
* Single Particle Object.
* @augments Renderable
*/
export default class Particle extends Renderable {
/**
* @param {ParticleEmitter} emitter - the particle emitter
*/
constructor(emitter) {
// Call the super constructor
super(
emitter.getRandomPointX(),
emitter.getRandomPointY(),
emitter.settings.image.width,
emitter.settings.image.height
);
this.onResetEvent(emitter, true);
}
/**
* @ignore
*/
onResetEvent(emitter, newInstance = false) {
if (newInstance === false) {
this.pos.set(
emitter.getRandomPointX(),
emitter.getRandomPointY()
);
this.resize(
emitter.settings.image.width,
emitter.settings.image.height
);
this.currentTransform.identity();
} else {
// particle velocity
this.vel = pool.pull("Vector2d");
}
this.image = emitter.settings.image;
// Particle will always update
this.alwaysUpdate = true;
if (typeof emitter.settings.tint === "string") {
this.tint.parseCSS(emitter.settings.tint);
}
if (emitter.settings.textureAdditive === true) {
this.blendMode = "additive";
}
if (emitter.settings.blendMode !== "normal") {
this.blendMode = emitter.settings.blendMode;
}
// Set the start particle Angle and Speed as defined in emitter
let angle = emitter.settings.angle + ((emitter.settings.angleVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.settings.angleVariation : 0);
let speed = emitter.settings.speed + ((emitter.settings.speedVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.settings.speedVariation : 0);
// Set the start particle Velocity
this.vel.set(speed * Math.cos(angle), -speed * Math.sin(angle));
// Set the start particle Time of Life as defined in emitter
this.life = randomFloat(emitter.settings.minLife, emitter.settings.maxLife);
this.startLife = this.life;
// Set the start and end particle Scale as defined in emitter
// clamp the values as minimum and maximum scales range
this.startScale = clamp(
randomFloat(emitter.settings.minStartScale, emitter.settings.maxStartScale),
emitter.settings.minStartScale,
emitter.settings.maxStartScale
);
this.endScale = clamp(
randomFloat(emitter.settings.minEndScale, emitter.settings.maxEndScale),
emitter.settings.minEndScale,
emitter.settings.maxEndScale
);
// Set the particle Gravity and Wind (horizontal gravity) as defined in emitter
this.gravity = emitter.settings.gravity;
this.wind = emitter.settings.wind;
// Set if the particle update the rotation in accordance the trajectory
this.followTrajectory = emitter.settings.followTrajectory;
// Set if the particle update only in Viewport
this.onlyInViewport = emitter.settings.onlyInViewport;
// cache inverse of the expected delta time
this._deltaInv = timer.maxfps / 1000;
// Set the start particle rotation as defined in emitter
// if the particle not follow trajectory
if (!emitter.settings.followTrajectory) {
this.angle = randomFloat(emitter.settings.minRotation, emitter.settings.maxRotation);
}
}
/**
* Update the Particle <br>
* This is automatically called by the game manager {@link game}
* @ignore
* @param {number} dt - time since the last update in milliseconds
*/
update(dt) {
// move things forward independent of the current frame rate
let skew = dt * this._deltaInv;
// Decrease particle life
this.life = this.life > dt ? this.life - dt : 0;
if (this.life <= 0) {
this.ancestor.removeChild(this);
return false;
}
// Calculate the particle Age Ratio
let ageRatio = this.life / this.startLife;
// Resize the particle as particle Age Ratio
let scale = this.startScale;
if (this.startScale > this.endScale) {
scale *= ageRatio;
scale = (scale < this.endScale) ? this.endScale : scale;
}
else if (this.startScale < this.endScale) {
scale /= ageRatio;
scale = (scale > this.endScale) ? this.endScale : scale;
}
// Set the particle opacity as Age Ratio
this.alpha = ageRatio;
// Adjust the particle velocity
this.vel.x += this.wind * skew;
this.vel.y += this.gravity * skew;
// If necessary update the rotation of particle in accordance the particle trajectory
let angle = this.followTrajectory ? Math.atan2(this.vel.y, this.vel.x) : this.angle;
this.pos.x += this.vel.x * skew;
this.pos.y += this.vel.y * skew;
// Update particle transform
this.currentTransform.setTransform(
scale, 0, 0,
0, scale, 0,
this.pos.x, this.pos.y, 1
).rotate(angle);
// mark as dirty if the particle is not dead yet
this.isDirty = this.inViewport || !this.onlyInViewport;
return super.update(dt);
}
/**
* @ignore
*/
draw(renderer) {
let w = this.width, h = this.height;
renderer.drawImage(
this.image,
0, 0,
w, h,
-w / 2, -h / 2,
w, h
);
}
}