void CGameView::actionEvent(int eventid, EventParm data) { switch (eventid) { case EVENT_WIN: h_actionWin(data); break; case EVENT_TIMEOUT: { setState(STATE_LOSE); gameFinal(); auto gameover = HelloWorld::create(); gameover->setString("Time out you lose!"); addChild(gameover); } break; case EVENT_PLAYERDIE: { setState(STATE_LOSE); gameFinal(); auto gameover = HelloWorld::create(); gameover->setString("GAME OVER!"); addChild(gameover); } break; default: break; } }
// JFL 30 Jul 06 int qeMain(int argc,chr *argv[]) { int fail = 0; // failure // // INIT // //qePrintf("%s / %s / %s\n",__FILE__,glGetString(GL_VERSION),qeVersion()); if(gameInit() < 0) // initialize game { fail=1; } // if if(fail) // check for catastrophic failure in init { goto BAIL; // end } // if // // BODY // // add a function object to engine if(!qeObjAddFnc(gameMainLoop)) { BRK(); // error goto BAIL; // end } gameSetup(); // setup game objects qeForever(); // turn over control to the engine until the program exits BAIL: // // FINAL // gameFinal(); // cleanup and free game objects if(qefnCheckForCleanExit() <0 ) { BRK(); // check qelog.txt for problem } // if return 0; } // qeMain()
void CGameView::setState(int stata) { switch (stata) { case STATE_INIT: log("GAME STATE_INIT"); schedule(schedule_selector(CGameView::initGame)); break; case STATE_RUN: log("GAME STATE_RUN"); m_pSp->setVisible(true); m_pPlayer->setVisible(true); m_oAllRander.push_back(m_pGameLogic); m_oAllRunner.push_back(m_pGameLogic); break; case STATE_WIN: { log("GAME STATE_WIN"); gameFinal(); auto gameover = HelloWorld::create(); gameover->setString("You Win!"); addChild(gameover); } break; case STATE_LOSE: log("GAME STATE_LOSE"); break; } this->m_State = stata; }
// JFL 22 Jul 09 // JS 21 Apr 12; Added court, paddles, and ball objects // JS 22 Apr 12; Added many object and collision resolution // JS 24 Apr 12; Implemented winning condition int gameMainLoop() { int errorCode = 0; // in case functions return errors // Game object pointers Camera *cam; HeadsUp *hud; InputController *inpCtrl; Court *court; Paddle *paddles[NUM_PLYRS]; Ball *ball; CollisionController *collCtrl; // // UPDATE // // Get input from Keyboard and QE if(inpCtrl = Game.inpCtrl) { inpCtrl->update(); } // if // Update state of the HUD if(hud = Game.hud) { hud->update(); } // if // Only update game-playing state if currently in play if(Game.inPlay) { // Check collisions if(collCtrl = Game.collCtrl) { collCtrl->update(); } // if // Update and resolve paddles for(int i=0; i<NUM_PLYRS; i++) { if(paddles[i] = Game.paddles[i]) { errorCode = paddles[i]->update(); // Print error if any if (errorCode != 0) { qePrintf("**Error: Paddle%d's *inpCtrl invalid!", i); } // if // Resolve collision of the paddles if(collCtrl && errorCode == 0) { if(collCtrl->phw[i]) { errorCode = collCtrl->resolvePaddle(i); // Print error if any if (errorCode != 0) { qePrintf("**CollCtrl's *xyzPad%d invalid!", i); } // if } // if } // if } // if } // for // Update and resolve ball if(ball = Game.ball) { ball->update(); // Resolve collision of ball if(collCtrl) { if(collCtrl->bhp[IDNX_PLY_1] || // After hitting Player1 collCtrl->bhp[IDNX_PLY_2] || // After hitting Player2 collCtrl->bhhw || // After hitting horizontal walls collCtrl->bhbw) // After hitting side walls { errorCode = collCtrl->resolveBall(); // Print error if any if (errorCode != 0) { qePrintf("**CollCtrl's *xyzBal invalid!"); } // if } // if } // if } // if ball } // if inPlay // // WIN CONDITION CHECK // // Check to see if the game is over (If someone gets 3 points) if(Game.scores[IDNX_PLY_1] == 3 || Game.scores[IDNX_PLY_2] == 3) { // Not the first game anymore, regardless Game.firstGame = false; // Game is not in play anymore (until game is restarted) Game.inPlay = false; // Play game end sound Game.sndId = qeSndPlay(snd[SDR_INDX_STR_END_GAME].name); // Manage scores of each paddle for(int i=0; i<NUM_PLYRS; i++) { // Record last scores Game.lastScores[i] = Game.scores[i]; // Clear scores, to prevent this routine from running concurrently Game.scores[i] = 0; } // for } // if // // RESET CONDITONS // // Implement soft reset (spacebar) if(inpCtrl && inpCtrl->pressedSpace()) { // Play game end sound Game.sndId = qeSndPlay(snd[SDR_INDX_STR_END_GAME].name); // Reposition ball's position if(ball = Game.ball) { ball->xyz.x = 0; ball->xyz.z = 0; } // if // Pause game very briefly qeSleep(0.5); // Manage scores of each paddle for(int i=0; i<NUM_PLYRS; i++) { // Record last scores Game.lastScores[i] = Game.scores[i]; // Clear scores, to prevent this routine from running concurrently Game.scores[i] = 0; // Reset the z-position of each paddle if(paddles[i] = Game.paddles[i]) { paddles[i]->xyz.z = PADDLE_INIT_Z; } // if } // for // Skip rest of game loop goto END_RESET; } // if // Implement hard reset (pressing "1") if(inpCtrl && inpCtrl->pressedOne()) { // Play game end sound Game.sndId = qeSndPlay(snd[SDR_INDX_STR_END_GAME].name); // Pause game qeSleep(1.5); // Reinitialize game gameFinal(); gameInit(); gameSetup(); // Skip rest of game loop goto END_RESET; } // if // // DRAW // // DRAW 3D // set 3D camera if((cam=Game.cam) && !(qeInpButton(QEINPBUTTON_ZERO)&2)) { cam->apply(); // set the camera } // if // DRAW COURT, PADDLES, BALL // Draw court if(court=Game.court) { court->draw(); } // if // Draw paddles for(int i=0; i<NUM_PLYRS; i++) { if(paddles[i] = Game.paddles[i]) { paddles[i]->draw(); } // if } // for // Draw ball if(ball=Game.ball) { ball->draw(); } // if // DRAW HUD qefnSysCamBitmap(); // set camera (also turns off depth testing) if((hud=Game.hud)) { hud->draw(); } // if END_RESET: return 0; } // gameMainLoop()