Skip to content

Commit 2d71b1b

Browse files
committed
Chapter 4 (first push)
1 parent 481d2c7 commit 2d71b1b

49 files changed

Lines changed: 4019 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Chapter04/Actor.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "Actor.h"
10+
#include "Game.h"
11+
#include "Component.h"
12+
#include <algorithm>
13+
14+
Actor::Actor(Game* game)
15+
:mState(EActive)
16+
, mPosition(Vector2::Zero)
17+
, mScale(1.0f)
18+
, mRotation(0.0f)
19+
, mGame(game)
20+
{
21+
mGame->AddActor(this);
22+
}
23+
24+
Actor::~Actor()
25+
{
26+
mGame->RemoveActor(this);
27+
// Need to delete components
28+
// Because ~Component calls RemoveComponent, need a different style loop
29+
while (!mComponents.empty())
30+
{
31+
delete mComponents.back();
32+
}
33+
}
34+
35+
void Actor::Update(float deltaTime)
36+
{
37+
if (mState == EActive)
38+
{
39+
UpdateComponents(deltaTime);
40+
UpdateActor(deltaTime);
41+
}
42+
}
43+
44+
void Actor::UpdateComponents(float deltaTime)
45+
{
46+
for (auto comp : mComponents)
47+
{
48+
comp->Update(deltaTime);
49+
}
50+
}
51+
52+
void Actor::UpdateActor(float deltaTime)
53+
{
54+
}
55+
56+
void Actor::AddComponent(Component* component)
57+
{
58+
mComponents.emplace_back(component);
59+
std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
60+
return a->GetUpdateOrder() < b->GetUpdateOrder();
61+
});
62+
}
63+
64+
void Actor::RemoveComponent(Component* component)
65+
{
66+
auto iter = std::find(mComponents.begin(), mComponents.end(), component);
67+
if (iter != mComponents.end())
68+
{
69+
mComponents.erase(iter);
70+
}
71+
}

Chapter04/Actor.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.txt for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include <vector>
11+
#include "Math.h"
12+
class Actor
13+
{
14+
public:
15+
enum State
16+
{
17+
EActive,
18+
EPaused,
19+
EDead
20+
};
21+
22+
Actor(class Game* game);
23+
virtual ~Actor();
24+
25+
// Update function called from Game (not overridable)
26+
void Update(float deltaTime);
27+
// Updates all the components attached to the actor (not overridable)
28+
void UpdateComponents(float deltaTime);
29+
// Any actor-specific update code (overridable)
30+
virtual void UpdateActor(float deltaTime);
31+
32+
// Getters/setters
33+
const Vector2& GetPosition() const { return mPosition; }
34+
void SetPosition(const Vector2& pos) { mPosition = pos; }
35+
float GetScale() const { return mScale; }
36+
void SetScale(float scale) { mScale = scale; }
37+
float GetRotation() const { return mRotation; }
38+
void SetRotation(float rotation) { mRotation = rotation; }
39+
40+
Vector2 GetForward() const { return Vector2(Math::Cos(mRotation), -Math::Sin(mRotation)); }
41+
42+
State GetState() const { return mState; }
43+
void SetState(State state) { mState = state; }
44+
45+
class Game* GetGame() { return mGame; }
46+
47+
48+
// Add/remove components
49+
void AddComponent(class Component* component);
50+
void RemoveComponent(class Component* component);
51+
private:
52+
// Actor's state
53+
State mState;
54+
55+
// Transform
56+
Vector2 mPosition;
57+
float mScale;
58+
float mRotation;
59+
60+
std::vector<class Component*> mComponents;
61+
class Game* mGame;
62+
};

Chapter04/Assets/Airplane.png

1.6 KB
Loading

Chapter04/Assets/Base.png

2.18 KB
Loading

Chapter04/Assets/LICENSE.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These sprites created by Kenney.nl
2+
3+
Used under CC0 license.

Chapter04/Assets/Missile.png

869 Bytes
Loading

Chapter04/Assets/Projectile.png

707 Bytes
Loading

Chapter04/Assets/TileBrown.png

1.46 KB
Loading
1.31 KB
Loading

Chapter04/Assets/TileGreen.png

1.7 KB
Loading

0 commit comments

Comments
 (0)