Arcade Shooter
ParticleTemplate.h
1 
2 /* ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
3  ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
4  █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
5  ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
6  ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
7  ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
8  /vvvvvvvvvvvvvvvvvvv \=========================================,
9  `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
10  Katana Engine \/ © 2012 - Shuriken Studios LLC */
11 
12 #pragma once
13 
14 namespace KatanaEngine
15 {
18  {
19 
20  friend void Particle::Update(const GameTime *pGameTime);
21  friend void Particle::Draw(SpriteBatch *pSpriteBatch);
22  friend void Emitter::Update(const GameTime *pGameTime);
23 
24  public:
25 
26 
30  ParticleTemplate(const bool poolExpands = true);
31 
32  virtual ~ParticleTemplate();
33 
36  virtual Particle *GetInactiveParticle() = 0;
37 
40  virtual void SetTexture(Texture *pTexture) { m_pTexture = pTexture; }
41 
42 
43  protected:
44 
47  virtual Texture *GetTexture() { return m_pTexture; }
48 
51  virtual void SetParticleLifespan(const float seconds) { m_pParticleLifespan = seconds; }
52 
55  virtual void InitializeParticle(Particle *pParticle);
56 
60  virtual void UpdateParticle(Particle *pParticle, const GameTime *pGameTime) { }
61 
64  template <typename T>
66  {
67  m_it = m_particles.begin();
68  for (; m_it != m_particles.end(); ++m_it)
69  {
70  bool active = (*m_it)->IsActive();
71  if (!active) return (T *)(*m_it);
72  }
73 
74  if (m_poolExpands) return GenerateParticles<T>();
75 
76  return nullptr;
77  }
78 
81  template <typename T>
82  T *GenerateParticles(const int count = 1)
83  {
84  T *pT = nullptr;
85 
86  for (int i = 0; i < count; ++i)
87  {
88  pT = new T();
89  Particle *pParticle = (Particle *)pT;
90  pParticle->SetTemplate(this);
91  m_particles.push_back(pParticle);
92  }
93 
94  return pT;
95  }
96 
97 
98  private:
99 
100  Texture *m_pTexture;
101 
102  float m_pParticleLifespan;
103 
104  std::vector<Particle *> m_particles;
105  std::vector<Particle *>::iterator m_it;
106 
107  bool m_poolExpands;
108 
109  };
110 }
Represents a 2D grid of texels.
Definition: Texture.h:17
virtual void SetParticleLifespan(const float seconds)
Sets the lifespan of the particle in seconds.
Definition: ParticleTemplate.h:51
virtual void UpdateParticle(Particle *pParticle, const GameTime *pGameTime)
Updates a particle.
Definition: ParticleTemplate.h:60
virtual void SetTexture(Texture *pTexture)
Sets the texture.
Definition: ParticleTemplate.h:40
ParticleTemplate(const bool poolExpands=true)
Instantiates a new particle template object.
Definition: ParticleTemplate.cpp:16
virtual void InitializeParticle(Particle *pParticle)
Initializes a particle.
Definition: ParticleTemplate.cpp:28
T * GetInactiveParticleOfType()
Gets a particle of a specific type.
Definition: ParticleTemplate.h:65
Contains timing values for game updates and rendering.
Definition: GameTime.h:17
Base class for particle templates which are used to control the updating of a corresponding particle ...
Definition: ParticleTemplate.h:17
T * GenerateParticles(const int count=1)
Generates particles of a specific type.
Definition: ParticleTemplate.h:82
virtual Texture * GetTexture()
Gets the texture to render.
Definition: ParticleTemplate.h:47
virtual Particle * GetInactiveParticle()=0
Get the next available particle.
Enables a group of sprites to be drawn using the same settings.
Definition: SpriteBatch.h:44
A single component of a particle system.
Definition: Particle.h:19
virtual void SetTemplate(ParticleTemplate *pTemplate)
Sets template responsible for updating the particle.
Definition: Particle.h:42
virtual void Update(const GameTime *pGameTime)
Called when the game has determined that game logic needs to be processed.
Definition: Emitter.cpp:32
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition: Animation.cpp:14