Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
particle_script.h
1 #pragma once
2 
3 #include "../generic/identifiable.h"
4 #include "../generic/managed.h"
5 #include "../asset.h"
6 #include "../loadable.h"
7 #include "../types.h"
8 
9 
10 namespace smlt {
11 
12 enum EmitterType {
13  PARTICLE_EMITTER_POINT,
14  PARTICLE_EMITTER_BOX
15 };
16 
17 struct Emitter {
18  EmitterType type = PARTICLE_EMITTER_POINT;
19  Vec3 relative_position;
20  Vec3 direction = Vec3::POSITIVE_Y;
21  Vec3 dimensions = Vec3(100, 100, 100);
22 
23  std::pair<float, float> duration_range = std::make_pair(0.0, 0.0);
24  std::pair<float, float> repeat_delay_range = std::make_pair(0.0, 0.0);
25  std::pair<float, float> velocity_range = std::make_pair(1.0, 1.0);
26  std::pair<float, float> ttl_range = std::make_pair(5.0, 5.0);
27 
28  Degrees angle;
29  std::vector<Colour> colours = {Colour::WHITE};
30 
31  float emission_rate = 10.0f;
32 };
33 
34 
35 struct Particle;
36 class ParticleScript;
37 
38 class Manipulator {
39 public:
40  Manipulator(ParticleScript* script, const std::string& name):
41  name_(name),
42  particle_script_(script) {}
43 
44  virtual ~Manipulator() {}
45 
46  void manipulate(ParticleSystem* system, Particle* particles, std::size_t particle_count, float dt) const {
47  do_manipulate(system, particles, particle_count, dt);
48  }
49 
50  virtual void set_linear_curve(float rate);
51 
52  virtual void set_bell_curve(float peak, float deviation);
53 
54 private:
55  std::string name_;
56  virtual void do_manipulate(ParticleSystem* system, Particle* particles, std::size_t particle_count, float dt) const = 0;
57 
58 protected:
59  typedef std::function<float (float, float, float)> CurveFunc;
60  ParticleScript* particle_script_;
61  CurveFunc curve_;
62 };
63 
64 typedef std::shared_ptr<Manipulator> ManipulatorPtr;
65 
67  public Asset,
68  public Loadable,
69  public generic::Identifiable<ParticleScriptID>,
70  public RefCounted<ParticleScript>,
71  public ChainNameable<ParticleScript> {
72 
73 public:
74  const static int MAX_EMITTER_COUNT = 8;
75 
76  struct BuiltIns {
77  static const std::string FIRE;
78  };
79 
80 
81  ParticleScript(ParticleScriptID id, AssetManager* asset_manager);
82 
83  std::size_t emitter_count() const;
84  const Emitter* emitter(std::size_t i) const;
85 
86  /* Note: this is an unstable API */
87  Emitter* mutable_emitter(std::size_t i);
88 
89  std::size_t manipulator_count() const;
90  const Manipulator* manipulator(std::size_t i) const;
91 
92  std::size_t quota() const;
93 
94  /* FIXME: Surely this should be on the emitter? */
95  float particle_width() const;
96  float particle_height() const;
97 
98  bool cull_each() const;
99  MaterialPtr material() const;
100  bool has_repeating_emitters() const;
101 
102  void add_manipulator(std::shared_ptr<Manipulator> manipulator);
103  void clear_manipulators();
104 
105  void push_emitter(const Emitter& emitter);
106  void clear_emitters();
107 
108  void set_quota(std::size_t quota);
109 
110  void set_particle_width(float w);
111  void set_particle_height(float h);
112  void set_cull_each(bool v);
113  void set_material(MaterialPtr material);
114 
115 private:
116  ParticleScript* owner_ = nullptr;
117 
118  std::size_t quota_ = 0;
119  float particle_width_ = 100.0f;
120  float particle_height_ = 100.0f;
121  bool cull_each_ = false;
122 
123  std::array<Emitter, ParticleScript::MAX_EMITTER_COUNT> emitters_;
124  uint16_t emitter_count_ = 0;
125 
126  std::vector<ManipulatorPtr> manipulators_;
127  MaterialPtr material_;
128 };
129 
130 }
smlt::Manipulator
Definition: particle_script.h:38
smlt::Vec3
Definition: vec3.h:23
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::ParticleScript::BuiltIns
Definition: particle_script.h:76
smlt::ParticleSystem
Definition: particle_system.h:43
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::UniqueID
Definition: unique_id.h:77
smlt::Degrees
Definition: degrees.h:7
smlt::Emitter
Definition: particle_script.h:17
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::Particle
Definition: particle_system.h:21
smlt::ParticleScript
Definition: particle_script.h:71
smlt::Asset
Definition: asset.h:37
smlt::AssetManager
Definition: asset_manager.h:107