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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
\brief Wrapper class for managing particles through SEffectManager
*/
class EffectParticle : Effect
{
	//! The main Particle effect that this Effect wrapper manages
	protected Particle 			m_ParticleObj;
	
	/** \name Generic data
	*	Generic data for the particle
	*/
	//@{
	//! The ID in the ParticleList to create Particle from
	protected int				m_ParticleID;
	//! Orientation set by SetOrientation
	protected vector			m_Orientation;
	//! Orientation setting to be used by the effect when the Effect starts
	protected bool    			m_ForceRotationRelativeToWorld;
	//@}
		
	/** \name DEPRECATED
	*	Simply exist because of backwards compatibility, might have never been used
	*/
	//@{
	protected vector    		m_ParticleOrientation;
	protected Object 			m_Object;
	//@}
	
	
	
	/**
	\brief ctor
	*/
	void EffectParticle()
	{
		
	}
	
	/**
	\brief dtor
	*/
	void ~EffectParticle()
	{

	}
		
	/**
	\brief init
	*/
	override void InitEffect()
	{
		super.InitEffect();
		
		// Would be neat, but since particles are often already playing
		// BEFORE they are even registered as the particle for the Effect
		// Better to just keep that one I guess..
		// Event_OnStarted.Remove(Event_OnEffectStarted);
		
		// Will be called by the particle events
		Event_OnStopped.Remove(Event_OnEffectEnded);
	}
	
	
	/**
	\brief Override when getting debug information
	*/
	override string GetDebugName()
	{
		string identifier;
		if (GetParticle())
		{
			identifier = GetParticle().GetDebugNameNative();
		}
		else
		{
			identifier = "NO_PARTICLE";
		}
		
		return string.Format("%1:%2:%3", super.GetDebugName(), m_ParticleID, identifier);
	}
	
	/**
	\brief Validation whether an effect truly started playing or if the Effect should stop as none is present
		\note Override this when inheriting to create own validation check
		\note Is called from Event_OnStarted invoker after Event_OnStarted has been performed
	*/
	override void ValidateStart()
	{
		if (!GetParticle())
		{
			ErrorEx(string.Format("No Particle started playing, stopping EffectParticle: %1", GetDebugName()), ErrorExSeverity.WARNING);
			Stop();
		}
	}
	
	
	
	/** \name EffectType
		Information about what type of effect the Effect is, without the need for casting
	*/
	//@{
	
	/**
	\brief Get what type of effect the Effect is
		\return \p EffectType What type of effect the Effect is
	*/
	override EffectType GetEffectType()
	{
		return EffectType.PARTICLE;
	}
	
	/**
	\brief Check whether the Effect is EffectParticle without casting
		\return \p bool Whether the Effect is EffectParticle
	*/
	override bool IsParticle()
	{
		return true;
	}
	
	//@}
	
	
	
	/** \name Main particle
		Set or get the main particle which this Effect will manage
	*/
	//@{
	
	/**
	\brief Sets the main particle which this Effect will manage
		\param p \p Particle Main particle which this Effect will manage
	*/
	void SetParticle(Particle p)
	{
		// Unregister the events on the old
		if (m_ParticleObj)
		{
			ParticleEvents ope = m_ParticleObj.GetEvents();
			ope.Event_OnParticleStart.Remove(Event_OnEffectStarted);
			ope.Event_OnParticleStop.Remove(Event_OnEffectEnded);
		}
		
		// Assign the new main Particle
		m_ParticleObj = p;
		
		// Register the events on the new
		if (m_ParticleObj)
		{
			ParticleEvents npe = m_ParticleObj.GetEvents();
			npe.Event_OnParticleStart.Insert(Event_OnEffectStarted);
			// We will use Stop instead of End, as old particles were destroyed when they stopped
			// And this system kinda relies on that
			npe.Event_OnParticleStop.Insert(Event_OnEffectEnded);
		}
	}
	
	/**
	\brief Gets the main particle which this Effect is managing
		\return \p Particle Main particle which this Effect is managing
	*/
	Particle GetParticle()
	{
		return m_ParticleObj;
	}
	
	//@}
	
	
	
	/** \name Playback
	*	Methods to Play/Stop Effect
	*	Generally, SEffectManager.Play methods are used instead of Start
	*/
	//@{
	
	/**
	\brief Plays all elements this effect consists of
		\note Is called by SEffectManager.Play methods
	*/
    override void Start()
    {		
		if (m_ParticleID > 0)
		{
			vector pos = GetPosition();			
			vector ori = GetOrientation();
			
			if (m_ParentObject)
			{
				pos = GetLocalPosition();
				ori = GetAttachedLocalOri();
			}		
			
			SetParticle(ParticleManager.GetInstance().CreateParticle(m_ParticleID, pos, true, GetParent(), ori, IsParticleRotationRelativeToWorld()));
		}

		super.Start();
	}
	
	/**
	\brief Stops all elements this effect consists of
		\note Alternatively use SEffectManager.Stop( effect_id )
	*/
    override void Stop()
    {
		if ( GetParticle() )
		{
			GetParticle().Stop();
			SetParticle(null);
		}
		
		super.Stop();
    }
	
	//@}
	
	
	
	/** \name Attach
		Helper methods for attaching to prent
	*/
	//@{
	
	/**
	\brief Read Particle.AddAsChild
	*/
	void AttachTo(Object obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_rotation_to_world = false)
	{
		// Update the cached variables...
		SetParent(obj);
		SetLocalPosition(local_pos);
		SetAttachedLocalOri(local_ori);
		ForceParticleRotationRelativeToWorld(force_rotation_to_world);
		
		// Now attach it
		AddAsChild(obj, local_pos, local_ori, force_rotation_to_world);
	}
	
	/**
	\brief Helper method to attach to parent using stored settings
	*/
	void ReAttach()
	{
		// Skip the updating, as we are going to reuse what was set before
		AddAsChild( GetParent(), GetLocalPosition(), GetAttachedLocalOri(), IsParticleRotationRelativeToWorld());
	}
	
	/**
	\brief Helper method to attach to parent
	*/
	protected void AddAsChild(Object obj, vector local_pos, vector local_ori, bool force_rotation_to_world)
	{
		Particle p = GetParticle();		
		if (p)
		{
			p.AddAsChild(obj, local_pos, local_ori, force_rotation_to_world);
		}
	}
	
	//@}
	
	
	
	/** \name Events
		Various events that can be overriden for custom behaviour
	*/
	//@{
	
	/**
	\brief Event which just simply exists (DEPRECATED)
		\warning Never called or used
		\note Use Event_OnStarted instead
	*/
	void Event_OnPlayStart()
	{
		
	}
	
	/**
	\brief Event which just simply exists (DEPRECATED)
		\warning Never called or used
		\note Use Event_OnStarted instead
	*/
	void Event_OnPlayStarted()
	{

	}

	//@}
	
	
	
	/** \name Generic API
		Setters and getters for generic data and properties
	*/
	//@{
	
	/**
	\brief Sets the id of the particle to be used
		\note Only changes the cached variable, for immediate, use SetCurrentParticleID
		\param id \p int Particle ID registered in ParticleList
	*/
	void SetParticleID( int id )
    {
        m_ParticleID = id;
    }
	
	/**
	\brief Gets the id of the particle to be used
		\warning Only gets the cached variable, for immediate effect use GetCurrent variant
		\return \p int Particle ID registered in ParticleList
	*/
	int GetParticleID()
    {
        return m_ParticleID;
    }
	
	/**
	\brief Sets the id of the particle to be used
		\note Particle will not update immediately, but ParticleSource will
		\param id \p int Particle ID registered in ParticleList
	*/
	void SetCurrentParticleID( int id )
    {
        m_ParticleID = id;
		
		Particle p = GetParticle();
		if (p)
		{
			p.SetSource(id);
		}
    }
	
	/**
	\brief Gets the current id of the managed Particle
		\return \p int Particle ID registered in ParticleList
	*/
	int GetCurrentParticleID()
    {
		Particle p = GetParticle();
		if (p)
		{
			return p.GetParticleID();
		}
		else
		{
			return ParticleList.INVALID;
		}
    }
	
	/**
	\brief Set current parent of the managed Particle
		\param parent_obj \p Object The parent for the Particle
		\param updateCached \p bool Whether to update the cached variable
	*/
	override void SetCurrentParent( Object parent_obj, bool updateCached = true )
	{
		super.SetCurrentParent(parent_obj, updateCached);
		
		ReAttach();
	}
	
	/**
	\brief Get the current parent of the managed Particle
		\return \p Object The currrent parent of the Particle
	*/
	override Object GetCurrentParent()
	{
		Particle p = GetParticle();
			
		if (p)
			return Object.Cast(p.GetParent());
		else
			return super.GetParent();
	}
	
	/**
	\brief Set the current world position of the managed Particle
		\param pos \p vector The current world position for the Particle
		\param updateCached \p bool Whether to update the cached variable
	*/
	override void SetCurrentPosition( vector pos, bool updateCached = true )
	{
		super.SetCurrentPosition(pos, updateCached);
		
		Particle p = GetParticle();
		
		if (p)
			p.SetPosition(pos);
	}
	
	/**
	\brief Get the current world position of the managed Particle
		\return \p vector The current world position of the managed Particle
	*/
	override vector GetCurrentPosition()
	{
		Particle p = GetParticle();	
		
		if (p)
			return p.GetPosition();
		else
			return super.GetPosition();
	}
	
	/**
	\brief Set the current local position of the managed Particle
		\param pos \p vector The current local position for the managed Particle
		\param updateCached \p bool Whether to update the cached variable
	*/
	override void SetCurrentLocalPosition( vector pos, bool updateCached = true )
    {
		super.SetCurrentLocalPosition(pos, updateCached);
		
		Particle p = GetParticle();			
		if (p)
		{
			Object parent = GetParent();
			
			if (parent)
				ReAttach();
			else
				p.SetPosition(pos);
		}
    }

	/**
	\brief Get the current local position of the managed Particle
		\return \p vector The current local position of the managed Particle
	*/
	override vector GetCurrentLocalPosition()
	{
		Particle p = GetParticle(); 
		
		if (p)
		{
			Object parent = GetParent();
			
			if (parent)
				return parent.WorldToModel(p.GetPosition());
			else
				return p.GetPosition();
		}
		else
			return super.GetLocalPosition();
	}
	
	/**
	\brief Set orientation of the EffectParticle
		\warning Only sets the cached variable, for immediate effect use SetCurrent variant
		\param ori \p vector Orientation in degrees (yaw, pitch, roll)
	*/
	void SetOrientation( vector ori )
	{
		m_Orientation = ori;
	}
	
	/**
	\brief Get the orientation of the EffectParticle
		\warning Only gets the cached variable, for immediate effect use GetCurrent variant
		\return \p vector The orientation of EffectParticle
	*/
	vector GetOrientation()
	{
		return m_Orientation;
	}
	
	/**
	\brief Set the current orientation of the managed Particle
		\param ori \p vector Orientation in degrees (yaw, pitch, roll)
	*/
	void SetCurrentOrientation( vector ori, bool updateCached = true )
	{
		if ( updateCached)
			SetOrientation(ori);
		
		Particle p = GetParticle();	
				
		if (p)
			p.SetOrientation(ori);
	}
	
	/**
	\brief Get the current orientation of the managed Particle
		\return \p vector The current orientation of the managed Particle
	*/
	vector GetCurrentOrientation()
	{
		Particle p = GetParticle();	
		
		if (p)
			return p.GetOrientation();
		else
			return vector.Zero;
	}
	
	/**
	\brief Set orientation setting to be used by the effect when the Effect starts
		\warning Only caches it into a variable to be used by Start, does not live update when called afterwards
		\note There is no way to update this immediately
		\param state \p bool Whether to keep WS orientation when attaching it to parent
	*/
	void ForceParticleRotationRelativeToWorld(bool state)
	{
		m_ForceRotationRelativeToWorld = state;
	}
	
	/**
	\brief Get the orientation setting to be used by the effect when the Effect starts
		\warning Only gets the cached variable, for immediate effect use IsParticleCurrentRotationRelativeToWorld
		\return \p bool Whether to keep WS orientation when attaching it to parent
	*/
	bool IsParticleRotationRelativeToWorld()
	{
		Particle p = GetParticle();	
		
		if (p)
			return p.IsHierarchyPositionOnly();
		else
			return m_ForceRotationRelativeToWorld;
	}
	
	/**
	\brief Get the current orientation setting to be used by the managed Particle
		\return \p bool Whether the managed Particle is only updating position from parent
	*/
	bool IsParticleCurrentRotationRelativeToWorld()
	{
		Particle p = GetParticle();	
		
		if (p)
			return p.IsHierarchyPositionOnly();
		else
			return false;
	}
	
	//@}

	
	
	/** \name DEPRECATED
		Methods which exist for backwards compatibility and are no longer in use or have never been in use
	*/
	//@{
	
	/**
	\brief Was never called and probably should never be called
		\warning Emptied the functionality as it is relatively unsafe...
	*/
	void CheckLifeSpan()
	{
		/*
		if ( !m_ParticleObj )
		{
			delete this;
		}
		
		OnCheckUpdate();
		*/
	}

	void SetDecalOwner(Object o)
	{
		m_Object = o;
	}
	
	//@}
}