Panda3D
particleSystem.h
1 // Filename: particleSystem.h
2 // Created by: charles (14Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef NDEBUG
16 //#define PSDEBUG
17 #endif
18 
19 //#define PSSANITYCHECK
20 
21 #ifndef PARTICLESYSTEM_H
22 #define PARTICLESYSTEM_H
23 
24 #include "pandabase.h"
25 #include "pointerTo.h"
26 #include "physical.h"
27 #include "pandaNode.h"
28 #include "referenceCount.h"
29 #include "pdeque.h"
30 #include "pStatTimer.h"
31 #include "baseParticle.h"
32 #include "baseParticleRenderer.h"
33 #include "baseParticleEmitter.h"
34 #include "baseParticleFactory.h"
35 
37 
38 ////////////////////////////////////////////////////////////////////
39 // Class : ParticleSystem
40 // Description : Contains and manages a particle system.
41 ////////////////////////////////////////////////////////////////////
42 class EXPCL_PANDAPHYSICS ParticleSystem : public Physical {
43 PUBLISHED:
44  // constructor/destructor
45 
46  ParticleSystem(int pool_size = 0);
47  ParticleSystem(const ParticleSystem& copy);
48  ~ParticleSystem();
49 
50  // access/queries
51  INLINE void set_pool_size(int size);
52  INLINE void set_birth_rate(PN_stdfloat new_br);
53  INLINE void set_soft_birth_rate(PN_stdfloat new_br);
54  INLINE void set_litter_size(int new_ls);
55  INLINE void set_litter_spread(int new_ls);
56  INLINE void set_local_velocity_flag(bool lv);
57  INLINE void set_system_grows_older_flag(bool sgo);
58  INLINE void set_system_lifespan(PN_stdfloat sl);
59  INLINE void set_system_age(PN_stdfloat age);
60  INLINE void set_active_system_flag(bool a);
61  INLINE void set_spawn_on_death_flag(bool sod);
62  INLINE void set_spawn_render_node(PandaNode *node);
63  INLINE void set_spawn_render_node_path(const NodePath &node);
64  INLINE void set_template_system_flag(bool tsf);
65  INLINE void set_render_parent(PandaNode *node);
66  INLINE void set_render_parent(const NodePath &node);
67  INLINE void set_renderer(BaseParticleRenderer *r);
68  INLINE void set_emitter(BaseParticleEmitter *e);
69  INLINE void set_factory(BaseParticleFactory *f);
70  INLINE void set_floor_z(PN_stdfloat z);
71 
72  INLINE void clear_floor_z();
73 
74  INLINE int get_pool_size() const;
75  INLINE PN_stdfloat get_birth_rate() const;
76  INLINE PN_stdfloat get_soft_birth_rate() const;
77  INLINE int get_litter_size() const;
78  INLINE int get_litter_spread() const;
79  INLINE bool get_local_velocity_flag() const;
80  INLINE bool get_system_grows_older_flag() const;
81  INLINE PN_stdfloat get_system_lifespan() const;
82  INLINE PN_stdfloat get_system_age() const;
83  INLINE bool get_active_system_flag() const;
84  INLINE bool get_spawn_on_death_flag() const;
85  INLINE PandaNode *get_spawn_render_node() const;
86  INLINE NodePath get_spawn_render_node_path() const;
87  INLINE bool get_i_was_spawned_flag() const;
88  INLINE int get_living_particles() const;
89  INLINE NodePath get_render_parent() const;
90  INLINE BaseParticleRenderer *get_renderer() const;
91  INLINE BaseParticleEmitter *get_emitter() const;
92  INLINE BaseParticleFactory *get_factory() const;
93  INLINE PN_stdfloat get_floor_z() const;
94 
95  // particle template vector
96 
97  INLINE void add_spawn_template(ParticleSystem *ps);
98  INLINE void clear_spawn_templates();
99 
100  // methods
101 
102  INLINE void render();
103  INLINE void induce_labor();
104  INLINE void clear_to_initial();
105  INLINE void soft_stop(PN_stdfloat br = 0.0);
106  INLINE void soft_start(PN_stdfloat br = 0.0);
107  void update(PN_stdfloat dt);
108 
109  virtual void output(ostream &out) const;
110  virtual void write_free_particle_fifo(ostream &out, int indent=0) const;
111  virtual void write_spawn_templates(ostream &out, int indent=0) const;
112  virtual void write(ostream &out, int indent=0) const;
113 
114 private:
115  #ifdef PSSANITYCHECK
116  int sanity_check();
117  #endif
118 
119  bool birth_particle();
120  void kill_particle(int pool_index);
121  void birth_litter();
122  void resize_pool(int size);
123 
124  pdeque< int > _free_particle_fifo;
125 
126  int _particle_pool_size;
127  int _living_particles;
128  PN_stdfloat _cur_birth_rate;
129  PN_stdfloat _birth_rate;
130  PN_stdfloat _soft_birth_rate;
131  PN_stdfloat _tics_since_birth;
132  int _litter_size;
133  int _litter_spread;
134  PN_stdfloat _system_age;
135  PN_stdfloat _system_lifespan;
136  PN_stdfloat _floor_z;
137 
138  PT(BaseParticleFactory) _factory;
139  PT(BaseParticleEmitter) _emitter;
140  PT(BaseParticleRenderer) _renderer;
141  ParticleSystemManager *_manager;
142 
143  bool _template_system_flag;
144 
145  // _render_parent is the ALREADY ALLOC'D node under which this
146  // system will render its particles.
147 
148  NodePath _render_parent;
149  NodePath _render_node_path;
150 
151  bool _active_system_flag;
152  bool _local_velocity_flag;
153  bool _system_grows_older_flag;
154 
155  // information for systems that will spawn
156 
157  bool _spawn_on_death_flag;
158  NodePath _spawn_render_node_path;
159  pvector< PT(ParticleSystem) > _spawn_templates;
160 
161  void spawn_child_system(BaseParticle *bp);
162 
163  // information for spawned systems
164  bool _i_was_spawned_flag;
165 
166 public:
167  static TypeHandle get_class_type() {
168  return _type_handle;
169  }
170  static void init_type() {
171  Physical::init_type();
172  register_type(_type_handle, "ParticleSystem",
173  Physical::get_class_type());
174  }
175  virtual TypeHandle get_type() const {
176  return get_class_type();
177  }
178  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
179 
180 private:
181  static TypeHandle _type_handle;
182 
183  friend class ParticleSystemManager; // particleSystemManager.h
184 
185  static PStatCollector _update_collector;
186 };
187 
188 #include "particleSystem.I"
189 
190 #endif // PARTICLESYSTEM_H
191 
virtual void output(ostream &out=cout) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:143
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
Pure Virtual base class for creating particles.
Manages a set of individual ParticleSystem objects, so that each individual one doesn&#39;t have to be up...
A lightweight class that represents a single element that may be timed and/or counted via stats...
Contains and manages a particle system.
Pure virtual particle renderer base class.
virtual void write(ostream &out=cout, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Definition: physical.cxx:213
Defines a set of physically modeled attributes.
Definition: physical.h:40
An individual, physically-modelable particle abstract base class.
Definition: baseParticle.h:26
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Describes a physical region in space in which particles are randomly generated.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165