-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtower.cpp
65 lines (56 loc) · 1.52 KB
/
tower.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
#include "tower.h"
#include "minion.h"
#include <QtMath>
#include <QDebug>
Tower::Tower(int hp, int atk, int attackRange, int attackDelay, int group, int target, Battle *battle, QObject *parent)
: Unit(hp, 0, 0, atk, attackRange, attackDelay, group, battle, parent)
{
connect(battle, SIGNAL(signalLogHp()), this, SLOT(setPreviousHpRatio()));
if(group == 1) setTarget(2);
else setTarget(1);
}
void Tower::setPoint(int x, int y)
{
this->x = x;
this->y = y;
}
QJsonObject Tower::toJsonObject(bool isNew)
{
QJsonObject tower;
QString name("p");
name += QString::number(group);
name += "_";
switch(SN) {
case 1:case 4:
name += "top";
break;
case 2:case 5:
name += "main";
break;
case 3: case 6:
name += "down";
break;
}
tower["name"] = name;
tower["status"] = QString::number(getHpChange());
return tower;
}
void Tower::active()
{
Minion* Target = NULL;
if(attackCnt) --attackCnt;
/* search for nearest target */
for(Unit* iter : battle->UnitList) {
if(Minion* temp = dynamic_cast<Minion*>(iter)) {
if(temp->group == target &&
(Target == NULL || hypot(temp->x - x , temp->y - y) < hypot(Target->x - x , Target->y - y)))
{
Target = temp;
}
}
}
if(Target && hypot(Target->x - x , Target->y - y) < attackRange){
if(!attackCnt) attackCnt = attackDelay;
if(attackCnt == (attackDelay/2)) Target->onhit(atk);
}
}