Skip to content

Commit 75f84ce

Browse files
committed
Chapter 6 copied over from working
1 parent 8f8e374 commit 75f84ce

54 files changed

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

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

‎Chapter06/Actor.h‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
47+
State GetState() const { return mState; }
48+
void SetState(State state) { mState = state; }
49+
50+
class Game* GetGame() { return mGame; }
51+
52+
53+
// Add/remove components
54+
void AddComponent(class Component* component);
55+
void RemoveComponent(class Component* component);
56+
private:
57+
// Actor's state
58+
State mState;
59+
60+
// Transform
61+
Matrix4 mWorldTransform;
62+
Vector3 mPosition;
63+
Quaternion mRotation;
64+
float mScale;
65+
66+
std::vector<class Component*> mComponents;
67+
class Game* mGame;
68+
};

‎Chapter06/Assets/Cube.gpmesh‎

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

‎Chapter06/Assets/Cube.png‎

13.6 KB
Loading

‎Chapter06/Assets/Default.png‎

1.53 KB
Loading

‎Chapter06/Assets/HealthBar.png‎

3.93 KB
Loading

‎Chapter06/Assets/Plane.gpmesh‎

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

‎Chapter06/Assets/Plane.png‎

553 KB
Loading

‎Chapter06/Assets/Radar.png‎

11.5 KB
Loading

0 commit comments

Comments
 (0)