249 lines
8.3 KiB
C++
Raw Normal View History

2013-12-30 17:41:15 -05:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
// Model: game.qm
2013-10-10 20:01:51 -04:00
// File: ./ship.cpp
2012-08-14 18:00:48 -04:00
//
2013-10-10 20:01:51 -04:00
// This code has been generated by QM tool (see state-machine.com/qm).
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
2012-08-14 18:00:48 -04:00
//
2013-10-10 20:01:51 -04:00
// 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.
2013-12-30 17:41:15 -05:00
//****************************************************************************
2014-04-13 21:35:34 -04:00
//${.::ship.cpp} .............................................................
2015-05-14 16:05:04 -04:00
#include "qpcpp.h"
2012-08-14 18:00:48 -04:00
#include "bsp.h"
#include "game.h"
//Q_DEFINE_THIS_FILE
#define SHIP_WIDTH 5U
#define SHIP_HEIGHT 3U
// encapsulated delcaration of the Ship active object ------------------------
2013-12-30 17:41:15 -05:00
namespace GAME {
2016-12-01 10:31:49 -05:00
2017-02-08 20:24:04 -05:00
#if ((QP_VERSION < 580) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8)))
2016-12-01 10:31:49 -05:00
#error qpcpp version 5.8.0 or higher required
#endif
2014-04-13 21:35:34 -04:00
//${AOs::Ship} ...............................................................
2016-12-01 10:31:49 -05:00
class Ship : public QP::QActive {
2012-08-14 18:00:48 -04:00
private:
uint8_t m_x;
2016-06-10 21:51:18 -04:00
uint16_t m_y;
2012-08-14 18:00:48 -04:00
uint8_t m_exp_ctr;
uint16_t m_score;
public:
Ship();
protected:
static QP::QState initial(Ship * const me, QP::QEvt const * const e);
2016-12-01 10:31:49 -05:00
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);
2012-08-14 18:00:48 -04:00
};
2013-12-30 17:41:15 -05:00
} // namespace GAME
namespace GAME {
2013-10-10 20:01:51 -04:00
2012-08-14 18:00:48 -04:00
// local objects -------------------------------------------------------------
2015-05-14 16:05:04 -04:00
static Ship l_ship; // the sole instance of the Ship active object
2012-08-14 18:00:48 -04:00
// Public-scope objects ------------------------------------------------------
2016-12-01 10:31:49 -05:00
QP::QActive * const AO_Ship = &l_ship; // opaque pointer
2012-08-14 18:00:48 -04:00
2013-12-30 17:41:15 -05:00
} // namespace GAME
2012-08-14 18:00:48 -04:00
// Active object definition --------------------------------------------------
2013-12-30 17:41:15 -05:00
namespace GAME {
2014-04-13 21:35:34 -04:00
//${AOs::Ship} ...............................................................
//${AOs::Ship::Ship} .........................................................
2013-12-30 17:41:15 -05:00
Ship::Ship()
2016-12-01 10:31:49 -05:00
: QActive(Q_STATE_CAST(&Ship::initial)),
2012-08-14 18:00:48 -04:00
m_x(GAME_SHIP_X),
2016-06-10 21:51:18 -04:00
m_y(GAME_SHIP_Y << 2)
2013-12-30 17:41:15 -05:00
{}
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
//${AOs::Ship::SM} ...........................................................
2012-08-14 18:00:48 -04:00
QP::QState Ship::initial(Ship * const me, QP::QEvt const * const e) {
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::initial}
2012-08-14 18:00:48 -04:00
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);
2015-05-14 16:05:04 -04:00
(void)e; // unused parameter
2016-12-01 10:31:49 -05:00
return Q_TRAN(&active);
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//${AOs::Ship::SM::active} ...................................................
2012-08-14 18:00:48 -04:00
QP::QState Ship::active(Ship * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2016-12-01 10:31:49 -05:00
// ${AOs::Ship::SM::active::initial}
case Q_INIT_SIG: {
status_ = Q_TRAN(&parked);
break;
}
2012-08-14 18:00:48 -04:00
default: {
2016-12-01 10:31:49 -05:00
status_ = Q_SUPER(&top);
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//${AOs::Ship::SM::active::parked} ...........................................
2012-08-14 18:00:48 -04:00
QP::QState Ship::parked(Ship * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::parked::TAKE_OFF}
2012-08-14 18:00:48 -04:00
case TAKE_OFF_SIG: {
2016-12-01 10:31:49 -05:00
status_ = Q_TRAN(&flying);
2012-08-14 18:00:48 -04:00
break;
}
default: {
2016-12-01 10:31:49 -05:00
status_ = Q_SUPER(&active);
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//${AOs::Ship::SM::active::flying} ...........................................
2012-08-14 18:00:48 -04:00
QP::QState Ship::flying(Ship * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2016-12-01 10:31:49 -05:00
// ${AOs::Ship::SM::active::flying}
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;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::flying::TIME_TICK}
2012-08-14 18:00:48 -04:00
case TIME_TICK_SIG: {
2016-06-10 21:51:18 -04:00
if (BSP_isThrottle()) {
if (me->m_y > 0U) {
me->m_y -= 1U;
}
}
else {
if (me->m_y < (GAME_TUNNEL_HEIGHT << 2)) {
me->m_y += 1U;
}
}
2012-08-14 18:00:48 -04:00
// tell the Tunnel to draw the Ship and test for hits
ObjectImageEvt *oie = Q_NEW(ObjectImageEvt, SHIP_IMG_SIG);
oie->x = me->m_x;
2016-06-10 21:51:18 -04:00
oie->y = me->m_y >> 2;
2012-08-14 18:00:48 -04:00
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);
}
2016-12-01 10:31:49 -05:00
status_ = Q_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::flying::PLAYER_TRIGGER}
2012-08-14 18:00:48 -04:00
case PLAYER_TRIGGER_SIG: {
ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, MISSILE_FIRE_SIG);
ope->x = me->m_x;
2016-06-10 21:51:18 -04:00
ope->y = (me->m_y >> 2) + SHIP_HEIGHT - 1U;
2012-08-14 18:00:48 -04:00
AO_Missile->POST(ope, me);
2016-12-01 10:31:49 -05:00
status_ = Q_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::flying::DESTROYED_MINE}
2012-08-14 18:00:48 -04:00
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
2016-12-01 10:31:49 -05:00
status_ = Q_HANDLED();
2012-08-14 18:00:48 -04:00
break;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::flying::HIT_WALL}
2012-08-14 18:00:48 -04:00
case HIT_WALL_SIG: {
2016-12-01 10:31:49 -05:00
status_ = Q_TRAN(&exploding);
2012-08-14 18:00:48 -04:00
break;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::flying::HIT_MINE}
2012-08-14 18:00:48 -04:00
case HIT_MINE_SIG: {
2016-12-01 10:31:49 -05:00
status_ = Q_TRAN(&exploding);
2012-08-14 18:00:48 -04:00
break;
}
default: {
2016-12-01 10:31:49 -05:00
status_ = Q_SUPER(&active);
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//${AOs::Ship::SM::active::exploding} ........................................
2012-08-14 18:00:48 -04:00
QP::QState Ship::exploding(Ship * const me, QP::QEvt const * const e) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
2012-08-14 18:00:48 -04:00
switch (e->sig) {
2016-12-01 10:31:49 -05:00
// ${AOs::Ship::SM::active::exploding}
case Q_ENTRY_SIG: {
me->m_exp_ctr = 0U;
status_ = Q_HANDLED();
break;
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::exploding::TIME_TICK}
2012-08-14 18:00:48 -04:00
case TIME_TICK_SIG: {
2015-05-22 20:38:16 -04:00
// ${AOs::Ship::SM::active::exploding::TIME_TICK::[me->m_exp_ctr<15U]}
2012-08-14 18:00:48 -04:00
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
2016-06-10 21:51:18 -04:00
oie->y = (int8_t)((int)(me->m_y >> 2) - 4U + SHIP_HEIGHT);
2012-08-14 18:00:48 -04:00
AO_Tunnel->POST(oie, me);
2016-12-01 10:31:49 -05:00
status_ = Q_HANDLED();
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
// ${AOs::Ship::SM::active::exploding::TIME_TICK::[else]}
2012-08-14 18:00:48 -04:00
else {
ScoreEvt *gameOver = Q_NEW(ScoreEvt, GAME_OVER_SIG);
gameOver->score = me->m_score;
AO_Tunnel->POST(gameOver, me);
2016-12-01 10:31:49 -05:00
status_ = Q_TRAN(&parked);
2012-08-14 18:00:48 -04:00
}
break;
}
default: {
2016-12-01 10:31:49 -05:00
status_ = Q_SUPER(&active);
2012-08-14 18:00:48 -04:00
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
2012-08-14 18:00:48 -04:00
}
} // namespace GAME