mirror of
https://github.com/QuantumLeaps/qpcpp.git
synced 2025-01-21 05:52:58 +08:00
225 lines
7.2 KiB
C++
225 lines
7.2 KiB
C++
//////////////////////////////////////////////////////////////////////////////
|
|
// Model: game.qm
|
|
// File: ././ship.cpp
|
|
//
|
|
// This file has been generated automatically by QP Modeler (QM).
|
|
// DO NOT EDIT THIS FILE MANUALLY.
|
|
//
|
|
// Please visit www.state-machine.com/qm for more information.
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
#include "qp_port.h"
|
|
#include "bsp.h"
|
|
#include "game.h"
|
|
|
|
namespace GAME {
|
|
|
|
//Q_DEFINE_THIS_FILE
|
|
|
|
#define SHIP_WIDTH 5U
|
|
#define SHIP_HEIGHT 3U
|
|
|
|
// encapsulated delcaration of the Ship active object ------------------------
|
|
// @(/2/1) ...................................................................
|
|
/// Ship Active Object
|
|
class Ship : public QP::QActive {
|
|
private:
|
|
uint8_t m_x;
|
|
uint8_t m_y;
|
|
uint8_t m_exp_ctr;
|
|
uint16_t m_score;
|
|
|
|
public:
|
|
Ship();
|
|
|
|
protected:
|
|
static QP::QState initial(Ship * const me, QP::QEvt const * const e);
|
|
static QP::QState active(Ship * const me, QP::QEvt const * const e);
|
|
static QP::QState parked(Ship * const me, QP::QEvt const * const e);
|
|
static QP::QState flying(Ship * const me, QP::QEvt const * const e);
|
|
static QP::QState exploding(Ship * const me, QP::QEvt const * const e);
|
|
};
|
|
|
|
// local objects -------------------------------------------------------------
|
|
static Ship l_ship; // the sole instance of the Ship active object
|
|
|
|
// Public-scope objects ------------------------------------------------------
|
|
QP::QActive * const AO_Ship = &l_ship; // opaque pointer
|
|
|
|
// Active object definition --------------------------------------------------
|
|
// @(/2/1) ...................................................................
|
|
// @(/2/1/4) .................................................................
|
|
Ship::Ship()
|
|
: QActive(Q_STATE_CAST(&Ship::initial)),
|
|
m_x(GAME_SHIP_X),
|
|
m_y(GAME_SHIP_Y)
|
|
{
|
|
}
|
|
|
|
// @(/2/1/5) .................................................................
|
|
// @(/2/1/5/0)
|
|
QP::QState Ship::initial(Ship * const me, QP::QEvt const * const e) {
|
|
me->subscribe(TIME_TICK_SIG);
|
|
me->subscribe(PLAYER_TRIGGER_SIG);
|
|
// object dictionaries...
|
|
QS_OBJ_DICTIONARY(&l_ship);
|
|
// function dictionaries for Ship HSM...
|
|
QS_FUN_DICTIONARY(&Ship::initial);
|
|
QS_FUN_DICTIONARY(&Ship::active);
|
|
QS_FUN_DICTIONARY(&Ship::parked);
|
|
QS_FUN_DICTIONARY(&Ship::flying);
|
|
QS_FUN_DICTIONARY(&Ship::exploding);
|
|
// local signals...
|
|
QS_SIG_DICTIONARY(PLAYER_SHIP_MOVE_SIG, &l_ship);
|
|
QS_SIG_DICTIONARY(TAKE_OFF_SIG, &l_ship);
|
|
QS_SIG_DICTIONARY(HIT_WALL_SIG, &l_ship);
|
|
QS_SIG_DICTIONARY(HIT_MINE_SIG, &l_ship);
|
|
QS_SIG_DICTIONARY(DESTROYED_MINE_SIG, &l_ship);
|
|
return Q_TRAN(&Ship::active);
|
|
}
|
|
// @(/2/1/5/1) ...............................................................
|
|
QP::QState Ship::active(Ship * const me, QP::QEvt const * const e) {
|
|
QP::QState status;
|
|
switch (e->sig) {
|
|
// @(/2/1/5/1/0)
|
|
case Q_INIT_SIG: {
|
|
status = Q_TRAN(&Ship::parked);
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/1)
|
|
case PLAYER_SHIP_MOVE_SIG: {
|
|
me->m_x = Q_EVT_CAST(ObjectPosEvt)->x;
|
|
me->m_y = Q_EVT_CAST(ObjectPosEvt)->y;
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
default: {
|
|
status = Q_SUPER(&QHsm::top);
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
// @(/2/1/5/1/2) .............................................................
|
|
QP::QState Ship::parked(Ship * const me, QP::QEvt const * const e) {
|
|
QP::QState status;
|
|
switch (e->sig) {
|
|
// @(/2/1/5/1/2/0)
|
|
case TAKE_OFF_SIG: {
|
|
status = Q_TRAN(&Ship::flying);
|
|
break;
|
|
}
|
|
default: {
|
|
status = Q_SUPER(&Ship::active);
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
// @(/2/1/5/1/3) .............................................................
|
|
QP::QState Ship::flying(Ship * const me, QP::QEvt const * const e) {
|
|
QP::QState status;
|
|
switch (e->sig) {
|
|
// @(/2/1/5/1/3)
|
|
case Q_ENTRY_SIG: {
|
|
me->m_score = 0U; /* reset the score */
|
|
ScoreEvt *sev = Q_NEW(ScoreEvt, SCORE_SIG);
|
|
sev->score = me->m_score;
|
|
AO_Tunnel->POST(sev, me);
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/3/0)
|
|
case TIME_TICK_SIG: {
|
|
// tell the Tunnel to draw the Ship and test for hits
|
|
ObjectImageEvt *oie = Q_NEW(ObjectImageEvt, SHIP_IMG_SIG);
|
|
oie->x = me->m_x;
|
|
oie->y = me->m_y;
|
|
oie->bmp = SHIP_BMP;
|
|
AO_Tunnel->POST(oie, me);
|
|
|
|
++me->m_score; // increment the score for surviving another tick
|
|
|
|
if ((me->m_score % 10U) == 0U) { // is the score "round"?
|
|
ScoreEvt *sev = Q_NEW(ScoreEvt, SCORE_SIG);
|
|
sev->score = me->m_score;
|
|
AO_Tunnel->POST(sev, me);
|
|
}
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/3/1)
|
|
case PLAYER_TRIGGER_SIG: {
|
|
ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, MISSILE_FIRE_SIG);
|
|
ope->x = me->m_x;
|
|
ope->y = me->m_y + SHIP_HEIGHT - 1U;
|
|
AO_Missile->POST(ope, me);
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/3/2)
|
|
case DESTROYED_MINE_SIG: {
|
|
me->m_score += Q_EVT_CAST(ScoreEvt)->score;
|
|
// the score will be sent to the Tunnel by the next TIME_TICK
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/3/3)
|
|
case HIT_WALL_SIG: {
|
|
status = Q_TRAN(&Ship::exploding);
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/3/4)
|
|
case HIT_MINE_SIG: {
|
|
status = Q_TRAN(&Ship::exploding);
|
|
break;
|
|
}
|
|
default: {
|
|
status = Q_SUPER(&Ship::active);
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
// @(/2/1/5/1/4) .............................................................
|
|
QP::QState Ship::exploding(Ship * const me, QP::QEvt const * const e) {
|
|
QP::QState status;
|
|
switch (e->sig) {
|
|
// @(/2/1/5/1/4)
|
|
case Q_ENTRY_SIG: {
|
|
me->m_exp_ctr = 0U;
|
|
status = Q_HANDLED();
|
|
break;
|
|
}
|
|
// @(/2/1/5/1/4/0)
|
|
case TIME_TICK_SIG: {
|
|
// @(/2/1/5/1/4/0/0)
|
|
if (me->m_exp_ctr < 15U) {
|
|
++me->m_exp_ctr;
|
|
// tell the Tunnel to draw the current stage of Explosion
|
|
ObjectImageEvt *oie = Q_NEW(ObjectImageEvt, EXPLOSION_SIG);
|
|
oie->bmp = EXPLOSION0_BMP + (me->m_exp_ctr >> 2);
|
|
oie->x = me->m_x; // x of explosion
|
|
oie->y = (int8_t)((int)me->m_y - 4U + SHIP_HEIGHT);
|
|
AO_Tunnel->POST(oie, me);
|
|
status = Q_HANDLED();
|
|
}
|
|
// @(/2/1/5/1/4/0/1)
|
|
else {
|
|
ScoreEvt *gameOver = Q_NEW(ScoreEvt, GAME_OVER_SIG);
|
|
gameOver->score = me->m_score;
|
|
AO_Tunnel->POST(gameOver, me);
|
|
status = Q_TRAN(&Ship::parked);
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
status = Q_SUPER(&Ship::active);
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
|
|
|
|
} // namespace GAME
|