boat_p.h Example File
demos/sub-attaq/boat_p.h
#ifndef BOAT_P_H
#define BOAT_P_H
#include "bomb.h"
#include "graphicsscene.h"
#include <QtGui/QKeyEventTransition>
static const int MAX_BOMB = 5;
class KeyStopTransition : public QKeyEventTransition
{
public:
KeyStopTransition(Boat *b, QEvent::Type t, int k)
: QKeyEventTransition(b, t, k), boat(b), key(k)
{
}
protected:
virtual bool eventTest(QEvent *event)
{
if (!QKeyEventTransition::eventTest(event))
return false;
return (boat->currentSpeed() == 1);
}
private:
Boat * boat;
int key;
};
class KeyMoveTransition : public QKeyEventTransition
{
public:
KeyMoveTransition(Boat *b, QEvent::Type t, int k)
: QKeyEventTransition(b, t, k), boat(b), key(k)
{
}
protected:
virtual bool eventTest(QEvent *event)
{
if (!QKeyEventTransition::eventTest(event))
return false;
return (boat->currentSpeed() >= 0);
}
void onTransition(QEvent *)
{
if (key == Qt::Key_Left && boat->currentDirection() == Boat::Right)
boat->setCurrentSpeed(boat->currentSpeed() - 1);
else if (key == Qt::Key_Right && boat->currentDirection() == Boat::Left)
boat->setCurrentSpeed(boat->currentSpeed() - 1);
else if (boat->currentSpeed() < 3)
boat->setCurrentSpeed(boat->currentSpeed() + 1);
boat->updateBoatMovement();
}
private:
Boat * boat;
int key;
};
class KeyLaunchTransition : public QKeyEventTransition
{
public:
KeyLaunchTransition(Boat *boat, QEvent::Type type, int key)
: QKeyEventTransition(boat, type, key), boat(boat), key(key)
{
}
protected:
virtual bool eventTest(QEvent *event)
{
if (!QKeyEventTransition::eventTest(event))
return false;
return (boat->bombsLaunched() < MAX_BOMB);
}
private:
Boat * boat;
int key;
};
class MoveStateRight : public QState
{
public:
MoveStateRight(Boat *boat,QState *parent = 0) : QState(parent), boat(boat)
{
}
protected:
void onEntry(QEvent *)
{
boat->setCurrentDirection(Boat::Right);
boat->updateBoatMovement();
}
private:
Boat * boat;
};
class MoveStateLeft : public QState
{
public:
MoveStateLeft(Boat *boat,QState *parent = 0) : QState(parent), boat(boat)
{
}
protected:
void onEntry(QEvent *)
{
boat->setCurrentDirection(Boat::Left);
boat->updateBoatMovement();
}
private:
Boat * boat;
};
class StopState : public QState
{
public:
StopState(Boat *boat,QState *parent = 0) : QState(parent), boat(boat)
{
}
protected:
void onEntry(QEvent *)
{
boat->setCurrentSpeed(0);
boat->setCurrentDirection(Boat::None);
boat->updateBoatMovement();
}
private:
Boat * boat;
};
class LaunchStateRight : public QState
{
public:
LaunchStateRight(Boat *boat,QState *parent = 0) : QState(parent), boat(boat)
{
}
protected:
void onEntry(QEvent *)
{
Bomb *b = new Bomb();
b->setPos(boat->x()+boat->size().width(),boat->y());
GraphicsScene *scene = static_cast<GraphicsScene *>(boat->scene());
scene->addItem(b);
b->launch(Bomb::Right);
boat->setBombsLaunched(boat->bombsLaunched() + 1);
}
private:
Boat * boat;
};
class LaunchStateLeft : public QState
{
public:
LaunchStateLeft(Boat *boat,QState *parent = 0) : QState(parent), boat(boat)
{
}
protected:
void onEntry(QEvent *)
{
Bomb *b = new Bomb();
b->setPos(boat->x() - b->size().width(), boat->y());
GraphicsScene *scene = static_cast<GraphicsScene *>(boat->scene());
scene->addItem(b);
b->launch(Bomb::Left);
boat->setBombsLaunched(boat->bombsLaunched() + 1);
}
private:
Boat * boat;
};
#endif