////////////////////////////////////////////////////////// //======================================================// // Update // //======================================================// // This will update the game every frame. // ////////////////////////////////////////////////////////// void Game::Update() { // Set up Box2D variables. float time_step = 1.0f / (float)kFrameRate; int32 velocity_iterations = 8; int32 position_iterations = 6; // Setting up delta time. dt_ = clock_.restart(); while (window_->pollEvent(event_)) { // If the user wants to close the window. if (event_.type == sf::Event::Closed) { // Close the window. window_->close(); } } // Keep updating the physics world. // Updates the physics simulation based on iterations. world_->Step(time_step, velocity_iterations, position_iterations); // Handling any new state changes and input. HandleStates(); // Update everything else here. // Updating the current state. current_state_->Update(dt_.asSeconds()); }
void CPlayer::Update(CMap* Map) { int TilePos = 0; m_VelX = 5; m_VelY = 30; m_bScoreChanged = false; m_bLivesChanged = false; m_bCoinsChanged = false; m_bTouchesGround = false; //x axis first (--) if(m_bRight == true) { if (m_State != "JUMPING") { m_State = "WALKING_RIGHT"; } else { m_State = "JUMPING"; } if(Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 1) { m_PosX = (TilePos * Tile.GetSize()) - m_Width; Mix_PlayChannel(-1,cBlockhit,0); } else if (Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 2) { m_PosX = (TilePos * Tile.GetSize()) - m_Width; Mix_PlayChannel(-1,cBlockhit,0); } else if (Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 3) //Lava { m_VelX = 0; m_bDead = true; m_State = "DYING"; m_Lives -= 1; m_bLivesChanged = true; } else if (Collision_Ver(m_PosX + m_Width + m_VelX, m_PosY, TilePos, Map) == 5) //Coins { m_PosX += m_VelX; m_Score += 100; m_bScoreChanged = true; m_Coins += 1; m_bCoinsChanged = true; Mix_PlayChannel(-1,cCoinCollected,0); Map->SetTileAtPos( (int) (m_PosX / 32), (int) (m_PosY / 32),0,0); } else { m_PosX += m_VelX; } } else if(m_bLeft) { if (m_State != "JUMPING") { m_State = "WALKING_LEFT"; } else { m_State = "JUMPING"; } if(Collision_Ver(m_PosX - m_VelX, m_PosY, TilePos, Map) == 1) { m_PosX = (TilePos + 1)* Tile.GetSize(); Mix_PlayChannel(-1,cBlockhit,0); } else if (Collision_Ver(m_PosX - m_VelX, m_PosY, TilePos, Map) == 2) { m_PosX = (TilePos + 1)* Tile.GetSize(); } else if (Collision_Ver(m_PosX - m_VelX, m_PosY, TilePos, Map) == 3) { m_VelX = 0; m_State = "DYING"; m_Lives -= 1; m_bLivesChanged = true; } else if (Collision_Ver(m_PosX - m_VelX, m_PosY, TilePos, Map) == 5) //Coins { m_PosX -= m_VelX; m_Score += 100; m_bScoreChanged = true; m_Coins += 1; m_bCoinsChanged = true; Mix_PlayChannel(-1,cCoinCollected,0); Map->SetTileAtPos( (int) ((m_PosX - m_VelX) / 32), (int) (m_PosY / 32),0,0); } else if (Collision_Ver(m_PosX, m_PosY, TilePos, Map) == 6) // { m_PosX = (TilePos + 1)* Tile.GetSize(); } else { m_PosX -= m_VelX; } } //then y axis (|) if(m_bIsJumping == true) //up { m_State = "JUMPING"; Mix_PlayChannel(-1,cJump,0); if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 1) { m_PosY = (TilePos + 1)* Tile.GetSize(); m_VelY = 0; m_bLockJump = true; m_bApexReached = true; if (m_bApexReached == true) { m_PosY += m_Gravity; } Mix_PlayChannel(-1,cBlockhit,0); } else if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 2) { m_PosY -= m_VelY; } else if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 3) { m_VelX = 0; m_PosY += m_Gravity; m_State = "DYING"; m_Lives -= 1; m_bLivesChanged = true; } else if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 5) { m_PosY -= m_VelY; m_sScore = ""; m_Score += 100; m_bScoreChanged = true; Mix_PlayChannel(-1,cCoinCollected,0); m_sCoins = ""; m_Coins += 1; m_bCoinsChanged = true; Map->SetTileAtPos( (int) (m_PosX / 32), (int) (m_PosY / 32),0,0); } else if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 6) { m_PosY = (TilePos + 1)* Tile.GetSize(); m_VelY = 0; Mix_PlayChannel(-1,cBlockhit,0); } else if(Collision_Hor(m_PosX, (m_PosY - m_VelY), TilePos, Map) == 7) { m_PosY = (TilePos + 1)* Tile.GetSize(); m_VelY = 0; } else { if (m_bLockJump == false && m_bApexReached == false) { if (m_bOffsetIsSet == false) { m_JumpOffset = m_PosY; m_JumpApex = m_JumpOffset - m_JumpingHeight; m_bOffsetIsSet = true; } if (m_PosY > m_JumpApex) { m_PosY -= m_Gravity; //Move up at same speed like we move down } else if (m_PosY <= m_JumpApex) { m_bApexReached = true; m_bLockJump = true; } } if (m_bApexReached == true) { m_PosY += m_Gravity; } } } else { if(Collision_Hor(m_PosX, (m_PosY + m_Gravity + m_Height), TilePos, Map) == 1) { m_PosY = (TilePos * Tile.GetSize()) - m_Height - m_Gravity; m_VelY = 0; m_bTouchesGround = true; m_bIsJumping = false; m_bLockJump = false; m_bApexReached = false; } else if(Collision_Hor(m_PosX, (m_PosY + m_Gravity + m_Height), TilePos, Map) == 2) { m_PosY = (TilePos * Tile.GetSize()) - m_Height - m_Gravity; m_VelY = 0; m_bTouchesGround = true; m_bIsJumping = false; m_bLockJump = false; m_bApexReached = false; } else if(Collision_Hor(m_PosX, (m_PosY + m_Gravity + m_Height), TilePos, Map) == 3) { m_State = "DYING"; m_Lives -= 1; m_bLockJump = true; m_bApexReached = false; } else if(Collision_Hor(m_PosX, (m_PosY + m_Gravity + m_Height), TilePos, Map) == 5) { m_VelY = m_Gravity; m_Score += 100; m_Coins += 1; Mix_PlayChannel(-1,cCoinCollected,0); Map->SetTileAtPos( (int) (m_PosX / 32), (int) (m_PosY / 32),0,0); m_bLockJump = true; } else if(Collision_Hor(m_PosX, (m_PosY + m_Gravity + m_Height), TilePos, Map) == 6) { m_PosY += m_VelX; } else { m_VelY = m_Gravity; if(m_VelY >= Tile.GetSize()) { m_VelY = Tile.GetSize(); } m_bLockJump = true; } std::cout << "m_bTouchesGround: " << m_bTouchesGround << "\n"; if (m_bTouchesGround == true) { if (m_bRight == true) { m_State = "WALKING_RIGHT"; } else if (m_bLeft == true) { m_State = "WALKING_LEFT"; } } else { m_State = "JUMPING"; } m_PosY += m_VelX; } UpdateScore(); UpdateLives(); UpdateCoins(); HandleStates(); }