Skip to content

Commit 25cb1fe

Browse files
committed
Chapter 3 code done
1 parent de77525 commit 25cb1fe

21 files changed

Lines changed: 537 additions & 57 deletions

‎Chapter03/Actor.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ void Actor::UpdateActor(float deltaTime)
5353
{
5454
}
5555

56+
void Actor::ProcessInput(const uint8_t* keyState)
57+
{
58+
if (mState == EActive)
59+
{
60+
// First process input for components
61+
for (auto comp : mComponents)
62+
{
63+
comp->ProcessInput(keyState);
64+
}
65+
66+
ActorInput(keyState);
67+
}
68+
}
69+
70+
void Actor::ActorInput(const uint8_t* keyState)
71+
{
72+
}
73+
5674
void Actor::AddComponent(Component* component)
5775
{
5876
// Find the insertion point in the sorted vector

‎Chapter03/Actor.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#pragma once
1010
#include <vector>
1111
#include "Math.h"
12+
#include <cstdint>
13+
1214
class Actor
1315
{
1416
public:
@@ -29,6 +31,11 @@ class Actor
2931
// Any actor-specific update code (overridable)
3032
virtual void UpdateActor(float deltaTime);
3133

34+
// ProcessInput function called from Game (not overridable)
35+
void ProcessInput(const uint8_t* keyState);
36+
// Any actor-specific input code (overridable)
37+
virtual void ActorInput(const uint8_t* keyState);
38+
3239
// Getters/setters
3340
const Vector2& GetPosition() const { return mPosition; }
3441
void SetPosition(const Vector2& pos) { mPosition = pos; }

‎Chapter03/Asteroid.cpp‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "Asteroid.h"
10+
#include "SpriteComponent.h"
11+
#include "MoveComponent.h"
12+
#include "Game.h"
13+
#include "Random.h"
14+
#include "CircleComponent.h"
15+
16+
Asteroid::Asteroid(Game* game)
17+
:Actor(game)
18+
,mCircle(nullptr)
19+
{
20+
// Initialize to random position/orientation
21+
Vector2 randPos = Random::GetVector(Vector2::Zero,
22+
Vector2(1024.0f, 768.0f));
23+
SetPosition(randPos);
24+
25+
SetRotation(Random::GetFloatRange(0.0f, Math::TwoPi));
26+
27+
// Create a sprite component
28+
SpriteComponent* sc = new SpriteComponent(this);
29+
sc->SetTexture(game->GetTexture("Assets/Asteroid.png"));
30+
31+
// Create a move component, and set a forward speed
32+
MoveComponent* mc = new MoveComponent(this);
33+
mc->SetForwardSpeed(150.0f);
34+
35+
// Create a circle component (for collision)
36+
mCircle = new CircleComponent(this);
37+
mCircle->SetRadius(40.0f);
38+
39+
// Add to mAsteroids in game
40+
game->AddAsteroid(this);
41+
}
42+
43+
Asteroid::~Asteroid()
44+
{
45+
GetGame()->RemoveAsteroid(this);
46+
}

‎Chapter03/Asteroid.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include "Actor.h"
11+
class Asteroid : public Actor
12+
{
13+
public:
14+
Asteroid(class Game* game);
15+
~Asteroid();
16+
17+
class CircleComponent* GetCircle() { return mCircle; }
18+
private:
19+
class CircleComponent* mCircle;
20+
};

‎Chapter03/CircleComponent.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "CircleComponent.h"
10+
#include "Actor.h"
11+
12+
CircleComponent::CircleComponent(class Actor* owner)
13+
:Component(owner)
14+
,mRadius(0.0f)
15+
{
16+
17+
}
18+
19+
const Vector2& CircleComponent::GetCenter() const
20+
{
21+
return mOwner->GetPosition();
22+
}
23+
24+
float CircleComponent::GetRadius() const
25+
{
26+
return mOwner->GetScale() * mRadius;
27+
}
28+
29+
bool Intersect(const CircleComponent& a, const CircleComponent& b)
30+
{
31+
// Calculate distance squared
32+
Vector2 diff = a.GetCenter() - b.GetCenter();
33+
float distSq = diff.LengthSq();
34+
35+
// Calculate sum of radii squared
36+
float radiiSq = a.GetRadius() + b.GetRadius();
37+
radiiSq *= radiiSq;
38+
39+
return distSq <= radiiSq;
40+
}

‎Chapter03/CircleComponent.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include "Component.h"
11+
#include "Math.h"
12+
13+
class CircleComponent : public Component
14+
{
15+
public:
16+
CircleComponent(class Actor* owner);
17+
18+
void SetRadius(float radius) { mRadius = radius; }
19+
float GetRadius() const;
20+
21+
const Vector2& GetCenter() const;
22+
private:
23+
float mRadius;
24+
};
25+
26+
bool Intersect(const CircleComponent& a, const CircleComponent& b);

‎Chapter03/Component.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// ----------------------------------------------------------------
88

99
#pragma once
10+
#include <cstdint>
11+
1012
class Component
1113
{
1214
public:
@@ -17,6 +19,8 @@ class Component
1719
virtual ~Component();
1820
// Update this component by delta time
1921
virtual void Update(float deltaTime);
22+
// Process input for this component
23+
virtual void ProcessInput(const uint8_t* keyState) {}
2024

2125
int GetUpdateOrder() const { return mUpdateOrder; }
2226
protected:

‎Chapter03/Game.cpp‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "Actor.h"
1313
#include "SpriteComponent.h"
1414
#include "Ship.h"
15+
#include "Asteroid.h"
16+
#include "Random.h"
1517

1618
Game::Game()
1719
:mWindow(nullptr)
@@ -50,6 +52,8 @@ bool Game::Initialize()
5052
return false;
5153
}
5254

55+
Random::Init();
56+
5357
LoadData();
5458

5559
mTicksCount = SDL_GetTicks();
@@ -80,14 +84,18 @@ void Game::ProcessInput()
8084
}
8185
}
8286

83-
const Uint8* state = SDL_GetKeyboardState(NULL);
84-
if (state[SDL_SCANCODE_ESCAPE])
87+
const Uint8* keyState = SDL_GetKeyboardState(NULL);
88+
if (keyState[SDL_SCANCODE_ESCAPE])
8589
{
8690
mIsRunning = false;
8791
}
8892

89-
// Process ship input
90-
mShip->ProcessKeyboard(state);
93+
mUpdatingActors = true;
94+
for (auto actor : mActors)
95+
{
96+
actor->ProcessInput(keyState);
97+
}
98+
mUpdatingActors = false;
9199
}
92100

93101
void Game::UpdateGame()
@@ -138,7 +146,7 @@ void Game::UpdateGame()
138146

139147
void Game::GenerateOutput()
140148
{
141-
SDL_SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
149+
SDL_SetRenderDrawColor(mRenderer, 220, 220, 220, 255);
142150
SDL_RenderClear(mRenderer);
143151

144152
// Draw all sprite components
@@ -154,7 +162,15 @@ void Game::LoadData()
154162
{
155163
// Create player's ship
156164
mShip = new Ship(this);
157-
mShip->SetPosition(Vector2(100.0f, 384.0f));
165+
mShip->SetPosition(Vector2(512.0f, 384.0f));
166+
mShip->SetRotation(Math::PiOver2);
167+
168+
// Create asteroids
169+
const int numAsteroids = 20;
170+
for (int i = 0; i < numAsteroids; i++)
171+
{
172+
new Asteroid(this);
173+
}
158174
}
159175

160176
void Game::UnloadData()
@@ -207,6 +223,21 @@ SDL_Texture* Game::GetTexture(const std::string& fileName)
207223
return tex;
208224
}
209225

226+
void Game::AddAsteroid(Asteroid* ast)
227+
{
228+
mAsteroids.emplace_back(ast);
229+
}
230+
231+
void Game::RemoveAsteroid(Asteroid* ast)
232+
{
233+
auto iter = std::find(mAsteroids.begin(),
234+
mAsteroids.end(), ast);
235+
if (iter != mAsteroids.end())
236+
{
237+
mAsteroids.erase(iter);
238+
}
239+
}
240+
210241
void Game::Shutdown()
211242
{
212243
UnloadData();

‎Chapter03/Game.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Game
2727
void RemoveSprite(class SpriteComponent* sprite);
2828

2929
SDL_Texture* GetTexture(const std::string& fileName);
30+
31+
// Game-specific (add/remove asteroid)
32+
void AddAsteroid(class Asteroid* ast);
33+
void RemoveAsteroid(class Asteroid* ast);
34+
std::vector<class Asteroid*>& GetAsteroids() { return mAsteroids; }
3035
private:
3136
void ProcessInput();
3237
void UpdateGame();
@@ -54,4 +59,5 @@ class Game
5459

5560
// Game-specific
5661
class Ship* mShip; // Player's ship
62+
std::vector<class Asteroid*> mAsteroids;
5763
};

‎Chapter03/Game.vcxproj‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@
1212
</ItemGroup>
1313
<ItemGroup>
1414
<ClCompile Include="Actor.cpp" />
15+
<ClCompile Include="Asteroid.cpp" />
16+
<ClCompile Include="CircleComponent.cpp" />
1517
<ClCompile Include="Component.cpp" />
1618
<ClCompile Include="Game.cpp" />
19+
<ClCompile Include="InputComponent.cpp" />
20+
<ClCompile Include="Laser.cpp" />
1721
<ClCompile Include="Main.cpp" />
1822
<ClCompile Include="Math.cpp" />
1923
<ClCompile Include="MoveComponent.cpp" />
24+
<ClCompile Include="Random.cpp" />
2025
<ClCompile Include="Ship.cpp" />
2126
<ClCompile Include="SpriteComponent.cpp" />
2227
</ItemGroup>
2328
<ItemGroup>
2429
<ClInclude Include="Actor.h" />
30+
<ClInclude Include="Asteroid.h" />
31+
<ClInclude Include="CircleComponent.h" />
2532
<ClInclude Include="Component.h" />
2633
<ClInclude Include="Game.h" />
34+
<ClInclude Include="InputComponent.h" />
35+
<ClInclude Include="Laser.h" />
2736
<ClInclude Include="Math.h" />
2837
<ClInclude Include="MoveComponent.h" />
38+
<ClInclude Include="Random.h" />
2939
<ClInclude Include="Ship.h" />
3040
<ClInclude Include="SpriteComponent.h" />
3141
</ItemGroup>

0 commit comments

Comments
 (0)