-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
393 lines (329 loc) · 12.1 KB
/
player.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "player.h"
#include "entityManager.h"
#include "graphics.h"
#include "audio.h"
#include "game.h"
#include <iomanip>
Player::Player(Camera* cam) {
this->cam = cam;
transform->setVisibility(Visibility::HIDE_SELF);
Transform* model = Graphics::registerTransform();
model->setPos(0.0f, 1.5f, 0.0f);
model->setScale(1.0f, 1.2f, 1.0f);
Transform* face = Graphics::registerTransform();
face->setPos(0.0f, 1.8f, 0.4f);
face->setScale(0.8f, 0.4f, 0.9f);
Transform* lleg = Graphics::registerTransform();
lleg->setPos(0.35f, 0.5f, 0.0f);
lleg->setScale(0.25f, 1.0f, 0.25f);
Transform* rleg = Graphics::registerTransform();
rleg->setPos(-0.35f, 0.5f, 0.0f);
rleg->setScale(0.25f, 1.0f, 0.25f);
Transform* larm = Graphics::registerTransform();
larm->setPos(0.5f, 1.5f, 0.0f);
larm->setScale(1.0f, 0.25f, 0.25f);
Transform* rarm = Graphics::registerTransform();
rarm->setPos(-0.5f, 1.5f, 0.0f);
rarm->setScale(1.0f, 0.25f, 0.25f);
Transform* bazooka = Graphics::registerTransform();
bazooka->setPos(0.8f, 1.8f, 0.2f);
bazooka->setScale(0.5f, 0.5f, 2.0f);
transform->color = glm::vec3(0.0f, 1.0f, 0.4f);
transform->parentAllWithColor(model, face, lleg, rleg, larm, rarm, bazooka);
bazooka->color = glm::vec3(0.25f, 0.0f, 0.5f);
currRot = targRot = glm::quat();
glm::vec3 scale = glm::vec3(1.0f, 2.0f, 1.0f);
glm::vec3 min = glm::vec3(-0.5f, 0.0f, -0.5f)*scale;
glm::vec3 max = glm::vec3(0.5f, 1.0f, 0.5f)*scale;
collider->setExtents(min, max);
collider->tag = Tag::PLAYER;
collider->type = ColliderType::FULL;
Physics::setCollisionCallback(this);
}
void Player::spawn(glm::vec3 spawnPos, bool enabled) {
flyMode = false;
health = maxHealth;
burnTime = 0.0f;
boostParticleTime = 0.0f;
transform->setPos(spawnPos);
collider->enabled = enabled;
}
float Player::getHealth() const {
return health;
}
float Player::getMaxHealth() const {
return maxHealth;
}
float Player::getDamage() const {
return attackDamage;
}
bool Player::isDead() const {
return health <= 0.0f;
}
void Player::addHealth(float amount) {
health += amount;
health = std::max(0.0f, std::min(health, maxHealth));
}
void Player::update(GLfloat delta) {
if (isDead()) {
collider->enabled = false;
return;
}
updateTimers(delta);
glm::vec3 targetDir = checkInputs();
updateModel(targetDir, delta);
if (flyMode) { // debug mode, execute this and return
flyModeMovement(targetDir);
return;
}
calculateBurnDamage(delta);
checkClampXZVel();
checkJumpAndBoost();
calculateMovement(targetDir, delta);
}
void Player::updateTimers(float delta) {
// update timer variables
timeSinceHitJump += delta;
timeSinceGrounded += delta;
timeSinceShot += delta;
invulnTime += delta;
burnTime -= delta;
boostTimer -= delta;
boostParticleTime -= delta;
}
void Player::shoot() {
AudioInstance->playSound(Resources::get().shootSound);
glm::vec3 shotPos = transform->getWorldPos();
shotPos.y += 1.8f;
EntityManagerInstance->SpawnProjectile(
ProjectileType::ROCKET, Tag::PLAYER_PROJECTILE, shotPos, cam->forward*shootSpeed);
}
glm::vec3 Player::checkInputs() {
// toggle flying
if (Input::justPressed(sf::Keyboard::Q)) {
burnTime = 0.0f;
boostParticleTime = 0.0f;
flyMode = !flyMode;
}
// check jump input
if (Input::justPressed(sf::Keyboard::Space)) {
timeSinceHitJump = 0.0f;
}
// check shoot input
if (Input::pressed(sf::Keyboard::E) || Input::pressed(sf::Mouse::Button::Right)) {
if (timeSinceShot > 1 / shotsPerSecond) {
timeSinceShot = 0.0f;
shoot();
}
}
if (Input::pressed(sf::Keyboard::BackSpace)) {
glm::vec3 p = transform->getWorldPos();
std::cout << std::setprecision(2) << std::fixed;
std::cout << p.x << " " << p.y << " " << p.z << std::endl;
}
// calculate movement vector in xz plane
glm::vec3 input = getMovementDir(); // input from key presses
glm::vec3 xzCamForward = glm::normalize(glm::cross(cam->worldUp, cam->right)); // cam forward in xz plane
glm::vec3 targetDir = cam->right * input.x + xzCamForward * input.z; //normalized target movement direction
targetDir.y = 0.0f; // just to make sure
if (targetDir != glm::vec3(0.0f)) {
targetDir = glm::normalize(targetDir);
}
return targetDir;
}
void Player::updateModel(glm::vec3 targetDir, float delta) {
// only update model rotation if nonzero movement vector
if (targetDir != glm::vec3(0.0f)) {
targRot = glm::normalize(glm::rotation(glm::vec3(0.0f, 0.0f, 1.0f), targetDir));
}
// slerp keeps the quaternion normalized throughout
currRot = glm::slerp(currRot, targRot, delta * 8.0f);
transform->setRot(currRot);
// set your model visible based on how zoomed in camera is
bool childrenVisible = cam->getCamDist() > 1.0f;
transform->setVisibility(childrenVisible ? Visibility::HIDE_SELF : Visibility::HIDE_ALL);
}
void Player::flyModeMovement(glm::vec3 targetDir) {
collider->gravityMultiplier = 0.0f;
float flyspeed = speed * 20.0f;
if (Input::pressed(sf::Keyboard::LControl)) {
flyspeed *= 3.0f;
}
float yInput = 0.0f;
if (Input::pressed(sf::Keyboard::LShift)) {
yInput -= 1.0f;
}
if (Input::pressed(sf::Keyboard::Space)) {
yInput += 1.0f;
}
collider->vel = (targetDir + cam->worldUp * yInput) * flyspeed;
}
bool Player::recentlyGrounded() {
return timeSinceGrounded < groundedLenience;
}
void Player::calculateBurnDamage(float delta) {
if (collider->onTerrain && Game::isGroundLava()) {
burnTime = 0.5f;
}
if (burnTime > 0.0f) {
AudioInstance->playSoundSingle(Resources::get().burningSound);
glm::vec3 rvel = glm::vec3(Mth::randUnit(), Mth::rand01() + 0.5f, Mth::randUnit()) * 10.0f;
EntityManagerInstance->SpawnParticle(ParticleType::FIRE, transform->getWorldPos(), rvel, 5.0f);
addHealth(-delta*20.0f);
}
}
// work around for shortcoming with physics resolution
// problem is physics doesnt zero your velocity in the direction of things you hit (like walls)
// so if you slam into a corner of two buildings really fast you will be stuck for a bit while
// your speed cools down, so what this does is check if your last x and z positions are almost equal
// to your current then you set the x and z components of velocity to zero
void Player::checkClampXZVel() {
glm::vec3 curPos = transform->getPos();
curPos.y = 0.0f;
glm::vec3 diff = curPos - oldPos;
if (glm::dot(diff, diff) < 0.00001f) {
//std::cout << "decimating";
collider->vel.x = 0.0f;
collider->vel.z = 0.0f;
}
// save current position
oldPos = curPos;
}
void Player::checkJumpAndBoost() {
// jump if able
if (timeSinceHitJump < jumpLenience && recentlyGrounded()) {
AudioInstance->playSound(Resources::get().jumpSound);
collider->vel.y = jumpSpeed;
collider->grounded = false;
timeSinceGrounded = 10.0f;
timeSinceHitJump = 10.0f;
}
if (collider->grounded) { // set time since grounded
timeSinceGrounded = 0.0f;
}
if (Input::justPressed(sf::Mouse::Button::Left) && recentlyGrounded() && boostTimer < 0.0f) {
// set speed in negative camera facing direction
collider->vel += -cam->forward * boostSpeed;
boostTimer = boostCooldown;
boostParticleTime = 1.0f;
AudioInstance->playSound(Resources::get().boostSound);
addHealth(-1.0f); // slightly hurt player
timeSinceGrounded = 10.0f;
// spawn a bunch of particles
glm::vec3 pos = transform->getWorldPos();
for (int i = 0; i < 200; ++i) {
glm::vec3 r = Mth::randInsideSphere(40.0f);
r.y *= 0.75f;
EntityManagerInstance->SpawnParticle(BOOST, pos, r);
}
}
// continue launching boost particles for a bit
if (boostParticleTime > 0.0f) {
glm::vec3 pos = transform->getWorldPos();
float s = std::max(0.1f, boostParticleTime / 1.0f);
for (int i = 0; i < 2; ++i) {
EntityManagerInstance->SpawnParticle(BOOST, pos, Mth::randInsideSphere(20.0f * s), 0.0f, glm::vec3(s), false);
}
}
}
void Player::calculateMovement(glm::vec3 targetDir, float delta) {
collider->gravityMultiplier = 1.0f; // ensure normal gravity
// save y velocity and zero it
float oldY = collider->vel.y;
collider->vel.y = 0.0f;
// save max value out of square speed and square magnitude of velocity
float sqrMagSave = glm::max(speed*speed, glm::dot(collider->vel, collider->vel));
// acceleration based on groundedness
float accel = recentlyGrounded() ? 75.0f : 35.0f;
// add to velocity
collider->vel += targetDir * accel * delta;
// if velocity is over sqrMagSave that means it was both over
// character speed limit and the magnitude of previous velocity
// so clamp it to sqrMagSave
if (glm::dot(collider->vel, collider->vel) > sqrMagSave) {
collider->vel = glm::normalize(collider->vel) * glm::sqrt(sqrMagSave);
}
// if still over character speed limit then apply drag to velocity
float drag = 0.0f;
bool noInput = targetDir == glm::vec3(0.0f);
if (glm::dot(collider->vel, collider->vel) > speed * speed) { // apply light drag if over speed limit
drag = recentlyGrounded() ? 0.5f : 0.025f;
if (noInput) drag *= 2.0f; // increase drag if no inputs are pressed
} else if (noInput) { // else if within speed limit and no input then apply harder drag
drag = recentlyGrounded() ? 12.0f : 3.0f;
}
drag = Mth::saturate(drag * delta);
collider->vel -= collider->vel * drag;
// restore old y
collider->vel.y = oldY;
}
void Player::onCollision(Tag tag, Entity* other) {
switch (tag) {
case Tag::HEALER: {
if (collider->grounded && !collider->onTerrain && health < maxHealth) {
AudioInstance->playSoundSingle(Resources::get().healingSound);
addHealth(20.0f * Game::deltaTime());
glm::vec2 p = glm::normalize(Mth::randomPointInCircle(1.0f))*3.0f;
glm::vec3 rp = transform->getWorldPos() + glm::vec3(p.x, 0.0f, p.y);
EntityManagerInstance->SpawnParticle(ParticleType::HEAL, rp, glm::vec3(0.0f), 1.0f);
}
}break;
case Tag::SWITCH: {
Boss* b = EntityManagerInstance->getBoss();
if (b != nullptr) {
b->playerHitSwitch();
}
}break;
case Tag::ENEMY:
case Tag::ENEMY_PROJECTILE:
if (invulnTime >= 0.5f) {
AudioInstance->playSound(Resources::get().damageSound);
addHealth(-5);
invulnTime = 0.0f;
}
break;
case Tag::ITEM:
AudioInstance->playSound(Resources::get().itemGetSound);
Item* i = dynamic_cast<Item*>(other);
switch (i->type) {
case ItemType::HEAL:
addHealth(30);
break;
case ItemType::STAMINA:
maxHealth += 10.0f;
addHealth(10);
break;
case ItemType::STRENGTH:
attackDamage += 5.0f;
break;
case ItemType::AGILITY:
speed += 5.0f;
break;
case ItemType::DEXTERITY:
shootSpeed += 5.0f;
shotsPerSecond += 0.5f;
break;
}
break;
}
}
// calculate movement direction and return a normalized vector pointing in that direction
glm::vec3 Player::getMovementDir() {
glm::vec3 dir(0.0f, 0.0f, 0.0f);
if (Input::pressed(sf::Keyboard::W) || Input::pressed(sf::Keyboard::Up)) {
dir.z += 1.0f;
}
if (Input::pressed(sf::Keyboard::S) || Input::pressed(sf::Keyboard::Down)) {
dir.z -= 1.0f;
}
if (Input::pressed(sf::Keyboard::A) || Input::pressed(sf::Keyboard::Left)) {
dir.x -= 1.0f;
}
if (Input::pressed(sf::Keyboard::D) || Input::pressed(sf::Keyboard::Right)) {
dir.x += 1.0f;
}
if (dir != glm::vec3(0.0f)) {
dir = glm::normalize(dir);
}
return dir;
}