#define _USE_MATH_DEFINES #include #include "Game.h" #include "AIController.h" #include "KeyboardController.h" Game::Game(int width, int height): m_width(width), m_height(height) { initField(width, height); m_vpPlayers.clear(); } void Game::setSize(int width, int height) { m_width =width; m_height =height; initField(width, height); } Game::~Game(void) { while(m_vpPlayers.size() > 0) { delete m_vpPlayers.back(); m_vpPlayers.pop_back(); } } void Game::initField(int width, int height) { m_Field.init(width, height); } Player* Game::addPlayer() { PlayerNumber numPlayers = m_vpPlayers.size(); m_vpPlayers.push_back(new Player()); initPlayer(m_vpPlayers.back(), numPlayers); return m_vpPlayers.back(); } void Game::initPlayer(Player* pPlayer, PlayerNumber playerID) { pPlayer->setNumber(playerID); pPlayer->revive(); if (pPlayer->getController()) { pPlayer->getController()->reset(); } } void Game::start() { placePlayers(); m_gameRunning = false; } void Game::placePlayers() { PlayerNumber num = m_vpPlayers.size(); for(PlayerNumber i = 0; i= abs(y-m_height/2)) { if (x-m_width/2 >= 0) { velocity = VelocityVector(-1,0); } else { velocity = VelocityVector(1,0); } } else { if (y-m_height/2 >= 0) { velocity = VelocityVector(0,-1); } else { velocity = VelocityVector(0,1); } } m_vpPlayers[i]->setCoordinates(Coordinates(x,y)); m_vpPlayers[i]->setVelocity(velocity); m_Field.getCell(Coordinates(x,y)).setState(PLAYER); m_Field.getCell(Coordinates(x,y)).setPlayer(m_vpPlayers[i]); } } void Game::restart() { initField(m_width, m_height); PlayerNumber num = m_vpPlayers.size(); for(PlayerNumber i = 0; i::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) { if ((*it)->isAlive()) { if ((*it)->getController()) { (*it)->getController()->think(); } } } // move everyone and update cells std::vector path; std::vector< std::vector > secondPassPath; for(std::vector::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) { if ((*it)->isAlive()) { path = (*it)->move(); secondPassPath.push_back(path); std::vector::iterator coord = path.begin(); if (m_Field.getCell(*coord).getState() == PLAYER) // someone else could have gone BOOM m_Field.getCell(*coord).setState(PLAYER_WALL); ++coord; for( coord; coord != path.end(); ++coord) { try { Cell& cell = m_Field.getCell(*coord); if (!(cell.getState() == EMPTY_CELL || (cell.getState() == PLAYER && (cell.getPlayer()->getNumber() == (*it)->getNumber())))) { killPlayer(*it); cell.setState(BOOM); if (cell.getState() == PLAYER_JUST_MOVED) { killPlayer((cell.getPlayer())); } } else { cell.setState(PLAYER_JUST_MOVED); cell.setPlayer(*it); } } catch (std::out_of_range e) { killPlayer(*it); m_Field.getCell(*(coord-1)).setState(BOOM); } } } } // second pass to cleanup PLAYER_JUST_MOVED cells bool boomed = false; for(std::vector< std::vector >::iterator it = secondPassPath.begin(); it != secondPassPath.end(); ++it) { boomed = false; try { for(std::vector::iterator coord = it->begin(); coord != it->end(); ++coord) { Cell& cell = m_Field.getCell(*coord); if (cell.getState() == BOOM) boomed = true; else if (boomed) cell.setState(EMPTY_CELL); else if (coord == it->end() - 1) cell.setState(PLAYER); else cell.setState(PLAYER_WALL); } } catch (std::exception out_of_bounds) {} // don't do anything } } } void Game::killPlayer(Player *player) { player->kill(); PlayerNumber alivePlayers = 0; for(std::vector::iterator it = m_vpPlayers.begin(); it != m_vpPlayers.end(); ++it) { if ((*it)->isAlive()) { alivePlayers ++; } } if (alivePlayers <= 1) { pauseGame(); } }