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 *boat, QEvent::Type type, int key)
: QKeyEventTransition(boat, type, key)
{
this->boat = boat;
this->key = key;
}
protected:
virtual bool eventTest(QEvent *event)
{
Q_UNUSED(event);
if (!QKeyEventTransition::eventTest(event))
return false;
if (boat->currentSpeed() == 1)
return true;
else
return false;
}
private:
Boat * boat;
int key;
};
class KeyMoveTransition : public QKeyEventTransition
{
public:
KeyMoveTransition(Boat *boat, QEvent::Type type, int key)
: QKeyEventTransition(boat, type, key)
{
this->boat = boat;
this->key = key;
}
protected:
virtual bool eventTest(QEvent *event)
{
Q_UNUSED(event);
if (!QKeyEventTransition::eventTest(event))
return false;
if (boat->currentSpeed() >= 0)
return true;
else
return false;
}
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)
{
this->boat = boat;
this->key = key;
}
protected:
virtual bool eventTest(QEvent *event)
{
Q_UNUSED(event);
if (!QKeyEventTransition::eventTest(event))
return false;
if (boat->bombsLaunched() < MAX_BOMB)
return true;
else
return false;
}
private:
Boat * boat;
int key;
};
class MoveStateRight : public QState
{
public:
MoveStateRight(Boat *boat,QState *parent = 0) : QState(parent)
{
this->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)
{
this->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)
{
this->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)
{
this->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)
{
this->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
| Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) |
Trademarks |
Qt 4.6.0-tp1 |