Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
PlayerShip.cpp
Go to the documentation of this file.
1
2#include "PlayerShip.h"
3#include "Level.h"
4
6{
9
10 m_pTexture = resourceManager.Load<Texture>("Textures\\PlayerShip.png");
11
12 AudioSample* pAudio = resourceManager.Load<AudioSample>("Audio\\Effects\\Laser.wav");
13 pAudio->SetVolume(0.5f);
14 GetWeapon("Main Blaster")->SetFireSound(pAudio);
15
17
18}
19
20
21void PlayerShip::Initialize(Level* pLevel, Vector2& startPosition)
22{
23 SetPosition(startPosition);
24}
25
27{
28 if (IsActive())
29 {
30 Vector2 direction = Vector2::ZERO;
31 if (input.IsKeyDown(Key::DOWN)) direction.Y++;
32 if (input.IsKeyDown(Key::UP)) direction.Y--;
33 if (input.IsKeyDown(Key::Right)) direction.X++;
34 if (input.IsKeyDown(Key::Left)) direction.X--;
35
36 // Normalize the direction
37 if (direction.X != 0 && direction.Y != 0)
38 {
39 direction *= Math::NORMALIZE_PI_OVER4;
40 }
41
43 if (input.IsKeyDown(Key::SPACE)) type |= TriggerType::Primary;
44 //if (input.IsKeyDown(Key::D)) type |= TriggerType::Secondary;
45 //if (input.IsKeyDown(Key::S)) type |= TriggerType::Special;
46
48 //GamePadState* pState = input.GetGamePadState(0);
49 //if (pState->IsConnected)
50 //{
51 // // gamepad overrides keyboard input
52 // Vector2 thumbstick = pState->Thumbsticks.Left;
53 // if (thumbstick.LengthSquared() < 0.08f) thumbstick = Vector2::ZERO;
54 // direction = thumbstick;
55
56 // type = TriggerType::None;
57 // if (pState->Triggers.Right > 0.5f) type |= TriggerType::Primary;
58 // if (pState->Triggers.Left > 0.5f) type |= TriggerType::Secondary;
59 // if (pState->IsButtonDown(Button::Y)) type |= TriggerType::Special;
60 //}
61
62
63 SetDesiredDirection(direction);
64 if (type != TriggerType::None) FireWeapons(type);
65 }
66}
67
68
69void PlayerShip::Update(const GameTime& gameTime)
70{
71 // Get the velocity for the direction that the player is trying to go.
72 Vector2 targetVelocity = m_desiredDirection * GetSpeed() * gameTime.GetElapsedTime();
73 // We can't go from 0-100 mph instantly! This line interpolates the velocity for us.
74 m_velocity = Vector2::Lerp(m_velocity, targetVelocity, GetResponsiveness());
75 // Move that direction
76 TranslatePosition(m_velocity);
77
78 if (m_isConfinedToScreen)
79 {
80 const int PADDING = 4; // keep the ship 4 pixels from the edge of the screen
81 const int TOP = PADDING;
82 const int Left = PADDING;
83 const int Right = Game::GetScreenWidth() - PADDING;
84 const int BOTTOM = Game::GetScreenHeight() - PADDING;
85
86 Vector2* pPosition = &GetPosition(); // current position (middle of the ship)
87 if (pPosition->X - GetHalfDimensions().X < Left) // are we past the left edge?
88 {
89 // move the ship to the left edge of the screen (keep Y the same)
90 SetPosition(Left + GetHalfDimensions().X, pPosition->Y);
91 m_velocity.X = 0; // remove any velocity that could potentially
92 // keep the ship pinned against the edge
93 }
94 if (pPosition->X + GetHalfDimensions().X > Right) // right edge?
95 {
96 SetPosition(Right - GetHalfDimensions().X, pPosition->Y);
97 m_velocity.X = 0;
98 }
99 if (pPosition->Y - GetHalfDimensions().Y < TOP) // top edge?
100 {
101 SetPosition(pPosition->X, TOP + GetHalfDimensions().Y);
102 m_velocity.Y = 0;
103 }
104 if (pPosition->Y + GetHalfDimensions().Y > BOTTOM) // bottom edge?
105 {
106 SetPosition(pPosition->X, BOTTOM - GetHalfDimensions().Y);
107 m_velocity.Y = 0;
108 }
109 }
110
111 Ship::Update(gameTime);
112}
113
115{
116 if (IsActive())
117 {
118 const float alpha = GetCurrentLevel()->GetAlpha();
119 spriteBatch.Draw(m_pTexture, GetPosition(), Color::White * alpha, m_pTexture->GetCenter());
120 }
121}
122
123
125{
126 return m_pTexture->GetCenter();
127}
128
129void PlayerShip::SetResponsiveness(const float responsiveness)
130{
131 m_responsiveness = Math::Clamp(0, 1, responsiveness);
132}
virtual void TranslatePosition(const float x, const float y)
virtual void SetPosition(const float x, const float y)
virtual bool IsActive() const
Flag to determine if the object is active.
Definition GameObject.h:42
static Level * GetCurrentLevel()
Get the current level.
Definition GameObject.h:30
virtual Vector2 & GetPosition()
Get the position of the object.
Definition GameObject.h:52
Represents a 2D grid of texels.
Definition AudioSample.h:21
virtual void SetVolume(const float volume)
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
static Vector2 GetScreenCenter()
Gets the screen size in pixels.
Definition Game.h:44
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
Handles the state of multiple player input devices.
Definition InputState.h:22
bool IsKeyDown(Key key) const
Determines if a keyboard key is currently being pressed down.
static T Clamp(const T min, const T max, const T value)
Restricts a value to be within a specified range.
Definition MathUtil.h:83
static const float NORMALIZE_PI_OVER4
Represents the value of each component in a pi/4 unit vector.
Definition MathUtil.h:29
Loads and manages the lifespan of objects from external files.
T * Load(const std::string &path, const bool cache=true, const bool appendContentPath=true)
Load and manage a resource.
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
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
static const Vector2 ZERO
A vector with both of its components set to zero.
Definition Vector2.h:31
static Vector2 Lerp(const Vector2 &start, const Vector2 &end, const float value)
Linearly interpolate between two vectors.
Definition Vector2.cpp:70
float X
The x-coordinate of the vector.
Definition Vector2.h:186
Represents a level in the game.
Definition Level.h:15
virtual float GetAlpha() const
Get the alpha value of the screen.
Definition Level.cpp:212
virtual void ConfineToScreen(const bool isConfined=true)
Prevents the player ship from moving off the screen.
Definition PlayerShip.h:56
virtual void HandleInput(const InputState &input)
Handles input for the player ship.
virtual void SetResponsiveness(const float responsiveness)
Sets the responsiveness of the player ship.
virtual Vector2 GetHalfDimensions() const
Gets the half dimensions of the player ship.
virtual float GetResponsiveness() const
Gets the responsiveness of the player ship.
Definition PlayerShip.h:67
virtual void LoadContent(ResourceManager &resourceManager)
Loads the content for the player ship.
Definition PlayerShip.cpp:5
virtual void Draw(SpriteBatch &spriteBatch)
Draws the player ship.
virtual void SetDesiredDirection(const Vector2 direction)
Sets the desired direction of the player ship, based on input.
Definition PlayerShip.h:44
virtual void Update(const GameTime &gameTime)
Updates the player ship.
virtual void Update(const GameTime &gameTime)
Updates the ship.
Definition Ship.cpp:14
virtual void Initialize()
Initializes the ship.
Definition Ship.cpp:56
virtual void FireWeapons(TriggerType type=TriggerType::All)
Fires the ship's weapons.
Definition Ship.cpp:61
virtual Weapon * GetWeapon(const std::string &key)
Gets a weapon from the ship's weapon list.
Definition Ship.h:83
virtual float GetSpeed() const
Gets the speed of the ship.
Definition Ship.h:55
Represents a type of trigger, which can be used to activate weapons.
Definition TriggerType.h:8
static const TriggerType None
Definition TriggerType.h:16
static const TriggerType Primary
Definition TriggerType.h:17
virtual void SetFireSound(AudioSample *pSound)
Set the sound that will be played when the weapon is fired.
Definition Weapon.h:72