-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractalien.cpp
146 lines (130 loc) · 3.75 KB
/
abstractalien.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
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
141
142
143
144
145
146
#include "abstractalien.h"
#include "abstractfish.h"
#include <QDebug>
AbstractAlien::AbstractAlien(qreal w, qreal h, const QPointF &pos,
const QPixmaps2 &pixs2, QGraphicsScene *scene,
QGraphicsItem *parent)
: AbstractCreature(w, h, pos, pixs2, scene, parent),
m_health(0)
{
}
void AbstractAlien::advance(int)
{
AbstractCreature::advance(0);
if (!isAlive()){ //vanish if die
vanish();
return;
}
if (m_step % Config::UPDATE_PAINT_STEP == 0){ // update action, direction and pos
updateDirection();
m_pixIndex++;
if (m_pixIndex == Config::ALIEN_INDEX_COUNT){
m_pixIndex = 0;
if (turning()){ // finish turning
turning(false);
}
}
else if (turning() && isAlive()){
if (right()){
m_pixStateIndex = Config::ALIEN_TURN_RIGHT_STATE_INDEX;
}
else if (left()){
m_pixStateIndex = Config::ALIEN_TURN_LEFT_STATE_INDEX;
}
}
else if (isAlive()){
if (left()){
m_pixStateIndex = Config::ALIEN_SWIM_LEFT_STATE_INDEX;
}
else if (right()){
m_pixStateIndex = Config::ALIEN_SWIM_RIGHT_STATE_INDEX;
}
}
move();
}
if (m_step % Config::FIND_FOOD_STEP == 0
&& !m_hasTarget
&& isAlive()){
findFood();
}
}
void AbstractAlien::doCollide()
{
foreach (QGraphicsItem * t, collidingItems()){
AbstractGameItem * gameItem
= dynamic_cast<AbstractGameItem *> (t);
if (Config::COLLIDABLE_ITEMS[name()]
.contains(gameItem->name())){
if (!gameItem->isVisible()){
continue;
}
AbstractFish * fish = dynamic_cast<AbstractFish *> (gameItem);
eat(0);
fish->vanish();
}
}
}
void AbstractAlien::injured(const int deltaHealth)
{
m_health -= deltaHealth;
if (m_health <= 0){
die();
}
}
void AbstractAlien::move()
{
if (m_hasTarget){
if (m_step % Config::CHASE_STEP == 0){
aimAt(m_target);
setDirection(direction()
+ RandomMaker::creatRandom(-10, 11) / 40.0);
}
}
else if (!m_hasTarget && !turning()){
if (m_step % Config::CHANGE_DIRECTION_STEP == 0){
setDirection(direction()
+ RandomMaker::creatRandom(-5, 6) / 40.0);
}
}
AbstractMovableItem::move();
keepInScene();
}
void AbstractAlien::findFood()
{
QList<QGraphicsItem*> items_ = scene()->items();
QList<AbstractMovableItem*> edibleItems;
foreach (QGraphicsItem * item, items_){
AbstractGameItem * gameItem = dynamic_cast<AbstractGameItem *> (item);
if (Config::COLLIDABLE_ITEMS[name()]
.contains(gameItem->name())
&& gameItem->isVisible()){
edibleItems.append(dynamic_cast<AbstractMovableItem *>(gameItem));
}
}
if (edibleItems.size() > 0){
m_target = edibleItems.at(RandomMaker::creatRandom(edibleItems.size()));
m_hasTarget = true;
connect(m_target, SIGNAL(sgn_deleting()),
this, SLOT(slt_lostAim()));
}
}
void AbstractAlien::die()
{
if (!isVisible()){
return;
}
AbstractCreature::die();
emit sgn_alienDie();
yield();
}
void AbstractAlien::yield()
{
emit sgn_yieldMoney("diamond", scenePos());
}
void AbstractAlien::slt_attacked(const QPointF & pos)
{
QPointF mPos = centrePos();
setDirection(atan2(mPos.y() - pos.y(),
mPos.x() - pos.x()));
injured(Config::ATTACK_ALIEN_DAMAGE);
}