Brunot
Loading...
Searching...
No Matches
ParticleBlock.h
Go to the documentation of this file.
1
13// ____ __ __ __
14// /\__ _\/\ \ /\ \/\ \
15// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
16// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
17// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
18// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
19// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
20
21#pragma once
23#include <ranges>
24
25#include "Framework/Library.h"
26#include "System/Editor.h"
27#include "Utility/Math/Bezier.h"
28
29namespace gfx
30{
31class Mesh;
32class Shader;
33class Texture;
34}
35
36struct Particle;
37
45class ParticleBlock final
46{
47public:
51 ParticleBlock() = default;
52
57 auto addParticles(const std::vector<Particle>& particles) -> void;
58
63 auto update(float dt) -> void;
64
68 auto render() const -> void;
69
73 auto showMenu() -> void;
74
75 auto setTexture(gfx::Texture* texture) -> void;
76
77 auto setShader(gfx::Shader* shader) -> void;
78
79 auto setMesh(gfx::Mesh* mesh) -> void;
80
81 auto setName(std::string newName) -> void;
82
83 auto getName() const -> const std::string&;
84
85 auto destroy() -> void;
86 auto destroyed() const -> bool;
87
88private:
90 std::string name;
91
93 bool isDestroyed = false;
94
95 [[maybe_unused]] unsigned particleCount = 0u;
96
97#pragma region shared_particle_values
98
99 bool velocityChanges = false;
100 bool colorChanges = false;
101 bool scaleChanges = false;
102
106
107
111
112
113 unsigned totalFrames = texture->getFrames();
114 float frameTime = 0.f;
115 bool loops = false;
116
117#pragma endregion
118
119#pragma region struct_of_array_values
120 // these are the arrays which hold the individual particles values
121 // if these are updated often, we could pack them into a struct declared in Particle.h
122
123 std::vector<Vector4D> position;
124 std::vector<Vector4D> velocity;
125 std::vector<Vector4D> velocityBegin;
126 std::vector<Vector4D> velocityEnd;
127 std::vector<Vector4D> scale;
128 std::vector<Vector4D> scaleBegin;
129 std::vector<Vector4D> scaleEnd;
130 std::vector<float> rotation;
131 std::vector<float> rotationalVelocity;
132 std::vector<float> rotationalAcceleration;
133 std::vector<Vector4D> color;
134 std::vector<Vector4D> colorBegin;
135 std::vector<Vector4D> colorEnd;
136 std::vector<unsigned> frame;
137 std::vector<float> frameCountdown;
138 std::vector<float> lifetime;
140 std::vector<float> timeLeft;
141
142 // how long the particle has been alive, relative to it's total lifetime. range [0,1]
143 std::vector<float> normalizedAge;
144
145#pragma endregion
146
147#pragma region helper_functions
149 auto activeParticles() const
150 {
151 const auto active = [&](unsigned i)
152 {
153 return timeLeft[i] > 0;
154 };
155 return std::views::iota(0u, timeLeft.size()) | std::views::filter(active);
156 }
157
159 auto addParticleAtPosition(const Particle& particle, unsigned pos) -> void;
161 auto pushBackParticle(const Particle& particle) -> void;
162
164 auto updateAnimationForParticle(float dt, unsigned particlePosition) -> void;
165
166#pragma endregion
167};
Wrapper for cubic bezier curve class.
bool active
Definition Editor.cpp:49
static int pos[2]
Definition Editor.cpp:79
std::vector< float > lifetime
Definition ParticleBlock.h:138
auto render() const -> void
renders the particles
Definition ParticleBlock.cpp:238
auto update(float dt) -> void
Definition ParticleBlock.cpp:171
bool colorChanges
Definition ParticleBlock.h:100
gfx::Shader * shader
Definition ParticleBlock.h:109
bool velocityChanges
Definition ParticleBlock.h:99
unsigned particleCount
Definition ParticleBlock.h:95
std::vector< Vector4D > position
Definition ParticleBlock.h:123
std::vector< float > frameCountdown
Definition ParticleBlock.h:137
std::vector< float > normalizedAge
Definition ParticleBlock.h:143
auto updateAnimationForParticle(float dt, unsigned particlePosition) -> void
helper function to run animation logic for a specific particle (called in loop for every particle)
Definition ParticleBlock.cpp:151
std::vector< float > rotationalAcceleration
Definition ParticleBlock.h:132
std::vector< Vector4D > scale
Definition ParticleBlock.h:127
auto addParticleAtPosition(const Particle &particle, unsigned pos) -> void
helper function to add a particle at a specific point in the arrays
Definition ParticleBlock.cpp:58
auto getName() const -> const std::string &
Definition ParticleBlock.cpp:532
std::vector< Vector4D > velocity
Definition ParticleBlock.h:124
std::vector< unsigned > frame
Definition ParticleBlock.h:136
std::vector< float > rotation
Definition ParticleBlock.h:130
std::vector< Vector4D > scaleBegin
Definition ParticleBlock.h:128
std::vector< Vector4D > color
Definition ParticleBlock.h:133
auto setMesh(gfx::Mesh *mesh) -> void
Definition ParticleBlock.cpp:522
th::Bezier colorCurve
Definition ParticleBlock.h:104
bool isDestroyed
whether the block has been destroyed, and should be discarded by sys::ParticleManager
Definition ParticleBlock.h:93
th::Bezier velocityCurve
Definition ParticleBlock.h:103
bool scaleChanges
Definition ParticleBlock.h:101
th::Bezier scaleCurve
Definition ParticleBlock.h:105
gfx::Mesh * mesh
Definition ParticleBlock.h:110
auto showMenu() -> void
draw a table with the stats of every particle
Definition ParticleBlock.cpp:270
auto setName(std::string newName) -> void
Definition ParticleBlock.cpp:527
std::vector< Vector4D > velocityBegin
Definition ParticleBlock.h:125
std::vector< float > rotationalVelocity
Definition ParticleBlock.h:131
gfx::Texture * texture
Definition ParticleBlock.h:108
std::vector< Vector4D > colorEnd
Definition ParticleBlock.h:135
ParticleBlock()=default
constructor for ParticleBlock.
unsigned totalFrames
Definition ParticleBlock.h:113
std::string name
The name of the block of particles, for user reference. This name is set by the particleEmitter it's ...
Definition ParticleBlock.h:90
auto destroyed() const -> bool
Definition ParticleBlock.cpp:542
auto setTexture(gfx::Texture *texture) -> void
Definition ParticleBlock.cpp:511
bool loops
Definition ParticleBlock.h:115
std::vector< Vector4D > scaleEnd
Definition ParticleBlock.h:129
auto addParticles(const std::vector< Particle > &particles) -> void
Add a series of particles to the block.
Definition ParticleBlock.cpp:84
auto activeParticles() const
returns a list of the indices where there are active particles
Definition ParticleBlock.h:149
auto pushBackParticle(const Particle &particle) -> void
helper function to add a particle to the ends of the arrays
Definition ParticleBlock.cpp:33
std::vector< Vector4D > velocityEnd
Definition ParticleBlock.h:126
auto setShader(gfx::Shader *shader) -> void
Definition ParticleBlock.cpp:517
float frameTime
Definition ParticleBlock.h:114
std::vector< Vector4D > colorBegin
Definition ParticleBlock.h:134
auto destroy() -> void
Definition ParticleBlock.cpp:537
std::vector< float > timeLeft
how long the particle has before it is dead, if less than 0, particle is inactive
Definition ParticleBlock.h:140
Definition Mesh.h:56
Definition Shader.h:25
holds and manages textures for use with meshes in sprites
Definition Texture.h:33
static auto get(const std::string &name) -> T *
get a pointer to an object in the library.
Definition Library.h:49
Definition Sprite.h:25
Struct for passing around all the data a particle has, before a particle is added to a Particle Block...
Definition Particle.h:50
Definition Bezier.h:30