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
76 friend auto to_json(json& j, const ParticleSpawner& n) -> void;
77 friend auto from_json(const json& j, ParticleSpawner& n) -> void;
78
79protected:
89
91 unsigned spawnCount = 1u;
92
93 // bools for whether certain values of the particles should have random initial values
94 // all randomization bools follow a naming convention to make macros for the editor work.
95
96 bool positionRandomize = false;
97 bool velocityRandomize = false;
98 bool scaleRandomize = false;
99 bool rotationRandomize = false;
102 bool colorRandomize = false;
104 bool lifetimeRandomize = false;
105
106private:
107 // random engine
108 std::ranlux24_base randSeed{std::random_device()()};
109
119 template <typename T>
120 auto randIf(bool& isRandom, const T& min, const T& max) -> T
121 {
122 if (isRandom)
123 {
124 return randBetween(min, max);
125 }
126 return max;
127 }
128
136 template <typename T>
137 auto randBetween(const T& lhs, const T& rhs) -> T
138 {
139 // we need to use a different distribution based on ints and floats
140 if constexpr (std::is_integral_v<T>)
141 {
142 using std::max;
143 using std::min;
144 std::uniform_int_distribution<> range(T(0), max(lhs, rhs) - min(lhs, rhs));
145 return range(randSeed) + min(lhs, rhs);
146 }
147 else
148 {
149 using std::max;
150 using std::min;
151 std::uniform_real_distribution<float> range{T(0), max(lhs, rhs) - min(lhs, rhs)};
152 return range(randSeed) + min(lhs, rhs);
153 }
154 }
155
162 template <>
163 auto randBetween<Vector4D>(const Vector4D& lhs, const Vector4D& rhs) -> Vector4D
164 {
165 using std::max;
166 using std::min;
168 std::uniform_real_distribution<float> rangeX(0.f, max(lhs.x(), rhs.x()) - min(lhs.x(), rhs.x()));
169 std::uniform_real_distribution<float> rangeY(0.f, max(lhs.y(), rhs.y()) - min(lhs.y(), rhs.y()));
170 std::uniform_real_distribution<float> rangeZ(0.f, max(lhs.z(), rhs.z()) - min(lhs.z(), rhs.z()));
171 std::uniform_real_distribution<float> rangeW(0.f, max(lhs.w(), rhs.w()) - min(lhs.w(), rhs.w()));
172
173 result.x() = rangeX(randSeed) + min(lhs.x(), rhs.x());
174 result.y() = rangeY(randSeed) + min(lhs.y(), rhs.y());
175 result.z() = rangeZ(randSeed) + min(lhs.z(), rhs.z());
176 result.w() = rangeW(randSeed) + min(lhs.w(), rhs.w());
177 return result;
178 }
179
180
181};
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:104
Particle maxParticle
holds the initial state for a particle when it is spawned.
Definition ParticleSpawner.h:84
auto randBetween(const T &lhs, const T &rhs) -> T
returns a random number between 2 numbers
Definition ParticleSpawner.h:137
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:238
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:120
bool startFrameRandomize
Definition ParticleSpawner.h:103
bool scaleRandomize
Definition ParticleSpawner.h:98
bool rotationalVelocityRandomize
Definition ParticleSpawner.h:100
bool rotationRandomize
Definition ParticleSpawner.h:99
friend auto from_json(const json &j, ParticleSpawner &n) -> void
Definition ParticleSpawner.cpp:254
bool positionRandomize
Definition ParticleSpawner.h:96
unsigned spawnCount
how many particles to spawn at a time
Definition ParticleSpawner.h:91
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:102
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:101
std::ranlux24_base randSeed
Definition ParticleSpawner.h:108
bool velocityRandomize
Definition ParticleSpawner.h:97
Particle minParticle
for particle values that are randomized, this holds the minimum value they could be
Definition ParticleSpawner.h:88
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:23