Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Level.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "KatanaEngine.h"
5#include "PlayerShip.h"
6#include "CollisionManager.h"
7#include "Explosion.h"
8
9using namespace KatanaEngine;
10
11class GameplayScreen;
12
14class Level
15{
16
17public:
18
20 Level();
21 virtual ~Level();
22
26 virtual void LoadContent(ResourceManager& resourceManager);
27
29 virtual void UnloadContent() { };
30
33 virtual void HandleInput(const InputState& input);
34
37 virtual void Update(const GameTime& gameTime);
38
41 virtual void Draw(SpriteBatch& spriteBatch);
42
46 virtual void AddGameObject(GameObject* pGameObject) { m_gameObjects.push_back(pGameObject); }
47
51 virtual void UpdateSectorPosition(GameObject* pGameObject);
52
55 virtual void SetGameplayScreen(GameplayScreen* pGameplayScreen) { m_pGameplayScreen = pGameplayScreen; }
56
59 virtual void SpawnExplosion(GameObject* pExplodingObject);
60
63 virtual void SetBackground(Texture* pBackground) { m_pBackground = pBackground; }
64
67 virtual float GetAlpha() const;
68
71 virtual bool IsScreenTransitioning() const { return GetAlpha() < 1; }
72
77 template <typename T>
78 T* GetClosestObject(const Vector2 position, const float range)
79 {
80 float squaredRange = range * range;
81 if (range <= 0)
82 {
83 int w = Game::GetScreenWidth();
84 int h = Game::GetScreenHeight();
85 squaredRange = w * w + h * h;
86 }
87 float squaredDistance;
88
89 std::vector<GameObject*>::iterator m_it = m_gameObjects.begin();
90 T* pClosest = nullptr;
91
92 for (; m_it != m_gameObjects.end(); m_it++)
93 {
94 GameObject* pGameObject = *m_it;
95 if (pGameObject->IsActive()) continue;
96
97 squaredDistance = (position - pGameObject->GetPosition()).LengthSquared();
98 if (squaredDistance < squaredRange)
99 {
100 T* pObject = dynamic_cast<T*>(pGameObject);
101 if (pObject)
102 {
103 pClosest = pObject;
104 squaredRange = squaredDistance;
105 }
106 }
107 }
108
109 return pClosest;
110 }
111
112
113protected:
114
117 virtual CollisionManager* GetCollisionManager() { return m_pCollisionManager; }
118
121 virtual GameplayScreen* GetGameplayScreen() const { return m_pGameplayScreen; }
122
125 virtual void SetBackgroundAudio(AudioSample* pAudio) { m_pAudio = pAudio; }
126
129 virtual AudioSample* GetBackgroundAudio() { return m_pAudio; }
130
131private:
132
133 static std::vector<Explosion *> s_explosions;
134 std::vector<Explosion *>::iterator m_explosionIt;
135
136 CollisionManager* m_pCollisionManager = nullptr;
137
138 GameplayScreen* m_pGameplayScreen = nullptr;
139
140 AudioSample* m_pAudio = nullptr;
141
142 Texture* m_pBackground = nullptr;
143
144 std::vector<GameObject*>* m_pSectors;
145
146 Vector2 m_sectorCount;
147 Vector2 m_sectorSize;
148
149 unsigned int m_totalSectorCount = 0;
150
151 std::vector<GameObject*> m_gameObjects;
152 std::vector<GameObject*>::iterator m_gameObjectIt;
153
154 PlayerShip* m_pPlayerShip;
155 std::vector<Projectile*> m_projectiles;
156
157 void CheckCollisions(std::vector<GameObject*>& sector);
158
159 virtual Vector2 GetSectorCount() const { return m_sectorCount; }
160
161 virtual Vector2 GetSectorSize() const { return m_sectorSize; }
162
163 virtual unsigned int GetTotalSectorCount() const { return m_totalSectorCount; }
164
165 virtual std::vector<GameObject*>* GetSectors() { return m_pSectors; }
166
167};
Represents a collision manager that can be used to manage collisions between game objects.
Represents a game object in the game. This is the base class for all objects that can be updated,...
Definition GameObject.h:15
virtual bool IsActive() const
Flag to determine if the object is active.
Definition GameObject.h:42
virtual Vector2 & GetPosition()
Get the position of the object.
Definition GameObject.h:52
The gameplay screen for the Space Fighter Game. It is responsible for creating and loading levels.
Represents a 2D grid of texels.
Definition AudioSample.h:21
static int GetScreenHeight()
Gets the screen width in pixels.
Definition Game.h:40
static int GetScreenWidth()
Gets the screen width in pixels.
Definition Game.h:36
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Handles the state of multiple player input devices.
Definition InputState.h:22
Loads and manages the lifespan of objects from external files.
Enables a group of sprites to be drawn using the same settings.
Definition SpriteBatch.h:48
Represents a 2D grid of texels.
Definition Texture.h:21
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
Represents a level in the game.
Definition Level.h:15
virtual void UnloadContent()
Unload the content for the level.
Definition Level.h:29
virtual ~Level()
Definition Level.cpp:75
virtual void SetBackgroundAudio(AudioSample *pAudio)
Set the background audio for the level.
Definition Level.h:125
virtual void AddGameObject(GameObject *pGameObject)
Add a game object to the level. This object will be updated, rendered, and checked for collisions.
Definition Level.h:46
virtual AudioSample * GetBackgroundAudio()
Get the background audio for the level.
Definition Level.h:129
virtual void SpawnExplosion(GameObject *pExplodingObject)
Spawn an explosion at a specific position.
Definition Level.cpp:188
virtual GameplayScreen * GetGameplayScreen() const
Get a pointer to the gameplay screen.
Definition Level.h:121
Level()
Instantiate a level object.
Definition Level.cpp:32
virtual void Draw(SpriteBatch &spriteBatch)
Render the level, and all of the game objects within it.
Definition Level.cpp:243
virtual void LoadContent(ResourceManager &resourceManager)
Load the content for the level, including game objects and resources.
Definition Level.cpp:88
virtual bool IsScreenTransitioning() const
Check if the screen is transitioning.
Definition Level.h:71
virtual void SetGameplayScreen(GameplayScreen *pGameplayScreen)
Set a reference to the gameplay screen.
Definition Level.h:55
virtual void Update(const GameTime &gameTime)
Update the level.
Definition Level.cpp:118
T * GetClosestObject(const Vector2 position, const float range)
Get a pointer to the closest object of a specific type.
Definition Level.h:78
virtual void SetBackground(Texture *pBackground)
Set the background texture for the level.
Definition Level.h:63
virtual float GetAlpha() const
Get the alpha value of the screen.
Definition Level.cpp:212
virtual CollisionManager * GetCollisionManager()
Get a pointer to the collision manager.
Definition Level.h:117
virtual void UpdateSectorPosition(GameObject *pGameObject)
Update the position of a game object within the level, based on its collision sector (only objects wi...
Definition Level.cpp:153
virtual void HandleInput(const InputState &input)
Handle input for the level.
Definition Level.cpp:110
Represents the player's ship.
Definition PlayerShip.h:8
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18