OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ParticleValueProvider Class Reference

#include <C4Particles.h>

Public Member Functions

bool IsConstant () const
 
bool IsRandom () const
 
 C4ParticleValueProvider ()
 
 ~C4ParticleValueProvider ()
 
 C4ParticleValueProvider (const C4ParticleValueProvider &other)
 
C4ParticleValueProvideroperator= (const C4ParticleValueProvider &other)
 
void Floatify (float denominator)
 
void Set (const C4Value &value)
 
void Set (const C4ValueArray &fromArray)
 
void Set (float to)
 
float GetValue (C4Particle *forParticle)
 

Detailed Description

Definition at line 109 of file C4Particles.h.

Constructor & Destructor Documentation

C4ParticleValueProvider::C4ParticleValueProvider ( )
inline

Definition at line 159 of file C4Particles.h.

159  :
160  startValue(0.f), endValue(0.f), currentValue(0.f), rerollInterval(0), smoothing(0), keyFrameCount(0), valueFunction(nullptr), isConstant(true), floatValueToChange(nullptr), typeOfValueToChange(VAL_TYPE_FLOAT)
161  { }
C4ParticleValueProvider::~C4ParticleValueProvider ( )
inline

Definition at line 162 of file C4Particles.h.

163  {
164  for (auto &child : childrenValueProviders)
165  delete child;
166  }
C4ParticleValueProvider::C4ParticleValueProvider ( const C4ParticleValueProvider other)
inline

Definition at line 167 of file C4Particles.h.

167 { *this = other; }

Member Function Documentation

void C4ParticleValueProvider::Floatify ( float  denominator)

Definition at line 349 of file C4Particles.cpp.

Referenced by C4ParticleSystem::Create().

350 {
351  assert (denominator != 0.f && "Trying to floatify C4ParticleValueProvider with denominator of 0");
352 
353  if (valueFunction == &C4ParticleValueProvider::Direction)
354  {
355  FloatifyParameterValue(&C4ParticleValueProvider::startValue, 1000.f);
356  return;
357  }
358 
359  FloatifyParameterValue(&C4ParticleValueProvider::startValue, denominator);
360  FloatifyParameterValue(&C4ParticleValueProvider::endValue, denominator);
361  FloatifyParameterValue(&C4ParticleValueProvider::currentValue, denominator);
362 
363  // special treatment for keyframes
364  if (valueFunction == &C4ParticleValueProvider::KeyFrames)
365  {
366  for (size_t i = 0; i < keyFrameCount; ++i)
367  {
368  FloatifyParameterValue(0, 1000.f, 2 * i); // even numbers are the time values
369  FloatifyParameterValue(0, denominator, 2 * i + 1); // odd numbers are the actual values
370  }
371  }
372  else if (valueFunction == &C4ParticleValueProvider::Speed || valueFunction == &C4ParticleValueProvider::Wind || valueFunction == &C4ParticleValueProvider::Gravity)
373  {
374  FloatifyParameterValue(&C4ParticleValueProvider::speedFactor, 1000.0f);
375  }
376  else if (valueFunction == &C4ParticleValueProvider::Step)
377  {
378  FloatifyParameterValue(&C4ParticleValueProvider::maxValue, denominator);
379  }
380  else if (valueFunction == &C4ParticleValueProvider::Sin || valueFunction == &C4ParticleValueProvider::Cos)
381  {
382  FloatifyParameterValue(&C4ParticleValueProvider::parameterValue, 1.0f);
383  FloatifyParameterValue(&C4ParticleValueProvider::maxValue, denominator);
384  }
385 }

Here is the caller graph for this function:

float C4ParticleValueProvider::GetValue ( C4Particle forParticle)

Definition at line 387 of file C4Particles.cpp.

Referenced by C4ParticleSystem::Create(), C4Particle::Exec(), and C4Particle::SetPosition().

388 {
389  UpdateChildren(forParticle);
390  return (this->*valueFunction)(forParticle);
391 }

Here is the caller graph for this function:

bool C4ParticleValueProvider::IsConstant ( ) const
inline

Definition at line 157 of file C4Particles.h.

Referenced by C4Particle::Exec().

157 { return isConstant; }

Here is the caller graph for this function:

bool C4ParticleValueProvider::IsRandom ( ) const
inline

Definition at line 158 of file C4Particles.h.

158 { return valueFunction == &C4ParticleValueProvider::Random; }
C4ParticleValueProvider & C4ParticleValueProvider::operator= ( const C4ParticleValueProvider other)

Definition at line 210 of file C4Particles.cpp.

References C4Particle::C4ParticleValueProvider.

211 {
212  startValue = other.startValue;
213  endValue = other.endValue;
214  currentValue = other.currentValue;
215  rerollInterval = other.rerollInterval;
216  smoothing = other.smoothing;
217  valueFunction = other.valueFunction;
218  isConstant = other.isConstant;
219  keyFrameCount = other.keyFrameCount;
220  rng = other.rng;
221 
222  if (keyFrameCount > 0)
223  {
224  keyFrames.reserve(2 * keyFrameCount);
225  keyFrames.assign(other.keyFrames.begin(), other.keyFrames.end());
226  }
227 
228  typeOfValueToChange = other.typeOfValueToChange;
229  switch (typeOfValueToChange)
230  {
231  case VAL_TYPE_FLOAT:
232  floatValueToChange = other.floatValueToChange;
233  break;
234  case VAL_TYPE_INT:
235  intValueToChange = other.intValueToChange;
236  break;
237  case VAL_TYPE_KEYFRAMES:
238  keyFrameIndex = other.keyFrameIndex;
239  break;
240  default:
241  assert (false && "Trying to copy C4ParticleValueProvider with invalid value type");
242  break;
243  }
244 
245  // copy the other's children, too
246  for (std::vector<C4ParticleValueProvider*>::const_iterator iter = other.childrenValueProviders.begin(); iter != other.childrenValueProviders.end(); ++iter)
247  {
248  childrenValueProviders.push_back(new C4ParticleValueProvider(**iter)); // custom copy constructor usage
249  }
250  return (*this);
251 }
void C4ParticleValueProvider::Set ( const C4Value value)

Definition at line 685 of file C4Particles.cpp.

References C4Value::getArray(), and C4Value::getInt().

686 {
687  C4ValueArray *valueArray= value.getArray();
688 
689  if (valueArray != 0)
690  Set(*valueArray);
691  else
692  Set((float)value.getInt());
693 }
C4ValueArray * getArray() const
Definition: C4Value.h:118
int32_t getInt() const
Definition: C4Value.h:112
void Set(const C4Value &value)

Here is the call graph for this function:

void C4ParticleValueProvider::Set ( const C4ValueArray fromArray)

Definition at line 550 of file C4Particles.cpp.

References C4PV_Const, C4PV_Cos, C4PV_Direction, C4PV_Gravity, C4PV_KeyFrames, C4PV_Linear, C4PV_Random, C4PV_Sin, C4PV_Speed, C4PV_Step, C4PV_Wind, C4V_Nil, C4ValueArray::GetSize(), and UnsyncedRandom().

551 {
552  startValue = endValue = 1.0f;
553  valueFunction = &C4ParticleValueProvider::Const;
554 
555  size_t arraySize = (size_t) fromArray.GetSize();
556  if (arraySize < 2) return;
557 
558  int type = fromArray[0].getInt();
559 
560  switch (type)
561  {
562  case C4PV_Const:
563  if (arraySize >= 2)
564  {
565  SetType(C4PV_Const);
566  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::startValue);
567  }
568  break;
569 
570  case C4PV_Linear:
571  if (arraySize >= 3)
572  {
573  SetType(C4PV_Linear);
574  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::startValue);
575  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::endValue);
576  }
577  break;
578  case C4PV_Random:
579  if (arraySize >= 3)
580  {
581  SetType(C4PV_Random);
582  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::startValue);
583  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::endValue);
584  if (arraySize >= 4 && fromArray[3].GetType() != C4V_Type::C4V_Nil)
585  SetParameterValue(VAL_TYPE_INT, fromArray[3], 0, &C4ParticleValueProvider::rerollInterval);
586  if (arraySize >= 5 && fromArray[4].GetType() != C4V_Type::C4V_Nil)
587  {
588  // We don't need the seed later on, but SetParameterValue won't accept local
589  // variables. Use an unrelated member instead which is reset below.
590  SetParameterValue(VAL_TYPE_INT, fromArray[4], 0, &C4ParticleValueProvider::alreadyRolled);
591  rng.seed(alreadyRolled);
592  }
593  else
594  {
595  rng.seed(UnsyncedRandom());
596  }
597  alreadyRolled = 0;
598  }
599  break;
600  case C4PV_Direction:
601  if (arraySize >= 2)
602  {
603  SetType(C4PV_Direction);
604  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::startValue);
605  }
606  break;
607  case C4PV_Step:
608  if (arraySize >= 4)
609  {
610  SetType(C4PV_Step);
611  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::startValue);
612  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::currentValue);
613  SetParameterValue(VAL_TYPE_FLOAT, fromArray[3], &C4ParticleValueProvider::delay);
614  SetParameterValue(VAL_TYPE_FLOAT, fromArray[4], &C4ParticleValueProvider::maxValue);
615  if (delay == 0.f) delay = 1.f;
616  }
617  break;
618  case C4PV_KeyFrames:
619  if (arraySize >= 5)
620  {
621  SetType(C4PV_KeyFrames);
622  SetParameterValue(VAL_TYPE_INT, fromArray[1], 0, &C4ParticleValueProvider::smoothing);
623  keyFrames.resize(arraySize + 4 - 1); // 2*2 additional information floats at the beginning and ending, offset the first array item, though
624 
625  keyFrameCount = 0;
626  const size_t startingOffset = 2;
627  size_t i = startingOffset;
628  for (; i < arraySize; ++i)
629  {
630  SetParameterValue(VAL_TYPE_KEYFRAMES, fromArray[(int32_t)i], 0, 0, 2 + i - startingOffset);
631  }
632  keyFrameCount = (i - startingOffset) / 2 + 2;
633 
634  startValue = keyFrames[2 + 1];
635  endValue = keyFrames[2 * keyFrameCount - 1];
636 
637  // add two points for easier interpolation at beginning and ending
638  keyFrames[0] = -500.f;
639  keyFrames[1] = keyFrames[2 + 1];
640  keyFrames[2 * keyFrameCount - 2] = 1500.f;
641  keyFrames[2 * keyFrameCount - 1] = keyFrames[keyFrameCount - 1 - 2];
642 
643  }
644  break;
645  case C4PV_Sin: // fallthrough
646  case C4PV_Cos:
647  if (arraySize >= 3)
648  {
649  SetType(static_cast<C4ParticleValueProviderID> (type)); // Sin(parameterValue) * maxValue + startValue
650  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::parameterValue);
651  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::maxValue);
652  SetParameterValue(VAL_TYPE_FLOAT, fromArray[3], &C4ParticleValueProvider::startValue);
653  }
654  break;
655  case C4PV_Speed:
656  if (arraySize >= 3)
657  {
658  SetType(C4PV_Speed);
659  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::speedFactor);
660  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::startValue);
661  }
662  break;
663  case C4PV_Wind:
664  if (arraySize >= 3)
665  {
666  SetType(C4PV_Wind);
667  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::speedFactor);
668  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::startValue);
669  }
670  break;
671  case C4PV_Gravity:
672  if (arraySize >= 3)
673  {
674  SetType(C4PV_Gravity);
675  SetParameterValue(VAL_TYPE_FLOAT, fromArray[1], &C4ParticleValueProvider::speedFactor);
676  SetParameterValue(VAL_TYPE_FLOAT, fromArray[2], &C4ParticleValueProvider::startValue);
677  }
678  break;
679  default:
680  throw C4AulExecError("invalid particle value provider supplied");
681  break;
682  }
683 }
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
int32_t GetSize() const
Definition: C4ValueArray.h:36

Here is the call graph for this function:

void C4ParticleValueProvider::Set ( float  to)

Definition at line 695 of file C4Particles.cpp.

References C4PV_Const.

696 {
697  SetType(C4PV_Const);
698  startValue = endValue = to;
699 }

The documentation for this class was generated from the following files: