#include "pch.h" #include "Game.h" #include "utils.h" bool Game::DrawDebug{ false }; Game::Game( const Window& window ) : m_Window{ window } , m_Level{ 1, 1 } , m_ItemManager{ } , m_Camera{ window.width, window.height } , m_Hud{ Point2f { 10, window.height - 10 }, 3 } , m_EndReached{ false } , m_Player{ } , m_CursorTexture{ "Resources/Sprites/cursor.png" } , m_MousePos{ } , m_MouseIdleLimit{ 3.f } , m_CursorFadeoutAccu{ } , m_CursorFadeoutDuration{ 0.33f } , m_CursorSize{ 48.f } , m_EnemyManager{ } , m_ParticleManager{ } { Initialize( ); } Game::~Game( ) { for (SDL_GameController* controller : m_Controllers) { if (controller != nullptr) { SDL_GameControllerClose(controller); controller = nullptr; } } Cleanup( ); } void Game::Initialize( ) { SetupControllers(); m_Player.SetPosition(m_Level.GetStartPos(m_Player.GetHitbox().width)); m_ItemManager.LoadItems(m_Level.GetInfo()); m_EnemyManager.LoadEnemies(m_Level.GetInfo()); m_Camera.SetLevelBoundaries(m_Level.GetBoundaries()); m_Camera.SetTarget(m_Player.GetHitbox()); m_Camera.SnapToTarget(); ShowInstructions( ); } void Game::SetupControllers() { for (int i{}; i < SDL_NumJoysticks(); i++) { if (i == 0) { SDL_GameControllerEventState(SDL_ENABLE); } m_Controllers.push_back(SDL_GameControllerOpen(i)); } if (m_Controllers.size() > 0 && m_Controllers[0] != nullptr) { m_Player.SetController(m_Controllers[0]); } } void Game::Cleanup( ) { } void Game::Update( float elapsedSec ) { // Update game objects //if (!m_EndReached) { m_Player.Update(elapsedSec, m_Level, m_ItemManager.GetItems(), m_Camera.GetOffset(), m_Camera.GetSmoothing()); m_ItemManager.Update(elapsedSec, m_Level); m_EnemyManager.Update(elapsedSec, m_Level, &m_Player, &m_ParticleManager); m_ParticleManager.Update(elapsedSec, m_Level); m_Level.Update(elapsedSec); // update timer m_Camera.Update(elapsedSec); // follow target std::vector playersInfo{ m_Player.GetInfo() }; m_Hud.Update(elapsedSec, playersInfo, m_Level.GetInfo()); UpdateCursor(elapsedSec); //m_EndReached = m_Level.HasReachedEnd(m_Avatar.GetShape()); } // Do collision DoCollisionTests(); } void Game::UpdateCursor(float elapsedSec) { if (m_MouseIdleAccu < m_MouseIdleLimit) { m_MouseIdleAccu += elapsedSec; } else if (m_CursorFadeoutAccu > 0) { m_CursorFadeoutAccu -= elapsedSec; if (m_CursorFadeoutAccu < 0) m_CursorFadeoutAccu = 0; } } void Game::DrawCursor() const { int cursorOpacity = int(m_CursorFadeoutAccu * (1 / m_CursorFadeoutDuration) * 100); int cursorFrame = (100 - cursorOpacity) / 10; if (cursorFrame < 10) m_CursorTexture.Draw(m_MousePos, Rectf{ m_CursorSize * cursorFrame, 0, m_CursorSize, m_CursorSize }); } void Game::Draw( ) const { ClearBackground( ); glPushMatrix(); m_Camera.Translate(); m_Level.DrawBackground( ); m_EnemyManager.Draw(); m_Player.Draw( ); m_ItemManager.Draw( ); m_ParticleManager.Draw(); m_Level.DrawForeground( ); if (DrawDebug) { m_Level.DrawVertices(); } glPopMatrix(); m_Hud.Draw(); DrawCursor(); if (m_EndReached) { utils::SetColor(Color4f{ 0, 0, 0, 0.5f }); utils::FillRect(0, 0, m_Window.width, m_Window.height); } } void Game::ProcessKeyDownEvent( const SDL_KeyboardEvent & e ) { } void Game::ProcessKeyUpEvent( const SDL_KeyboardEvent& e ) { if (e.keysym.sym == SDLK_F1) { DrawDebug = !DrawDebug; } } void Game::ProcessMouseMotionEvent( const SDL_MouseMotionEvent& e ) { m_MousePos = Point2f{ float(e.x), float(m_Window.height - e.y) - m_CursorTexture.GetHeight() }; m_MouseIdleAccu = 0; m_CursorFadeoutAccu = m_CursorFadeoutDuration; } void Game::ProcessMouseDownEvent( const SDL_MouseButtonEvent& e ) { } void Game::ProcessMouseUpEvent( const SDL_MouseButtonEvent& e ) { } void Game::ClearBackground( ) const { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT ); } void Game::ShowInstructions( ) const { std::cout << "--> Spelunky <--\n"; std::cout << "- Left arrow: Move left\n"; std::cout << "- Right arrow: Move right\n"; std::cout << "- Up arrow: Jump\n"; } void Game::DoCollisionTests( ) { }