bool GameState::handleEvent(const sf::Event& event) { // Game input handling CommandQueue& commands = mWorld.getCommandQueue(); mPlayer.handleEvent(event, commands); // Escape pressed, trigger the pause screen if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) { requestStackPush(States::Pause); } // F3 is pressed, so bring up the debug overlay else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::F3) { if (mDebugOverlay == false) { requestStackPush(States::Debug); mDebugOverlay = true; } else { requestStackPop(); mDebugOverlay = false; } } return true; }
void GameState::onNotify(std::shared_ptr<baseEvent> e){ switch(e->getType()){ case EventType::LEVEL_COMPLETE:{ if(currentLevel<(totalLevels-1)){ ++currentLevel; eventQueue = level->getEventQueue(currentLevel); gameEventDispatcher = level->getGameEventDispatcher(currentLevel); gameEventDispatcher->LevelComplete.addObserver(this); gameEventDispatcher->LevelFailed.addObserver(this); gameEventDispatcher->UpdateScore.addObserver(this); }else if(currentLevel == (totalLevels-1)){ requestStackPush(AppStates::GameSucceed); } } break; case EventType::LEVEL_FAILED:{ requestStackPush(AppStates::GameFailed); } break; case EventType::UPDATE_SCORE:{ score += std::get<0>(e->getAttributes<EventsAlias::update_score>()); textScore.setString(Utils::toString(score)); } break; default: break; } }
MenuState::MenuState(StateStack& stack, Context context) : State(stack, context) , mGUIContainer() { sf::Texture& texture = context.textures->get(Textures::TitleScreen); mBackgroundSprite.setTexture(texture); auto playButton = std::make_shared<GUI::Button>(context); playButton->setPosition(100, 300); playButton->setText("Play"); playButton->setCallback([this] () { requestStackPop(); requestStackPush(States::Game); }); auto hostPlayButton = std::make_shared<GUI::Button>(context); hostPlayButton->setPosition(100, 350); hostPlayButton->setText("Host"); hostPlayButton->setCallback([this] () { requestStackPop(); requestStackPush(States::HostGame); }); auto joinPlayButton = std::make_shared<GUI::Button>(context); joinPlayButton->setPosition(100, 400); joinPlayButton->setText("Join"); joinPlayButton->setCallback([this] () { requestStackPop(); requestStackPush(States::JoinGame); }); auto settingsButton = std::make_shared<GUI::Button>(context); settingsButton->setPosition(100, 450); settingsButton->setText("Settings"); settingsButton->setCallback([this] () { requestStackPush(States::Settings); }); auto exitButton = std::make_shared<GUI::Button>(context); exitButton->setPosition(100, 500); exitButton->setText("Exit"); exitButton->setCallback([this] () { requestStackPop(); }); mGUIContainer.pack(playButton); mGUIContainer.pack(hostPlayButton); mGUIContainer.pack(joinPlayButton); mGUIContainer.pack(settingsButton); mGUIContainer.pack(exitButton); // Play menu theme context.music->play(Music::MenuTheme); }
bool MenuState::handleEvent(const sf::Event& event){ #if defined ANDROID auto& window = *getContext().window; if(event.type == sf::Event::TouchEnded){ sf::Vector2f pos = static_cast<sf::Vector2f>(window.mapPixelToCoords(sf::Vector2i(event.touch.x, event.touch.y), window.getView())); for(const auto& text:mOptions){ if(text.getGlobalBounds().contains(pos)){ if(text.getString() == "Play"){ requestStackPop(); requestStackPush(AppStates::Game); }else if(text.getString() == "Help"){ requestStackPush(AppStates::Help); }else if(text.getString() == "Exit"){ requestStackPop(); } } } }else if(event.type == sf::Event::KeyReleased){ if(event.key.code == sf::Keyboard::Escape){ requestStackPop(); } }else if(event.type == sf::Event::LostFocus){ requestStackPop(); } #else if(event.type != sf::Event::KeyReleased){ return false; } if(event.key.code == sf::Keyboard::Return){ if(mOptionIndex == Play){ requestStackPop(); requestStackPush(AppStates::Game); }else if(mOptionIndex == Help){ requestStackPush(AppStates::Help); }else if(mOptionIndex == Exit){ requestStackPop(); } }else if(event.key.code == sf::Keyboard::Up){ if(mOptionIndex > 0){ mOptionIndex--; } else{ mOptionIndex = mOptions.size() - 2; } updateOptionText(); }else if(event.key.code == sf::Keyboard::Down){ if(mOptionIndex < mOptions.size() - 2){ mOptionIndex++; }else{ mOptionIndex = 0; } updateOptionText(); } #endif return true; }
//private void MenuHighScoreState::buildMenu(const sf::Font& font) { const float centreX = xy::DefaultSceneSize.x / 2.f; const auto& scores = getContext().appInstance.getScores(); auto list = xy::UI::create<xy::UI::ScoreList>(font); list->setAlignment(xy::UI::Alignment::Centre); list->setPosition(centreX, 590.f); list->setList(scores); list->setIndex(getContext().appInstance.getLastScoreIndex()); m_uiContainer.addControl(list); auto upScroll = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/scroll_arrow_vertical.png")); upScroll->setAlignment(xy::UI::Alignment::Centre); upScroll->setPosition(1310, 470.f); upScroll->addCallback([list]() { list->scroll(list->getVerticalSpacing()); }); m_uiContainer.addControl(upScroll); auto downScroll = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/scroll_arrow_vertical.png")); downScroll->setAlignment(xy::UI::Alignment::Centre); downScroll->setRotation(180.f); downScroll->setPosition(1310.f, 720.f); downScroll->addCallback([list]() { list->scroll(-list->getVerticalSpacing()); }); m_uiContainer.addControl(downScroll); auto button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/start_button.png")); button->setString("OK"); button->setAlignment(xy::UI::Alignment::Centre); button->setPosition(centreX, 875.f); button->addCallback([this]() { auto msg = m_messageBus.post<xy::Message::UIEvent>(xy::Message::UIMessage); msg->type = xy::Message::UIEvent::MenuClosed; msg->value = 0.f; msg->stateID = (m_endGame) ? States::ID::HighScoresEnd : States::ID::HighScoresMenu; if (m_endGame) { requestStackClear(); requestStackPush(States::ID::MenuBackground); } else { requestStackPop(); requestStackPush(States::ID::MenuMain); } }); m_uiContainer.addControl(button); }
//private void MenuPauseState::buildMenu(const sf::Font& font) { const float centreX = xy::DefaultSceneSize.x / 2.f; auto label = xy::UI::create<xy::UI::Label>(font); label->setAlignment(xy::UI::Alignment::Centre); label->setPosition(centreX, 300.f); label->setString("PAUSED"); m_uiContainer.addControl(label); auto button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/start_button.png")); button->setString("Continue"); button->setAlignment(xy::UI::Alignment::Centre); button->setPosition(centreX, 425.f); button->addCallback([this]() { close(); }); m_uiContainer.addControl(button); button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/start_button.png")); button->setString("Options"); button->setAlignment(xy::UI::Alignment::Centre); button->setPosition(centreX, 525.f); button->addCallback([this]() { close(); requestStackPush(States::ID::PausedOptions); }); m_uiContainer.addControl(button); button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/start_button.png")); button->setString("Quit to Main"); button->setAlignment(xy::UI::Alignment::Centre); button->setPosition(centreX, 625.f); button->addCallback([this]() { close(); requestStackClear(); requestStackPush(States::ID::MenuBackground); }); m_uiContainer.addControl(button); button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/start_button.png")); button->setString("Exit to Desktop"); button->setAlignment(xy::UI::Alignment::Centre); button->setPosition(centreX, 725.f); button->addCallback([this]() { xy::App::quit(); }); m_uiContainer.addControl(button); }
MenuState::MenuState(trmb::StateStack& stack, trmb::State::Context context) : trmb::State(stack, context) , mFullscreen(0x5a0d2314) , mWindowed(0x11e3c735) , mGUIContainer(context.window) { // ALW - Calculate x, y coordinates relative to the center of the window, // ALW - so GUI elements are equidistance from the center in any resolution. const sf::Vector2f center = sf::Vector2f(context.window->getSize() / 2u); sf::Texture& texture = context.textures->get(Textures::ID::TitleScreen); mBackgroundSprite.setTexture(texture); trmb::centerOrigin(mBackgroundSprite); mBackgroundSprite.setPosition(center); const float x = center.x - 375.0f; const float y = center.y - 100.0f; const float buttonHeight = 50.0f; mPlayButton = std::make_shared<trmb::Button>(context, Fonts::ID::Main, SoundEffects::ID::Button, Textures::ID::Buttons, 200, 50); mPlayButton->setPosition(x, y); mPlayButton->setText("Play"); mPlayButton->setCallback([this]() { requestStackPop(); requestStackPush(States::ID::Game); }); mSettingsButton = std::make_shared<trmb::Button>(context, Fonts::ID::Main, SoundEffects::ID::Button, Textures::ID::Buttons, 200, 50); mSettingsButton->setPosition(x, y + buttonHeight); mSettingsButton->setText("Settings"); mSettingsButton->setCallback([this]() { requestStackPush(States::ID::Settings); }); mExitButton = std::make_shared<trmb::Button>(context, Fonts::ID::Main, SoundEffects::ID::Button, Textures::ID::Buttons, 200, 50); mExitButton->setPosition(x, y + 2.0f * buttonHeight); mExitButton->setText("Exit"); mExitButton->setCallback([this]() { requestStackPop(); }); mGUIContainer.pack(mPlayButton); mGUIContainer.pack(mSettingsButton); mGUIContainer.pack(mExitButton); // Play menu theme context.music->play(Music::ID::MenuTheme); }
PlayState::PlayState(StateStack& stack, Context context) : State(stack, context), mGUIContainer() { sf::Texture& texture = context.textures->get(Textures::TitleScreen); mBackgroundSprite.setTexture(texture); sf::Vector2f windowSize(context.window->getSize()); auto playButton = std::make_shared<GUI::Button>(context); playButton->setPosition(0.4f * windowSize.x, 0.3f * windowSize.y); playButton->setText("New Game"); playButton->setCallback([this]() { requestStackPop(); requestStackPush(States::Game); }); auto LevelButton = std::make_shared<GUI::Button>(context); LevelButton->setPosition(0.4f * windowSize.x, 0.4f * windowSize.y); LevelButton->setText("Select Level"); LevelButton->setCallback([this]() { requestStackPush(States::LevelSelect); }); auto UpgradesButton = std::make_shared<GUI::Button>(context); UpgradesButton->setPosition(0.4f * windowSize.x, 0.5f * windowSize.y); UpgradesButton->setText("Ship Modifications"); UpgradesButton->setCallback([this]() { requestStackPush(States::Upgrade); }); auto LeaderBButton = std::make_shared<GUI::Button>(context); LeaderBButton->setPosition(0.4f * windowSize.x, 0.6f * windowSize.y); LeaderBButton->setText("Leaderboards"); LeaderBButton->setCallback([this]() { requestStackPush(States::Leader); }); auto backButton = std::make_shared<GUI::Button>(context); backButton->setPosition(0.4f * windowSize.x, 0.8f * windowSize.y); backButton->setText("Back"); backButton->setCallback([this]() { requestStackPop(); requestStackPush(States::Menu); }); mGUIContainer.pack(playButton); mGUIContainer.pack(LevelButton); mGUIContainer.pack(UpgradesButton); mGUIContainer.pack(LeaderBButton); mGUIContainer.pack(backButton); }
bool TitleState::handleEvent(const sf::Event& event) { /* Lazy way to completely abort this state -- we don't need it */ requestStackPop(); requestStackPush(States::Menu); // If the any key is pressed, trigger the next screen if (event.type == sf::Event::KeyPressed) { requestStackPop(); requestStackPush(States::Menu); } return true; }
void SelectionState::onExit(int code) { switch (code) { case 3: emit requestStackPop(); break; case 1: { QFile file("qml/HCIProject/GameScreen/current.txt"); if (file.open(QIODevice::WriteOnly)) { QTextStream out(&file); out << m_charSlots[m_selected]->property("charName").toString() << " "; out << m_charSlots[m_selected]->property("charInfo").toString(); file.close(); } emit requestStackPush(State::LOADING, false); break; } } }
bool TitleState::handleEvent(const sf::Event& event) { if (event.type == sf::Event::KeyReleased) { requestStackPop(); requestStackPush(States::Game); } return true; }
PauseState::PauseState(StateStack& stack, Context context) : State(stack, context) , mBackgroundSprite() , mPausedText() , mGUIContainer() { sf::Font& font = context.fonts->get(Fonts::Main); sf::Vector2f windowSize(context.window->getSize()); mPausedText.setFont(font); mPausedText.setString("Game Paused"); mPausedText.setCharacterSize(70); centerOrigin(mPausedText); mPausedText.setPosition(0.5f * windowSize.x, 0.4f * windowSize.y); auto returnButton = std::make_shared<GUI::Button>(*context.fonts, *context.textures); returnButton->setPosition(0.5f * windowSize.x - 100, 0.4f * windowSize.y + 75); returnButton->setText("Return"); returnButton->setCallback([this] () { requestStackPop(); }); auto backToMenuButton = std::make_shared<GUI::Button>(*context.fonts, *context.textures); backToMenuButton->setPosition(0.5f * windowSize.x - 100, 0.4f * windowSize.y + 125); backToMenuButton->setText("Back to menu"); backToMenuButton->setCallback([this] () { requestStateClear(); requestStackPush(States::Menu); }); mGUIContainer.pack(returnButton); mGUIContainer.pack(backToMenuButton); }
bool GameState::handleEvent(const sf::Event& event) { if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) requestStackPush(States::Pause); return true; }
MenuState::MenuState(StateStack& stateStack, Context context, States::ID id) : State(stateStack, context, id), m_play_button(context.textures->getTexture(Textures::Button), context.fonts->getFont(Fonts::Main)), m_exit_button(context.textures->getTexture(Textures::Button), context.fonts->getFont(Fonts::Main)), m_background(*context.textures->getTexture(Textures::Titlescreen)), m_background_move(true), m_button_fade(false) { // Play button m_play_button.setAction([this] () { requestStackPop(); requestStackPush(States::Game); }); m_play_button.setText("Play"); m_play_button.setPosition(1280/2, 720/2 + 40); // Exit button m_exit_button.setAction([this] () { requestStackClear(); }); m_exit_button.setText("Exit"); m_exit_button.setPosition(1280/2, 720/2 + 80); // Background thing m_background.setPosition(640 - m_background.getLocalBounds().width/2, 360 - m_background.getLocalBounds().height/2); context.mouse->setState(gui::Mouse::Neutral); }
bool LoadingState::update(sf::Time) { //si la tarea está creada, la continúo if (taskCreated) { if (loadingTask->isFinished()) { //si la tarea se ha terminado, comienzo el siguiente estado taskCreated=false; requestStackPop(); requestStackPush(StatesID::Game); //avanzo en la completitud de la tarea } else { setCompletion(loadingTask->getCompletion()); } //creo la tarea } else { context->actualLevel = new Level(*context->systemManager); std::string* level=new std::string("level1.xml"); loadingTask = new LoadingLevel(context->nameLevel, context); loadingTask->execute(); taskCreated = true; } return true; }
bool GameState::update(sf::Time dt) { mWorld.update(dt); if (!mWorld.hasAlivePlayer()) { mPlayer.setMissionStatus(Player::MissionFailure); requestStackPush(States::GameOver); } else if (mWorld.hasPlayerReachedEnd()) { mPlayer.setMissionStatus(Player::MissionSuccess); requestStackPush(States::MissionSuccess); } CommandQueue& commands = mWorld.getCommandQueue(); mPlayer.handleRealtimeInput(commands); return true; }
bool TitleState::handleEvent(const sf::Event& event) { // If any key is pressed, trigger the next screen if (event.type == sf::Event::KeyReleased) { requestStackPop(); requestStackPush(States::Menu); } return true; }
bool GameState::handleEvent(const sf::Event& evt) { switch (evt.type) { case sf::Event::MouseButtonPressed: { //auto position = getContext().renderWindow.mapPixelToCoords(sf::Mouse::getPosition(getContext().renderWindow)); switch (evt.mouseButton.button) { case sf::Mouse::Left: //addBlock(position, blockSize); break; case sf::Mouse::Right: //addNpc(position); break; default: break; } break; } case sf::Event::KeyPressed: switch (evt.key.code) { case sf::Keyboard::Num1: break; case sf::Keyboard::Num2: //m_scoreBoard.enablePlayer(Category::PlayerTwo); break; case sf::Keyboard::P: requestStackPush(States::ID::Pause); break; default:break; } break; case sf::Event::JoystickButtonReleased: if (evt.joystickButton.button == 7) requestStackPush(States::ID::Pause); break; default: break; } return true; }
bool LoadingState::update( sf::Time dt ) { if ( mLoadingTask.isFinished() ) { requestStackPop(); requestStackPush( States::Game ); } else { setCompletion( mLoadingTask.getCompletion() ); } return true; }
bool TitleState::handleEvent(const sf::Event& event) { if (event.type == sf::Event::KeyPressed) { requestStackPop(); requestStackPush(States::ID::Menu); } return true; }
bool GameOverState::update(sf::Time dt){ mElapsedTime += dt; if (mElapsedTime > sf::seconds(3)){ requestStackClear(); requestStackPush(States::Menu); } return false; }
bool GameState::handleEvent(const sf::Event& event) { // Game input handling //mPlayer.handleEvent(event, commands); // Escape pressed, trigger the pause screen if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) requestStackPush(StateID::Pause); return true; }
void MyFirstState::createScene() { //add the systems auto& messageBus = getContext().appInstance.getMessageBus(); m_scene.addSystem<xy::TextSystem>(messageBus); m_scene.addSystem<xy::UISystem>(messageBus); m_scene.addSystem<xy::RenderSystem>(messageBus); m_font.loadFromFile("ProggyClean.ttf"); auto entity = m_scene.createEntity(); entity.addComponent<xy::Transform>().setPosition(200.f, 200.f); entity.addComponent<xy::Text>(m_font).setString("xygine tutorial"); entity.getComponent<xy::Text>().setCharacterSize(60); entity.addComponent<xy::Drawable>(); entity = m_scene.createEntity(); entity.addComponent<xy::Transform>().setPosition(200.f, 300.f); entity.addComponent<xy::Text>(m_font).setString("Play"); entity.getComponent<xy::Text>().setCharacterSize(40); entity.addComponent<xy::Drawable>(); entity.addComponent<xy::UIHitBox>().area = xy::Text::getLocalBounds(entity); auto callbackID = m_scene.getSystem<xy::UISystem>().addMouseButtonCallback( [&](xy::Entity, sf::Uint64 flags) { if(flags & xy::UISystem::LeftMouse) { requestStackClear(); requestStackPush(States::GameState); } }); entity.getComponent<xy::UIHitBox>().callbacks[xy::UIHitBox::MouseUp] = callbackID; entity = m_scene.createEntity(); entity.addComponent<xy::Transform>().setPosition(200.f, 360.f); entity.addComponent<xy::Text>(m_font).setString("Quit"); entity.getComponent<xy::Text>().setCharacterSize(40); entity.addComponent<xy::Drawable>(); entity.addComponent<xy::UIHitBox>().area = xy::Text::getLocalBounds(entity); callbackID = m_scene.getSystem<xy::UISystem>().addMouseButtonCallback( [&](xy::Entity, sf::Uint64 flags) { if(flags & xy::UISystem::LeftMouse) { getContext().appInstance.quit(); } }); entity.getComponent<xy::UIHitBox>().callbacks[xy::UIHitBox::MouseUp] = callbackID; }
bool GameOverState::update(sf::Time dt) { // Show state for 3 seconds, after return to menu mElapsedTime += dt; if (mElapsedTime > sf::seconds(3)) { requestStateClear(); requestStackPush(States::Menu); } return false; }
bool LevelSelectState::handleEvent(const sf::Event &event){ if (event.type == sf::Event::KeyPressed){ mLevelContainer->handleEvent(event); if (event.key.code == sf::Keyboard::Escape){ requestStackPop(); requestStackPush(ID::Menu); } } return false; }
bool TitleState::processEvent(const cpp3ds::Event& event) { // If any key is pressed, trigger the next screen if (event.type == cpp3ds::Event::KeyPressed) { if (event.key.code == cpp3ds::Keyboard::Select) { requestStackClear(); return true; } if (cpp3ds::Service::isEnabled(cpp3ds::NETWORK)) { requestStackPop(); requestStackPush(States::ServerSelect); } else { getContext().transition.message = _("No internet connection.\nConnect to internet and retry."); requestStackPush(States::TransitionMessage); } } return true; }
bool TitleState::handleEvent(const sf::Event event) { // Any key press will push the menu state if(event.type == sf::Event::KeyPressed) { requestStackPop(); requestStackPush(States::MENU); } // True means, this state doesn't block states under it return true; }
bool EndState::handleEvent(const sf::Event& e) { if (e.type == sf::Event::KeyPressed && e.key.code == sf::Keyboard::Escape) { ScoreGenerator& scores = *getContext().score; saveScore(scores.getScoreString()); scores.resetScores(); requestStackPop(); requestStackPush(States::Title); } return false; }
void ScoreState::closeMenu() { requestStackPop(); requestStackPush(States::ID::Menu); Message msg; msg.type = Message::Type::UI; msg.ui.type = Message::UIEvent::MenuClosed; msg.ui.value = 0.f; msg.ui.stateId = States::ID::Score; m_messageBus.send(msg); }
bool GameState::handleEvent(const sf::Event& event) { //game input handling //CommandQueue& commands = mScene.getCommandQueue(); //mPlayer.handleEvent(event, commands); //Escape pressed, trigger the pause screen if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) { requestStackPush(States::Pause); } return true; }