-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenemy.cpp
More file actions
70 lines (63 loc) · 1.69 KB
/
Copy pathenemy.cpp
File metadata and controls
70 lines (63 loc) · 1.69 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
#include "enemy.h"
Enemy* EnemyFactory::getEnemy(QString name, int x, int y)
{
if(name == "soldier"){
return new Soldier(x,y);
}
else if(name == "dog"){
return new Dog(x, y);
}
else if(name == "plane"){
return new Plane(x, y);
}
else if(name == "hammer"){
return new Hammer(x, y);
}
else return new Bully(x, y);
}
Enemy::Enemy(QString name, int x, int y, int atk, int def, int hp, int s): Unit(x, y, atk, def, hp, name), speed(s){
QTimer* timer = new QTimer;
timer->start(33);
connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
}
bool Enemy::win()
{
if((y < 200 && x < 200) || (y < 300 && x < 180) || (y < 400 && x < 160) || (y < 500 && x < 140)) return true;
return false;
}
void Enemy::advance(){
time_cnt = (time_cnt + 1) % 15;
bool collide_with_executor = false;
QList<QGraphicsItem *> colliding_items = collidingItems();
for(auto ele: colliding_items){
Executor* executor = dynamic_cast<Executor*>(ele);
if (executor){
if(std::abs(executor->gety() - y) > 30) continue;
collide_with_executor = true;
if(!battle && executor->passable()) continue;
if(!battle) {
battle = true;
// executor->modifyHolding(1);
}
if(!time_cnt) executor->modifyHp(-atk);
}
}
if(!collide_with_executor){
imageNo = 0;
battle = false;
}
if(battle && !time_cnt){
imageNo = 1 - imageNo;
update();
}
if(!battle){
x -= speed;
setPos(x, y);
}
}
void Plane::advance()
{
time_cnt = (time_cnt + 1) % 15;
x -= speed;
setPos(x, y);
}