Пример #1
0
/*
**      Game loop.
*/
void    GameEngine::start()
{
    while (_window.isOpen())
    {
        if (_gameStarted)
        {
            if (GAME_OVER(_character.getSprite().getPosition().x))
            {
                if (_gameOver())
                {
                    _window.close();
                    return;
                }
                else
                    _reinit();
            }
            _handleEvents();
            _moveSprites();
            _update();
        }
        else
            _displayStartMenu();
    }
}
Пример #2
0
/**
   Primary state machine for ninja fight game handles exchange of packets and setting up game
   between the two players
*/
static void _doNinja(NinjaState *state, NinjaPacket *packet) {
  uint32_t endTime = 0;

  //Init the game state
  state->p1Idle1 = getBitmapMetadata(NINJA_P1_IDLE1_address);
  state->p2Idle1 = getBitmapMetadata(NINJA_P2_IDLE1_address);
  state->p1Idle2 = getBitmapMetadata(NINJA_P1_IDLE2_address);
  state->p2Idle2 = getBitmapMetadata(NINJA_P2_IDLE2_address);

  state->p1Punch1 = getBitmapMetadata(NINJA_P1_PUNCH1_address);
  state->p2Punch1 = getBitmapMetadata(NINJA_P2_PUNCH1_address);
  state->p1Punch2 = getBitmapMetadata(NINJA_P1_PUNCH2_address);
  state->p2Punch2 = getBitmapMetadata(NINJA_P2_PUNCH2_address);

  state->p1Kick1 = getBitmapMetadata(NINJA_P1_KICK1_address);
  state->p2Kick1 = getBitmapMetadata(NINJA_P2_KICK1_address);
  state->p1Kick2 = getBitmapMetadata(NINJA_P1_KICK2_address);
  state->p2Kick2 = getBitmapMetadata(NINJA_P2_KICK2_address);

  state->p1Shield1 = getBitmapMetadata(NINJA_P1_SHIELD1_address);
  state->p2Shield1 = getBitmapMetadata(NINJA_P2_SHIELD1_address);
  state->p1Shield2 = getBitmapMetadata(NINJA_P1_SHIELD2_address);
  state->p2Shield2 = getBitmapMetadata(NINJA_P2_SHIELD2_address);

  state->p1Dead = getBitmapMetadata(NINJA_P1_DEAD_address);
  state->p2Dead = getBitmapMetadata(NINJA_P2_DEAD_address);

  state->troll = getBitmapMetadata(NINJA_DT_address);

  state->punch = getBitmapMetadata(NINJA_PUNCH_address);
  state->kick = getBitmapMetadata(NINJA_KICK_address);
  state->shield = getBitmapMetadata(NINJA_SHIELD_address);
  state->up = getBitmapMetadata(NINJA_UP_address);
  state->down = getBitmapMetadata(NINJA_DOWN_address);
  state->right = getBitmapMetadata(NINJA_RIGHT_address);
  state->state = STATE_SELECT_MOVE;
  state->round = 1;
  state->p1Score = 0;
  state->p2Score = 0;

  while (state->p1Score < 2 && state->p2Score < 2) {
    if (state->state == STATE_SELECT_MOVE) {
      state->lastACK = -1;
      //Set p2Move to -1 *before* _getPlayerMove (which could change it!)
      state->p2Move = -1; //other player move
      state->p1Move = _getPlayerMove(state, packet);


      endTime = rtMillis() + 20000;
      bool ackRecv = false;
      bool moveRecv = false;

      //Wait for a bit sending and receiving until we're synced with other player
      while (state->lastACK != TYPE_MOVE || state->p2Move < 0) {

        //Show a dialog to the player
        char wait[32];
        sprintf(wait, "Waiting for\nplayer %d", (endTime - rtMillis()) / 1000);
        statusDialog(wait);
        safeDisplay();

        //Keep sending until an ACK is received
        if (state->lastACK != TYPE_MOVE) {
          //Fill out the rest of the packet
          packet->type = TYPE_MOVE;
          packet->data = state->p1Move;
          packet->round = state->round;
          ANXRFSend(packet, NINJA_PACKET_SIZE);
        }

        //Delay and do some ticking (400ms)
        for (uint8_t i = 0; i < 16; i++) {
          deepSleep(25);
          tick();
          //Process any data that comes in
          _handlePackets(state, packet);
        }

        //give up on other player
        if (rtMillis() > endTime) {
          state->state = STATE_ABORT;
          break;
        }
      }

      //Bump them over to fight
      if (state->state != STATE_ABORT) state->state = STATE_FIGHT;

    } else if (state->state == STATE_FIGHT) {
      _fight(state, packet);
      state->state = STATE_SELECT_MOVE;
      state->round++;
    } else if (state->state == STATE_ABORT) {
      break;
    }

    deepSleep(200);
    tick();
  }

  _gameOver(state);
  enablePopups();
}