summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/tankgameplugins/spin_ai/spin_ai.cpp
blob: de95f412832b5c77f9313c0a02fc1170a769e902 (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
#include "spin_ai.h"

#include <QtPlugin>

QState *SpinAi::create(QState *parentState, QObject *tank)
{
    QState *topLevel = new QState(parentState);
    QState *spinState = new SpinState(tank, topLevel);    
    topLevel->setInitialState(spinState);

    // When tank is spotted, fire two times and go back to spin state
    QState *fireState = new QState(topLevel);

    QState *fireOnce = new QState(fireState);
    fireState->setInitialState(fireOnce); 
    connect(fireOnce, SIGNAL(entered()), tank, SLOT(fireCannon()));

    QState *fireTwice = new QState(fireState);
    connect(fireTwice, SIGNAL(entered()), tank, SLOT(fireCannon()));
    
    fireOnce->addTransition(tank, SIGNAL(actionCompleted()), fireTwice);
    fireTwice->addTransition(tank, SIGNAL(actionCompleted()), spinState);
    
    spinState->addTransition(tank, SIGNAL(tankSpotted(qreal,qreal)), fireState);
    
    return topLevel;
}

Q_EXPORT_PLUGIN2(spin_ai, SpinAi)