-
Notifications
You must be signed in to change notification settings - Fork 0
/
carnivore.cpp
72 lines (64 loc) · 1.78 KB
/
carnivore.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
#include "carnivore.h"
Carnivore::Carnivore(qreal w, qreal h, const QPointF &pos,
const QPixmaps2 &pixs2, QGraphicsScene *scene,
QGraphicsItem *parent)
: AbstractFish(w, h, pos, pixs2, scene, parent),
m_name("carnivore")
{
m_hungry = Config::FISH_INIT_HUNGRY[m_name];
}
void Carnivore::advance(int)
{
if (!isVisible()){
return;
}
AbstractFish::advance(0);
}
void Carnivore::doCollide()
{
if (!m_hasTarget){
return;
}
foreach (QGraphicsItem * t, collidingItems()) {
AbstractGameItem * gameItem
= dynamic_cast<AbstractGameItem *> (t);
if (Config::COLLIDABLE_ITEMS[name()]
.contains(gameItem->name())){
AbstractFish * fish = dynamic_cast<AbstractFish *> (gameItem);
eat(0);
fish->vanish();
}
}
}
void Carnivore::findFood()
{
QList<QGraphicsItem*> items_ = scene()->items();
QVector<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()));
}
}
const QString &Carnivore::name() const
{
return m_name;
}
void Carnivore::upgrade()
{
// do nothing
}
void Carnivore::yield()
{
emit sgn_yieldMoney("diamond", scenePos());
}