/* update - This method should be called once per frame. It updates both the sprites and the game world. Note that even though the game world is for static data, should the user wish to put anything dynamic there (like a non-collidable moving layer), the updateWorld method is called. */ void GameStateManager::update(Game *game) { game->getGameRules()->spawnEnemies(game); spriteManager->update(game); world.update(game); physics.update(game); AnimatedSprite *p = spriteManager->getPlayer(); PhysicalProperties *pp = p->getPhysicalProperties(); float pVelX = pp->getVelocityX(); float pVelY = pp->getVelocityY(); //VIEWPORT SCOPING /*Viewport *vp = game->getGUI()->getViewport(); if(pVelX < 0 && (pp->round(pp->getX() - vp->getViewportX())) < vp->getViewportWidth()/3) { vp->setScrollSpeedX(pVelX); if(vp->getViewportX() == 0) vp->setScrollSpeedX(0); } else if(pVelX > 0 && (pp->round(pp->getX() - vp->getViewportX())) > (vp->getViewportWidth()/3*2)) { vp->setScrollSpeedX(pVelX); if(vp->getViewportX()+vp->getViewportWidth() == world.getWorldWidth()) vp->setScrollSpeedX(0); } else { vp->setScrollSpeedX(0); } if(pVelY < 0 && (pp->round(pp->getY() - vp->getViewportY())) < vp->getViewportHeight()/3) { vp->setScrollSpeedY(pVelY); if(vp->getViewportY() == 0) vp->setScrollSpeedY(0); } else if(pVelY > 0 && (pp->round(pp->getY() - vp->getViewportY())) > (vp->getViewportHeight()/3*2)) { vp->setScrollSpeedY(pVelY); if(vp->getViewportY()+vp->getViewportHeight() == world.getWorldHeight()) vp->setScrollSpeedY(0); } else { vp->setScrollSpeedY(0); } vp->moveViewport( vp->getScrollSpeedX(), vp->getScrollSpeedY(), world.getWorldWidth(), world.getWorldHeight());*/ }
void ScienceMouseEventHandler::handleMousePressEvent(Game *game, int mouseX, int mouseY) { if (game->getGSM()->isGameInProgress()) { Viewport *viewport = game->getGUI()->getViewport(); // DETERMINE WHERE ON THE MAP WE HAVE CLICKED int worldCoordinateX = mouseX + viewport->getViewportX(); int worldCoordinateY = mouseY + viewport->getViewportY(); // NOW LET'S SEE IF THERE IS A SPRITE THERE GameStateManager *gsm = game->getGSM(); SpriteManager *spriteManager = gsm->getSpriteManager(); // IF THERE IS NO SELECTED SPRITE LOOK FOR ONE if (!(spriteManager->getIsSpriteSelected())) { // IF THIS DOES NOT RETURN NULL THEN YOU FOUND A SPRITE AT THAT LOCATION if((spriteManager->getSpriteAt(worldCoordinateX, worldCoordinateY) != NULL)) { AnimatedSprite *selected = spriteManager->getSpriteAt(worldCoordinateX, worldCoordinateY); spriteManager->setIsSpriteSelected(true); } } else if (spriteManager->getSelectedSprite() != NULL) { // MOVE A SPRITE IN A DESIRED DIRECTION AnimatedSprite *selected = spriteManager->getSelectedSprite(); PhysicalProperties *pp = selected->getPhysicalProperties(); float spriteX = pp->getX(); float spriteY = pp->getY(); //IF A SPRITE IS WHERE YOU WANT IT THEN STOP IT if (((spriteX > worldCoordinateX - 64) && (spriteX < worldCoordinateX + 64)) && (spriteY > worldCoordinateY - 64) && (spriteY < worldCoordinateY + 64)) { pp->setVelocity(0, 0); } else { float deltaX = worldCoordinateX - spriteX; float deltaY = worldCoordinateY - spriteY; float hyp = sqrtf((deltaX * deltaX) + (deltaY * deltaY)); pp->setVelocity((deltaX / hyp) * 3, (deltaY / hyp) * 3); } spriteManager->setIsSpriteSelected(false); // GridPathfinder *pathfinder = spriteManager->getPathfinder(); // pathfinder->mapPath(selected, (float)worldCoordinateX, (float)worldCoordinateY); // gsm->setSpriteSelected(false, selected); } else { spriteManager->setIsSpriteSelected(false); } } }
void BalloonEscapeCollisionListener::respondToCollision(Game *game, Collision *collision) { // NOTE FROM THE COLLIDABLE OBJECTS, WHICH ARE IN THE COLLISION, // WE CAN CHECK AND SEE ON WHICH SIDE THE COLLISION HAPPENED AND // CHANGE SOME APPROPRIATE STATE ACCORDINGLY GameStateManager *gsm = game->getGSM(); AnimatedSprite *player = gsm->getSpriteManager()->getPlayer(); PhysicalProperties *pp = player->getPhysicalProperties(); //AnimatedSprite *bot = collision CollidableObject *sprite2 = collision->getCO2(); PhysicalProperties *pp2 = sprite2->getPhysicalProperties(); AnimatedSprite *bot = (AnimatedSprite*)sprite2; if (!collision->isCollisionWithTile() && player->getCurrentState() != L"DYING") { CollidableObject *sprite = collision->getCO1(); if (sprite2->getPhysicalProperties()->getZ() == 1.0f) { bot = (AnimatedSprite*)sprite; pp = sprite2->getPhysicalProperties(); pp2 = sprite->getPhysicalProperties(); sprite = collision->getCO2(); sprite2 = collision->getCO1(); if (sprite->getCollisionEdge() == BOTTOM_EDGE) { bot->setCurrentState(L"DYING"); pp2->setVelocity(0.0f, 0.0f); pp2->setAccelerationX(0); pp2->setAccelerationY(0); // ENEMY IS DEAD - WE SHOULD PLAY A DEATH ANIMATION // AND MARK IT FOR REMOVAL } else if (bot->getCurrentState() != L"DYING") { if (pp->getDelay() == 0) { if (pp->getHP() == 10 ) { //lives->setCurrentState(L"TWO"); if(deadonce == true) //lives->setCurrentState(L"ONE"); player->setCurrentState(L"DYING"); pp->setDelay(90); pp->setVelocity(0.0f, 0.0f); pp->setAccelerationX(0); pp->setAccelerationY(0); deadonce=true; } else { pp->setDelay(90); pp->setHP(pp->getHP()-10); SpriteManager *spriteManager = gsm->getSpriteManager(); AnimatedSpriteType *yellowman = spriteManager->getSpriteType(3); player->setSpriteType(yellowman); } } // PLAYER IS DEAD - WE SHOULD PLAY A DEATH ANIMATION // AND MARK IT FOR REMOVAL/RESPAWN/RESTART GAME, WHATEVER // THE DEMANDS OF THE GAME ARE } } else if(sprite->getPhysicalProperties()->getZ() == 1.0f) { PhysicalProperties *pp = sprite->getPhysicalProperties(); if (sprite->getCollisionEdge() == BOTTOM_EDGE) { bot->setCurrentState(L"DYING"); pp2->setVelocity(0.0f, 0.0f); pp2->setAccelerationX(0); pp2->setAccelerationY(0); // ENEMY IS DEAD - WE SHOULD PLAY A DEATH ANIMATION // AND MARK IT FOR REMOVAL } else if (bot->getCurrentState() != L"DYING") { if (pp->getDelay() == 0) { if (pp->getHP() == 10 ) { //lives->setCurrentState(L"TWO"); if(deadonce == true) //lives->setCurrentState(L"ONE"); player->setCurrentState(L"DYING"); pp->setDelay(90); pp->setVelocity(0.0f, 0.0f); pp->setAccelerationX(0); pp->setAccelerationY(0); deadonce=true; } else { pp->setDelay(90); pp->setHP(pp->getHP()-10); SpriteManager *spriteManager = gsm->getSpriteManager(); AnimatedSpriteType *yellowman = spriteManager->getSpriteType(3); player->setSpriteType(yellowman); } } // PLAYER IS DEAD - WE SHOULD PLAY A DEATH ANIMATION // AND MARK IT FOR REMOVAL/RESPAWN/RESTART GAME, WHATEVER // THE DEMANDS OF THE GAME ARE } } } if (pp->getDelay() == 0) { SpriteManager *spriteManager = gsm->getSpriteManager(); AnimatedSpriteType *redman = spriteManager->getSpriteType(2); player->setSpriteType(redman); } }
/* handleKeyEvent - this method handles all keyboard interactions. Note that every frame this method gets called and it can respond to key interactions in any custom way. Ask the GameInput class for key states since the last frame, which can allow us to respond to key presses, including when keys are held down for multiple frames. */ void BugsKeyEventHandler::handleKeyEvents(Game *game) { // WE CAN QUERY INPUT TO SEE WHAT WAS PRESSED GameInput *input = game->getInput(); // LET'S GET THE PLAYER'S PHYSICAL PROPERTIES, IN CASE WE WANT TO CHANGE THEM GameStateManager *gsm = game->getGSM(); AnimatedSprite *player = gsm->getSpriteManager()->getPlayer(); PhysicalProperties *pp = player->getPhysicalProperties(); Viewport *viewport = game->getGUI()->getViewport(); // IF THE GAME IS IN PROGRESS if (gsm->isGameInProgress()) { if (input->isKeyDownForFirstTime(D_KEY)) { viewport->toggleDebugView(); game->getGraphics()->toggleDebugTextShouldBeRendered(); } if (input->isKeyDownForFirstTime(L_KEY)) { game->getGraphics()->togglePathfindingGridShouldBeRendered(); } if (input->isKeyDownForFirstTime(F_KEY)) { game->getGraphics()->togglePathfindingPathShouldBeRendered(); } if (input->isKeyDownForFirstTime(R_KEY)) { if (wcscmp(player->getCurrentState().c_str(), L"RUNNING") == 0) player->setCurrentState(L"WALKING"); else if ((wcscmp(player->getCurrentState().c_str(), L"IDLE") != 0) && (wcscmp(player->getCurrentState().c_str(), L"DANCING") != 0)) player->setCurrentState(L"RUNNING"); } if (input->isKeyDownForFirstTime(Y_KEY)){ player->setCurrentState(DYING); } if (input->isKeyDownForFirstTime(I_KEY)){ player->setCurrentState(IDLE); } bool viewportMoved = false; float viewportVx = 0.0f; float viewportVy = 0.0f; float vX = pp->getVelocityX(); float vY = pp->getVelocityY(); if (input->isKeyDown(UP_KEY)) { if (pp->getY() < (viewport->getViewportY() + 0.5f * viewport->getViewportHeight())) viewportVy -= MAX_PLAYER_VELOCITY; vY = -1 * MAX_PLAYER_VELOCITY; vX = 0; viewportMoved = true; } else if (input->isKeyDown(DOWN_KEY)) { if (pp->getY() > (viewport->getViewportY() + 0.5f * viewport->getViewportHeight())) viewportVy += MAX_PLAYER_VELOCITY; vY = MAX_PLAYER_VELOCITY; vX = 0; viewportMoved = true; } else if (input->isKeyDown(LEFT_KEY)) { if (pp->getX() < (viewport->getViewportX() + 0.5f * viewport->getViewportWidth())) viewportVx -= MAX_PLAYER_VELOCITY; vX = -1 * MAX_PLAYER_VELOCITY; vY = 0; viewportMoved = true; } else if (input->isKeyDown(RIGHT_KEY)) { if (pp->getX() > (viewport->getViewportX() + 0.5f * viewport->getViewportWidth())) viewportVx += MAX_PLAYER_VELOCITY; vX = MAX_PLAYER_VELOCITY; vY = 0; viewportMoved = true; } else { vX = 0.0f; vY = 0.0f; } if (viewportMoved) viewport->moveViewport((int)floor(viewportVx+0.5f), (int)floor(viewportVy+0.5f), game->getGSM()->getWorld()->getWorldWidth(), game->getGSM()->getWorld()->getWorldHeight()); pp->setVelocity(vX, vY); } // 0X43 is HEX FOR THE 'C' VIRTUAL KEY // THIS CHANGES THE CURSOR IMAGE if ((input->isKeyDownForFirstTime(C_KEY)) && input->isKeyDown(VK_SHIFT)) { Cursor *cursor = game->getGUI()->getCursor(); unsigned int id = cursor->getActiveCursorID(); id++; if (id == cursor->getNumCursorIDs()) id = 0; cursor->setActiveCursorID(id); } // LET'S MESS WITH THE TARGET FRAME RATE IF THE USER PRESSES THE HOME OR END KEYS WindowsTimer *timer = (WindowsTimer*)game->getTimer(); int fps = timer->getTargetFPS(); // THIS SPEEDS UP OUR GAME LOOP AND THUS THE GAME, NOTE THAT WE COULD ALTERNATIVELY SCALE // DOWN THE GAME LOGIC (LIKE ALL VELOCITIES) AS WE SPEED UP THE GAME. THAT COULD PROVIDE // A BETTER PLAYER EXPERIENCE if (input->isKeyDown(VK_HOME) && (fps < MAX_FPS)) timer->setTargetFPS(fps + FPS_INC); // THIS SLOWS DOWN OUR GAME LOOP, BUT WILL NOT GO BELOW 5 FRAMES PER SECOND else if (input->isKeyDown(VK_END) && (fps > MIN_FPS)) timer->setTargetFPS(fps - FPS_INC); }
/* handleKeyEvent - this method handles all keyboard interactions. Note that every frame this method gets called and it can respond to key interactions in any custom way. Ask the GameInput class for key states since the last frame, which can allow us to respond to key presses, including when keys are held down for multiple frames. */ void SoSKeyEventHandler::handleKeyEvents(Game *game) { // WE CAN QUERY INPUT TO SEE WHAT WAS PRESSED GameInput *input = game->getInput(); // LET'S GET THE PLAYER'S PHYSICAL PROPERTIES, IN CASE WE WANT TO CHANGE THEM GameStateManager *gsm = game->getGSM(); AnimatedSprite *player = gsm->getSpriteManager()->getPlayer(); PhysicalProperties *pp = player->getPhysicalProperties(); if(gsm->isAtSplashScreen()) { if(input->isKeyDown(ENTER_KEY)) { gsm->goToMainMenu(); } } // IF THE GAME IS IN PROGRESS if (gsm->isGameInProgress()) { // CHECK FOR WASD KEYS FOR MOVEMENT int incX = 0; int incY = 0; bool movingLR = false; bool attacking = false; if(!pp->isStunned()) { if(input->isKeyDown(SPACE_KEY)) { attacking = true; if(input->isKeyDownForFirstTime(SPACE_KEY)) { player->setCurrentState(L"ATTACK_STATE"); if(!pp->isOrientedRight()) player->setCurrentState(L"ATTACKL_STATE"); } } // WASD AND DIRECTION KEY PRESSES WILL CONTROL THE PLAYER, // SO WE'LL UPDATE THE PLAYER VELOCITY WHEN THESE KEYS ARE // PRESSED, THAT WAY PHYSICS CAN CORRECT AS NEEDED float vX = pp->getVelocityX(); float vY = pp->getVelocityY(); if (input->isKeyDown(A_KEY) || input->isKeyDown(LEFT_KEY)) { movingLR = true; pp->setOrientedLeft(); vX = -PLAYER_SPEED; if (vY == 0 && player->getCurrentState().compare(L"LEFT_STATE") != 0) player->setCurrentState(L"LEFT_STATE"); else if(vY != 0 && player->getCurrentState().compare(L"JUMPL_STATE") != 0) player->setCurrentState(L"JUMPL_STATE"); } if (input->isKeyDown(D_KEY) || input->isKeyDown(RIGHT_KEY)) { movingLR = true; pp->setOrientedRight(); vX = PLAYER_SPEED; if (vY == 0 && player->getCurrentState().compare(L"RIGHT_STATE") != 0) player->setCurrentState(L"RIGHT_STATE"); else if(vY != 0 && player->getCurrentState().compare(L"JUMP_STATE") != 0) player->setCurrentState(L"JUMP_STATE"); } /*if (input->isKeyDown(S_KEY) || input->isKeyDown(DOWN_KEY)) { vY = PLAYER_SPEED; }*/ if (input->isKeyDown(W_KEY) || input->isKeyDown(UP_KEY)) { if ((input->isKeyDownForFirstTime(W_KEY) || input->isKeyDownForFirstTime(UP_KEY)) && pp->hasDoubleJumped() == false) { if(pp->hasJumped() == true) pp->setDoubleJumped(true); pp->setJumped(true); vY = -PLAYER_SPEED; player->setCurrentState(L"JUMP_STATE"); if(vX < 0) player->setCurrentState(L"JUMPL_STATE"); } } if(!movingLR) { vX = 0; } if(vY == 0 && vX == 0 && !attacking && player->getCurrentState().compare(L"IDLE_STATE") != 0 && player->getCurrentState().compare(L"IDLEL_STATE") != 0 ) { player->setCurrentState(L"IDLE_STATE"); if(!pp->isOrientedRight()) player->setCurrentState(L"IDLEL_STATE"); } // NOW SET THE ACTUAL VELOCITY Physics *physics = gsm->getPhysics(); pp->setVelocitySafely(physics, vX, vY); } // NOTE THAT THE VIEWPORT SHOULD FOLLOW THE PLAYER, AND SO SHOULD BE CORRECTED AFTER PHYSICS // ARE APPLIED. I HAVE PROVIDED A SIMPLE WAY OF DOING SO, WHICH SHOULD BE IMPROVED, DEPENDING // ON THE GAME'S NEED if(input->isKeyDownForFirstTime(TAB_KEY)) { gsm->goToPaused(); } } else if(gsm->isGamePaused() && input->isKeyDownForFirstTime(TAB_KEY)) { gsm->goToGame(); } // 0X43 is HEX FOR THE 'C' VIRTUAL KEY // THIS CHANGES THE CURSOR IMAGE if ((input->isKeyDownForFirstTime(C_KEY)) && input->isKeyDown(VK_SHIFT)) { Cursor *cursor = game->getGUI()->getCursor(); unsigned int id = cursor->getActiveCursorID(); id++; if (id == cursor->getNumCursorIDs()) id = 0; cursor->setActiveCursorID(id); } // LET'S MESS WITH THE TARGET FRAME RATE IF THE USER PRESSES THE HOME OR END KEYS WindowsTimer *timer = (WindowsTimer*)game->getTimer(); int fps = timer->getTargetFPS(); // THIS SPEEDS UP OUR GAME LOOP AND THUS THE GAME, NOTE THAT WE COULD ALTERNATIVELY SCALE // DOWN THE GAME LOGIC (LIKE ALL VELOCITIES) AS WE SPEED UP THE GAME. THAT COULD PROVIDE // A BETTER PLAYER EXPERIENCE if (input->isKeyDown(VK_HOME) && (fps < MAX_FPS)) timer->setTargetFPS(fps + FPS_INC); // THIS SLOWS DOWN OUR GAME LOOP, BUT WILL NOT GO BELOW 5 FRAMES PER SECOND else if (input->isKeyDown(VK_END) && (fps > MIN_FPS)) timer->setTargetFPS(fps - FPS_INC); }
void Physics::update(Game *game) { // REMEMBER, AT THIS POINT, ALL PLAYER INPUT AND AI // HAVE ALREADY BEEN PROCESSED AND BOT AND PLAYER // STATES, VELOCITIES, AND ACCELERATIONS HAVE ALREADY // BEEN UPDATED. NOW WE HAVE TO PROCESS THE PHYSICS // OF ALL THESE OBJECTS INTERACTING WITH EACH OTHER // AND THE STATIC GAME WORLD. THIS MEANS WE NEED TO // DETECT AND RESOLVE COLLISIONS IN THE ORDER THAT // THEY WILL HAPPEN, AND WITH EACH COLLISION, EXECUTE // ANY GAMEPLAY RESPONSE CODE, UPDATE VELOCITIES, AND // IN THE END, UPDATE POSITIONS // FIRST, YOU SHOULD START BY ADDING ACCELERATION TO ALL // VELOCITIES, WHICH INCLUDES GRAVITY, NOTE THE EXAMPLE // BELOW DOES NOT DO THAT // FOR NOW, WE'LL JUST ADD THE VELOCITIES TO THE // POSITIONS, WHICH MEANS WE'RE NOT APPLYING GRAVITY OR // ACCELERATION AND WE ARE NOT DOING ANY COLLISION // DETECTION OR RESPONSE float timer = 0; GameStateManager *gsm = game->getGSM(); SpriteManager *sm = gsm->getSpriteManager(); World *w = gsm->getWorld(); GameRules* gR = game->getGameRules(); vector<WorldLayer*> *layers = w->getLayers(); AnimatedSprite *player; PhysicalProperties *pp; TiledLayer *tL; list<Collision*> collisions; //finding TileLayer for(unsigned int i = 0; i < layers->size(); i++) { WorldLayer *currentLayer = (*layers)[i]; if(currentLayer->hasCollidableTiles() == true) { tL = dynamic_cast<TiledLayer*>(currentLayer); if(tL != 0) { i = layers->size(); }//end if }//end if } player = sm->getPlayer(); pp = player->getPhysicalProperties(); //UPDATING ALL VELOCITIES AND DOING TILE COLLISION pp->incVelocity(this,pp->getAccelerationX(), pp->getAccelerationY() + gravity); collideTestWithTiles(player, tL, &collisions); list<Bot*>::iterator botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->incVelocity(this, pp->getAccelerationX(), pp->getAccelerationY()); if(pp->isGravAffected() == true) pp->incVelocity(this, 0, gravity); collideTestWithTiles(bot, tL, &collisions); botIterator++; } //HERE, COLLIDE SPRITES WITH OTHER SPRITES collideTestWithSprites(game, player, &collisions); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); if(bot->isCurrentlyCollidable() == true); collideTestWithSprites(game, bot, &collisions); botIterator++; } //SORT COLLISIONS collisions.sort(compare_collisionTime); //RESOLVING ALL THE COLLISIONS while(collisions.empty() == false) { Collision* currentCollision = collisions.front(); collisions.pop_front(); float colTime = currentCollision->getTOC(); CollidableObject* co1 = currentCollision->getCO1(); CollidableObject* co2 = currentCollision->getCO2(); if(colTime >= 0 && colTime <= 1) { pp = co1->getPhysicalProperties(); //pp->setVelocity(pp->getVelocityX()*9.99f,pp->getVelocityY()*9.99f); pp = co2->getPhysicalProperties(); //pp->setVelocity(pp->getVelocityX()*9.99f,pp->getVelocityY()*9.99f); pp = player->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(colTime-timer)),pp->getY() + (pp->getVelocityY()*(colTime-timer))); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(colTime-timer)), pp->getY() + (pp->getVelocityY()*(colTime-timer))); botIterator++; } gsm->updateViewport(game, colTime-timer); resolveCollision(game, currentCollision); gR->gameSpecificResolve(game, currentCollision); boolean deleteLast = false; list<Collision*>::iterator cIterator = collisions.begin(); list<Collision*>::iterator lastIterator; while(cIterator != collisions.end()) { if(deleteLast == true) { collisions.erase(lastIterator); } deleteLast = false; Collision* check = (*cIterator); if(check->contains(co1) || check->contains(co2)) { CollidableObject* checkStatic = check->getCO2(); if(checkStatic->isStaticObject()) { coStackCounter ++; coStack[coStackCounter] = checkStatic; } collisionStackCounter ++; collisionStack[collisionStackCounter] = check; lastIterator = cIterator; deleteLast = true; } else { //check->calculateTimes(); } cIterator++; } if(deleteLast == true) { collisions.erase(lastIterator); } collideTestWithTiles(co1, tL, &collisions); collideTestWithSprites(game, co1, &collisions); if(co2->isStaticObject() == false) { collideTestWithTiles(co2, tL, &collisions); collideTestWithSprites(game, co2, &collisions); } collisions.sort(compare_collisionTime); timer += (colTime-timer); }//end if if(co2->isStaticObject() == true) { coStackCounter ++; coStack[coStackCounter] = co2; } collisionStackCounter ++; collisionStack[collisionStackCounter] = currentCollision; } if(timer < 1) { gsm->updateViewport(game, 1-timer); pp = player->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(1-timer)),pp->getY() + (pp->getVelocityY()*(1-timer))); //pp->setVelocity(0.0f, pp->getVelocityY()); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(1-timer)), pp->getY() + (pp->getVelocityY()*(1-timer))); botIterator++; } gsm->updateViewport(game, 1-timer); } pp = player->getPhysicalProperties(); if(pp->getX() < 0) { pp->setX(0); } if(pp->getY() < 0) { pp->setY(0); } //pp->setVelocity(0.0f, pp->getVelocityY()); /*pp->setPosition(pp->getX() + pp->getVelocityX(), pp->getY() + pp->getVelocityY()); // FOR NOW THE PLAYER IS DIRECTLY CONTROLLED BY THE KEYBOARD, // SO WE'LL NEED TO TURN OFF ANY VELOCITY APPLIED BY INPUT // SO THE NEXT FRAME IT DOESN'T GET ADDED pp->setVelocity(0.0f, pp->getVelocityY()); // AND NOW MOVE ALL THE BOTS list<Bot*>::iterator botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + pp->getVelocityX(), pp->getY() + pp->getVelocityY()); botIterator++; }*/ }
/* update - This method should be called once per frame. It goes through all of the sprites, including the player, and calls their update method such that they may update themselves. */ void SpriteManager::update(Game *game) { // UPDATE THE PLAYER SPRITE player.updateSprite(); player.getPhysicalProperties()->setX(player.body->GetPosition().x / 0.01f); player.getPhysicalProperties()->setY(player.body->GetPosition().y / 0.01f); if (game->getGSM()->gravN) { //Remake the proper fixture b2PolygonShape box = player.getBox(); b2FixtureDef fixtureDef = player.getFixDef(); box.SetAsBox(player.getBoundingVolume()->getWidth() / 2 * 0.01f, player.getBoundingVolume()->getHeight() / 2 * 0.01f); fixtureDef.shape = &box; fixtureDef.density = 15.0f; fixtureDef.friction = 1.0f; player.body->CreateFixture(&fixtureDef); game->getGSM()->gravN = false; } if (game->getGSM()->gravR) { //Remake the proper fixture b2PolygonShape box = player.getBox(); b2FixtureDef fixtureDef = player.getFixDef(); box.SetAsBox(player.getBoundingVolume()->getHeight() / 2 * 0.01f, player.getBoundingVolume()->getWidth() / 2 * 0.01f); fixtureDef.shape = &box; fixtureDef.density = 15.0f; fixtureDef.friction = 1.0f; player.body->CreateFixture(&fixtureDef); game->getGSM()->gravR = false; } if (game->getGSM()->gravU) { //Remake the proper fixture b2PolygonShape box = player.getBox(); b2FixtureDef fixtureDef = player.getFixDef(); box.SetAsBox(player.getBoundingVolume()->getWidth() / 2 * 0.01f, player.getBoundingVolume()->getHeight() / 2 * 0.01f); fixtureDef.shape = &box; fixtureDef.density = 15.0f; fixtureDef.friction = 1.0f; player.body->CreateFixture(&fixtureDef); game->getGSM()->gravU = false; } if (game->getGSM()->gravL) { //Remake the proper fixture b2PolygonShape box = player.getBox(); b2FixtureDef fixtureDef = player.getFixDef(); box.SetAsBox(player.getBoundingVolume()->getHeight() / 2 * 0.01f, player.getBoundingVolume()->getWidth() / 2 * 0.01f); fixtureDef.shape = &box; fixtureDef.density = 15.0f; fixtureDef.friction = 1.0f; player.body->CreateFixture(&fixtureDef); game->getGSM()->gravL = false; } // NOW ADD THE REST OF THE SPRITES list<AnimatedSprite*>::iterator blockIterator; blockIterator = blocks.begin(); while (blockIterator != blocks.end()) { AnimatedSprite *block = (*blockIterator); block->getPhysicalProperties()->setX(block->body->GetPosition().x / 0.01f + 16.0f); block->getPhysicalProperties()->setY(block->body->GetPosition().y / 0.01f + 16.0f); blockIterator++; } // NOW UPDATE THE REST OF THE SPRITES list<AnimatedSprite*>::iterator botIterator; botIterator = enemies.begin(); while (botIterator != enemies.end()) { AnimatedSprite *bot = (*botIterator); bot->updateSprite(); bot->getPhysicalProperties()->setX(bot->body->GetPosition().x / 0.01f); bot->getPhysicalProperties()->setY(bot->body->GetPosition().y / 0.01f); bot->body->SetGravityScale(0); if (bot->body->GetUserData() == L"walker" || bot->body->GetUserData() == L"uwalker") { bot->body->SetTransform(b2Vec2(bot->body->GetPosition().x + bot->deltaMove, bot->body->GetPosition().y), 0.0f); if ((bot->body->GetPosition().x + bot->deltaMove) / 0.01f > bot->originalx + 50.0f && bot->deltaMove > 0) bot->deltaMove *= -1; else if ((bot->body->GetPosition().x + bot->deltaMove) / 0.01f < bot->originalx - 50.0f && bot->deltaMove < 0) bot->deltaMove *= -1; } else if (bot->body->GetUserData() == L"rwalker" || bot->body->GetUserData() == L"lwalker") { bot->body->SetTransform(b2Vec2(bot->body->GetPosition().x, bot->body->GetPosition().y + bot->deltaMove), 0.0f); if ((bot->body->GetPosition().y + bot->deltaMove) / 0.01f > bot->originaly + 50.0f) bot->deltaMove *= -1; else if ((bot->body->GetPosition().y + bot->deltaMove) / 0.01f < bot->originaly - 50.0f) bot->deltaMove *= -1; } if (player.dead) { //bot->deltaMove = 0; } botIterator++; } list<AnimatedSprite*>::iterator exitIt; exitIt = exits.begin(); while (exitIt != exits.end()) { AnimatedSprite *exit = (*exitIt); exit->updateSprite(); exitIt++; } if (player.dead) { player.timeSinceDeath++; } if (player.timeSinceDeath == 90) { player.timeSinceDeath = 0; game->getGSM()->levelResetTrigger = true; } }
/* handleKeyEvent - this method handles all keyboard interactions. Note that every frame this method gets called and it can respond to key interactions in any custom way. Ask the GameInput class for key states since the last frame, which can allow us to respond to key presses, including when keys are held down for multiple frames. */ void BugginOutKeyEventHandler::handleKeyEvents(Game *game) { // WE CAN QUERY INPUT TO SEE WHAT WAS PRESSED GameInput *input = game->getInput(); // LET'S GET THE PLAYER'S PHYSICAL PROPERTIES, IN CASE WE WANT TO CHANGE THEM GameStateManager *gsm = game->getGSM(); AnimatedSprite *player = gsm->getSpriteManager()->getPlayer(); list<AnimatedSprite*>::iterator blocks; PhysicalProperties *pp = player->getPhysicalProperties(); Viewport *viewport = game->getGUI()->getViewport(); // IF THE GAME IS IN PROGRESS if (gsm->isGameInProgress()) { Physics *phy = game->getGSM()->getPhysics(); // WASD KEY PRESSES WILL CONTROL THE PLAYER // SO WE'LL UPDATE THE PLAYER VELOCITY WHEN THESE KEYS ARE // PRESSED, THAT WAY PHYSICS CAN CORRECT AS NEEDED float vX = pp->getVelocityX(); float vY = pp->getVelocityY(); // YOU MIGHT WANT TO UNCOMMENT THIS FOR SOME TESTING, b2Body *body = player->body; if (game->getGSM()->levelSwitchTrigger == true) { game->quitGame(); if (game->getCurrentLevelFileName() == W_LEVEL_1_NAME) game->setCurrentLevelFileName(W_LEVEL_2_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_2_NAME) game->setCurrentLevelFileName(W_LEVEL_3_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_3_NAME) game->setCurrentLevelFileName(W_LEVEL_4_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_4_NAME) game->setCurrentLevelFileName(W_LEVEL_5_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_5_NAME) game->setCurrentLevelFileName(W_LEVEL_6_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_6_NAME) game->setCurrentLevelFileName(W_LEVEL_7_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_7_NAME) game->setCurrentLevelFileName(W_LEVEL_8_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_8_NAME) game->setCurrentLevelFileName(W_LEVEL_9_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_9_NAME) game->setCurrentLevelFileName(W_LEVEL_10_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_10_NAME) game->setCurrentLevelFileName(W_LEVEL_1_NAME); game->startGame(); game->getGSM()->levelSwitchTrigger = false; } if (game->getGSM()->levelResetTrigger == true) { game->quitGame(); if (game->getCurrentLevelFileName() == W_LEVEL_1_NAME) game->setCurrentLevelFileName(W_LEVEL_1_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_2_NAME) game->setCurrentLevelFileName(W_LEVEL_2_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_3_NAME) game->setCurrentLevelFileName(W_LEVEL_3_NAME); else if (game->getCurrentLevelFileName() == W_LEVEL_4_NAME) game->setCurrentLevelFileName(W_LEVEL_4_NAME); game->startGame(); game->getGSM()->levelResetTrigger = false; } if (input->isKeyDown('1')){ game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_1_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_1_NAME); /*game->setCurrentLevelFileName(W_LEVEL_1_NAME); game->startGame();*/ } else if (input->isKeyDown('2')){ game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_2_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_2_NAME); } else if (input->isKeyDown('3')){ game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_3_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_3_NAME); } else if (input->isKeyDown('4')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_4_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('5')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_5_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('6')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_6_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('7')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_7_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('8')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_8_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('9')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_9_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown('0')) { game->quitGame(); game->setCurrentLevelFileName(W_LEVEL_10_NAME); game->startGame(); //game->getGSM()->unloadCurrentLevel(); //game->getGSM()->loadLevel(game, W_LEVEL_1_DIR + W_LEVEL_4_NAME); } else if (input->isKeyDown(A_KEY)) { if (player->dead == false) { pp->facing = 1; if ((phy->CurrentGrav == phy->Normal_Grav || phy->CurrentGrav == phy->Reverse_Grav)) { if (player->onGround()) body->ApplyForce(b2Vec2(body->GetMass() * -0.1 / (1 / 60.0), 0), body->GetWorldCenter(), true); else body->ApplyForce(b2Vec2(body->GetMass() * -0.1 / (1 / 60.0), 0), body->GetWorldCenter(), true); } if ((phy->CurrentGrav == phy->Left_Grav || phy->CurrentGrav == phy->Right_Grav)) { if (player->onGround()) body->ApplyForce(b2Vec2(0, body->GetMass() * -0.1 / (1 / 60.0)), body->GetWorldCenter(), true); else body->ApplyForce(b2Vec2(0, body->GetMass() * -0.1 / (1 / 60.0)), body->GetWorldCenter(), true); } if (player->onGround()) { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(LWALK); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(ULWALK); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RLWALK); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LLWALK); } else { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(LFALL); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(ULFALL); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RLFALL); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LLFALL); } } } else if (input->isKeyDown(D_KEY)) { if (player->dead == false) { pp->facing = 0; if ((phy->CurrentGrav == phy->Normal_Grav || phy->CurrentGrav == phy->Reverse_Grav)) { if (player->onGround()) body->ApplyForce(b2Vec2(body->GetMass() * 0.1 / (1 / 60.0), 0), body->GetWorldCenter(), true); else body->ApplyForce(b2Vec2(body->GetMass() * 0.1 / (1 / 60.0), 0), body->GetWorldCenter(), true); } if ((phy->CurrentGrav == phy->Left_Grav || phy->CurrentGrav == phy->Right_Grav)) { if (player->onGround()) body->ApplyForce(b2Vec2(0, body->GetMass() * 0.1 / (1 / 60.0)), body->GetWorldCenter(), true); else body->ApplyForce(b2Vec2(0, body->GetMass() * 0.1 / (1 / 60.0)), body->GetWorldCenter(), true); } if (player->onGround()) { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(RWALK); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(URWALK); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RRWALK); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LRWALK); } else { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(RFALL); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(URFALL); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RRFALL); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LRFALL); } } } else if (input->isKeyDownForFirstTime(G_KEY)) { /*((BugginOutDataLoader*)game->getDataLoader())->remotePlaySound(((BugginOutDataLoader*)game->getDataLoader())->gravSound); b2World *world = game->getGSM()->getPhysics()->getBoxWorld(); if (phy->CurrentGrav == phy->Normal_Grav) { player->body->SetTransform(player->body->GetPosition(), 0.0f); blocks = gsm->getSpriteManager()->getBlocksIterator(); while (blocks != gsm->getSpriteManager()->getEndOfBlocksIterator()) { (*blocks)->body->SetGravityScale(1); blocks++; } world->SetGravity(phy->Reverse_Grav); phy->CurrentGrav = phy->Reverse_Grav; } else if (phy->CurrentGrav == phy->Reverse_Grav) { player->body->SetTransform(player->body->GetPosition(), 270.0f); blocks = gsm->getSpriteManager()->getBlocksIterator(); while (blocks != gsm->getSpriteManager()->getEndOfBlocksIterator()) { (*blocks)->body->SetGravityScale(0); blocks++; } world->SetGravity(phy->Left_Grav); phy->CurrentGrav = phy->Left_Grav; } else if (phy->CurrentGrav == phy->Left_Grav) { player->body->SetTransform(player->body->GetPosition(), 270.0f); blocks = gsm->getSpriteManager()->getBlocksIterator(); while (blocks != gsm->getSpriteManager()->getEndOfBlocksIterator()) { (*blocks)->body->SetGravityScale(0); blocks++; } world->SetGravity(phy->Right_Grav); phy->CurrentGrav = phy->Right_Grav; } else if (phy->CurrentGrav == phy->Right_Grav) { player->body->SetTransform(player->body->GetPosition(), 0.0f); blocks = gsm->getSpriteManager()->getBlocksIterator(); while (blocks != gsm->getSpriteManager()->getEndOfBlocksIterator()) { (*blocks)->body->SetGravityScale(1); blocks++; } world->SetGravity(phy->Normal_Grav); phy->CurrentGrav = phy->Normal_Grav; } player->setGrounded(false);*/ } else if (player->onGround()) { if (player->dead == false) { if (pp->facing == 0) { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(RIDLE); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(URIDLE); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RRIDLE); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LRIDLE); } else { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(LIDLE); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(ULIDLE); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RLIDLE); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LLIDLE); } } else { if (player->getCurrentState() != L"LDEATH") { if (pp->facing == 0){ if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(RFALL); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(URFALL); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RRFALL); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LRFALL); } else{ if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(LFALL); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(ULFALL); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RLFALL); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LLFALL); } } } } if (input->isKeyDownForFirstTime(SPACE_KEY) && player->onGround() && player->dead == false) { ((BugginOutDataLoader*)game->getDataLoader())->remotePlaySound(((BugginOutDataLoader*)game->getDataLoader())->jumpSound); //float mass = body->GetMass(); float impulse = 3.0f; if (phy->CurrentGrav == phy->Normal_Grav) { body->SetLinearVelocity(b2Vec2(body->GetLinearVelocity().x, -impulse)); } if (phy->CurrentGrav == phy->Reverse_Grav) { body->SetLinearVelocity(b2Vec2(body->GetLinearVelocity().x, impulse)); } if (phy->CurrentGrav == phy->Right_Grav) { body->SetLinearVelocity(b2Vec2(-impulse, body->GetLinearVelocity().y)); } if (phy->CurrentGrav == phy->Left_Grav) { body->SetLinearVelocity(b2Vec2(impulse, body->GetLinearVelocity().y)); } if (pp->facing == 0) { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(RJUMP); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(URJUMP); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RRJUMP); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LRJUMP); } else { if (phy->CurrentGrav == phy->Normal_Grav) player->setCurrentState(LJUMP); else if (phy->CurrentGrav == phy->Reverse_Grav) player->setCurrentState(ULJUMP); else if (phy->CurrentGrav == phy->Right_Grav) player->setCurrentState(RLJUMP); else if (phy->CurrentGrav == phy->Left_Grav) player->setCurrentState(LLJUMP); } player->setGrounded(false); } if (input->isKeyDownForFirstTime(P_KEY)) { game->getGSM()->getPhysics()->activated = !(game->getGSM()->getPhysics()->activated); } if (input->isKeyDownForFirstTime(R_KEY)) { game->getGSM()->levelResetTrigger = true; } // NOW SET THE ACTUAL PLAYER VELOCITY pp->setVelocity(vX, vY); // 0X43 is HEX FOR THE 'C' VIRTUAL KEY // THIS CHANGES THE CURSOR IMAGE if ((input->isKeyDownForFirstTime(C_KEY)) && input->isKeyDown(VK_SHIFT)) { Cursor *cursor = game->getGUI()->getCursor(); unsigned int id = cursor->getActiveCursorID(); id++; if (id == cursor->getNumCursorIDs()) id = 0; cursor->setActiveCursorID(id); } // LET'S MESS WITH THE TARGET FRAME RATE IF THE USER PRESSES THE HOME OR END KEYS WindowsTimer *timer = (WindowsTimer*)game->getTimer(); int fps = timer->getTargetFPS(); // THIS SPEEDS UP OUR GAME LOOP AND THUS THE GAME, NOTE THAT WE COULD ALTERNATIVELY SCALE // DOWN THE GAME LOGIC (LIKE ALL VELOCITIES) AS WE SPEED UP THE GAME. THAT COULD PROVIDE // A BETTER PLAYER EXPERIENCE if (input->isKeyDown(VK_HOME) && (fps < MAX_FPS)) timer->setTargetFPS(fps + FPS_INC); // THIS SLOWS DOWN OUR GAME LOOP, BUT WILL NOT GO BELOW 5 FRAMES PER SECOND else if (input->isKeyDown(VK_END) && (fps > MIN_FPS)) timer->setTargetFPS(fps - FPS_INC); } }