summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp
blob: c196247b01aac206389feab0baaab34dfdfedb7f (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
#include "random_ai_plugin.h"

#include <QState>
#include <QtPlugin>

#include <time.h>

QState *RandomAiPlugin::create(QState *parentState, QObject *tank)
{
    qsrand(uint(time(NULL)));

    QState *topLevel = new QState(parentState);

    QState *selectNextActionState = new SelectActionState(topLevel);
    topLevel->setInitialState(selectNextActionState);

    QState *fireState = new RandomDistanceState(topLevel);
    connect(fireState, SIGNAL(distanceComputed(qreal)), tank, SLOT(fireCannon()));
    selectNextActionState->addTransition(selectNextActionState, SIGNAL(fireSelected()), fireState);

    QState *moveForwardsState = new RandomDistanceState(topLevel);
    connect(moveForwardsState, SIGNAL(distanceComputed(qreal)), tank, SLOT(moveForwards(qreal)));
    selectNextActionState->addTransition(selectNextActionState, SIGNAL(moveForwardsSelected()), moveForwardsState);

    QState *moveBackwardsState = new RandomDistanceState(topLevel);
    connect(moveBackwardsState, SIGNAL(distanceComputed(qreal)), tank, SLOT(moveBackwards(qreal)));
    selectNextActionState->addTransition(selectNextActionState, SIGNAL(moveBackwardsSelected()), moveBackwardsState);

    QState *turnState = new RandomDistanceState(topLevel);
    connect(turnState, SIGNAL(distanceComputed(qreal)), tank, SLOT(turn(qreal)));
    selectNextActionState->addTransition(selectNextActionState, SIGNAL(turnSelected()), turnState);

    topLevel->addTransition(tank, SIGNAL(actionCompleted()), selectNextActionState);

    return topLevel;
}

Q_EXPORT_PLUGIN2(random_ai, RandomAiPlugin)