-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.h
More file actions
188 lines (162 loc) · 4.28 KB
/
Character.h
File metadata and controls
188 lines (162 loc) · 4.28 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
#include "SkeletalMesh.h"
enum AnimState
{
Idle,
Run,
Attack,
Fire,
React,
Death,
WalkForward,
WalkForwardLeft,
WalkForwardRight,
WalkBackward,
WalkBackwardLeft,
WalkBackwardRight,
WalkLeft,
WalkRight,
};
std::string AnimStateToString(AnimState state) {
switch (state) {
case AnimState::Idle: return "Idle";
case AnimState::Run: return "Run";
case AnimState::WalkForward: return "walk forward";
case AnimState::WalkForwardLeft: return "walk forward left";
case AnimState::WalkForwardRight: return "walk forward right";
case AnimState::WalkBackward: return "walk backward";
case AnimState::WalkBackwardLeft: return "walk backward left";
case AnimState::WalkBackwardRight: return "walk backward right";
case AnimState::WalkLeft: return "walk left";
case AnimState::WalkRight: return "walk right";
case AnimState::Attack: return "attack";
case AnimState::Fire: return "firing rifle";
case AnimState::React: return "roar";
case AnimState::Death: return "death";
default: return "Idle";
}
}
class Character {
private:
AnimationInstance animInstance;
AnimState state;
int health ;
float reactTime = 0.f;
bool react = false;
float deathTime = 0.f;
bool dead = false;
bool show = true;
float shootTime = 0.f;
bool shoot = false;
Ray testRay;
Line testLine;
public:
AnimatedModel animModel;
void init(DXCOre* core, std::string filename) {
animModel.init(core, filename);
animInstance.animation = &animModel.animation;
health = 100;
state = AnimState::Idle;
}
void drawLine(DXCOre* core) {
testLine.init(core, testRay.at(30.f), testRay.o);
testLine.draw(core);
}
void update(float dt, float x, float y, DXCOre* core) {
if (dead) {
deathTime += dt;
if (deathTime > 4.f) {
deathTime = 0.f;
show = false;
state = AnimState::Idle;
}
}
else if (react) {
reactTime += dt;
if (reactTime > 0.5f) {
reactTime = 0.f;
react = false;
state = AnimState::Idle;
}
}
else if (shoot) {
shootTime += dt;
if (shootTime > 0.5f) {
shootTime = 0.f;
shoot = false;
state = AnimState::Idle;
}
else {
drawLine(core);
}
}
else if (x > 0) {
if (y > 0) {
state = AnimState::WalkForwardLeft;
}
else if (y < 0) {
state = AnimState::WalkForwardRight;
}
else {
state = AnimState::WalkForward;
}
}
else if (x < 0) {
if (y > 0) {
state = AnimState::WalkBackwardLeft;
}
else if (y < 0) {
state = AnimState::WalkBackwardRight;
}
else {
state = AnimState::WalkBackward;
}
}
else if (y > 0) {
state = AnimState::WalkLeft;
}
else if (y < 0) {
state = AnimState::WalkRight;
}
else {
state = AnimState::Idle;
}
animInstance.update(AnimStateToString(state), dt);
}
void takeDamage(int damage) {
if (dead)return;
state = AnimState::Attack;
health -= damage;
health = max(0, health);
react = true;
if (health == 0) {
dead = true;
react = false;
state = AnimState::Death;
}
std::cout << health << "\n";
}
void shootAt(Character &Trex, Vec3 camDir) {
state = AnimState::Fire;
shoot = true;
testRay.init(animModel.getPos_Add_Y(), camDir * -1);
float TimeOfIntersection = 0.f;
if (Trex.animModel.checkRayCollision(testRay, TimeOfIntersection)) {
std::cout << "Ray collided at " << TimeOfIntersection << "\n";
Trex.takeDamage(5);
}
}
void draw(ShaderManager& shaders, std::string shadername, DXCOre* core, TextureManager* textures, std::string texVarname) {
if (!show)return;
//animInstance.update("Idle", dt);
shaders.updateConstantVS(shadername, "animatedMeshBuffer", "bones", animInstance.matrices);
animModel.drawTexture(shaders, shadername, core, textures, "animatedMeshBuffer", "W", texVarname);
}
void drawTextureNormal(ShaderManager& shaders, std::string shadername, DXCOre* core, TextureManager* textures, std::string buffername, std::string worldVarname, std::string texVarname, std::string NormalVarname, std::string NormaltexVarname) {
if (!show)return;
shaders.updateTexturePS(shadername, core, NormalVarname, textures->find(NormaltexVarname));
shaders.updateConstantVS(shadername, "animatedMeshBuffer", "bones", animInstance.matrices);
shaders.apply(shadername, core);
animModel.drawTexture(shaders, shadername, core, textures, "animatedMeshBuffer", "W", texVarname);
}
};