Arcade Shooter
Level.h
1 
2 /* . ,'`. .
3  . .." _.-;' ⁄‚ `. . `
4  _.-"`.##%"_.--" ,' ⁄` `. "#" ___,,od000
5  ,'"-_ _.-.--"\ ,' `-_ '%#%',,/00000000000
6  ,' |_.' )`/- __..--""`-_`-._ J L/00000000000000
7  . + ,' _.-" / / _-"" `-._`-_/___\///0000 000M
8  .'_.-"" ' :_/_.-' _,`-/__V__\0000 00MMM
9  . _-"" . ' _,/000\ | /000 0MMMMM
10 _-" . ' . . ,/ 000\ | /000000000MMMMM
11  ` Shooter Library ' ,/ 000\|/000000000MMMMMM
12 . © 2017 - Shuriken Studios LLC ,/0 00000|0000000000MMMMMM */
13 
14 #pragma once
15 
16 namespace ShooterLibrary
17 {
18  class GameObject;
19 
21  class Level
22  {
23 
24  public:
25 
26  Level();
27  virtual ~Level();
28 
32  virtual void LoadContent(ResourceManager *pResourceManager);
33 
36  virtual void UnloadContent();
37 
41  virtual void Update(const GameTime *pGameTime);
42 
45  virtual void Draw(SpriteBatch *pSpriteBatch);
46 
50  virtual void AddGameObject(GameObject *pGameObject) { m_gameObjects.push_back(pGameObject); }
51 
53  virtual void Complete() { }
54 
60  virtual void UpdateSectorPosition(GameObject *pGameObject);
61 
68  template <typename T>
69  T *GetClosestObject(const Vector2 position, const float range)
70  {
71  float squaredRange = range * range;
72  if (range <= 0)
73  {
74  int w = Game::GetScreenWidth();
75  int h = Game::GetScreenHeight();
76  squaredRange = w * w + h * h;
77  }
78  float squaredDistance;
79 
80  std::vector<GameObject *>::iterator m_it = m_gameObjects.begin();
81  T *pClosest = nullptr;
82 
83  for (; m_it != m_gameObjects.end(); m_it++)
84  {
85  GameObject *pGameObject = *m_it;
86  if (!pGameObject->IsActive()) continue;
87 
88  squaredDistance = (position - pGameObject->GetPosition()).LengthSquared();
89  if (squaredDistance < squaredRange)
90  {
91  T *pObject = dynamic_cast<T *>(pGameObject);
92  if (pObject)
93  {
94  pClosest = pObject;
95  squaredRange = squaredDistance;
96  }
97  }
98  }
99 
100  return pClosest;
101  }
102 
103 
104  protected:
105 
107  virtual void InitializeCollisionManager() { m_pCollisionManager = new CollisionManager(); }
108 
111  virtual CollisionManager *GetCollisionManager() { return m_pCollisionManager; }
112 
115  virtual ParticleManager *GetParticleManager() const = 0;
116 
119  virtual void AddProjectilePool(ProjectilePool *pProjectilePool) { m_pProjectilePools.push_back(pProjectilePool); }
120 
123  virtual void SetBackground(Background *pBackground) { m_pBackground = pBackground; }
124 
125  private:
126 
127  CollisionManager *m_pCollisionManager = nullptr;;
128 
129  std::vector<GameObject *> *m_pSectors;
130 
131  Vector2 m_sectorCount;
132  Vector2 m_sectorSize;
133 
134  unsigned int m_totalSectorCount;
135 
136  std::vector<GameObject *> m_gameObjects;
137  std::vector<GameObject *>::iterator m_gameObjectIt;
138 
139  ALLEGRO_SAMPLE *m_pSample;
140  ALLEGRO_SAMPLE_ID m_sampleID;
141 
142  std::vector<ProjectilePool *> m_pProjectilePools;
143 
144  Background *m_pBackground;
145 
146  void CheckCollisions(std::vector<GameObject *> &sector);
147 
148  virtual Vector2 GetSectorCount() const { return m_sectorCount; }
149 
150  virtual Vector2 GetSectorSize() const { return m_sectorSize; }
151 
152  virtual unsigned int GetTotalSectorCount() const { return m_totalSectorCount; }
153 
154  virtual std::vector<GameObject *> *GetSectors() { return m_pSectors; }
155 
156  };
157 }
Used to recycle projectiles.
Definition: ProjectilePool.h:20
virtual ParticleManager * GetParticleManager() const =0
Gets the ParticleManager for loading the level&#39;s resources.
virtual void Complete()
Should be called when the victory conditions are met for the level.
Definition: Level.h:53
Handles collisions between game objects.
Definition: CollisionManager.h:30
virtual void UpdateSectorPosition(GameObject *pGameObject)
Updates an object&#39;s position on the sector grid.
Definition: Level.cpp:159
static int GetScreenHeight()
Gets the screen width in pixels.
Definition: Game.h:37
Base class for all shooter levels.
Definition: Level.h:21
virtual void Update(const GameTime *pGameTime)
Called when the game has determined that game logic needs to be processed.
virtual void AddGameObject(GameObject *pGameObject)
Adds a game object so it can be managed by the level.
Definition: Level.h:50
Base class for all objects that will be updated and rendered on a GameplayScreen. ...
Definition: GameObject.h:29
Shooter Library is a library of classes, interfaces, and value types that provides a foundation for d...
Definition: Background.h:16
virtual void AddProjectilePool(ProjectilePool *pProjectilePool)
Adds a pool of projectiles to the level.
Definition: Level.h:119
virtual CollisionManager * GetCollisionManager()
Gets the CollisionManager for testing collisions between game objects.
Definition: Level.h:111
virtual Vector2 & GetPosition()
Gets the position of the game object.
Definition: GameObject.h:78
virtual void UnloadContent()
Called when resources need to be unloaded. Override this method to unload any game-specific resources...
Definition: Level.cpp:67
virtual void LoadContent(ResourceManager *pResourceManager)
Called when resources need to be loaded.
virtual void InitializeCollisionManager()
Initailizes the level&#39;s default CollisionManager.
Definition: Level.h:107
virtual void Draw(SpriteBatch *pSpriteBatch)
Called when the game determines it is time to draw a frame.
virtual void SetBackground(Background *pBackground)
Sets the background object for the level.
Definition: Level.h:123
T * GetClosestObject(const Vector2 position, const float range)
Get the closest active game object of a specific type.
Definition: Level.h:69
static int GetScreenWidth()
Gets the screen width in pixels.
Definition: Game.h:33
Abstract base class for a scrolling background.
Definition: Background.h:20
virtual bool IsActive() const
Determines if the game object is active.
Definition: GameObject.h:68