2020-11-09 12:57:45 -05:00

267 lines
8.4 KiB
C++

//.$file${.::ship.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//
// Model: game.qm
// File: ${.::ship.cpp}
//
// This code has been generated by QM 5.1.0 <www.state-machine.com/qm/>.
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
// This program is open source software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
//.$endhead${.::ship.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include "qpcpp.hpp"
#include "bsp.hpp"
#include "game.hpp"
//Q_DEFINE_THIS_FILE
#define SHIP_WIDTH 5U
#define SHIP_HEIGHT 3U
// encapsulated delcaration of the Ship active object ------------------------
//.$declare${AOs::Ship} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace GAME {
//.${AOs::Ship} ..............................................................
class Ship : public QP::QActive {
private:
uint8_t m_x;
uint16_t m_y;
uint8_t m_exp_ctr;
uint16_t m_score;
public:
Ship();
protected:
Q_STATE_DECL(initial);
Q_STATE_DECL(active);
Q_STATE_DECL(parked);
Q_STATE_DECL(flying);
Q_STATE_DECL(exploding);
};
} // namespace GAME
//.$enddecl${AOs::Ship} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
namespace GAME {
// local objects -------------------------------------------------------------
static Ship l_ship; // the sole instance of the Ship active object
} // namespace GAME
// Public-scope objects ------------------------------------------------------
//.$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//. Check for the minimum required QP version
#if (QP_VERSION < 680U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 6.8.0 or higher required
#endif
//.$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//.$define${AOs::AO_Ship} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace GAME {
// opaque pointer
//.${AOs::AO_Ship} ...........................................................
QP::QActive * const AO_Ship = &l_ship;
} // namespace GAME
//.$enddef${AOs::AO_Ship} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Active object definition --------------------------------------------------
//.$define${AOs::Ship} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
namespace GAME {
//.${AOs::Ship} ..............................................................
//.${AOs::Ship::Ship} ........................................................
Ship::Ship()
: QActive(&initial),
m_x(GAME_SHIP_X),
m_y(GAME_SHIP_Y << 2)
{}
//.${AOs::Ship::SM} ..........................................................
Q_STATE_DEF(Ship, initial) {
//.${AOs::Ship::SM::initial}
subscribe(TIME_TICK_SIG);
subscribe(PLAYER_TRIGGER_SIG);
// local signals...
QS_SIG_DICTIONARY(PLAYER_SHIP_MOVE_SIG, this);
QS_SIG_DICTIONARY(TAKE_OFF_SIG, this);
QS_SIG_DICTIONARY(HIT_WALL_SIG, this);
QS_SIG_DICTIONARY(HIT_MINE_SIG, this);
QS_SIG_DICTIONARY(DESTROYED_MINE_SIG, this);
(void)e; // unused parameter
QS_FUN_DICTIONARY(&Ship::active);
QS_FUN_DICTIONARY(&Ship::parked);
QS_FUN_DICTIONARY(&Ship::flying);
QS_FUN_DICTIONARY(&Ship::exploding);
return tran(&active);
}
//.${AOs::Ship::SM::active} ..................................................
Q_STATE_DEF(Ship, active) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Ship::SM::active::initial}
case Q_INIT_SIG: {
status_ = tran(&parked);
break;
}
default: {
status_ = super(&top);
break;
}
}
return status_;
}
//.${AOs::Ship::SM::active::parked} ..........................................
Q_STATE_DEF(Ship, parked) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Ship::SM::active::parked::TAKE_OFF}
case TAKE_OFF_SIG: {
status_ = tran(&flying);
break;
}
default: {
status_ = super(&active);
break;
}
}
return status_;
}
//.${AOs::Ship::SM::active::flying} ..........................................
Q_STATE_DEF(Ship, flying) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Ship::SM::active::flying}
case Q_ENTRY_SIG: {
m_score = 0U; // reset the score
ScoreEvt *sev = Q_NEW(ScoreEvt, SCORE_SIG);
sev->score = m_score;
AO_Tunnel->POST(sev, this);
// lauch the ship from the initial position
m_x = GAME_SHIP_X;
m_y = (GAME_SHIP_Y << 2);
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Ship::SM::active::flying::TIME_TICK}
case TIME_TICK_SIG: {
if (BSP_isThrottle()) {
if (m_y > 0U) {
m_y -= 1U;
}
}
else {
if (m_y < (GAME_TUNNEL_HEIGHT << 2)) {
m_y += 1U;
}
}
// tell the Tunnel to draw the Ship and test for hits
ObjectImageEvt *oie = Q_NEW(ObjectImageEvt, SHIP_IMG_SIG);
oie->x = m_x;
oie->y = m_y >> 2;
oie->bmp = SHIP_BMP;
AO_Tunnel->POST(oie, this);
++m_score; // increment the score for surviving another tick
if ((m_score % 10U) == 0U) { // is the score "round"?
ScoreEvt *sev = Q_NEW(ScoreEvt, SCORE_SIG);
sev->score = m_score;
AO_Tunnel->POST(sev, this);
}
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Ship::SM::active::flying::PLAYER_TRIGGER}
case PLAYER_TRIGGER_SIG: {
ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, MISSILE_FIRE_SIG);
ope->x = m_x;
ope->y = (m_y >> 2) + SHIP_HEIGHT - 1U;
AO_Missile->POST(ope, this);
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Ship::SM::active::flying::DESTROYED_MINE}
case DESTROYED_MINE_SIG: {
m_score += Q_EVT_CAST(ScoreEvt)->score;
// the score will be sent to the Tunnel by the next TIME_TICK
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Ship::SM::active::flying::HIT_WALL}
case HIT_WALL_SIG: {
status_ = tran(&exploding);
break;
}
//.${AOs::Ship::SM::active::flying::HIT_MINE}
case HIT_MINE_SIG: {
status_ = tran(&exploding);
break;
}
default: {
status_ = super(&active);
break;
}
}
return status_;
}
//.${AOs::Ship::SM::active::exploding} .......................................
Q_STATE_DEF(Ship, exploding) {
QP::QState status_;
switch (e->sig) {
//.${AOs::Ship::SM::active::exploding}
case Q_ENTRY_SIG: {
m_exp_ctr = 0U;
status_ = Q_RET_HANDLED;
break;
}
//.${AOs::Ship::SM::active::exploding::TIME_TICK}
case TIME_TICK_SIG: {
//.${AOs::Ship::SM::active::exploding::TIME_TICK::[m_exp_ctr<15U]}
if (m_exp_ctr < 15U) {
++m_exp_ctr;
// tell the Tunnel to draw the current stage of Explosion
ObjectImageEvt *oie = Q_NEW(ObjectImageEvt, EXPLOSION_SIG);
oie->bmp = EXPLOSION0_BMP + (m_exp_ctr >> 2);
oie->x = m_x; // x of explosion
oie->y = (int8_t)((int)(m_y >> 2) - 4U + SHIP_HEIGHT);
AO_Tunnel->POST(oie, this);
status_ = Q_RET_HANDLED;
}
//.${AOs::Ship::SM::active::exploding::TIME_TICK::[else]}
else {
ScoreEvt *gameOver = Q_NEW(ScoreEvt, GAME_OVER_SIG);
gameOver->score = m_score;
AO_Tunnel->POST(gameOver, this);
status_ = tran(&parked);
}
break;
}
default: {
status_ = super(&active);
break;
}
}
return status_;
}
} // namespace GAME
//.$enddef${AOs::Ship} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^