World::World(GameState& gameState , const Controller& controller , sf::RenderWindow& window , std::string username , int worldDim , int numSheep , sf::Time levelTime) : mWorldX(worldDim) , mWorldY(worldDim) , mExitRadius(controller.getParams().ExitRadius) , mLevelBlockSize(controller.getParams().LevelBlockSize) , mNumSheep(numSheep) , mWaypointRadius(controller.getParams().WaypointRadius) , mScrollSpeed(controller.getParams().ScrollSpeed) , mGameState(gameState) , mWindow(window) , mWorldView(mWindow.getDefaultView()) , mWorldBounds(sf::Vector2i(0, 0), sf::Vector2i(mWorldX, mWorldY)) , mFocusPoint(mWorldBounds.width / 2.f, mWorldBounds.height / 2.f) , mSheepHerded(0) , mTimeLeft(levelTime) , mTimeTaken(sf::Time::Zero) , mLevel(mLevelBlockSize , controller.getParams().ExitWidth , mWorldBounds) , mHUD(this , controller.getFont(Controller::Fonts::Sansation) , getViewBounds() , username) , mExitPos(mWorldBounds.width / 2.f, 0.f) , mDog(nullptr) { initialiseStatesAndStats(); buildScene(controller); }
sf::FloatRect World::getBattlefieldBounds() const { // Return view bounds + some area at top, where enemies spawn sf::FloatRect bounds = getViewBounds(); bounds.top -= 100.f; bounds.height += 100.f; return bounds; }
sf::FloatRect World::getBattleField() const { sf::FloatRect bounds = getViewBounds(); bounds.top -= 100.f; bounds.height += 100.f; return bounds; }
sf::FloatRect World::getBattlefieldBounds() const { sf::FloatRect bounds = getViewBounds(); bounds.top -= 1000.f; bounds.height += 1000.f; bounds.left -= 500.f; bounds.width += 1000.f; return bounds; }
void World::display() { adjustView(); mWorldView.setCenter(mFocusPoint); mWindow.setView(mWorldView); mWindow.draw(mSceneGraph); mHUD.setHUDPosition(getViewBounds()); mWindow.draw(mHUD); }
void World::adaptPlayerPosition() { // Keep player's position inside the screen bounds, at least borderDistance units from the border sf::FloatRect viewBounds = getViewBounds(); const float borderDistance = 40.f; sf::Vector2f position = mPlayerAircraft->getPosition(); position.x = std::max(position.x, viewBounds.left + borderDistance); position.x = std::min(position.x, viewBounds.left + viewBounds.width - borderDistance); position.y = std::max(position.y, viewBounds.top + borderDistance); position.y = std::min(position.y, viewBounds.top + viewBounds.height - borderDistance); mPlayerAircraft->setPosition(position); }
void World::handleInput() { sf::Event event; while(mWindow.pollEvent(event)) { if(event.type == sf::Event::Closed) mWindow.close(); else if(event.type == sf::Event::KeyReleased) { if(event.key.code == sf::Keyboard::Escape) mGameState.pause(); } else if(event.type == sf::Event::MouseButtonPressed) { sf::Vector2i mousePos; sf::FloatRect vBounds = getViewBounds(); mousePos.x = sf::Mouse::getPosition(mWindow).x + vBounds.left; mousePos.y = sf::Mouse::getPosition(mWindow).y + vBounds.top; sf::Vector2f mousePosF(mousePos.x, mousePos.y); if(event.mouseButton.button == sf::Mouse::Left) { mousePosF.x = std::min(mousePosF.x, static_cast<float>(mWorldBounds.width - (mLevelBlockSize + mWaypointRadius))); mousePosF.x = std::max(mousePosF.x, static_cast<float>(mLevelBlockSize + mWaypointRadius)); mousePosF.y = std::min(mousePosF.y, static_cast<float>(mWorldBounds.height - (mLevelBlockSize + mWaypointRadius))); mousePosF.y = std::max(mousePosF.y, static_cast<float>(mLevelBlockSize + mWaypointRadius)); if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) { if(mDog) mDog->addToPath(mousePosF); } else { if(mDog) mDog->startNewPath(mousePosF); } } } } handleRealTimeInput(); }
void UIViewport::maximizeVisibility(const Pnt2f& TopLeft, const Pnt2f& BottomRight) { //Scroll as little as possible until as much as can be is visible Pnt2f ViewTopLeft, ViewBottomRight; getViewBounds(ViewTopLeft,ViewBottomRight); Pnt2f NewViewPosition(getViewPosition()); //Vertical if(ViewTopLeft.y() > TopLeft.y()) { //Scroll up NewViewPosition[1] = TopLeft.y(); } else if(ViewBottomRight.y() < BottomRight.y()) { Pnt2f InsetsTopLeft, InsetsBottomRight; getInsideInsetsBounds(InsetsTopLeft, InsetsBottomRight); //Scroll down NewViewPosition[1] = BottomRight.y() - (InsetsBottomRight - InsetsTopLeft).y(); } //Horizontal if(ViewTopLeft.x() > TopLeft.x()) { //Scroll left NewViewPosition[0] = TopLeft.x(); } else if(ViewBottomRight.x() < BottomRight.x()) { Pnt2f InsetsTopLeft, InsetsBottomRight; getInsideInsetsBounds(InsetsTopLeft, InsetsBottomRight); //Scroll right NewViewPosition[0] = BottomRight.x() - (InsetsBottomRight - InsetsTopLeft).x(); } setViewPosition(NewViewPosition); }
void World::adaptPlayerPosition() { sf::FloatRect viewBounds = getViewBounds(); const float borderDistance = 40.0f; for ( Aircraft* aircraft : mPlayerAircrafts ) { sf::Vector2f position = aircraft->getPosition(); position.x = std::max( position.x, viewBounds.left + borderDistance ); position.x = std::min( position.x, viewBounds.left + viewBounds.width - borderDistance ); position.y = std::max( position.y, viewBounds.top + borderDistance ); position.y = std::min( position.y, viewBounds.top + viewBounds.height - borderDistance ); aircraft->setPosition( position ); } }