Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
particle_system.h
1 #pragma once
2 
3 #include <memory>
4 #include <unordered_map>
5 
6 #include "stage_node.h"
7 
8 #include "../generic/identifiable.h"
9 #include "../generic/managed.h"
10 #include "../generic/manual_object.h"
11 #include "../renderers/renderer.h"
12 #include "../sound.h"
13 #include "../interfaces.h"
14 #include "../types.h"
15 #include "../vertex_data.h"
16 #include "../utils/random.h"
17 #include "../assets/particle_script.h"
18 
19 namespace smlt {
20 
21 struct Particle {
22  smlt::Vec3 position;
23  smlt::Vec3 velocity;
24  smlt::Vec2 dimensions;
25  smlt::Vec2 initial_dimensions;
26  float ttl;
27  float lifetime;
28  smlt::Colour colour;
29 };
30 
31 
32 class ParticleSystem;
33 
35 
37  public TypedDestroyableObject<ParticleSystem, Stage>,
38  public StageNode,
39  public generic::Identifiable<ParticleSystemID>,
40  public AudioSource,
41  public Loadable,
43  public ChainNameable<ParticleSystem> {
44 
45  DEFINE_SIGNAL(ParticleSystemMaterialChangedSignal, signal_material_changed);
46 
47 public:
48  ParticleSystem(Stage* stage, SoundDriver *sound_driver, ParticleScriptPtr script);
49  virtual ~ParticleSystem();
50 
51  const AABB& aabb() const override;
52  const AABB transformed_aabb() const override {
53  return StageNode::transformed_aabb();
54  }
55 
56  bool emitters_active() const;
57  void set_emitters_active(bool value=true);
58 
59  void set_destroy_on_completion(bool value=true) { destroy_on_completion_ = value; }
60  bool destroy_on_completion() const { return destroy_on_completion_; }
61  bool has_active_emitters() const;
62 
68  bool update_when_hidden() const;
69  void set_update_when_hidden(bool value=true);
70 
71  VertexData* vertex_data() const {
72  return vertex_data_;
73  }
74 
75  IndexData* index_data() const {
76  return index_data_;
77  }
78 
79  void clean_up() override {
80  StageNode::clean_up();
81  }
82 
83  void _get_renderables(batcher::RenderQueue* render_queue, const CameraPtr camera, const DetailLevel detail_level) override;
84 
85  ParticleScript* script() const {
86  return script_.get();
87  }
88 
89  void update(float dt) override;
90 
91  std::size_t particle_count() const {
92  return particle_count_;
93  }
94 
95  const Particle& particle(const std::size_t i) const {
96  return particles_[i];
97  }
98 
99 private:
100  struct EmitterState {
101  bool is_active = true;
102  float time_active = 0.0f;
103  float time_inactive = 0.0f;
104  float current_duration = 0.0f;
105  float repeat_delay = 0.0f;
106  float emission_accumulator = 0.0f;
107  };
108 
109  std::array<EmitterState, ParticleScript::MAX_EMITTER_COUNT> emitter_states_;
110 
111  void update_emitter(uint16_t emitter, float dt);
112  void update_active_state(uint16_t emitter, float dt);
113  void emit_particles(uint16_t emitter, float dt, uint32_t max);
114 
115 
116  AABB aabb_;
117  void calc_aabb();
118 
119  ParticleScriptPtr script_;
120 
121  std::vector<Particle> particles_;
122  std::size_t particle_count_ = 0;
123 
124  VertexData* vertex_data_ = nullptr;
125  IndexData* index_data_ = nullptr;
126 
127  bool destroy_on_completion_ = false;
128  bool update_when_hidden_ = false;
129 
130  void rebuild_vertex_data(const smlt::Vec3& up, const smlt::Vec3& right);
131 
132  bool emitters_active_ = true;
133 
134  RandomGenerator random_;
135 };
136 
137 }
smlt::VertexData
Definition: vertex_data.h:57
smlt::RandomGenerator
Definition: random.h:16
smlt::Vec3
Definition: vec3.h:23
smlt::AudioSource
Definition: sound.h:103
smlt::Stage
Definition: stage.h:92
smlt
Definition: animation.cpp:25
smlt::ParticleSystem
Definition: particle_system.h:43
smlt::HasMutableRenderPriority
Definition: renderable.h:38
smlt::batcher::RenderQueue
Definition: render_queue.h:156
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::UniqueID< MaterialPtr >
smlt::TypedDestroyableObject
Definition: manual_object.h:33
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::AABB
Definition: aabb.h:22
smlt::StageNode
Definition: stage_node.h:61
smlt::Particle
Definition: particle_system.h:21
smlt::IndexData
Definition: vertex_data.h:334
smlt::default_init_ptr< Camera >
smlt::ParticleScript
Definition: particle_script.h:71
smlt::ParticleSystem::update_when_hidden
bool update_when_hidden() const
If update_when_hidden is true, then particles will continue to be simulated even if the particle syst...
Definition: particle_system.cpp:129
smlt::SoundDriver
Definition: sound_driver.h:74
smlt::Colour
Definition: colour.h:29
smlt::Vec2
Definition: vec2.h:13
smlt::sig::signal
Definition: signal.h:319