File size: 22,761 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/**
\brief Legacy way of using particles in the game
*	\note They work okay when just needing to play a particle once every once in a while
*		  But are extremely wasteful when it comes to playing multiple Particles at the same time
*/
class Particle : ParticleBase
{
	/** \name Generic data
		Generic data for the Particle
	*/
	//@{
	//! ID from ParticleList if assigned
	protected 	int		m_ParticleID;
	//! Approx. remaining lifetime of particle
	protected 	float	m_Lifetime;
	//! Whether this particle repeats
	protected 	bool 	m_IsRepeat;
	//! Whether this particle is queued for deletion
	private 	bool 	m_MarkedForDeletion;
	//@}
	
	/** \name Wiggle API
		Data for the wiggle API
	*/
	//@{
	//! Used for Wiggle API, to signal that wiggle API is currently doing work
	bool 				m_WiggleProcessing;
	//! Used for Wiggle API, to restore after unparenting
	bool 				m_ForceOrientationRelativeToWorld;
	//! Used for Wiggle API, to restore after unparenting
	vector 				m_DefaultOri;
	//! Used for Wiggle API, to restore after unparenting
	vector 				m_DefaultPos;
	//! Used for Wiggle API, to restore after unparenting
	vector 				m_DefaultWorldOri;
	//! Used for Wiggle API, to restore after unparenting
	vector 				m_DefaultWorldPos;
	
	//! Used for Wiggle API, Wiggle room [-m_MaxOriWiggle, m_MaxOriWiggle]
	float 				m_MaxOriWiggle;
	//! Used for Wiggle API, Interval for wiggling [0, m_MaxOriInterval[
	float 				m_MaxOriInterval;
	//! Used for Wiggle API, calls the Wiggle functionality
	ref Timer 			m_RandomizeOri;
	//@}
	
	//! Parent Object the Particle is child of
	protected Object 	m_ParentObject;
	//! The child object which contains the actual particle
	protected Object 	m_ParticleEffect;
	
	//! DEPRECATED
	protected int		m_PreviousFrame;
	//! DEPRECATED
	private vector 		m_GlobalPosPreviousFrame;
	//! DEPRECATED	
	static private const int MAX_EMITORS = 30;
	
	
	//! ctor
	void Particle()
	{
		ParticleInit();
	}
	
	//! Purely here so that it can be emptied in ParticleSource
	protected void ParticleInit()
	{
		SetFlags(EntityFlags.VISIBLE, true);
		SetEventMask(EntityEvent.INIT);
		SetEventMask(EntityEvent.FRAME);
	}	
	
	/** \name Create a particle (static)
		You can create a particle either at some position, or create it as a child on some object.
	*/
	//@{
	
	/**
	\brief Creates a particle emitter and attaches it on the given object
		\param particle_id \p int Particle ID registered in ParticleList
		\param parent_obj \p Object Instance on which this particle will be attached
		\param local_pos \p vector Attachment position local to the parent (Optional)
		\param local_ori \p vector Orientation local to the parent (Pitch, Yawn, Roll in degrees) (Optional)
		\param force_world_rotation \p bool Forces particle's orientation to rotate relative to the world and not with the object (Optional)
		\return \p Particle Created particle instance
	*/
	static Particle CreateOnObject( int particle_id, Object parent_obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_world_rotation = false )
	{
		if (!parent_obj)
			Error("ERROR when creating a particle! Parameter parent_obj is NULL!");
		
		vector global_pos = parent_obj.GetPosition();
		Particle p = CreateInWorld(particle_id, global_pos, Vector(0,0,0), force_world_rotation);
		p.AddAsChild(parent_obj, local_pos, local_ori, force_world_rotation);
		p.m_DefaultOri = local_ori;
		
		return p;
	}
	
	/**
	\brief Legacy function for backwards compatibility
	*/
	static Particle Create( int particle_id, Object parent_obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0" )
	{
		return CreateOnObject( particle_id, parent_obj, local_pos, local_ori);
	}
	
	/**
	\brief Creates a particle emitter on the given position
		\param particle_id \p int Particle ID registered in ParticleList
		\param global_pos \p Vector Position where the particel will be created
		\param global_ori \p vector Orientation (Pitch, Yawn, Roll in degrees) (Optional)
		\param force_world_rotation \p bool Forces particle's orientation to rotate relative to the world and not with the object (Optional)
		\return \p Particle Created particle instance
	*/
	static Particle CreateInWorld( int particle_id, vector global_pos, vector global_ori = "0 0 0", bool force_world_rotation = false )
	{
		Particle p = Particle.Cast( GetGame().CreateObjectEx("Particle", global_pos, ECE_LOCAL) );
		p.SetSource(particle_id);
		p.SetOrientation(global_ori);
		p.m_ForceOrientationRelativeToWorld = force_world_rotation;
		return p;
	}
	
	/**
	\brief Legacy function for backwards compatibility with 1.01 and below
	*/
	static Particle Create( int particle_id, vector global_pos, vector global_ori = "0 0 0" )
	{
		return CreateInWorld( particle_id, global_pos, global_ori );
	}
	
	//@}
	
	
	
	/** \name Static play on creation
		You can use the following Play(...) functions to create and activate a particle in 1 line of your script.
	*/
	//@{
	
	/**
	\brief Creates a particle emitter, attaches it on the given object and activates it
		\param particle_id \p int Particle ID registered in ParticleList
		\param parent_obj \p Object Instance on which this particle will be attached
		\param local_pos \p vector Attachment position local to the parent (Optional)
		\param local_ori \p vector Orientation local to the parent (Pitch, Yaw, Roll in degrees) (Optional)
		\param force_world_rotation \p bool Forces particle's orientation to rotate relative to the world and not with the object (Optional)
		\return \p Particle Created particle instance
	*/
	static Particle PlayOnObject( int particle_id, Object parent_obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_world_rotation = false )
	{
		Particle p = CreateOnObject(particle_id, parent_obj, local_pos, local_ori, force_world_rotation);
		p.PlayParticle();
		
		return p;
	}
	
	/**
	\brief Legacy function for backwards compatibility with 1.01 and below
	*/
	static Particle Play( int particle_id, Object parent_obj, vector local_pos = "0 0 0", vector local_ori = "0 0 0" )
	{
		return PlayOnObject( particle_id, parent_obj, local_pos, local_ori);
	}
	
	/**
	\brief Creates a particle emitter on the given position and activates it
		\param particle_id \p int Particle ID registered in ParticleList
		\param global_pos \p Vector Position where the particel will be created
		\return \p Particle Created particle instance
	*/
	static Particle PlayInWorld( int particle_id, vector global_pos)
	{
		Particle p = CreateInWorld(particle_id, global_pos);
		p.PlayParticle();
		
		return p;
	}

	/**
	\brief Legacy function for backwards compatibility with 1.01 and below
	*/
	static Particle Play( int particle_id, vector global_pos)
	{
		return PlayInWorld( particle_id, global_pos);
	}
	
	//@}
	
	
	
	/** \name Playback
 		Methods regarding playing/stopping of particle
	*/
	//@{
	
	/**
	\brief Method to tell the particle to start playing
		\param particle_id \p int Particle ID registered in ParticleList to start playing
	*/
	override void PlayParticle(int particle_id = -1)
	{
		PlayParticleEx(particle_id, 0);
	}
	
	/**
	\brief Method to tell the particle to start playing
		\note The parameter to set the ID will only work when the particle is not already playing
		\param particle_id \p int Particle ID registered in ParticleList to start playing
		\param flags \p int Flags to pass to the playing (None on this level)
		\return \p bool Whether the particle successfully started
	*/
	override bool PlayParticleEx(int particle_id = -1, int flags = 0)
	{
		if ( particle_id > -1 )
		{
			SetSource(particle_id);
		}
		
		OnParticleStart();

		UpdateState();
		
		return true;
	}
	
	/**
	\brief Legacy function for backwards compatibility with 1.01 and below
		\param particle_id \p int Particle ID registered in ParticleList to start playing
	*/
	void Play(int particle_id = -1)
	{
		PlayParticle(particle_id);
	}
	
	/**
	\brief Method to tell the particle to stop playing
		\note No flags available for Particle
		\note Emitors are automatically removed later when its particle count is 0
		\param flags \p int Flags to pass to the stopping (None on this level)
		\return \p bool Whether the particle successfully stopped
	*/
	override bool StopParticle(int flags = 0)
	{
		OnParticleStop();		
		
		// Without the following we might get an error when a particle parent is despawned client-side.
		Object parent = Object.Cast( GetParent() );
		if ( parent && !ToDelete())
		{
			vector world_pos = GetPosition();
			parent.RemoveChild(this);
			SetPosition(world_pos);
		}
		
		UpdateState();
		
		return true;
	}
	
	/**
	\brief Legacy function for backwards compatibility with 1.14 and below
	*/
	void Stop()
	{
		StopParticle();
	}
	
	//@}
	
	
	
	/** \name Properties and state
		Obtain information or set properties regarding the state of the Particle
	*/
	//@{
	
	/**
	\brief Sets particle id
		\note Does not work at runtime, particle object needs to be destroyed and then Particle needs to play again
		\param particle_id \p int Particle ID registered in ParticleList to start playing
	*/
	void SetSource(int particle_id)
	{
		m_ParticleID = particle_id;
	}
	
	/**
	\brief Gets particle id
		\note This is not necessarily the CURRENT particle
			* As one can use SetSource while the Particle is still playing
			* But that will not change the particle before Particle is played again
		\return \p int The last set Particle ID registered in ParticleList
	*/
	int GetParticleID()
	{
		return m_ParticleID;
	}
	
	/**
	\brief Returns direct particle effect entity which is usually handled by this class 'Particle' if there is one
		\note Is a child of this Particle
		\return \p Object The Object with the particle component or null
	*/
	Object GetDirectParticleEffect()
	{
		return m_ParticleEffect;
	}
	
	/**
	\brief Returns the parent of this Particle if there is one
		\return \p Object The registered parent or null
	*/
	Object GetParticleParent()
	{
		return m_ParentObject;
	}
	
	/**
	\brief Returns if there is any particle active
		\return \p bool Whether there is any particle active
	*/
	bool HasActiveParticle()
	{
		if (m_ParticleEffect)
		{
			return ParticleHasActive(m_ParticleEffect);
		}
		
		return false;
	}
	
	/**
	\brief Returns the total count of active particles in all emitors
		\note Internally does a sum, HasActiveParticle is better for a quick check
		\return \p int Total count of active particles
	*/
	int GetParticleCount()
	{
		if (m_ParticleEffect)
		{
			return ParticleGetCount(m_ParticleEffect);
		}
		
		return 0;
	}
	
	/**
	\brief Returns whether there is a repeating particle
		\return \p bool whether there is a repeating particle
	*/
	bool IsRepeat()
	{
		if (m_ParticleEffect)
		{
			bool repeat = false;
			
			int emitors = GetParticleEmitorCount(m_ParticleEffect);
			
			for (int i = 0; i < emitors; ++i)
			{
				GetParticleParm(m_ParticleEffect, i, EmitorParam.REPEAT, repeat);
				
				if (repeat)
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	\brief Returns the approx. max lifetime
		\return \p float The largest lifetime sum among the emitors
	*/
	float GetMaxLifetime()
	{
		float lifetime_return = 0;
		
		if (m_ParticleEffect)
		{
			float lifetime_min = 0;
			float lifetime_random = 0;			
			float effect_time = 0;
			
			float lifetime_sum = 0;
			
			int emitors = GetParticleEmitorCount(m_ParticleEffect);
			
			for (int i = 0; i < emitors; ++i)
			{
				GetParticleParm(m_ParticleEffect, i, EmitorParam.LIFETIME, lifetime_min);
				GetParticleParm(m_ParticleEffect, i, EmitorParam.LIFETIME_RND, lifetime_random);
				GetParticleParm(m_ParticleEffect, i, EmitorParam.EFFECT_TIME, effect_time);
				
				lifetime_sum = lifetime_min + lifetime_random + effect_time;
				
				if ( lifetime_sum > lifetime_return )
				{
					lifetime_return = lifetime_sum;
				}
			}
		}
		
		return lifetime_return;
	}
	
	//@}
	
	
	
	/** \name Misc Particle specific helpers
		Functionality specific for Particle
	*/
	//@{
	
	/**
	\brief Creates/Destroys ParticleEffect child according to current state
		\note Is called from Play/Stop methods
	*/
	protected void UpdateState()
	{
		if ( m_IsPlaying == false && m_ParticleEffect)
		{
			DestroyParticleEffect();
		}
		else if ( m_IsPlaying == true && m_ParticleEffect == null )
		{
			CreateParticleEffect();
		}
	}
	
	/**
	\brief Creates ParticleEffect child, called from UpdateState
	*/
	private void CreateParticleEffect()
	{
		if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
		{
			string fullPath = ParticleList.GetParticleFullPath(m_ParticleID);
			if (fullPath == "")
			{
				ErrorEx("Could not play Particle as there is no valid particle id assigned.");
				m_IsPlaying = false;
				return;
			}
			
			if ( m_ParticleEffect == null )
			{
				m_ParticleEffect = GetGame().CreateObjectEx("#particlesourceenf", vector.Zero, ECE_LOCAL); // particle source must be lowercase!
			}
			
			AddChild(m_ParticleEffect, -1, m_ForceOrientationRelativeToWorld);
			
			vobject vobj = GetObject( fullPath );			
			m_ParticleEffect.SetObject(vobj, "");
			ReleaseObject(vobj);
			
			m_IsRepeat = IsRepeat();
			m_Lifetime = GetMaxLifetime();
		}
	}
	
	/**
	\brief Destroys ParticleEffect child, called from UpdateState
		\note Does not destroy it immediately
			* As it simply nulls the lifetime
			* Then it will be cleaned up by EOnFrame eventually
	*/
	private void DestroyParticleEffect()
	{
		if ( m_ParticleEffect && GetGame() )
		{
			SetParameter(-1, EmitorParam.LIFETIME, 0);
			SetParameter(-1, EmitorParam.LIFETIME_RND, 0);
			SetParameter(-1, EmitorParam.REPEAT, 0);
			
			m_IsRepeat = false;
		}
	}
	
	/**
	\brief OnFrame update event decrementing the stored approx. lifetime and checking for deletion
	*/
	override void EOnFrame(IEntity other, float timeSlice)
	{		
		m_Lifetime -= timeSlice;
		OnCheckAutoDelete();
	}
	
	/**
	\brief Creates ParticleEffect child, called from UpdateState
	*/
	void OnCheckAutoDelete()
	{
		if (m_Lifetime <= 0)
		{
			if (!m_MarkedForDeletion)
			{
				m_IsRepeat = IsRepeat(); // It is possible that the REPEAT flag was changed during lifetime, so it needs to be checked again.
				
				if ( m_IsRepeat )
				{
					m_Lifetime = GetMaxLifetime();
				}
				else
				{
					OnParticleStop();
					
					if ( GetParticleCount() == 0 )
					{
						m_MarkedForDeletion = true;
						OnToDelete();
						OnParticleEnd();
					}
				}
			}
			else
			{
				if ( m_MarkedForDeletion )
				{
					if (m_ParticleEffect)
					{
						m_ParticleEffect.Delete();
						m_ParticleEffect = null;
					}
					
					Delete();
				}
			}
		}
	}
	
	/**
	\brief Called before deletion from OnCheckAutoDelete
	*/
	private void OnToDelete()
	{

	}
	
	//@}
	
	
	
	/** \name Misc
 		Various helpers
	*/
	//@{
	
	/**
	\brief Attaches this particle onto some object. If null value is provided then the particle will be detached from the current parent.
		\note Due to the members being filled in, AddChild/RemoveChild cannot be used with Particle when using Wiggle
		\param parent \p Object Parent onto which this particle will be attached
		\param local_pos \p vector Attachment position local to the parent (optional)
		\param local_ori \p vector Orientation local to the parent (Pitch, Yawn, Roll in degrees) (Optional)
		\param force_rotation_to_world \p bool Force rotation to be in WS (Optional)
	*/
	void AddAsChild(Object parent, vector local_pos = "0 0 0", vector local_ori = "0 0 0", bool force_rotation_to_world = false)
	{
		if (ToDelete())
			return;
		
		if (parent)
		{
			// AddAsChild method is sometimes called from a timer.
			// Due to that it is necesarry to use ToDelete() here to check if the parent object is flagged for deletion or not on client,
			// because sometimes this code is executed before the parent's destructor from where this would normally be handled.
			if (!parent.ToDelete())
			{
				SetPosition(local_pos);
				SetOrientation(local_ori);
				m_ParentObject = parent;
				m_DefaultPos = local_pos;
				m_ForceOrientationRelativeToWorld = force_rotation_to_world;
				
				if (m_ParticleEffect)
					AddChild(m_ParticleEffect, -1, m_ForceOrientationRelativeToWorld);
				
				parent.AddChild(this, -1, false);
			}
		}
		else
		{
			if (m_ParentObject && !m_ParentObject.ToDelete())
			{
				m_ParentObject.RemoveChild(this, true);
				m_ParentObject = null;
			}
		}
	}
	
	//@}
	
	
	
	/** \name Parameter API
 		Helpful methods for getting or setting parameters
	*/
	//@{
	
	/**
	\brief Set the value of a parameter of all emitors in the particle
		\param parameter \p int The parameter to apply the new value to (enum EmitorParam)
		\param value \p float The value to apply
	*/
	void SetParticleParam(int parameter_id, float value )
	{
		if (!m_ParticleEffect)
			return;
		
		SetParticleParm(m_ParticleEffect, -1, parameter_id, value);
	}
	
	/**
	\brief Set the value of a parameter of an emitor in the particle
		\param emitter \p int The emitter to apply the new value to, -1 for all emitter
		\param parameter \p int The parameter to apply the new value to (enum EmitorParam)
		\param value \p float The value to apply
	*/
	void SetParameter(int emitter, int parameter, float value)
	{
		if (!m_ParticleEffect)
			return;
		
		SetParticleParm(m_ParticleEffect, emitter, parameter, value);
	}
	
	/**
	\brief Get the value of a parameter of an emitor in the particle
		\param emitter \p int The emitor to get the value from
		\param parameter \p int The parameter to get the value from (enum EmitorParam)
		\param value \p float The value
	*/
	void GetParameter(int emitter, int parameter, out float value)
	{
		if (!m_ParticleEffect)
			return;
		
		GetParticleParm(m_ParticleEffect, emitter, parameter, value);
	}
	
	/**
	\brief Get the value of a parameter of an emitor in the particle
		\param emitter \p int The emitor to get the value from
		\param parameter \p int The parameter to get the value from (enum EmitorParam)
		\return \p float The value
	*/
	float GetParameterEx(int emitter, int parameter)
	{
		if (!m_ParticleEffect)
			return 0;
		
		float value;
		GetParticleParm(m_ParticleEffect, emitter, parameter, value);
		return value;
	}
	
	/**
	\brief Scales the given parameter on all emitors relatively to their ORIGINAL value.
		\param parameter_id \p int The parameter to adjust (enum EmitorParam)
		\param coef \p float The multiplier to apply
	*/
	void ScaleParticleParamFromOriginal(int parameter_id, float coef )
	{
		if (!m_ParticleEffect)
			return;
		
		int emitors = GetParticleEmitorCount(m_ParticleEffect);			
		for (int i = 0; i < emitors; ++i)
		{
			float value;
			GetParticleParmOriginal(m_ParticleEffect, i, parameter_id, value);
			SetParticleParm(m_ParticleEffect, i, parameter_id, value * coef);
		}
	}
	
	/**
	\brief Scales the given parameter on all emitors relatively to their CURRENT value.
		\param parameter_id \p int The parameter to adjust (enum EmitorParam)
		\param coef \p float The multiplier to apply
	*/
	void ScaleParticleParam(int parameter_id, float coef )
	{
		if (!m_ParticleEffect)
			return;
		
		int emitors = GetParticleEmitorCount(m_ParticleEffect);	
		for (int i = 0; i < emitors; ++i)
		{
			float value;
			GetParticleParm(m_ParticleEffect, i, parameter_id, value);
			SetParticleParm(m_ParticleEffect, i, parameter_id, value * coef);
		}
	}
	
	/**
	\brief Increments the value of the given parameter relatively from the ORIGINAL value.
		\note It's a simple sum, so negative value decrements
		\param parameter_id \p int The parameter to adjust (enum EmitorParam)
		\param value \p float The value to sum
	*/
	void IncrementParticleParamFromOriginal(int parameter_id, float value )
	{
		if (!m_ParticleEffect)
			return;
		
		int emitors = GetParticleEmitorCount(m_ParticleEffect);	
		for (int i = 0; i < emitors; ++i)
		{
			float param;
			GetParticleParmOriginal(m_ParticleEffect, i, parameter_id, param);
			SetParticleParm(m_ParticleEffect, i, parameter_id, param + value);
		}
	}
	
	/**
	\brief Increments the value of the given parameter relatively from the CURRENT value.
		\note It's a simple sum, so negative value decrements
		\param parameter_id \p int The parameter to adjust (enum EmitorParam)
		\param value \p float The value to sum
	*/
	void IncrementParticleParam(int parameter_id, float value )
	{
		if (!m_ParticleEffect)
			return;
		
		int emitors = GetParticleEmitorCount(m_ParticleEffect);	
		for (int i = 0; i < emitors; ++i)
		{
			float param;
			GetParticleParm(m_ParticleEffect, i, parameter_id, param);
			SetParticleParm(m_ParticleEffect, i, parameter_id, param + value);
		}
	}
	
	//@}
	
	
	
	/** \name Wiggle API
 		Settings to make the Particle wiggle
	*/
	//@{
	
	/**
	\brief Checks if particle is currently wiggling
	*/
	bool IsWiggling()
	{
		return m_RandomizeOri && m_RandomizeOri.IsRunning();
	}
	
	/**
	\brief Makes the particle change direction by random_angle every random_interval seconds.
		\note This does not actually work on Particle with no parent, it should on ParticleSource
		\note Calling SetWiggle(0,0) will effectively stop all wiggle functionality
		\param random_angle \p float Will be the range [-random_angle, random_angle[ to wiggle between
		\param random_interval \p float Will be the time range [0, random_interval] to wiggle next time
	*/
	void SetWiggle(float random_angle, float random_interval)
	{	
		if ( random_angle != 0 || random_interval != 0 )
		{	
			m_MaxOriWiggle = random_angle;
			m_MaxOriInterval = random_interval;
			
			if ( !m_RandomizeOri )
				m_RandomizeOri = new Timer( CALL_CATEGORY_GAMEPLAY );
			
			if ( !m_RandomizeOri.IsRunning() ) // Makes sure the timer is NOT running already
				m_RandomizeOri.Run( Math.RandomFloat(0, m_MaxOriInterval) , this, "RandomizeOrientation", null, false);
		}
		else
		{
			StopWiggle();
		}
	}
	
	/**
	\brief Stops randomized wiggle
	*/
	void StopWiggle()
	{			
		if ( m_RandomizeOri )
		{
			m_RandomizeOri.Stop();
		}
		
		m_MaxOriWiggle = 0;
		m_MaxOriInterval = 0;
	}
	
	/**
	\brief Randomizes a new orientation and applies it
	*/
	void RandomizeOrientation()
	{
		m_WiggleProcessing = true;
		
		if (m_ParentObject)
		{
			if ( !m_RandomizeOri.IsRunning() )
			{
				m_RandomizeOri.Run( Math.RandomFloat(0, m_MaxOriInterval) , this, "RandomizeOrientation", NULL, false);
			}
			
			Object old_parent = m_ParentObject;
			AddAsChild( null );
			AddAsChild( old_parent, m_DefaultPos, m_DefaultOri + RandWiggleVector() );
		}
		
		m_WiggleProcessing = false;
	}
	
	/**
	\brief Helper to get a randomized wiggle vector
	*/
	protected vector RandWiggleVector()
	{
		return Vector( RandWiggleFloat(), RandWiggleFloat(), RandWiggleFloat() );
	}
	
	/**
	\brief Helper to get a randomized wiggle float value
	*/
	protected float RandWiggleFloat()
	{
		return Math.RandomFloatInclusive(-m_MaxOriWiggle, m_MaxOriWiggle);
	}
	
	//@}
}