-
Notifications
You must be signed in to change notification settings - Fork 12
/
ParticleEffectAsset.ts
157 lines (146 loc) · 3.83 KB
/
ParticleEffectAsset.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
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
import { InvertAxis, InvertAxisFlags } from './enums';
import { Entity } from './models/';
import { Vector3 } from './utils';
/**
* UNFINISHED! Class that represents a particle effect asset.
*/
export class ParticleEffectAsset {
/**
* Returns the name of the asset. Same as AssetName.
*/
public get Asset(): string {
return this.assetName;
}
private readonly assetName: string;
constructor(assetName: string) {
this.assetName = assetName;
}
/**
* Get the name of the particle effect.
*/
public get AssetName(): string {
return this.assetName;
}
/**
* Get whether the particle effect has loaded into game memory.
*/
public get IsLoaded(): boolean {
return HasNamedPtfxAssetLoaded(this.assetName);
}
/**
* Start a particle effect at a world position.
*
* @param effectName Name of effect.
* @param entity Entity to use effect on.
* @param off Offset from entity position.
* @param rot Rotation from entity position.
* @param scale Size of the effect.
* @param invertAxis Which axis to invert (default none).
*/
public startNonLoopedAtCoord(
effectName: string,
pos: Vector3,
rot: Vector3 = new Vector3(0, 0, 0),
scale = 1.0,
invertAxis: InvertAxis = { flags: InvertAxisFlags.None },
): boolean {
if (!this.setNextCall()) {
return false;
}
const invertAxisFlags = invertAxis.flags;
SetPtfxAssetNextCall(this.assetName);
return (
StartParticleFxLoopedAtCoord(
effectName,
pos.x,
pos.y,
pos.z,
rot.x,
rot.y,
rot.z,
scale,
!!(invertAxisFlags & InvertAxisFlags.X),
!!(invertAxisFlags & InvertAxisFlags.Y),
!!(invertAxisFlags & InvertAxisFlags.Z),
false,
) > 0
);
}
/**
* Start a particle effect on an entity
*
* @param effectName Name of effect.
* @param entity Entity to use effect on.
* @param off Offset from entity position.
* @param rot Rotation from entity position.
* @param scale Size of the effect.
* @param invertAxis Which axis to invert (default none).
*/
public startNonLoopedOnEntity(
effectName: string,
entity: Entity,
off: Vector3 = new Vector3(0, 0, 0),
rot: Vector3 = new Vector3(0, 0, 0),
scale = 1.0,
invertAxis: InvertAxis = { flags: InvertAxisFlags.None },
): boolean {
if (!this.setNextCall()) {
return false;
}
const invertAxisFlags = invertAxis.flags;
SetPtfxAssetNextCall(this.assetName);
return !!StartParticleFxLoopedOnEntity(
effectName,
entity.Handle,
off.x,
off.y,
off.z,
rot.x,
rot.y,
rot.z,
scale,
!!(invertAxisFlags & InvertAxisFlags.X),
!!(invertAxisFlags & InvertAxisFlags.Y),
!!(invertAxisFlags & InvertAxisFlags.Z),
);
}
/**
* Load a particle effect into the game memory.
*
* @param timeout Max time to load Particle Effect
*/
public request(timeout: number): Promise<boolean> {
return new Promise(resolve => {
if (!this.IsLoaded) {
RequestNamedPtfxAsset(this.assetName);
const start = GetGameTimer();
const interval = setInterval(() => {
if (this.IsLoaded || GetGameTimer() - start >= timeout) {
clearInterval(interval);
resolve(this.IsLoaded);
}
}, 0);
} else {
resolve(this.IsLoaded);
}
});
}
/**
* Allow game engine to safely unload particle effect model from memory.
*/
public markAsNoLongerNeeded(): void {
RemoveNamedPtfxAsset(this.assetName);
}
public toString(): string {
return this.assetName;
}
private setNextCall(): boolean {
if (!this.IsLoaded) {
RequestNamedPtfxAsset(this.assetName);
} else {
SetPtfxAssetNextCall(this.assetName);
return true;
}
return false;
}
}