#include "game.h" #include "general.h" #include "tile.h" #include "map.h" #include "GFXframework.h" #include #include Game::Game() { Init(); } Game::~Game() { delete m_LevelMap; GFXframework::ResetInstance(); } void Game::Init() { m_Finished = false; m_numOfBombs = SMALL_MAP_BOMBS; m_SizeX = SMALL_MAP_SIZE_X; m_SizeY = SMALL_MAP_SIZE_Y; int window_width = m_SizeX * TILE_SIZE_X; int window_height = m_SizeY * TILE_SIZE_Y + TILE_SIZE_Y/2; GFXframework::GetInstance()->InitWindow(window_width, window_height, (char *)"Don't Explode!"); GFXframework::GetInstance()->LoadSprites(); // bottom bar, part of GUI m_GUITexture.loadFromFile("bar.png"); m_GUISprite =sf::Sprite(m_GUITexture); // create basic map m_LevelMap = new Map(m_SizeX, m_SizeY, m_numOfBombs); m_FirstClick = true; } void Game::Shutdown(int returnCode) { (void)returnCode; } void Game::MainLoop() { while (GFXframework::GetInstance()->GetWindow()->isOpen()) { sf::Event event; // main events processing while (GFXframework::GetInstance()->GetWindow()->pollEvent(event)) { if (event.type == sf::Event::Closed) GFXframework::GetInstance()->GetWindow()->close(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) { m_FirstClick = true; m_LevelMap->Generate(); m_Finished = false; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape )) { GFXframework::GetInstance()->GetWindow()->close(); } // if the game isn't finished yet, allow all mechanics to work if ( !m_Finished) { if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { if ( m_LeftMouseDown != true) { m_LeftMouseDown = true; sf::Vector2i localPosition = sf::Mouse::getPosition(*(GFXframework::GetInstance()->GetWindow())); if (m_FirstClick) { if (m_LevelMap->GenerateFromClick(localPosition.x, localPosition.y)) { m_FirstClick = false; HandleLeftMouse(localPosition); } } else { HandleLeftMouse(localPosition); } } } else { // prevent repeating signals of mousedown (SFML only has MouseDown, so we have to work around it) m_LeftMouseDown = false; } if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && !m_FirstClick) { if ( m_RightMouseDown != true) { m_RightMouseDown = true; sf::Vector2i localPosition = sf::Mouse::getPosition(*(GFXframework::GetInstance()->GetWindow())); HandleRightMouse(localPosition); } } else { // prevent repeating signals of mousedown (SFML only has MouseDown, so we have to work around it) m_RightMouseDown = false; } if (m_LevelMap->IsMapDone()) { m_Finished = true; if (m_LevelMap->IsMapWon()) { m_LevelMap->WinMap(); } else { m_LevelMap->LoseMap(); } } } GFXframework::GetInstance()->GetWindow()->clear(); Render(); GFXframework::GetInstance()->GetWindow()->display(); } } bool Game::HandleLeftMouse(sf::Vector2i localPosition) { m_LevelMap->ClickAt(localPosition.x, localPosition.y); return true; } bool Game::HandleRightMouse(sf::Vector2i localPosition) { m_LevelMap->RightClickAt(localPosition.x, localPosition.y); return true; } void Game::HandleGame() { } void Game::DrawGui() { // draw bottom bar, for MORE BLING! for (int x = 0; x < m_SizeX; x++) { m_GUISprite.setPosition((float)(x * TILE_SIZE_X), (float)(m_SizeY * TILE_SIZE_Y)); GFXframework::GetInstance()->GetWindow()->draw(m_GUISprite); } } void Game::Render() { DrawGui(); m_LevelMap->Render(); }