Skip to content

Commit deac8f1

Browse files
committed
Chapter 9 initial copy
1 parent 225600f commit deac8f1

84 files changed

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

‎Chapter09/Actor.cpp‎

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

‎Chapter09/Actor.h‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// Any actor-specific input code (overridable)
32+
virtual void ProcessInput(const uint8_t* keys) { }
33+
34+
// Getters/setters
35+
const Vector3& GetPosition() const { return mPosition; }
36+
void SetPosition(const Vector3& pos) { mPosition = pos; ComputeWorldTransform(); }
37+
float GetScale() const { return mScale; }
38+
void SetScale(float scale) { mScale = scale; ComputeWorldTransform(); }
39+
const Quaternion& GetRotation() const { return mRotation; }
40+
void SetRotation(const Quaternion& rotation) { mRotation = rotation; ComputeWorldTransform(); }
41+
42+
void ComputeWorldTransform();
43+
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
44+
45+
Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
46+
Vector3 GetRight() const { return Vector3::Transform(Vector3::UnitY, mRotation); }
47+
48+
State GetState() const { return mState; }
49+
void SetState(State state) { mState = state; }
50+
51+
class Game* GetGame() { return mGame; }
52+
53+
54+
// Add/remove components
55+
void AddComponent(class Component* component);
56+
void RemoveComponent(class Component* component);
57+
private:
58+
// Actor's state
59+
State mState;
60+
61+
// Transform
62+
Matrix4 mWorldTransform;
63+
Vector3 mPosition;
64+
Quaternion mRotation;
65+
float mScale;
66+
67+
std::vector<class Component*> mComponents;
68+
class Game* mGame;
69+
};

‎Chapter09/Assets/Crosshair.png‎

249 Bytes
Loading

‎Chapter09/Assets/Cube.gpmesh‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"version":1,
3+
"vertexformat":"PosNormTex",
4+
"shader":"BasicMesh",
5+
"textures":[
6+
"Assets/Cube.png"
7+
],
8+
"vertices":[
9+
[-0.5,-0.5,-0.5,0,0,-1,0,0],
10+
[0.5,-0.5,-0.5,0,0,-1,1,0],
11+
[-0.5,0.5,-0.5,0,0,-1,0,-1],
12+
[0.5,0.5,-0.5,0,0,-1,1,-1],
13+
[-0.5,0.5,0.5,0,1,0,0,-1],
14+
[0.5,0.5,0.5,0,1,0,1,-1],
15+
[-0.5,-0.5,0.5,0,0,1,0,0],
16+
[0.5,-0.5,0.5,0,0,1,1,0],
17+
[-0.5,0.5,-0.5,0,0,-1,0,-1],
18+
[0.5,-0.5,-0.5,0,0,-1,1,0],
19+
[-0.5,0.5,-0.5,0,1,0,0,-1],
20+
[0.5,0.5,-0.5,0,1,0,1,-1],
21+
[-0.5,0.5,0.5,0,1,0,0,-1],
22+
[-0.5,0.5,0.5,0,0,1,0,-1],
23+
[0.5,0.5,0.5,0,0,1,1,-1],
24+
[-0.5,-0.5,0.5,0,0,1,0,0],
25+
[-0.5,-0.5,0.5,0,-1,0,0,0],
26+
[0.5,-0.5,0.5,0,-1,0,1,0],
27+
[-0.5,-0.5,-0.5,0,-1,0,0,0],
28+
[0.5,-0.5,-0.5,0,-1,0,1,0],
29+
[0.5,-0.5,-0.5,1,0,0,1,0],
30+
[0.5,-0.5,0.5,1,0,0,1,0],
31+
[0.5,0.5,-0.5,1,0,0,1,-1],
32+
[0.5,0.5,0.5,1,0,0,1,-1],
33+
[-0.5,-0.5,0.5,-1,0,0,0,0],
34+
[-0.5,-0.5,-0.5,-1,0,0,0,0],
35+
[-0.5,0.5,0.5,-1,0,0,0,-1],
36+
[-0.5,0.5,-0.5,-1,0,0,0,-1]
37+
],
38+
"indices":[
39+
[2,1,0],
40+
[3,9,8],
41+
[4,11,10],
42+
[5,11,12],
43+
[6,14,13],
44+
[7,14,15],
45+
[18,17,16],
46+
[19,17,18],
47+
[22,21,20],
48+
[23,21,22],
49+
[26,25,24],
50+
[27,25,26]
51+
]
52+
}

‎Chapter09/Assets/Cube.png‎

13.6 KB
Loading

‎Chapter09/Assets/Default.png‎

1.53 KB
Loading

‎Chapter09/Assets/HealthBar.png‎

3.93 KB
Loading

‎Chapter09/Assets/LICENSE.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Racing Car by Willy Decarpentrie, licensed under CC Attribution and downloaded from https://sketchfab.com

‎Chapter09/Assets/Master Bank.bank‎

2.94 MB
Binary file not shown.
946 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)