-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.cpp
71 lines (66 loc) · 1.62 KB
/
enemy.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
#include "enemy.h"
#include<QObject>
#include<QTime>
#include<QPainter>
#include<math.h>
#include<QDebug>
enemy::enemy(QPoint m_pos,QString pixfilename,double HP):m_pos(m_pos),m_sprite(pixfilename),m_maxHP(HP),tower(nullptr)
{
}
void enemy::move()
{
int n=1;
if(m_maxHP==100)
n=2;
if(m_pos.x()<187)
m_pos.setX(m_pos.x()+n);
else if((m_pos.x()==187||m_pos.x()==188)&&m_pos.y()>=277&&m_pos.y()<379)
m_pos.setY(m_pos.y()+n);
else if(m_pos.y()>=379&&m_pos.x()>=187&&m_pos.x()<530)
m_pos.setX(m_pos.x()+n);
else if(m_pos.x()==530&&m_pos.y()<=379&&m_pos.y()>=277)
m_pos.setY(m_pos.y()-n);
else
m_pos.setX(m_pos.x()+n);
}
void enemy::draw(QPainter *painter)
{
m_sprite=m_sprite.scaled(70,70,Qt::KeepAspectRatio,Qt::SmoothTransformation);
painter->drawPixmap(m_pos,m_sprite);
painter->setPen(QPen(Qt::white,2));
painter->drawRect(m_pos.x()+8,m_pos.y()-10,50,5);
painter->fillRect(m_pos.x()+8,m_pos.y()-10,m_currentHP/2,5,Qt::red);
}
QPoint enemy::getpos() const
{
return m_pos;
}
void enemy::getfired(Tower*tower)
{
double deltx=tower->getpos().x()+20-m_pos.x();
double delty=tower->getpos().y()+20-m_pos.y();
double dis=sqrt(deltx*deltx+delty*delty);
if(dis<=radius)
{
if(tower->get_level()==1)
{
m_currentHP=m_currentHP-5;
}
else if(tower->get_level()==2)
{
m_currentHP=m_currentHP-10;
}
else
{
m_currentHP=m_currentHP-20;
}
}
}
bool enemy::live()
{
if(m_currentHP==0)
{
life=false;
return false;
}
}