summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/errorstateplugins/seek_ai/seek_ai.cpp
blob: 2fb05d431c38ba89bd87650f7067ca46ef8e3e3a (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
40
41
42
43
44
45
46
47
48
#include "seek_ai.h"

QState *SeekAi::create(QState *parentState, QObject *tank)
{
    QState *topLevel = new QState(parentState);
    topLevel->setObjectName("topLevel");

    QState *seek = new QState(topLevel);
    seek->setObjectName("seek");
    topLevel->setInitialState(seek);

    QState *lookForNearestWall = new SearchState(tank, seek);
    lookForNearestWall->setObjectName("lookForNearestWall");
    seek->setInitialState(lookForNearestWall);
        
    QState *driveToFirstObstacle = new QState(seek);
    driveToFirstObstacle->setObjectName("driveToFirstObstacle");
    lookForNearestWall->addTransition(lookForNearestWall, SIGNAL(nearestObstacleStraightAhead()), 
        driveToFirstObstacle);

    QState *drive = new QState(driveToFirstObstacle);
    drive->setObjectName("drive");
    driveToFirstObstacle->setInitialState(drive);
    connect(drive, SIGNAL(entered()), tank, SLOT(moveForwards()));
    connect(drive, SIGNAL(exited()), tank, SLOT(stop()));
    
    // Go in loop
    QState *finishedDriving = new QState(driveToFirstObstacle);
    finishedDriving->setObjectName("finishedDriving");
    drive->addTransition(tank, SIGNAL(actionCompleted()), finishedDriving);
    finishedDriving->addTransition(drive);
        
    QState *turnTo = new QState(seek);
    turnTo->setObjectName("turnTo");
    driveToFirstObstacle->addTransition(new CollisionTransition(tank, turnTo));
    
    turnTo->addTransition(tank, SIGNAL(actionCompleted()), driveToFirstObstacle);

    ChaseState *chase = new ChaseState(tank, topLevel);
    chase->setObjectName("chase");    
    seek->addTransition(new TankSpottedTransition(tank, chase));   
    chase->addTransition(chase, SIGNAL(finished()), driveToFirstObstacle);
    chase->addTransition(new TankSpottedTransition(tank, chase));
    
    return topLevel;
}

Q_EXPORT_PLUGIN2(seek_ai, SeekAi)