forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.h
More file actions
125 lines (103 loc) · 3.27 KB
/
Actor.h
File metadata and controls
125 lines (103 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#pragma once
#include <vector>
#include "Math.h"
#include <rapidjson/document.h>
#include "Component.h"
class Actor
{
public:
enum TypeID
{
TActor = 0,
TBallActor,
TFollowActor,
TPlaneActor,
TTargetActor,
NUM_ACTOR_TYPES
};
static const char* TypeNames[NUM_ACTOR_TYPES];
enum State
{
EActive,
EPaused,
EDead
};
Actor(class Game* game);
virtual ~Actor();
// Update function called from Game (not overridable)
void Update(float deltaTime);
// Updates all the components attached to the actor (not overridable)
void UpdateComponents(float deltaTime);
// Any actor-specific update code (overridable)
virtual void UpdateActor(float deltaTime);
// ProcessInput function called from Game (not overridable)
void ProcessInput(const uint8_t* keyState);
// Any actor-specific input code (overridable)
virtual void ActorInput(const uint8_t* keyState);
// Getters/setters
const Vector3& GetPosition() const { return mPosition; }
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeTransform = true; }
float GetScale() const { return mScale; }
void SetScale(float scale) { mScale = scale; mRecomputeTransform = true; }
const Quaternion& GetRotation() const { return mRotation; }
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeTransform = true; }
void ComputeWorldTransform();
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
Vector3 GetForward() const { return Vector3::Transform(Vector3::UnitX, mRotation); }
Vector3 GetRight() const { return Vector3::Transform(Vector3::UnitY, mRotation); }
void RotateToNewForward(const Vector3& forward);
State GetState() const { return mState; }
void SetState(State state) { mState = state; }
class Game* GetGame() { return mGame; }
// Add/remove components
void AddComponent(class Component* component);
void RemoveComponent(class Component* component);
// Load/Save
virtual void LoadProperties(const rapidjson::Value& inObj);
virtual void SaveProperties(rapidjson::Document::AllocatorType& alloc,
rapidjson::Value& inObj) const;
// Create an actor with specified properties
template <typename T>
static Actor* Create(class Game* game, const rapidjson::Value& inObj)
{
// Dynamically allocate actor of type T
T* t = new T(game);
// Call LoadProperties on new actor
t->LoadProperties(inObj);
return t;
}
// Search throuch component vector for one of type
Component* GetComponentOfType(Component::TypeID type)
{
Component* comp = nullptr;
for (Component* c : mComponents)
{
if (c->GetType() == type)
{
comp = c;
break;
}
}
return comp;
}
virtual TypeID GetType() const { return TActor; }
const std::vector<Component*>& GetComponents() const { return mComponents; }
private:
// Actor's state
State mState;
// Transform
Matrix4 mWorldTransform;
Vector3 mPosition;
Quaternion mRotation;
float mScale;
bool mRecomputeTransform;
std::vector<Component*> mComponents;
class Game* mGame;
};