Brunot
Loading...
Searching...
No Matches
ParticleSpawner.h
Go to the documentation of this file.
1
13// ____ __ __ __
14// /\__ _\/\ \ /\ \/\ \
15// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
16// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
17// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
18// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
19// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
20
21
22#pragma once
23#include <random>
24#include <vector>
25#include <algorithm>
26#include <utility>
27#include <type_traits>
28
29#include "Graphics/Particle.h"
31
32struct Particle;
33
43{
47 enum type
48 {
49 sphere = 1,
50 cone = 2,
51 orbit = 3,
53 };
54
55public:
56 ParticleSpawner() = default;
57 virtual ~ParticleSpawner() = default;
58
66 virtual auto activate(const AffineMatrix& parentTransform, const Vector4D& scale,
67 float rot) -> std::vector<Particle>;
68
70 virtual auto particleSpawnMenu() -> void;
71
73 auto particleUpdateMenu() -> void;
74
75 auto setParticleColor(const Vector4D& vec) -> void;
76 auto setParticleEndColor(const Vector4D& vec) -> void;
77
78 friend auto to_json(json& j, const ParticleSpawner& n) -> void;
79 friend auto from_json(const json& j, ParticleSpawner& n) -> void;
80
81protected:
91
93 unsigned spawnCount = 1u;
94
95 // bools for whether certain values of the particles should have random initial values
96 // all randomization bools follow a naming convention to make macros for the editor work.
97
98 bool positionRandomize = false;
99 bool velocityRandomize = false;
100 bool scaleRandomize = false;
101 bool rotationRandomize = false;
104 bool colorRandomize = false;
106 bool lifetimeRandomize = false;
107
108private:
109 // random engine
110 std::ranlux24_base randSeed{std::random_device()()};
111
121 template <typename T>
122 auto randIf(bool& isRandom, const T& min, const T& max) -> T
123 {
124 if (isRandom)
125 {
126 return randBetween(min, max);
127 }
128 return max;
129 }
130
138 template <typename T>
139 auto randBetween(const T& lhs, const T& rhs) -> T
140 {
141 // we need to use a different distribution based on ints and floats
142 if constexpr (std::is_integral_v<T>)
143 {
144 using std::max;
145 using std::min;
146 std::uniform_int_distribution<> range(T(0), max(lhs, rhs) - min(lhs, rhs));
147 return range(randSeed) + min(lhs, rhs);
148 }
149 else
150 {
151 using std::max;
152 using std::min;
153 std::uniform_real_distribution<float> range{T(0), max(lhs, rhs) - min(lhs, rhs)};
154 return range(randSeed) + min(lhs, rhs);
155 }
156 }
157
164 template <>
165 auto randBetween<Vector4D>(const Vector4D& lhs, const Vector4D& rhs) -> Vector4D
166 {
167 using std::max;
168 using std::min;
170 std::uniform_real_distribution<float> rangeX(0.f, max(lhs.x(), rhs.x()) - min(lhs.x(), rhs.x()));
171 std::uniform_real_distribution<float> rangeY(0.f, max(lhs.y(), rhs.y()) - min(lhs.y(), rhs.y()));
172 std::uniform_real_distribution<float> rangeZ(0.f, max(lhs.z(), rhs.z()) - min(lhs.z(), rhs.z()));
173 std::uniform_real_distribution<float> rangeW(0.f, max(lhs.w(), rhs.w()) - min(lhs.w(), rhs.w()));
174
175 result.x() = rangeX(randSeed) + min(lhs.x(), rhs.x());
176 result.y() = rangeY(randSeed) + min(lhs.y(), rhs.y());
177 result.z() = rangeZ(randSeed) + min(lhs.z(), rhs.z());
178 result.w() = rangeW(randSeed) + min(lhs.w(), rhs.w());
179 return result;
180 }
181
182
183};
static FMOD_RESULT result
Definition AudioObject.cpp:27
nlohmann::json json
Definition Json.cpp:19
public object interface for a particle, to be passed to Particle system and for saving and loading
type
a type enum that is a remnant of the original plan for subclasses.
Definition ParticleSpawner.h:48
@ orbit
Definition ParticleSpawner.h:51
@ cone
Definition ParticleSpawner.h:50
@ singleLoopingParticle
Definition ParticleSpawner.h:52
@ sphere
Definition ParticleSpawner.h:49
bool lifetimeRandomize
Definition ParticleSpawner.h:106
Particle maxParticle
holds the initial state for a particle when it is spawned.
Definition ParticleSpawner.h:86
auto randBetween(const T &lhs, const T &rhs) -> T
returns a random number between 2 numbers
Definition ParticleSpawner.h:139
auto particleUpdateMenu() -> void
draws the menu for the particle editor that pertains to the
Definition ParticleSpawner.cpp:154
ParticleSpawner()=default
friend auto to_json(json &j, const ParticleSpawner &n) -> void
Definition ParticleSpawner.cpp:250
auto randIf(bool &isRandom, const T &min, const T &max) -> T
randomizes between 2 values, if a boolean is true if the boolean isn't true, simply return max (even ...
Definition ParticleSpawner.h:122
bool startFrameRandomize
Definition ParticleSpawner.h:105
auto setParticleColor(const Vector4D &vec) -> void
Definition ParticleSpawner.cpp:238
bool scaleRandomize
Definition ParticleSpawner.h:100
bool rotationalVelocityRandomize
Definition ParticleSpawner.h:102
bool rotationRandomize
Definition ParticleSpawner.h:101
friend auto from_json(const json &j, ParticleSpawner &n) -> void
Definition ParticleSpawner.cpp:266
bool positionRandomize
Definition ParticleSpawner.h:98
unsigned spawnCount
how many particles to spawn at a time
Definition ParticleSpawner.h:93
virtual auto activate(const AffineMatrix &parentTransform, const Vector4D &scale, float rot) -> std::vector< Particle >
spawns particles, based on values set in the editor
Definition ParticleSpawner.cpp:34
bool colorRandomize
Definition ParticleSpawner.h:104
auto setParticleEndColor(const Vector4D &vec) -> void
Definition ParticleSpawner.cpp:245
virtual auto particleSpawnMenu() -> void
draw the menu for the particle editor that pertains to how particles spawn
Definition ParticleSpawner.cpp:95
bool rotationalAccelerationRandomize
Definition ParticleSpawner.h:103
std::ranlux24_base randSeed
Definition ParticleSpawner.h:110
bool velocityRandomize
Definition ParticleSpawner.h:99
Particle minParticle
for particle values that are randomized, this holds the minimum value they could be
Definition ParticleSpawner.h:90
virtual ~ParticleSpawner()=default
Definition AffineMatrix.h:30
Struct for passing around all the data a particle has, before a particle is added to a Particle Block...
Definition Particle.h:50
Definition Vector4D.h:28