summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/tankgame/gameovertransition.cpp
blob: 21c35109f9cf54ea458255fd4c621833dc6f8925 (plain)
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
#include "gameovertransition.h"
#include "tankitem.h"

#include <QSignalEvent>
#include <QSignalMapper>

GameOverTransition::GameOverTransition(QAbstractState *targetState)
    : QSignalTransition(new QSignalMapper(), SIGNAL(mapped(QObject*)))
{
    setTargetState(targetState);
    
    QSignalMapper *mapper = qobject_cast<QSignalMapper *>(senderObject());
    mapper->setParent(this);
}

void GameOverTransition::addTankItem(TankItem *tankItem)
{
    m_tankItems.append(tankItem);

    QSignalMapper *mapper = qobject_cast<QSignalMapper *>(senderObject());
    mapper->setMapping(tankItem, tankItem);
    connect(tankItem, SIGNAL(aboutToBeDestroyed()), mapper, SLOT(map()));
}

bool GameOverTransition::eventTest(QEvent *e) const
{
    bool ret = QSignalTransition::eventTest(e);

    if (ret) {
        QSignalEvent *signalEvent = static_cast<QSignalEvent *>(e);
        QObject *sender = qvariant_cast<QObject *>(signalEvent->arguments().at(0));
        TankItem *tankItem = qobject_cast<TankItem *>(sender);
        m_tankItems.removeAll(tankItem);

        return m_tankItems.size() <= 1;
    } else {
        return false;
    }
}