-
Notifications
You must be signed in to change notification settings - Fork 123
/
RandomTexture.ts
51 lines (45 loc) · 1.41 KB
/
RandomTexture.ts
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
import { Texture } from '@pixi/core';
import { Particle } from '../Particle';
import { IEmitterBehavior, BehaviorOrder } from './Behaviors';
import { GetTextureFromString } from '../ParticleUtils';
import { BehaviorEditorConfig } from './editor/Types';
/**
* A Texture behavior that assigns a random texture to each particle from its list.
* String values will be converted to textures with {@link ParticleUtils.GetTextureFromString}.
*
* Example config:
* ```javascript
* {
* type: 'textureRandom',
* config: {
* textures: ["myTex1Id", "myTex2Id", "myTex3Id", "myTex4Id"],
* }
* }
* ```
*/
export class RandomTextureBehavior implements IEmitterBehavior
{
public static type = 'textureRandom';
public static editorConfig: BehaviorEditorConfig = null;
public order = BehaviorOrder.Normal;
private textures: Texture[];
constructor(config: {
/**
* Images to use for each particle, randomly chosen from the list.
*/
textures: (Texture|string)[];
})
{
this.textures = config.textures.map((tex) => (typeof tex === 'string' ? GetTextureFromString(tex) : tex));
}
initParticles(first: Particle): void
{
let next = first;
while (next)
{
const index = Math.floor(Math.random() * this.textures.length);
next.texture = this.textures[index];
next = next.next;
}
}
}