-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
569 lines (486 loc) · 15.3 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "Player.h"
#include "Hoe.h"
#include "./UI/GUI.h"
#include "Network/NetworkPacket.h"
#include "GameManager.h"
#include "VeggieNet.h"
Player::Player() {
translate = new glm::vec2(0.f,0.f);
rotate = glm::vec3(0.0f);
scale = glm::vec3(0.0f);
ColliderCircle *collider = new ColliderCircle(glm::vec2(0,0), 3, false);
ColliderCircle *collider_trigger = new ColliderCircle(glm::vec2(0,0), 3, false);
collider_trigger->collider_is_trigger = true;
colliders_ = {collider, collider_trigger};
dust_particle = new Particle(PARTICLE_DUST);
dust_particle->modelAnim = PARTICLE_STOP;
dust_particle->SetPosition(glm::vec3(( * translate)[0], dust_particle->dustParticleHeight, -(*translate)[1]));
//GameManager::AddEntities({ dust_particle });
}
Player::Player(ModelEnum curr) : Player() {
// Set initial values
model = curr;
//std::cout << model.getBoneCount() << std::endl;
}
Player::~Player()
{
delete translate;
for (ColliderCircle *col : colliders_)
{
delete col;
}
delete dust_particle;
}
GameEntity* Player::GetHoldEntity()
{
return entityHeld;
}
void Player::SetRotation(glm::vec3 newRot)
{
rotate = newRot;
}
void Player::FixedUpdate() {
if (!(translate->x < 140 && translate->x > -140))
(*translate)[0] = 10.f;
if (!(translate->y < 140 && translate->y > -140))
(*translate)[1] = 10.f;
if (glm::length(move_input) > 1) move_input = glm::normalize(move_input);
const auto delta = static_cast<float>(GameManager::GetFixedDeltaTime());
// If no movement is given apply friction (epsilon to account for FP errors)
if (glm::length(move_input) <= 0 || isGlued) {
// increment stamina
float appliedStaminaIncreaseRate = oatTimeRemaining > 0 ? oatStaminaIncreaseRate : baseStaminaIncreaseRate;
curr_stamina = fmin(curr_stamina + (delta*appliedStaminaIncreaseRate), 100);
float applied_friction = intoxicationTimeRemaining > 0 ? drunk_friction_ : base_friction_;
if (glm::length(curr_vel_) < applied_friction * delta)
{
curr_vel_ = glm::vec2(0,0);
}
else {
curr_vel_ -= glm::normalize(curr_vel_) * applied_friction * delta;
}
if (isHolding) {
this->modelAnim = IDLE_HOLD;
}
else if (!isDancing)
//else
this->modelAnim = IDLE;
else
this->modelAnim = DANCE;
}
else {
// decrement stamina
float appliedStaminaIncreaseRate = oatTimeRemaining > 0 ? oatStaminaIncreaseRate : baseStaminaIncreaseRate;
float appliedStaminaDecreaseRate = oatTimeRemaining > 0 ? oatStaminaDecreaseRate : baseStaminaDecreaseRate;
if(sprint) {
curr_stamina = fmax(curr_stamina - (delta * appliedStaminaDecreaseRate), 0);
dust_particle->modelAnim = PARTICLE_PLAY;
}
else {
curr_stamina = fmin(curr_stamina + (delta * appliedStaminaIncreaseRate), 100);
}
// check if enough stamina to run
if (curr_stamina == 0) {
dust_particle->modelAnim = PARTICLE_STOP;
sprint = false;
}
// Accelerate in our inputted direction
// Transform input from 2D to 3D Plane
float applied_accel = intoxicationTimeRemaining > 0 ? drunk_accel_ : base_accel_;
curr_vel_ += applied_accel * move_input * delta;
// Cap our speed at some max velocity
Vegetable* eggplant = dynamic_cast<Vegetable*>(entityHeld);
bool holding_eggplant = eggplant != nullptr && eggplant->type == VegetableType::GOLDEN_EGGPLANT;
float max_vel = holding_eggplant ? max_eggplant_velocity_ : max_velocity_;
if (sprint) max_vel *= sprint_modifier;
if (glm::length(curr_vel_) > max_vel * glm::length(move_input)) {
curr_vel_ = glm::normalize(curr_vel_) * max_vel * glm::length(move_input);
}
if (isHolding)
this->modelAnim = IDLE_WALK;
else
this->modelAnim = WALK;
isDancing = false;
}
if (glm::length(curr_vel_) > 0) {
Move();
}
MoveHeld();
for (ColliderCircle *collider_ : colliders_)
{
collider_->center = *translate;
}
// move dust particle
const glm::vec4 particleLocation = glm::vec4(0, dust_particle->dustParticleHeight, -dust_particle->dustParticleZOffset, 1) * GetRotation();
const glm::mat4 particle_rotation = glm::rotate(rotate.y, glm::vec3(0, 1, 0));
dust_particle->SetPosition(glm::vec3(translate->x + (particleLocation.x), dust_particle->dustParticleHeight, (-1) * translate->y - (particleLocation.z)));
dust_particle->SetRotation(particle_rotation);
if (intoxicationTimeRemaining > 0) {
intoxicationTimeRemaining -= delta;
}
if (oatTimeRemaining > 0) {
oatTimeRemaining -= delta;
}
}
void Player::Move() {
if (GetMoveable()) {
//move player
const auto delta = static_cast<float>(GameManager::GetFixedDeltaTime());
const glm::vec2 distance = delta * curr_vel_;
*translate += distance;
rotate.y = atan2(curr_vel_.x, -curr_vel_.y); //TODO fix this potentially
}
}
glm::mat4 Player::GetParentTransform()
{
return GetTranslation() * GetRotation() * GetScale();
}
ModelEnum Player::GetModelEnum() { return model; }
AniMode Player::GetAniMode() { return modelAnim; }
std::vector<SoundInfo> Player::GetSounds()
{
std::vector<SoundInfo> output{};
if (sound_buy) output.push_back(SoundInfo{SFX_BUY, GetPosition()});
if (sound_sell) output.push_back(SoundInfo{SFX_SELL, GetPosition()});
if (sound_plot_placement) output.push_back(SoundInfo{SFX_PLOT_PLACE, GetPosition()});
if (sound_poison) output.push_back(SoundInfo{ SFX_POISON, GetPosition() });
if (sound_plant) output.push_back(SoundInfo{ SFX_PLANT, GetPosition() });
if (sound_harvest) output.push_back(SoundInfo{ SFX_HARVEST, GetPosition() });
if (sound_eggplant_pickup) output.push_back(SoundInfo{ SFX_EGGPLANT_PICKUP, GetPosition() });
if (sound_steal) output.push_back(SoundInfo{ SFX_STEAL, GetPosition() });
if (sound_shovel) output.push_back(SoundInfo{ SFX_SHOVEL, GetPosition() });
if (sound_watering) output.push_back(SoundInfo{ SFX_WATERING, GetPosition() });
if (sound_no) output.push_back(SoundInfo{ SFX_NO, GetPosition() });
if (sound_fertilize) output.push_back(SoundInfo{ SFX_FERTILIZE, GetPosition() });
if (sound_net) output.push_back(SoundInfo{ SFX_STEAL, GetPosition() });
return output;
}
bool Player::CanInteract(Player* other_player) {
bool holds_soju = (other_player->GetIsHolding() && dynamic_cast<Soju*>(other_player->GetHoldEntity()));
return holds_soju;
}
void Player::OnInteract(Player* other_player) {
if (auto soju = dynamic_cast<Soju*>(other_player->GetHoldEntity()))
{
other_player->Drop();
other_player->SetTriggeringEntity(nullptr);
GameManager::RemoveEntities({ soju });
intoxicationTimeRemaining = maxIntoxicationTime;
}
}
void Player::OnTrigger(PhysicsObject* object)
{
auto entity = dynamic_cast<GameEntity*>(object);
auto interactable = dynamic_cast<Interactable*>(object);
if (entity != nullptr && entity != entityHeld && interactable && interactable->CanInteract(this)) {
if (entityTriggered == nullptr) {
SetTriggeringEntity(entity);
}
else {
const auto phys_entity = dynamic_cast<PhysicsObject*>(entityTriggered);
const float dist = glm::length(*object->GetWorldPosition() - *GetWorldPosition());
const float curr_dist = glm::length(*phys_entity->GetWorldPosition() - *GetWorldPosition());
if (dist < curr_dist) {
SetTriggeringEntity(entity);
}
}
}
}
std::vector<Collider*> Player::GetColliders()
{
std::vector<Collider*> to_return;
for (ColliderCircle *col : colliders_)
{
to_return.push_back(col);
}
return to_return;
}
glm::vec2* Player::GetWorldPosition()
{
return translate;
}
void Player::Use() {
if (auto hoe = dynamic_cast<Hoe*>(entityHeld))
{
auto plot = new Plot(plot_ownership[this->GetModelEnum()]);
const float plotOffset = 7.5f;
glm::vec4 direction4 = glm::vec4(0, 0, -plotOffset, 1) * GetRotation();
glm::vec3 direction = glm::vec3(direction4 / direction4.w);
glm::vec3 plotPosition = glm::vec3((*translate)[0], -4, -(*translate)[1]) + glm::vec3(direction.x, direction.y, -direction.z);
Collider* testCollider = new ColliderCircle{glm::vec2{plotPosition.x, -plotPosition.z}, 5};
if (!GameManager::physics.IsPlotObstructed(testCollider)) {
plot->SetPosition(plotPosition);
GameManager::AddEntities({ plot });
Drop();
GameManager::RemoveEntities({ hoe });
sound_plot_placement = true;
}
else {
sound_no = true;
}
}
else if (auto glue = dynamic_cast<Glue*>(entityHeld)) {
auto glueOnGround = new GlueOnGround();
const float glueOffset = 9.5f;
glm::vec4 direction4 = glm::vec4(0, 0, -glueOffset, 1) * GetRotation();
glm::vec3 direction = glm::vec3(direction4 / direction4.w);
glm::vec3 gluePosition = glm::vec3((*translate)[0], -4, -(*translate)[1]) + glm::vec3(direction.x, direction.y, -direction.z);;
glueOnGround->SetPosition(gluePosition);
GameManager::AddEntities({ glueOnGround });
glueOnGround->ownerOfGlue = this; // set owner
Drop();
GameManager::RemoveEntities({ glue });
}
else if (auto oatmeal = dynamic_cast<Oat*>(entityHeld)) {
Drop();
GameManager::RemoveEntities({ oatmeal });
oatTimeRemaining = maxOatTime;
}
else {
auto entity = dynamic_cast<PhysicsObject*>(entityTriggered);
if (entity != nullptr) {
Collider* this_trigger_col;
Collider* that_trigger_col;
for (Collider *c : this->GetColliders())
{
if (c->GetColliderIsTrigger())
{
this_trigger_col = c;
break;
}
}
for (Collider *c : entity->GetColliders())
{
if (c->GetColliderIsTrigger())
{
that_trigger_col = c;
break;
}
}
if (this_trigger_col && that_trigger_col && this_trigger_col->CollidesWith(that_trigger_col))
{
auto interactable = dynamic_cast<Interactable*>(entityTriggered);
if (interactable != nullptr && interactable->CanInteract(this))
{
interactable->OnInteract(this);
}
}
}
}
}
void Player::Drop() {
auto holdable = dynamic_cast<Holdable*>(entityHeld);
if (holdable != nullptr) {
holdable->OnDrop();
}
isHolding = false;
entityHeld = nullptr;
}
void Player::Dance() {
if (!isHolding)
isDancing = true;
}
void Player::Buy(VegetableType bought_vegetable) {
VeggieInfo veggie = veggie_map[bought_vegetable];
if (!isHolding && veggie.seed_price <= curr_balance) {
curr_balance -= veggie.seed_price;
isHolding = true;
// Spawn the correct vegetable on the player
Seed* bought_seed = new Seed{ bought_vegetable, veggie.seed_model };
GameManager::AddEntities({ bought_seed });
SetHoldEntity(bought_seed);
bought_seed->holding_player = this;
bought_seed->isHeld = true;
sound_buy = true;
}
CloseUI();
//printf("BUYING VEGGIE %f\n", curr_balance);
}
void Player::Buy(ModelEnum tool) {
ToolInfo tool_ = tool_map[tool];
if (!isHolding && tool_.price <= curr_balance) {
curr_balance -= tool_.price;
isHolding = true;
// Spawn the correct tool
GardenTool* tempEntity = nullptr;
switch (tool) {
case ModelEnum::NET:
tempEntity = new VeggieNet();
break;
case ModelEnum::SHOVEL:
tempEntity = new Shovel();
break;
case ModelEnum::POISON:
tempEntity = new Poison();
break;
case ModelEnum::WATERING_CAN:
tempEntity = new WateringCan();
break;
case ModelEnum::FERTILIZER:
tempEntity = new Fertilizer();
break;
case ModelEnum::GLUE:
tempEntity = new Glue();
break;
case ModelEnum::SOJU:
tempEntity = new Soju();
break;
case ModelEnum::OATS:
tempEntity = new Oat();
break;
case ModelEnum::HOE:
tempEntity = new Hoe();
break;
default: break;
}
GameManager::AddEntities({ tempEntity });
SetHoldEntity(tempEntity);
tempEntity->holding_player = this;
tempEntity->isHeld = true;
sound_buy = true;
}
CloseUI();
//printf("BUYING VEGGIE %f\n", curr_balance);
}
void Player::Sell(){
if (!isHolding)
return;
// TODO: Check if NPC is interactable... unless we don't have to do that?
if (auto vegetable = dynamic_cast<Vegetable*>(entityHeld)) {
//SetTriggeringEntity(nullptr);
if (vegetable == nullptr)
return;
VeggieInfo veggie = veggie_map[vegetable->type];
curr_balance += veggie.sell_price;
//Drop but with remove :)))
auto holdable = dynamic_cast<Holdable*>(entityHeld);
if (holdable != nullptr) {
holdable->OnDrop();
}
GameManager::RemoveEntities({ entityHeld });
entityHeld = nullptr;
isHolding = false;
sound_sell = true;
}
//printf("SELLING VEGGIE %f\n", curr_balance);
CloseSaleUI();
}
void Player::SetSprint(bool sprinting) {
// ending sprint
if (sprint && !sprinting) {
dust_particle->modelAnim = PARTICLE_STOP;
}
sprint = sprinting;
}
void Player::CloseUI()
{
ui_open = false;
moveable = true;
GUI::sale_tools = false;
}
void Player::OpenUI()
{
ui_open = true;
moveable = false;
}
void Player::OpenSaleUI()
{
if (auto vegetable = dynamic_cast<Vegetable*>(entityHeld)) {
//SetTriggeringEntity(nullptr);
if (vegetable != nullptr) {
sale_confirm_ui_open = true;
moveable = false;
}
}
}
void Player::CloseSaleUI() {
sale_confirm_ui_open = false;
moveable = true;
}
void Player::EnableMovement()
{
moveable = true;
}
void Player::DisableMovement()
{
moveable = false;
}
bool Player::GetMoveable()
{
return moveable;
}
void Player::ResetSoundBools()
{
sound_buy = false;
sound_sell = false;
sound_plot_placement = false;
sound_poison = false;
sound_plant = false;
sound_harvest = false;
sound_eggplant_pickup = false;
sound_steal = false;
sound_shovel = false;
sound_watering = false;
sound_no = false;
sound_fertilize = false;
sound_net = false;
}
glm::mat4 Player::GetRotation() {
/*
return glm::rotate(rotate.z, glm::vec3(0.0f, 0.0f, 1.0f))
* glm::rotate(rotate.y, glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(rotate.x, glm::vec3(1.0f, 0.0f, 0.0f));
*/
return glm::rotate(rotate.y, glm::vec3(0.0f, 1.0f, 0.0f));
}
glm::mat4 Player::GetTranslation() {
return glm::translate(glm::vec3((* translate)[0], playerHeight, -(*translate)[1]));
}
glm::mat4 Player::GetScale() {
return glm::translate(scale);
}
glm::vec3 Player::GetPosition() const
{
return glm::vec3((*translate)[0], 0, -(*translate)[1]);
}
void Player::SetWorldPosition(const glm::vec3 position)
{
*translate = glm::vec2(position[0], -position[2]);
}
void Player::SetTriggeringEntity(GameEntity* entity)
{
entityTriggered = entity;
}
GameEntity* Player::GetTriggeringEntity()
{
return entityTriggered;
}
void Player::SetHoldEntity(GameEntity* entity)
{
isHolding = true;
entityHeld = entity;
MoveHeld();
// Switch models here?
}
void Player::MoveHeld() {
if (isHolding) {
if (auto vegetable = dynamic_cast<Vegetable*>(entityHeld)) {
const glm::vec4 vegetableLocation = glm::vec4(0, 0, -veggieHeldDist, 1) * GetRotation();
const glm::mat4 held_rotation = glm::rotate(rotate.y, glm::vec3(0, 1, 0));
vegetable->SetPosition(glm::vec3(translate->x + (vegetableLocation.x), vegetable->GetHeight(), (-1) * translate->y - (vegetableLocation.z)));
vegetable->SetRotation(held_rotation);
}
else if (auto seed = dynamic_cast<Seed*>(entityHeld)) {
const glm::vec4 seedLocation = glm::vec4(0, 0, -seedHeldDist, 1) * GetRotation();
const glm::mat4 held_rotation = glm::rotate(rotate.y, glm::vec3(0, 1, 0));
seed->SetPosition(glm::vec3(translate->x + (seedLocation.x), seed->GetHeight(), (-1) * translate->y - (seedLocation.z)));
seed->SetRotation(held_rotation);
}
else if (auto garden_tool = dynamic_cast<GardenTool*>(entityHeld)) {
const glm::vec4 location = glm::vec4(0, 0, -entityHeldDist, 1) * GetRotation();
const glm::mat4 held_rotation = glm::rotate(rotate.y, glm::vec3(0, 1, 0));
garden_tool->SetPosition(glm::vec3(translate->x + (location.x), garden_tool->GetHeight(), (-1) * translate->y - (location.z)));
garden_tool->SetRotation(held_rotation);
}
}
}