-
Notifications
You must be signed in to change notification settings - Fork 1
/
Knight.cpp
140 lines (109 loc) · 2.86 KB
/
Knight.cpp
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
#include "Knight.h"
Knight::Knight()
{
}
Knight::~Knight()
{
}
bool Knight::Initialize(Terrain * terrainPTR)
{
// initialize all the variables
speed = 100.0f;
strafeSpeed = 100.0f;
turnSpeed = 2.0f;
terrain = terrainPTR;
// initialize the models
characterMesh.Initialize("models/knight.x");
// set the scale
D3DXMatrixScaling(&S, 1.0f, 1.0f, 1.0f);
// set start position
_pos = { 900.0f, 0.0f, -900.0f };
// set start state
currentState = IDLE;
canJump = true;
return true;
}
void Knight::Render()
{
// IDirect3DDevice9* Device = graphics.GetDevice();
// the position and rotation translation matrix is the inverse of the characters's view matrix
getViewMatrix(&T);
D3DXMatrixInverse(&T, NULL, &T);
P = S*T;
Device->SetTransform(D3DTS_WORLD, &P);
characterMesh.Render();
// update the bounding box coordinates to match the mesh
D3DXVec3TransformCoord(&box.MIN, &characterMesh.min, &P);
D3DXVec3TransformCoord(&box.MAX, &characterMesh.max, &P);
}
void Knight::Update()
{
// make sure model doesn't go below the terrain
if (_pos.y < terrain->getHeight(_pos.x, _pos.z))
_pos.y = terrain->getHeight(_pos.x, _pos.z);
if (!isAuto) {
// update third person camera position (rear view)
thirdPersonCamera._pos = ((-_look * 20.0) + (_up * 10.0f)) + _pos;
Device->SetTransform(D3DTS_VIEW, &getRearView());
if (GetAsyncKeyState(VK_SPACE) && 0x8000f && canJump) {
canJump = false;
currentState = JUMP;
velocity.y = 20.0f;
prevPos = _pos;
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000f) {
yaw(timer.DeltaTime()* turnSpeed);
}
else
if (GetAsyncKeyState(VK_LEFT) & 0x8000f) {
yaw(-timer.DeltaTime() * turnSpeed);
}
else
if (GetAsyncKeyState('W') & 0x8000f) {
walk(timer.DeltaTime() * speed);
}
else
if (GetAsyncKeyState('S') & 0x8000f) {
walk(-timer.DeltaTime() * speed);
}
else
if (GetAsyncKeyState('A') & 0x8000f) {
strafe(-timer.DeltaTime() * strafeSpeed);
}
else
if (GetAsyncKeyState('D') & 0x8000f) {
strafe(timer.DeltaTime() * strafeSpeed);
}
if (currentState == JUMP) {
_pos.y += velocity.y*timer.DeltaTime();
velocity.y += -20.0f*timer.DeltaTime();
if (velocity.y <= 0.0f)
currentState = FALL;
if (_pos.y <= terrain->getHeight(_pos.x, _pos.z)) {
currentState = IDLE;
canJump = true;
}
}
if (currentState == FALL) {
_pos.y += velocity.y * timer.DeltaTime();
velocity.y += -20.0f*timer.DeltaTime();
}
}
}
void Knight::Reset()
{
// set start position
_pos = { 900.0f, 0.0f, -900.0f };
// set start state
currentState = IDLE;
canJump = true;
}
void Knight::GetMessages(UINT msg, WPARAM wParam, LPARAM lParam, void * Data)
{
}
D3DXMATRIX Knight::getRearView()
{
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &thirdPersonCamera._pos, &D3DXVECTOR3(_pos.x, box.MAX.y, _pos.z), &D3DXVECTOR3(0, 1, 0));
return V;
}