Skip to content

Commit 3a8b3a6

Browse files
committed
First copy of Chapter 5 code
1 parent 57c3130 commit 3a8b3a6

61 files changed

Lines changed: 4145 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.

Chapter05/Actor.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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::ComputeWorldTransform()
57+
{
58+
mWorldTransform = Matrix4::CreateScale(mScale);
59+
mWorldTransform *= Matrix4::CreateRotationZ(mRotation);
60+
mWorldTransform *= Matrix4::CreateTranslation(Vector3(mPosition.x, mPosition.y, 0.0f));
61+
}
62+
63+
void Actor::AddComponent(Component* component)
64+
{
65+
mComponents.emplace_back(component);
66+
std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
67+
return a->GetUpdateOrder() < b->GetUpdateOrder();
68+
});
69+
}
70+
71+
void Actor::RemoveComponent(Component* component)
72+
{
73+
auto iter = std::find(mComponents.begin(), mComponents.end(), component);
74+
if (iter != mComponents.end())
75+
{
76+
mComponents.erase(iter);
77+
}
78+
}

Chapter05/Actor.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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; ComputeWorldTransform(); }
35+
float GetScale() const { return mScale; }
36+
void SetScale(float scale) { mScale = scale; ComputeWorldTransform(); }
37+
float GetRotation() const { return mRotation; }
38+
void SetRotation(float rotation) { mRotation = rotation; ComputeWorldTransform(); }
39+
40+
void ComputeWorldTransform();
41+
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
42+
43+
Vector2 GetForward() const { return Vector2(Math::Cos(mRotation), Math::Sin(mRotation)); }
44+
45+
State GetState() const { return mState; }
46+
void SetState(State state) { mState = state; }
47+
48+
class Game* GetGame() { return mGame; }
49+
50+
51+
// Add/remove components
52+
void AddComponent(class Component* component);
53+
void RemoveComponent(class Component* component);
54+
private:
55+
// Actor's state
56+
State mState;
57+
58+
// Transform
59+
Matrix4 mWorldTransform;
60+
Vector2 mPosition;
61+
float mScale;
62+
float mRotation;
63+
64+
std::vector<class Component*> mComponents;
65+
class Game* mGame;
66+
};

Chapter05/Assets/Airplane.png

1.6 KB
Loading

Chapter05/Assets/Base.png

2.18 KB
Loading

Chapter05/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.

Chapter05/Assets/Missile.png

869 Bytes
Loading

Chapter05/Assets/Projectile.png

707 Bytes
Loading

Chapter05/Assets/TileBrown.png

1.46 KB
Loading
1.31 KB
Loading

Chapter05/Assets/TileGreen.png

1.7 KB
Loading

0 commit comments

Comments
 (0)