-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer_game_object.cpp
More file actions
119 lines (104 loc) · 2.35 KB
/
player_game_object.cpp
File metadata and controls
119 lines (104 loc) · 2.35 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
#include "player_game_object.h"
#include "collision.h"
namespace game {
/*
PlayerGameObject inherits from GameObject
It overrides GameObject's update method, so that you can check for input to change the velocity of the player
*/
PlayerGameObject::PlayerGameObject(const glm::vec3 &position, GLuint* textures, GLint num_elements)
: GameObject(position, textures[0], num_elements, true, 0.5) {
power_up_ = None;
name_ = player;
health_ = 3;
shielded_tex_ = textures[1];
default_tex_ = textures[0];
num_missiles_ = 2;
}
// Update function for moving the player object around
void PlayerGameObject::Update(double delta_time) {
// Special player updates go here
glm::vec3 v = GetVelocity();
if (v.x > 0.0)
{
v.x = v.x - 0.0065;
if (v.x < 0.0)
{
v.x = 0;
}
}
if (v.y > 0.0)
{
v.y = v.y - 0.0065;
if (v.y < 0.0)
{
v.y = 0;
}
}
if (v.x < 0.0)
{
v.x = v.x + 0.0065;
if (v.x > 0.0)
{
v.x = 0;
}
}
if (v.y < 0.0)
{
v.y = v.y + 0.0065;
if (v.y > 0.0)
{
v.y = 0;
}
}
SetVelocity(glm::vec3(v.x, v.y, 0.0f));
// Call the parent's update method to move the object in standard way, if desired
GameObject::Update(delta_time);
}
bool PlayerGameObject::ValidCollision(GameObject* other_game_object, double deltatime) {
switch (other_game_object->GetName()) {
case enemy:
return Collision::CircleCircleCollision(other_game_object, position_, radius_);
case powerup:
return Collision::CircleCircleCollision(other_game_object, position_, radius_);
//Leave laser-player to be handled by Laser class, difficult to call ray-circle here
}
}
bool PlayerGameObject::HandleCollision(GameObject* other_game_object, double deltatime)
{
switch (other_game_object->GetName()) {
case enemy:
TakeDamage(2);
break;
case laser:
TakeDamage(1);
break;
case powerup:
PowerUp* pwrup = dynamic_cast<PowerUp*>(other_game_object);
if (pwrup->GetType() == shield_type) {
texture_ = shielded_tex_;
power_up_ = shield_type;
shield_power_ = 3;
}
if (pwrup->GetType() == satellite_type) {
num_missiles_++;
}
break;
}
return true;
}
void PlayerGameObject::TakeDamage(int amt) {
if (power_up_ == shield_type) {
shield_power_--;
if (shield_power_ == 0) {
power_up_ = None;
texture_ = default_tex_;
}
}
else {
health_--;
if (health_ == 0) {
dead_ = true;
}
}
}
} // namespace game