Skip to content

Commit 8495919

Browse files
committed
Chapter 7 copied over
1 parent fbde111 commit 8495919

62 files changed

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

‎Chapter07/Actor.cpp‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 "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+
,mRecomputeWorldTransform(true)
21+
{
22+
mGame->AddActor(this);
23+
}
24+
25+
Actor::~Actor()
26+
{
27+
mGame->RemoveActor(this);
28+
// Need to delete components
29+
// Because ~Component calls RemoveComponent, need a different style loop
30+
while (!mComponents.empty())
31+
{
32+
delete mComponents.back();
33+
}
34+
}
35+
36+
void Actor::Update(float deltaTime)
37+
{
38+
if (mState == EActive)
39+
{
40+
ComputeWorldTransform();
41+
42+
UpdateComponents(deltaTime);
43+
UpdateActor(deltaTime);
44+
45+
ComputeWorldTransform();
46+
}
47+
}
48+
49+
void Actor::UpdateComponents(float deltaTime)
50+
{
51+
for (auto comp : mComponents)
52+
{
53+
comp->Update(deltaTime);
54+
}
55+
}
56+
57+
void Actor::UpdateActor(float deltaTime)
58+
{
59+
}
60+
61+
void Actor::ProcessInput(const uint8_t* keyState)
62+
{
63+
if (mState == EActive)
64+
{
65+
// First process input for components
66+
for (auto comp : mComponents)
67+
{
68+
comp->ProcessInput(keyState);
69+
}
70+
71+
ActorInput(keyState);
72+
}
73+
}
74+
75+
void Actor::ActorInput(const uint8_t* keyState)
76+
{
77+
}
78+
79+
void Actor::ComputeWorldTransform()
80+
{
81+
if (mRecomputeWorldTransform)
82+
{
83+
mRecomputeWorldTransform = false;
84+
// Scale, then rotate, then translate
85+
mWorldTransform = Matrix4::CreateScale(mScale);
86+
mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
87+
mWorldTransform *= Matrix4::CreateTranslation(mPosition);
88+
89+
// Inform components world transform updated
90+
for (auto comp : mComponents)
91+
{
92+
comp->OnUpdateWorldTransform();
93+
}
94+
}
95+
}
96+
97+
void Actor::AddComponent(Component* component)
98+
{
99+
// Find the insertion point in the sorted vector
100+
// (The first element with a order higher than me)
101+
int myOrder = component->GetUpdateOrder();
102+
auto iter = mComponents.begin();
103+
for (;
104+
iter != mComponents.end();
105+
++iter)
106+
{
107+
if (myOrder < (*iter)->GetUpdateOrder())
108+
{
109+
break;
110+
}
111+
}
112+
113+
// Inserts element before position of iterator
114+
mComponents.insert(iter, component);
115+
}
116+
117+
void Actor::RemoveComponent(Component* component)
118+
{
119+
auto iter = std::find(mComponents.begin(), mComponents.end(), component);
120+
if (iter != mComponents.end())
121+
{
122+
mComponents.erase(iter);
123+
}
124+
}

‎Chapter07/Actor.h‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 <vector>
11+
#include "Math.h"
12+
#include <cstdint>
13+
14+
class Actor
15+
{
16+
public:
17+
enum State
18+
{
19+
EActive,
20+
EPaused,
21+
EDead
22+
};
23+
24+
Actor(class Game* game);
25+
virtual ~Actor();
26+
27+
// Update function called from Game (not overridable)
28+
void Update(float deltaTime);
29+
// Updates all the components attached to the actor (not overridable)
30+
void UpdateComponents(float deltaTime);
31+
// Any actor-specific update code (overridable)
32+
virtual void UpdateActor(float deltaTime);
33+
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+
39+
// Getters/setters
40+
const Vector3& GetPosition() const { return mPosition; }
41+
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
42+
float GetScale() const { return mScale; }
43+
void SetScale(float scale) { mScale = scale; mRecomputeWorldTransform = true; }
44+
const Quaternion& GetRotation() const { return mRotation; }
45+
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeWorldTransform = true; }
46+
47+
void ComputeWorldTransform();
48+
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
49+
50+
Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
51+
52+
State GetState() const { return mState; }
53+
void SetState(State state) { mState = state; }
54+
55+
class Game* GetGame() { return mGame; }
56+
57+
58+
// Add/remove components
59+
void AddComponent(class Component* component);
60+
void RemoveComponent(class Component* component);
61+
private:
62+
// Actor's state
63+
State mState;
64+
65+
// Transform
66+
Matrix4 mWorldTransform;
67+
Vector3 mPosition;
68+
Quaternion mRotation;
69+
float mScale;
70+
bool mRecomputeWorldTransform;
71+
72+
std::vector<class Component*> mComponents;
73+
class Game* mGame;
74+
};

‎Chapter07/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+
}

‎Chapter07/Assets/Cube.png‎

13.6 KB
Loading

‎Chapter07/Assets/Default.png‎

1.53 KB
Loading

‎Chapter07/Assets/HealthBar.png‎

3.93 KB
Loading

‎Chapter07/Assets/Master Bank.bank‎

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

‎Chapter07/Assets/Plane.gpmesh‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"version":1,
3+
"vertexformat":"PosNormTex",
4+
"shader":"BasicMesh",
5+
"textures":[
6+
"Assets/Plane.png"
7+
],
8+
"vertices":[
9+
[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
10+
[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],
11+
[25.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.750000,1.000000],
12+
[25.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.750000,0.750000],
13+
[0.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.500000,1.000000],
14+
[50.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.500000],
15+
[0.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.500000,0.750000],
16+
[-25.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.250000,1.000000],
17+
[25.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.500000],
18+
[50.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.250000],
19+
[-25.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.250000,0.750000],
20+
[-50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,0.000000,1.000000],
21+
[-50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,0.000000,0.750000],
22+
[0.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.500000],
23+
[25.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.250000],
24+
[50.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,1.000000,0.000000],
25+
[25.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.750000,0.000000],
26+
[-25.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.500000],
27+
[-50.000000,-0.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.500000],
28+
[0.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.250000],
29+
[0.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.500000,0.000000],
30+
[-25.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.250000],
31+
[-50.000000,-25.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.250000],
32+
[-25.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.250000,0.000000],
33+
[-50.000000,-50.000000,0.000000,-0.003922,-0.003922,1.000000,0.000000,0.000000]
34+
],
35+
"indices":[
36+
[0,1,2],
37+
[3,2,1],
38+
[2,3,4],
39+
[1,5,3],
40+
[6,4,3],
41+
[4,6,7],
42+
[8,3,5],
43+
[3,8,6],
44+
[5,9,8],
45+
[10,7,6],
46+
[7,10,11],
47+
[12,11,10],
48+
[13,6,8],
49+
[6,13,10],
50+
[14,8,9],
51+
[8,14,13],
52+
[9,15,14],
53+
[16,14,15],
54+
[10,17,12],
55+
[17,10,13],
56+
[18,12,17],
57+
[19,13,14],
58+
[14,16,19],
59+
[13,19,17],
60+
[20,19,16],
61+
[17,21,18],
62+
[21,17,19],
63+
[19,20,21],
64+
[22,18,21],
65+
[23,21,20],
66+
[21,23,22],
67+
[24,22,23]
68+
]
69+
}

‎Chapter07/Assets/Plane.png‎

553 KB
Loading

0 commit comments

Comments
 (0)