-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.cpp
More file actions
140 lines (128 loc) · 2.9 KB
/
Projectile.cpp
File metadata and controls
140 lines (128 loc) · 2.9 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
#include "Projectile.hpp"
#include "Game.hpp"
#include "Display.hpp"
#include "Logger.hpp"
#include "Player.hpp"
#include "EntityChild.hpp"
#include "LivingEntity.hpp"
Projectile::Projectile(void) {
}
Projectile::Projectile(Projectile const & src) {
*this = src;
}
Projectile::Projectile(int posX, int posY, char drawingChar, int speed, bool dir, bool ally, int damage , int up) :
Entity(posX, posY, drawingChar, speed, ally), _dir(dir), _damage(damage), _up(up)
{
}
Projectile::~Projectile(void) {
}
Projectile & Projectile::operator=(Projectile const & rhs) {
if (this != &rhs){
this->_dir = rhs._dir;
this->_damage = rhs._damage;
this->_up = rhs._up;
this->_posX = rhs._posX;
this->_posY = rhs._posY;
this->_oldX = rhs._oldX;
this->_oldY = rhs._oldY;
this->_hasPosChanged = rhs._hasPosChanged;
this->_drawingChar = rhs._drawingChar;
this->_speed = rhs._speed;
this->_frameCount = rhs._frameCount;
this->_isCollide = rhs._isCollide;
this->_ally = rhs._ally;
for (int i = 0; i < NB_CHILD; i++) {
this->entitiesChild[i] = rhs.entitiesChild[i];
}
this->_nbChild = rhs._nbChild;
}
return (*this);
}
int Projectile::GetDamage() const
{
return this->_damage;
}
void Projectile::Colision(Entity *entity)
{
if (this->_ally)
{
Enemy* enemy = dynamic_cast<Enemy*>(entity);
if (enemy)
{
enemy->TakeDamage(this->_damage);
Display::Erase(this->_posX, this->_posY);
Game::Instance->RemoveEntity(this);
return;
}
}
else
{
Player* player = dynamic_cast<Player*>(entity);
if (player)
{
player->TakeDamage(this->_damage);
Display::Erase(this->_posX, this->_posY);
Game::Instance->RemoveEntity(this);
return;
}
}
EntityChild* child = dynamic_cast<EntityChild*>(entity);
if (child)
{
if (child->GetAlly() != this->_ally)
{
LivingEntity* live = dynamic_cast<LivingEntity*>(child->GetParent());
live->TakeDamage(this->_damage);
Display::Erase(this->_posX, this->_posY);
Game::Instance->RemoveEntity(this);
}
return;
}
}
void Projectile::Update()
{
this->_frameCount++;
if (this->_frameCount % this->_speed == 0)
{
this->_oldX = this->_posX;
this->_oldY = this->_posY;
if (this->_dir)
this->_posX++;
else
this->_posX--;
if (this->_up == 1)
this->_posY++;
else if (this->_up == -1)
this->_posY--;
else if (this->_up == 2)
{
if (rand() % 2 == 0)
this->_posY++;
else
this->_posY--;
}
this->_hasPosChanged = true;
if (_hasPosChanged)
{
if (Display::IsInMap(this->_posX, this->_posY))
{
Display::Erase(this->_oldX, this->_oldY);
if (this->_ally)
attron(COLOR_PAIR(3));
else
attron(COLOR_PAIR(2));
Display::PutChar(_drawingChar, this->_posX, this->_posY);
if (this->_ally)
attroff(COLOR_PAIR(3));
else
attroff(COLOR_PAIR(2));
}
else
{
Display::Erase(this->_oldX, this->_oldY);
Game::Instance->RemoveEntity(this);
}
_hasPosChanged = false;
}
}
}