Simulant  21.09-46
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 "../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  void deactivate_emitters() { emitters_active_ = false; }
57  void activate_emitters() { emitters_active_ = 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 
63  VertexData* vertex_data() const {
64  return vertex_data_;
65  }
66 
67  IndexData* index_data() const {
68  return index_data_;
69  }
70 
71  void clean_up() override {
72  StageNode::clean_up();
73  }
74 
75  void _get_renderables(batcher::RenderQueue* render_queue, const CameraPtr camera, const DetailLevel detail_level) override;
76 
77  ParticleScript* script() const {
78  return script_.get();
79  }
80 
81  void update(float dt) override;
82 
83 private:
84  struct EmitterState {
85  bool is_active = true;
86  float time_active = 0.0f;
87  float time_inactive = 0.0f;
88  float current_duration = 0.0f;
89  float repeat_delay = 0.0f;
90  float emission_accumulator = 0.0f;
91  };
92 
93  std::array<EmitterState, ParticleScript::MAX_EMITTER_COUNT> emitter_states_;
94 
95  void update_emitter(uint16_t emitter, float dt);
96  void update_active_state(uint16_t emitter, float dt);
97  void emit_particles(uint16_t emitter, float dt, uint32_t max);
98 
99 
100  AABB aabb_;
101  void calc_aabb();
102 
103  ParticleScriptPtr script_;
104 
105  std::vector<Particle> particles_;
106  std::size_t particle_count_ = 0;
107 
108  VertexData* vertex_data_ = nullptr;
109  IndexData* index_data_ = nullptr;
110 
111  bool destroy_on_completion_ = false;
112 
113  void rebuild_vertex_data(const smlt::Vec3& up, const smlt::Vec3& right);
114 
115  bool emitters_active_ = true;
116 
117  RandomGenerator random_;
118 };
119 
120 }
smlt::VertexData
Definition: vertex_data.h:57
smlt::RandomGenerator
Definition: random.h:9
smlt::Vec3
Definition: vec3.h:23
smlt::AudioSource
Definition: sound.h:162
smlt::Stage
Definition: stage.h:91
smlt
Definition: animation.cpp:25
smlt::ParticleSystem
Definition: particle_system.h:43
smlt::HasMutableRenderPriority
Definition: renderable.h:37
smlt::batcher::RenderQueue
Definition: render_queue.h:156
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::UniqueID< MaterialPtr >
smlt::TypedDestroyableObject
Definition: manual_object.h:32
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::AABB
Definition: aabb.h:22
smlt::StageNode
Definition: stage_node.h:60
smlt::Particle
Definition: particle_system.h:21
smlt::IndexData
Definition: vertex_data.h:323
smlt::default_init_ptr< Camera >
smlt::ParticleScript
Definition: particle_script.h:71
smlt::SoundDriver
Definition: sound_driver.h:70
smlt::Colour
Definition: colour.h:29
smlt::Vec2
Definition: vec2.h:13
smlt::sig::signal
Definition: signal.h:319