forked from GuillemFP/DoubleDragon3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cpp
145 lines (123 loc) · 2.97 KB
/
Entity.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
#include "Application.h"
#include "ModuleRender.h"
#include "ModuleCollision.h"
#include "Entity.h"
Entity::Entity(Entity::Type type, SDL_Texture* texture, ModuleStages* stage, Entity* parent, bool active) : type(type), texture(texture), stage(stage), parent(parent), active(active)
{
if (parent != nullptr)
parent->contains.push_back(this);
timer = new Timer();
}
update_status Entity::Update()
{
if (timer->MaxTimeReached() == true)
{
timer->Stop();
Disable();
}
return UPDATE_CONTINUE;
}
bool Entity::Draw()
{
if (active && texture != nullptr)
{
if (inverted_texture == false)
{
if (parent == nullptr)
App->renderer->Blit(texture, { position.x,position.y }, &entity_rect);
else
App->renderer->Blit(texture, { position.x + parent->position.x, position.y + parent->position.y }, &entity_rect);
}
else
{
if (parent == nullptr)
App->renderer->Blit(texture, { position.x + dimensions.x - entity_rect.w,position.y }, &entity_rect, true);
else
App->renderer->Blit(texture, { position.x + parent->position.x + dimensions.x - entity_rect.w, position.y + parent->position.y }, &entity_rect, true);
}
}
return active;
}
bool Entity::Enable()
{
if (parent != nullptr && parent->active == false)
{
return false;
}
else
{
if (has_timer == true)
timer->Start();
if (collider != nullptr)
collider->active = true;
active = true;
return true;
}
}
bool Entity::Disable()
{
bool ret = true;
if (has_timer == true)
timer->Stop();
active = false;
if (collider != nullptr)
collider->active = false;
for (std::list<Entity*>::iterator it = contains.begin(); it != contains.end() && ret == true; ++it)
ret = (*it)->Disable();
return ret;
}
bool Entity::Delete()
{
bool ret = true;
if (has_timer == true)
timer->Stop();
if (active == false)
{
to_delete = true;
if (collider != nullptr)
collider->to_delete = true;
for (std::list<Entity*>::iterator it = contains.begin(); it != contains.end() && ret == true; ++it)
ret = (*it)->Delete();
}
contains.clear();
return ret;
}
void Entity::EnableCollider()
{
if (collider != nullptr)
collider->active = true;
}
void Entity::DisableCollider()
{
if (collider != nullptr)
collider->active = false;
}
bool Entity::ColliderIsActive() const
{
bool ret = false;
if (collider != nullptr)
ret = collider->active;
return ret;
}
void Entity::SetCollider(const SDL_Rect& rect)
{
if (inverted_texture == true)
collider->position.x = position.x + dimensions.x - rect.x - rect.w - shifted_draw.x;
else
collider->position.x = position.x + rect.x + shifted_draw.x;
collider->position.y = position.y + rect.y + shifted_draw.y;
collider->position.z = position.z;
collider->dimensions.x = rect.w;
collider->dimensions.y = rect.h;
collider->dimensions.z = dimensions.z;
}
void Entity::ChangeParent(Entity* new_parent)
{
if (parent != nullptr)
{
parent->contains.remove(this);
parent = new_parent;
if (new_parent != nullptr)
parent->contains.push_back(this);
}
}