-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenemy.cpp
93 lines (79 loc) · 2.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "enemy.h"
#include "mainwindow.h"
#include <QPainter>
#include <QColor>
#include <QDebug>
#include <QMatrix>
#include <QtMath>
static const int Health_Bar_Width = 20;
const QSize Enemy::ms_fixedSize(52,52);
Enemy::Enemy(MainWindow * game,const QPixmap &worrior)
:QObject(0)
,m_worrior(worrior)
,position_x(100)
,position_y(100)
,m_walkingSpeed(1)
,m_maxHp(20)
,m_currentHp(20)
,m_active(false)
,m_game(game)
{}
Enemy::~Enemy(){
}
void Enemy::setPosition(int x, int y){
position_x=x;
position_y=y;
}
void Enemy::move(){
if(!m_active)
return ;
if(position_x>=100 && position_y==100 &&position_x<300)
{ int _x=position_x+m_walkingSpeed;
setPosition(_x,position_y);
}
else if(position_x==300 && position_y>=100 && position_y<200)
{int _y=position_y+m_walkingSpeed;
setPosition(position_x,_y);
}
else if(position_x<=300 && position_x>100 && position_y==200)
{int _x=position_x-m_walkingSpeed;
setPosition(_x,position_y);
}
else m_game->removedEnemy(this);
}
void Enemy::getAttack(int attack){
m_currentHp-=attack;
}
void Enemy::countofAttack(){
if(position_y==100 && position_x>=150 &&position_x<=155)
{
if(m_currentHp>0)
getAttack(1);
else m_game->removedEnemy(this);
}
else getAttack(0);
}
void Enemy::doActivate()
{
m_active = true;
}
void Enemy::draw(QPainter* painter) {
if(!m_active)
return ;
painter->save();
countofAttack();
// QPoint healthBarPoint = m_pos + QPoint(-Health_Bar_Width / 2 - 5, -ms_fixedSize.height() / 3);
QPoint healthBarPoint = QPoint(position_x+2,position_y-4);
// 绘制血条
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::red);
QRect healthBarBackRect(healthBarPoint, QSize(Health_Bar_Width, 2));
painter->drawRect(healthBarBackRect);
if(m_currentHp>0){
painter->setBrush(Qt::green);
QRect healthBarRect(healthBarPoint, QSize(m_currentHp, 2));
painter->drawRect(healthBarRect);
}
painter->drawPixmap(position_x,position_y,20,30,m_worrior);
painter->restore();
}