-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
213 lines (171 loc) · 5.41 KB
/
Copy pathGame.cpp
File metadata and controls
213 lines (171 loc) · 5.41 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <functional>
#include <vector>
#define _USE_MATH_DEFINES
#include "Window.h"
#include "Geometry.h"
#include "Timer.h"
#include "Player.h"
#include "World.h"
#include "AnimationController.h"
void playerMovement(Window& win, Player& player, float dt) {
player.yaw -= win.deltaX * player.sensXZ;
player.pitch -= win.deltaY * player.sensY;
player.pitch = clamp(player.pitch, (float)((-M_PI / 2) + 0.01f), (float)(M_PI / 2 - 0.01f));
player.moveDir.x = player.lookDir.x = cos(player.pitch) * sin(player.yaw);
player.lookDir.y = sin(player.pitch);
player.moveDir.z = player.lookDir.z = cos(player.pitch) * cos(player.yaw);
player.moveDir.y = 0;
player.right = player.moveDir.Cross(player.up).normalise();//pitch clamped, if not clamped could get the whole orthonormal basis
bool isGrounded = true;
if (player.from.y > 3.0f) {
isGrounded = false;
}
float isSprint = 1.0f;
if (win.keys[VK_SHIFT] == 1)
isSprint = player.sprintSpeedMult;
if (win.keys['W'] == 1)
player.from += player.moveDir * player.moveSpeed * isSprint * dt;
if (win.keys['S'] == 1)
player.from -= player.moveDir * player.moveSpeed * isSprint * dt;
if (win.keys['A'] == 1)
player.from -= player.right * player.moveSpeed * isSprint * dt;
if (win.keys['D'] == 1)
player.from += player.right * player.moveSpeed * isSprint * dt;
if (win.keys[VK_SPACE] && isGrounded == true) {
player.jumpForce = 0.3f;
}
player.jumpForce += player.gravity * dt;
player.from.y += player.jumpForce;
if (player.from.y <= 3.0f) {
player.from.y = 3.0f;
player.jumpForce = 0;
}
//when looking up forawrd movement is severly reduced - do to how im calculating the forward vector.
player.to = player.from + player.moveDir + player.lookDir;
win.deltaX = 0;
win.deltaY = 0;
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, int nCmdShow) {
Window win;
Core core;
PSOManager psos;
Shader shader;
ShaderManager shaderMgr;
TextureManager texMgr;
ConstantBuffer cb;
CBtime cbTime;
CameraCB camCB;
VertexCB vertexTime;
Matrix view;
Timer tim;
World world;
SaveSystem saveLoad;
cbTime.time = vertexTime.time = 0;
unsigned int WIDTH = 1024;
unsigned int HEIGHT = 768;
const float fov = M_PI / 2.0f;
const float _near = 0.1f;
const float _far = 10000.0f;
const float aspect = WIDTH / HEIGHT;
float totalTime = 0;
Player player;
player.from = Vec3(0.0f, 3.0f, 0.0f); // camera position //from
player.to = Vec3(0.0f, 1.0f, 0.0f); // look direction //to
player.up = Vec3(0.0f, 1.0f, 0.0f);
player.projection.projMat(fov, aspect, _far, _near);
player.pitch = 0;
player.yaw = 0;
player.sensY = 0.01f;
player.sensXZ = 0.01f;
player.moveSpeed = 10.0f;
player.sprintSpeedMult = 1.5f;
player.isJumping = false;
player.gravity = -1.0f;
player.jumpForce = 0;
player.lookDir = Vec3(0, 0, 0);
player.moveDir = Vec3(0, 0, 0);
player.right = Vec3(0, 0, 0);
player.offset = Vec3(0, 0, 0);
srand(time(0));
win.init(WIDTH, HEIGHT, "My Window");
core.init(win.hwnd, WIDTH, HEIGHT);
GameState saveState;
std::string saveName = "SaveSlot1.txt";
if (saveLoad.findSave(saveName)) {
saveState = saveLoad.loadGame(saveName);
player.from = saveState.playerPosition;
world.isLoaded = true;
}
world.init(core, psos, shaderMgr, texMgr, player, saveState);
int frameCount = 0;
float fpsTimer = 0.0f;
tim.reset();
while (1) {
core.beginFrame();
win.processMessages();
win.updateMouseOnScreen();
core.beginRenderPass();
if (win.keys[VK_ESCAPE] == 1)
{
break;
}
float dt = tim.dt();
totalTime += dt;
cbTime.time = totalTime;
vertexTime.time = totalTime;
frameCount++;
fpsTimer += dt;
if (fpsTimer >= 1.0f) {
float fps = frameCount / fpsTimer;
std::string fpsStr = "FPS: " + std::to_string(fps) + "\n";
OutputDebugStringA(fpsStr.c_str());
frameCount = 0;
fpsTimer = 0.0f;
}
playerMovement(win, player, dt);
SphereCollider playerCol;
playerCol.centre = player.from;
playerCol.radius = 1.0f;
float checkDist = 10.0f;
for (int i = 0; i < world.staticBoxColliders.size(); i++) {
Vec3 boxCentre = (world.staticBoxColliders[i].min + world.staticBoxColliders[i].max) * 0.5f;
Vec3 diff = player.from - boxCentre;
if (diff.lengthsq() > checkDist * checkDist) continue;
CollisionResult result = sphereAABBCollision(playerCol, world.staticBoxColliders[i]);
if (result.hit) {
player.from = player.from + result.normal * result.pen;
playerCol.centre = player.from;
}
}
for (int i = 0; i < world.rexEntities.size(); i++) {
Vec3 diff = player.from - world.rexEntities[i].collider.centre;
if (diff.lengthsq() > checkDist * checkDist) continue;
CollisionResult result = sphereSphereCollision(playerCol, world.rexEntities[i].collider);
if (result.hit) {
player.from.x = player.from.x + result.normal.x * result.pen;
player.from.z = player.from.z + result.normal.z * result.pen;
playerCol.centre = player.from;
}
}
view = player.view.lookAt(player.from, player.to, player.up);
Matrix vp = player.projection.mul(view);
world.update(dt, player);
camCB.CamFrom = player.from;
world.draw(core, psos, texMgr, vp, player, vertexTime, cbTime, camCB);
if (win.keys['O']) {
GameState save;
save.playerPosition = player.from;
world.saveEntities(save);
saveLoad.saveGame(save, saveName);
break;
}
core.finishFrame();
}
core.flushGraphicsQueue();
core.release();
}