2012-08-14 18:07:04 -04:00
|
|
|
/*****************************************************************************
|
|
|
|
* Model: game.qm
|
2012-12-10 16:00:27 -05:00
|
|
|
* File: ./ship.c
|
2012-08-14 18:07:04 -04:00
|
|
|
*
|
2013-09-23 14:34:35 -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:07:04 -04:00
|
|
|
*
|
2013-09-23 14:34:35 -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.
|
2012-08-14 18:07:04 -04:00
|
|
|
*****************************************************************************/
|
2013-09-23 14:34:35 -04:00
|
|
|
/* @(/3/2) .................................................................*/
|
2012-08-14 18:07:04 -04:00
|
|
|
#include "qp_port.h"
|
|
|
|
#include "bsp.h"
|
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
/* Q_DEFINE_THIS_FILE */
|
|
|
|
|
|
|
|
#define SHIP_WIDTH 5
|
|
|
|
#define SHIP_HEIGHT 3
|
|
|
|
|
|
|
|
/* encapsulated delcaration of the Ship active object ----------------------*/
|
|
|
|
/* @(/2/1) .................................................................*/
|
|
|
|
typedef struct ShipTag {
|
|
|
|
/* protected: */
|
|
|
|
QActive super;
|
|
|
|
|
|
|
|
/* private: */
|
|
|
|
uint8_t x;
|
|
|
|
uint8_t y;
|
|
|
|
uint8_t exp_ctr;
|
|
|
|
uint16_t score;
|
|
|
|
} Ship;
|
|
|
|
|
|
|
|
/* protected: */
|
|
|
|
static QState Ship_initial(Ship * const me, QEvt const * const e);
|
|
|
|
static QState Ship_active(Ship * const me, QEvt const * const e);
|
|
|
|
static QState Ship_parked(Ship * const me, QEvt const * const e);
|
|
|
|
static QState Ship_flying(Ship * const me, QEvt const * const e);
|
|
|
|
static QState Ship_exploding(Ship * const me, QEvt const * const e);
|
|
|
|
|
|
|
|
|
|
|
|
/* local objects -----------------------------------------------------------*/
|
|
|
|
static Ship l_ship; /* the sole instance of the Ship active object */
|
|
|
|
|
|
|
|
/* Public-scope objects ----------------------------------------------------*/
|
|
|
|
QActive * const AO_Ship = (QActive *)&l_ship; /* opaque pointer */
|
|
|
|
|
|
|
|
/* Active object definition ------------------------------------------------*/
|
|
|
|
/* @(/2/10) ................................................................*/
|
|
|
|
void Ship_ctor(void) {
|
|
|
|
Ship *me = &l_ship;
|
|
|
|
QActive_ctor(&me->super, (QStateHandler)&Ship_initial);
|
|
|
|
me->x = GAME_SHIP_X;
|
|
|
|
me->y = GAME_SHIP_Y;
|
|
|
|
}
|
|
|
|
/* @(/2/1) .................................................................*/
|
|
|
|
/* @(/2/1/4) ...............................................................*/
|
|
|
|
/* @(/2/1/4/0) */
|
|
|
|
static QState Ship_initial(Ship * const me, QEvt const * const e) {
|
|
|
|
(void)e; /* avoid the compiler warning 'usused parameter' */
|
|
|
|
QActive_subscribe((QActive *)me, TIME_TICK_SIG);
|
|
|
|
QActive_subscribe((QActive *)me, 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/4/1) .............................................................*/
|
|
|
|
static QState Ship_active(Ship * const me, QEvt const * const e) {
|
2013-09-23 14:34:35 -04:00
|
|
|
QState status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
switch (e->sig) {
|
|
|
|
/* @(/2/1/4/1/0) */
|
|
|
|
case Q_INIT_SIG: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_TRAN(&Ship_parked);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/1) */
|
|
|
|
case PLAYER_SHIP_MOVE_SIG: {
|
|
|
|
me->x = Q_EVT_CAST(ObjectPosEvt)->x;
|
|
|
|
me->y = Q_EVT_CAST(ObjectPosEvt)->y;
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_SUPER(&QHsm_top);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:34:35 -04:00
|
|
|
return status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/2) ...........................................................*/
|
|
|
|
static QState Ship_parked(Ship * const me, QEvt const * const e) {
|
2013-09-23 14:34:35 -04:00
|
|
|
QState status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
switch (e->sig) {
|
|
|
|
/* @(/2/1/4/1/2/0) */
|
|
|
|
case TAKE_OFF_SIG: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_TRAN(&Ship_flying);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_SUPER(&Ship_active);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:34:35 -04:00
|
|
|
return status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/3) ...........................................................*/
|
|
|
|
static QState Ship_flying(Ship * const me, QEvt const * const e) {
|
2013-09-23 14:34:35 -04:00
|
|
|
QState status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
switch (e->sig) {
|
|
|
|
/* @(/2/1/4/1/3) */
|
|
|
|
case Q_ENTRY_SIG: {
|
|
|
|
ScoreEvt *sev;
|
|
|
|
me->score = 0U; /* reset the score */
|
|
|
|
sev = Q_NEW(ScoreEvt, SCORE_SIG);
|
|
|
|
sev->score = me->score;
|
|
|
|
QACTIVE_POST(AO_Tunnel, (QEvt *)sev, me);
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/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->x;
|
|
|
|
oie->y = me->y;
|
|
|
|
oie->bmp = SHIP_BMP;
|
|
|
|
QACTIVE_POST(AO_Tunnel, (QEvt *)oie, me);
|
|
|
|
|
|
|
|
++me->score; /* increment the score for surviving another tick */
|
|
|
|
|
|
|
|
if ((me->score % 10U) == 0U) { /* is the score "round"? */
|
|
|
|
ScoreEvt *sev = Q_NEW(ScoreEvt, SCORE_SIG);
|
|
|
|
sev->score = me->score;
|
|
|
|
QACTIVE_POST(AO_Tunnel, (QEvt *)sev, me);
|
|
|
|
}
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/3/1) */
|
|
|
|
case PLAYER_TRIGGER_SIG: {
|
|
|
|
ObjectPosEvt *ope = Q_NEW(ObjectPosEvt, MISSILE_FIRE_SIG);
|
|
|
|
ope->x = me->x;
|
|
|
|
ope->y = me->y + SHIP_HEIGHT - 1U;
|
|
|
|
QACTIVE_POST(AO_Missile, (QEvt *)ope, me);
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/3/2) */
|
|
|
|
case DESTROYED_MINE_SIG: {
|
|
|
|
me->score += Q_EVT_CAST(ScoreEvt)->score;
|
|
|
|
/* the score will be sent to the Tunnel by the next TIME_TICK */
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/3/3) */
|
|
|
|
case HIT_WALL_SIG: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_TRAN(&Ship_exploding);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/3/4) */
|
|
|
|
case HIT_MINE_SIG: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_TRAN(&Ship_exploding);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_SUPER(&Ship_active);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:34:35 -04:00
|
|
|
return status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/4) ...........................................................*/
|
|
|
|
static QState Ship_exploding(Ship * const me, QEvt const * const e) {
|
2013-09-23 14:34:35 -04:00
|
|
|
QState status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
switch (e->sig) {
|
|
|
|
/* @(/2/1/4/1/4) */
|
|
|
|
case Q_ENTRY_SIG: {
|
|
|
|
me->exp_ctr = 0U;
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/4/0) */
|
|
|
|
case TIME_TICK_SIG: {
|
|
|
|
/* @(/2/1/4/1/4/0/0) */
|
|
|
|
if (me->exp_ctr < 15U) {
|
|
|
|
ObjectImageEvt *oie;
|
|
|
|
++me->exp_ctr;
|
|
|
|
/* tell the Tunnel to draw the current stage of Explosion */
|
|
|
|
oie = Q_NEW(ObjectImageEvt, EXPLOSION_SIG);
|
|
|
|
oie->bmp = EXPLOSION0_BMP + (me->exp_ctr >> 2);
|
|
|
|
oie->x = me->x; /* x of explosion */
|
|
|
|
oie->y = (int8_t)((int)me->y - 4U + SHIP_HEIGHT);
|
|
|
|
QACTIVE_POST(AO_Tunnel, (QEvt *)oie, me);
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_HANDLED();
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
/* @(/2/1/4/1/4/0/1) */
|
|
|
|
else {
|
|
|
|
ScoreEvt *gameOver = Q_NEW(ScoreEvt, GAME_OVER_SIG);
|
|
|
|
gameOver->score = me->score;
|
|
|
|
QACTIVE_POST(AO_Tunnel, (QEvt *)gameOver, me);
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_TRAN(&Ship_parked);
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2013-09-23 14:34:35 -04:00
|
|
|
status_ = Q_SUPER(&Ship_active);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 14:34:35 -04:00
|
|
|
return status_;
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
|