-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerShip.cpp
More file actions
229 lines (201 loc) · 8.5 KB
/
Copy pathPlayerShip.cpp
File metadata and controls
229 lines (201 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
///////////////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2019, 2025 Craig J. Lipinski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////
#include <math.h>
#include "Game.h"
#include "GameConfig.h"
#include "PlayerShip.h"
#include "PlayerBullet.h"
//////////////////////////////////////////////////////////////////////////
// Ctor
//////////////////////////////////////////////////////////////////////////
PlayerShip::PlayerShip(Game* pGame, int x, int y) : GameEntity(pGame, x, y)
{
// Set initial defaults
m_velocityX = 0;
m_velocityY = 0;
m_nLastPlayerBulletFireTime = 0;
m_bEnginesOn = false;
m_nEnginesOnFrameCounter = 0;
// Try and create the animated image
m_pShipImage = new AnimImage("gfx/00ShipFrames1.bmp", SHIP_IMAGE_NUM_FRAMES, SHIP_IMAGE_WIDTH, SHIP_IMAGE_HEIGHT);
// Now load the images displayed when the ship is firing engines
m_pShipImagesThrusting[0] = new AnimImage("gfx/00ShipFrames2.bmp", SHIP_IMAGE_NUM_FRAMES, SHIP_IMAGE_WIDTH, SHIP_IMAGE_HEIGHT);
m_pShipImagesThrusting[1] = new AnimImage("gfx/00ShipFrames3.bmp", SHIP_IMAGE_NUM_FRAMES, SHIP_IMAGE_WIDTH, SHIP_IMAGE_HEIGHT);
m_pShipImagesThrusting[2] = new AnimImage("gfx/00ShipFrames4.bmp", SHIP_IMAGE_NUM_FRAMES, SHIP_IMAGE_WIDTH, SHIP_IMAGE_HEIGHT);
}
//////////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////////
PlayerShip::~PlayerShip()
{
// Clean up main image
if (m_pShipImage)
{
delete m_pShipImage;
}
// Clean up array of "thrusting" images
for( auto pImage : m_pShipImagesThrusting )
{
if (pImage)
{
delete pImage;
}
}
}
//////////////////////////////////////////////////////////////////////////
// HandleUserInput
//////////////////////////////////////////////////////////////////////////
void PlayerShip::HandleUserInput()
{
// Query the current state of the keyboard
const Uint8* stateArray = SDL_GetKeyboardState(NULL);
// Reset defaults
m_bEnginesOn = false;
// Check Turn Left Key
if (stateArray[GameConfig::Controls::TURN_LEFT] == 1)
{
m_rotationAngle -= GameConfig::PlayerShip::TURN_RATE;
if ( m_rotationAngle < 0.0 )
{
m_rotationAngle = m_rotationAngle + 360.0;
}
}
// Check Turn Right Key
if (stateArray[GameConfig::Controls::TURN_RIGHT] == 1)
{
m_rotationAngle += GameConfig::PlayerShip::TURN_RATE;
if ( m_rotationAngle > 360.0 )
{
m_rotationAngle = m_rotationAngle - 360.0;
}
}
// Check Forward Thrust Key
if (stateArray[GameConfig::Controls::FORWARD_THRUST] == 1)
{
m_velocityX += ( GameConfig::PlayerShip::FORWARD_THRUST_AMT * cos(GetRotationAngleInRads()) );
m_velocityY += ( GameConfig::PlayerShip::FORWARD_THRUST_AMT * sin(GetRotationAngleInRads()) );
// Engines are "on" so we'll make sure and display that animation later
m_bEnginesOn = true;
}
// Check Reverse Thrust Key
if (stateArray[GameConfig::Controls::REVERSE_THRUST] == 1)
{
m_velocityX -= ( GameConfig::PlayerShip::REVERSE_THRUST_AMT * cos(GetRotationAngleInRads()) );
m_velocityY -= ( GameConfig::PlayerShip::REVERSE_THRUST_AMT * sin(GetRotationAngleInRads()) );
}
// Check Fire Key
if (stateArray[GameConfig::Controls::FIRE] == 1)
{
TryFireBullet();
}
}
//////////////////////////////////////////////////////////////////////////
// TryFireBullet
//////////////////////////////////////////////////////////////////////////
void PlayerShip::TryFireBullet()
{
const unsigned int currentTime = SDL_GetTicks();
// Limit firing speed so holding the fire key does not create a bullet each frame.
if ((m_nLastPlayerBulletFireTime != 0) &&
((currentTime - m_nLastPlayerBulletFireTime) < GameConfig::PlayerShip::PLAYER_BULLET_FIRE_RATE))
{
return;
}
const int bulletSpawnX = static_cast<int>(m_X);
const int bulletSpawnY = static_cast<int>(m_Y);
m_pGame->AddEntityToBackLayer(new PlayerBullet(m_pGame,
bulletSpawnX,
bulletSpawnY,
m_rotationAngle,
m_velocityX,
m_velocityY));
m_nLastPlayerBulletFireTime = currentTime;
}
//////////////////////////////////////////////////////////////////////////
// Update
//////////////////////////////////////////////////////////////////////////
/*virtual*/ void PlayerShip::Update()
{
// Update oursleves based on any input from the user
HandleUserInput();
// NOTE: We've implemented a different movement system than the
// default GameEntity object. We apply thrust in the X and Y
// direction to make the ship control in an "Asteroids" style.
m_X += m_velocityX;
m_Y += m_velocityY;
// Call base class to update position
GameEntity::HandleWrapAround();
}
//////////////////////////////////////////////////////////////////////////
// Render
//////////////////////////////////////////////////////////////////////////
/*virtual*/ void PlayerShip::Render(SDL_Surface* pSurface)
{
// Calculate what row and col in the image we will display based on
// the rotation of the ship
int nFrame = (m_rotationAngle / (360.0 / (double)SHIP_IMAGE_NUM_FRAMES));
// NOTE: We only have 1 "point right" frame, so if we wrap around 360 degrees, just
// pass in frame 0
if (nFrame == SHIP_IMAGE_NUM_FRAMES)
{
nFrame = 0;
}
// Copy the correct frame to the screen
// NOTE: We want the X and Y to be in the center of the ship, so
// we will adjust by half the size of the image
if (!m_bEnginesOn)
{
// Draw the "no engines firing" version of the ship
m_pShipImage->BltFrame(nFrame, m_X - ( SHIP_IMAGE_WIDTH / 2 ), m_Y - ( SHIP_IMAGE_HEIGHT / 2 ), pSurface);
// Reset "engines on" animation frame counter
m_nEnginesOnFrameCounter = 0;
}
else
{
// Show each "engines on" animation frame for ENGINES_ON_ANIMATION_FRAMES number of frames
// Low Engine Fire
if (m_nEnginesOnFrameCounter < (GameConfig::PlayerShip::ENGINES_ON_ANIMATION_FRAMES * 1))
{
m_pShipImagesThrusting[0]->BltFrame(nFrame, m_X - ( SHIP_IMAGE_WIDTH / 2 ), m_Y - ( SHIP_IMAGE_HEIGHT / 2 ), pSurface);
}
// Med Engine Fire
else if (m_nEnginesOnFrameCounter < (GameConfig::PlayerShip::ENGINES_ON_ANIMATION_FRAMES * 2))
{
m_pShipImagesThrusting[1]->BltFrame(nFrame, m_X - ( SHIP_IMAGE_WIDTH / 2 ), m_Y - ( SHIP_IMAGE_HEIGHT / 2 ), pSurface);
}
// High Engine Fire
else if (m_nEnginesOnFrameCounter < (GameConfig::PlayerShip::ENGINES_ON_ANIMATION_FRAMES * 3))
{
m_pShipImagesThrusting[2]->BltFrame(nFrame, m_X - ( SHIP_IMAGE_WIDTH / 2 ), m_Y - ( SHIP_IMAGE_HEIGHT / 2 ), pSurface);
}
// Back to Low
else
{
m_pShipImagesThrusting[0]->BltFrame(nFrame, m_X - ( SHIP_IMAGE_WIDTH / 2 ), m_Y - ( SHIP_IMAGE_HEIGHT / 2 ), pSurface);
m_nEnginesOnFrameCounter = 0;
}
// Increment "engines on" frame counter
m_nEnginesOnFrameCounter++;
}
}