Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Projectile.cpp
Go to the documentation of this file.
1
2#include "Projectile.h"
3#include "Level.h"
4
5Texture *Projectile::s_pTexture = nullptr;
6
12
13void Projectile::Update(const GameTime& gameTime)
14{
15 if (IsActive())
16 {
17 Vector2 translation = m_direction * m_speed * gameTime.GetElapsedTime();
18 TranslatePosition(translation);
19
20 Vector2 position = GetPosition();
21 Vector2 size = s_pTexture->GetSize();
22
23 // Is the projectile off the screen?
24 if (position.Y < -size.Y) Deactivate();
25 else if (position.X < -size.X) Deactivate();
26 else if (position.Y > Game::GetScreenHeight() + size.Y) Deactivate();
27 else if (position.X > Game::GetScreenWidth() + size.X) Deactivate();
28 }
29
30 GameObject::Update(gameTime);
31}
32
33void Projectile::Draw(SpriteBatch& spriteBatch)
34{
35 if (IsActive())
36 {
37 const float alpha = GetCurrentLevel()->GetAlpha();
38 spriteBatch.Draw(s_pTexture, GetPosition(), Color::White * alpha, s_pTexture->GetCenter());
39 }
40}
41
42void Projectile::Activate(const Vector2 &position, bool wasShotByPlayer)
43{
44 m_wasShotByPlayer = wasShotByPlayer;
45 SetPosition(position);
46
48}
49
50std::string Projectile::ToString() const
51{
52 return std::string((WasShotByPlayer()) ? "Player" : "Enemy").append(" Projectile");
53}
54
Represents a type of collision.
static const CollisionType Enemy
static const CollisionType Player
virtual void TranslatePosition(const float x, const float y)
virtual void SetPosition(const float x, const float y)
virtual void SetCollisionRadius(const int radius)
Definition GameObject.h:98
virtual void Activate()
Activate the object.
Definition GameObject.h:45
virtual bool IsActive() const
Flag to determine if the object is active.
Definition GameObject.h:42
virtual void Deactivate()
Deactivate the object.
Definition GameObject.h:48
static Level * GetCurrentLevel()
Get the current level.
Definition GameObject.h:30
virtual void Update(const GameTime &gameTime)
Update the object.
virtual Vector2 & GetPosition()
Get the position of the object.
Definition GameObject.h:52
static const Color White
White.
Definition Color.h:201
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
double GetElapsedTime() const
Gets the time in seconds since last frame.
Definition GameTime.h:30
Enables a group of sprites to be drawn using the same settings.
Definition SpriteBatch.h:48
void Draw(const Texture *pTexture, const Vector2 position, const Region region, const Color color=Color::White, const Vector2 origin=Vector2::ZERO, const Vector2 scale=Vector2::ONE, const float rotation=0, const float drawDepth=0)
Adds a sprite to a batch of sprites to be rendered.
Represents a 2D grid of texels.
Definition Texture.h:21
Vector2 GetCenter() const
Gets the center position of the texture.
Definition Texture.h:48
Vector2 GetSize() const
Gets the dimensions of the texture.
Definition Texture.h:44
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
float Y
The y-coordinate of the vector.
Definition Vector2.h:187
static const Vector2 UNIT_Y
A unit vector on the y-axis.
Definition Vector2.h:34
float X
The x-coordinate of the vector.
Definition Vector2.h:186
virtual float GetAlpha() const
Get the alpha value of the screen.
Definition Level.cpp:212
virtual CollisionType GetProjectileType() const
Get the collision type of the projectile.
Definition Projectile.h:75
virtual CollisionType GetCollisionType() const
Get the type of collision the projectile will have.
virtual bool WasShotByPlayer() const
Determine if the projectile was fired by the player.
Definition Projectile.h:71
virtual void SetDirection(const Vector2 direction)
Set the direction of the projectile.
Definition Projectile.h:59
Projectile()
Instantiate a projectile object.
Definition Projectile.cpp:7
virtual std::string ToString() const
Get the string representation of the projectile.
virtual void Draw(SpriteBatch &spriteBatch)
Render the projectile.
virtual void Update(const GameTime &gameTime)
Update the projectile.