int main() { enum GameState { MainMenu, Playing, GameOver }; GameState state = GameState::MainMenu; VideoMode videoMode(SCREEN_WIDTH, SCREEN_HEIGHT); RenderWindow renderWindow(videoMode, "SFML Tetris"); Event gameEvent; Texture backgroundTexture; Texture brickPreviewBackground; Texture gameOverBackground; Texture mainMenuBackground; Sprite mainMenuBackgroundSprite; Sprite brickPreviewSprite; Sprite backgroundSprite; Sprite gameOverBackgroundSprite; SoundBuffer dropSoundBuffer; Sound dropSound; Music music; Text scoreHeaderText("Score: "); Text scoreDisplayText; Text levelHeaderText("Level: "); Text levelDisplayText; Text nextBrickText("Next: "); Text gameOverHeaderText("\t GAME OVER!\nHere are your final stats:"); Text mainMenuHeaderText("SFML TETRIS!"); Text mainMenuStartText("Start"); Text mainMenuQuitText("Quit"); Text brickCountText[NUMBER_OF_BRICK_TYPES]; bool gameBoard[GB_HEIGHT][GB_WIDTH]; unsigned int score = 0; unsigned int level = 1; unsigned int totalLines = 0; unsigned int brickCount[NUMBER_OF_BRICK_TYPES] = {0}; vector<Brick> brickQueue; vector<Brick> brickList; Clock brickLockTimer; Clock brickFallTimer; bool isCheckingLockTime = false; backgroundTexture.LoadFromFile("Images/background.png"); brickPreviewBackground.LoadFromFile("Images/previewBG.png"); gameOverBackground.LoadFromFile("Images/gameOverBG.png"); mainMenuBackground.LoadFromFile("Images/mainMenuBG.png"); brickPreviewSprite.SetTexture(brickPreviewBackground); brickPreviewSprite.SetPosition(450, 150); backgroundSprite.SetTexture(backgroundTexture); gameOverBackgroundSprite.SetTexture(gameOverBackground); gameOverBackgroundSprite.SetPosition(110, 82); mainMenuBackgroundSprite.SetTexture(mainMenuBackground); scoreHeaderText.SetCharacterSize(TEXT_CHAR_SIZE); scoreHeaderText.SetPosition(450, 20); char* scoreDisplayBuffer = new char; scoreDisplayText.SetCharacterSize(TEXT_CHAR_SIZE); scoreDisplayText.SetPosition(520, 20); levelHeaderText.SetCharacterSize(TEXT_CHAR_SIZE); levelHeaderText.SetPosition(450, 60); char* levelDisplayBuffer = new char; levelDisplayText.SetCharacterSize(TEXT_CHAR_SIZE); levelDisplayText.SetPosition(520, 60); nextBrickText.SetCharacterSize(TEXT_CHAR_SIZE); nextBrickText.SetPosition(450, 120); gameOverHeaderText.SetCharacterSize(TEXT_CHAR_SIZE); gameOverHeaderText.SetPosition(210, 265); mainMenuHeaderText.SetCharacterSize(40); mainMenuHeaderText.SetPosition(180, 50); mainMenuStartText.SetCharacterSize(32); mainMenuStartText.SetPosition(285, 210); mainMenuQuitText.SetCharacterSize(32); mainMenuQuitText.SetPosition(285, 310); char* brickCountTextBuffer[NUMBER_OF_BRICK_TYPES]; for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++) { brickCountTextBuffer[i] = new char; brickCountText[i].SetPosition(100, 50 * (i + 1)); } dropSoundBuffer.LoadFromFile("Sounds/drop.wav"); dropSound.SetBuffer(dropSoundBuffer); for (int i = 0; i < GB_HEIGHT; i++) { for (int j = 0; j < GB_WIDTH; j++) { gameBoard[i][j] = false; } } RefillBrickQueue(&brickQueue); Brick activeBrick = PullBrickFromQueue(&brickQueue); Brick previewBrick = PullBrickFromQueue(&brickQueue); previewBrick.SetPreviewPosition(); bool isGameOver = false; while (renderWindow.IsOpen()) { switch (state) { case GameState::MainMenu: { while (renderWindow.PollEvent(gameEvent)) { if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape) { renderWindow.Close(); } else if (gameEvent.Type == Event::MouseButtonPressed) { Vector2i mousePos = Mouse::GetPosition(renderWindow); if (mousePos.x >= 220 && mousePos.x <= 420 && mousePos.y >= 200 && mousePos.y <= 260) { state = GameState::Playing; } else if ((mousePos.x >= 220 && mousePos.x <= 420 && mousePos.y >= 300 && mousePos.y <= 360) || gameEvent.Key.Code == Keyboard::Escape) { renderWindow.Close(); } } } renderWindow.Draw(mainMenuBackgroundSprite); renderWindow.Draw(mainMenuHeaderText); renderWindow.Draw(mainMenuStartText); renderWindow.Draw(mainMenuQuitText); renderWindow.Display(); break; } case GameState::GameOver: { Text gameOverScoreHeaderText = scoreHeaderText; gameOverScoreHeaderText.SetPosition(130, 320); Text gameOverScoreDisplayText = scoreDisplayText; gameOverScoreDisplayText.SetPosition(210, 320); Text gameOverLevelHeaderText = levelHeaderText; gameOverLevelHeaderText.SetPosition(130, 350); Text gameOverLevelDisplayText = levelDisplayText; gameOverLevelDisplayText.SetPosition(210, 350); while (renderWindow.PollEvent(gameEvent)) { if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape) { renderWindow.Close(); } } renderWindow.Draw(gameOverBackgroundSprite); renderWindow.Draw(gameOverHeaderText); renderWindow.Draw(gameOverScoreHeaderText); renderWindow.Draw(gameOverScoreDisplayText); renderWindow.Draw(gameOverLevelHeaderText); renderWindow.Draw(gameOverLevelDisplayText); renderWindow.Display(); break; } case GameState::Playing: { if (activeBrick.IsLocked()) { dropSound.Play(); brickList.push_back(activeBrick); brickCount[(int)activeBrick.GetBrickType()]++; int linesCleared = 0; for (int i = 0; i < SPRITES_IN_BRICK; i++) { if (activeBrick.GetSpriteYPosition(i) < BRICK_UPPER_BOUNDRY) { state = GameState::GameOver; break; } else { gameBoard[activeBrick.GetSpriteYPosition(i)][activeBrick.GetSpriteXPosition(i)] = true; } } if (state == GameState::GameOver) { break; } //Check if there are any full rows that need to be cleared. for (int i = BRICK_UPPER_BOUNDRY; i < GB_HEIGHT; i++) { for (int j = 0; j < GB_WIDTH; j++) { if (gameBoard[i][j]) { if (j == GB_WIDTH - 1) { ClearRow(&brickList, i, gameBoard); linesCleared++; totalLines++; if (totalLines % 8 == 0 && level < LEVEL_MAX) { level++; } } else { continue; } } else { break; } } } if (linesCleared > 0) { if (linesCleared > 1 && linesCleared < 4) { score += (linesCleared * MULTI_LINE_CLEAR_SCORE) + BRICK_LOCK_SCORE; } else if (linesCleared == 4) { score += TETRIS_CLEAR_SCORE + BRICK_LOCK_SCORE; } else { score += SINGLE_LINE_CLEAR_SCORE + BRICK_LOCK_SCORE; } } else { score += BRICK_LOCK_SCORE; } activeBrick = previewBrick; activeBrick.SetActivePosition(); previewBrick = PullBrickFromQueue(&brickQueue); previewBrick.SetPreviewPosition(); } else { Time fallTimer = brickFallTimer.GetElapsedTime(); Time lockTimer = brickLockTimer.GetElapsedTime(); float dropSpeed = 1 - (float)(level * 0.05f); if (!IsBottomCollision(activeBrick, gameBoard) && fallTimer.AsSeconds() >= dropSpeed) { for (int i = 0; i < SPRITES_IN_BRICK; i++) { activeBrick.SetSpriteYPosition(i, 1); brickFallTimer.Restart(); isCheckingLockTime = false; } } else if (IsBottomCollision(activeBrick, gameBoard)) { if (!isCheckingLockTime) { isCheckingLockTime = true; brickLockTimer.Restart(); } else if (lockTimer.AsSeconds() >= 0.5f) { activeBrick.LockBrick(); isCheckingLockTime = false; } } } while (renderWindow.PollEvent(gameEvent)) { if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape) { renderWindow.Close(); } if (gameEvent.Type == Event::KeyPressed) { CheckInput(&gameEvent, &activeBrick, gameBoard, &score); } } scoreDisplayText.SetString(_itoa(score, scoreDisplayBuffer, 10)); levelDisplayText.SetString(_itoa(level, levelDisplayBuffer, 10)); for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++) { brickCountText[i].SetString(_itoa(brickCount[i], brickCountTextBuffer[i], 10)); } renderWindow.Clear(Color(0, 255, 255)); renderWindow.Draw(backgroundSprite); renderWindow.Draw(scoreHeaderText); renderWindow.Draw(scoreDisplayText); renderWindow.Draw(levelHeaderText); renderWindow.Draw(levelDisplayText); renderWindow.Draw(brickPreviewSprite); renderWindow.Draw(nextBrickText); for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++) { renderWindow.Draw(brickCountText[i]); } for (int i = 0; i < brickList.size(); i++) { for (int j = 0; j < SPRITES_IN_BRICK; j++) { renderWindow.Draw(brickList[i].GetSpriteArray()[j]); } } for (int i = 0; i < SPRITES_IN_BRICK; i++) { renderWindow.Draw(activeBrick.GetSpriteArray()[i]); renderWindow.Draw(previewBrick.GetSpriteArray()[i]); } renderWindow.Display(); break; } //END CASE } //END SWITCH } //END GAME LOOP return 0; }
void Urho3DTemplate::CreateUI() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); //Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will control the camera, and when visible it will point the raycast target XMLFile* style = cache->GetResource<XMLFile>("UI/Defaultstyle.xml"); SharedPtr<Cursor> cursor(new Cursor(context_)); cursor->SetStyleAuto(style); ui->SetCursor(cursor); //Set starting position of the cursor at the rendering window center Graphics* graphics = GetSubsystem<Graphics>(); cursor->SetPosition(graphics->GetWidth()/2, graphics->GetHeight()/2); //Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Lorum ipsum, dolor set amet" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); //The text has multiple rows. Center them in relation to each other instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight()/4); }
void Navigation::CreateUI() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will // control the camera, and when visible, it will point the raycast target XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); SharedPtr<Cursor> cursor(new Cursor(context_)); cursor->SetStyleAuto(style); ui->SetCursor(cursor); // Set starting position of the cursor at the rendering window center Graphics* graphics = GetSubsystem<Graphics>(); cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys to move, RMB to rotate view\n" "Shift+LMB to set path start, LMB to set path end\n" "MMB to add or remove obstacles\n" "Space to toggle debug geometry" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void VehicleDemo::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys to drive, mouse/touch to rotate camera\n" "F5 to save scene, F7 to load" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); // insert speed Text* speedText = ui->GetRoot()->CreateChild<Text>("UITextSpeed"); speedText->SetText("x km/h"); speedText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other speedText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center speedText->SetHorizontalAlignment(HA_CENTER); speedText->SetVerticalAlignment(VA_CENTER); speedText->SetPosition(ui->GetRoot()->GetWidth()/8, ui->GetRoot()->GetHeight() / 8); Text* gearText = ui->GetRoot()->CreateChild<Text>("UITextGear"); gearText->SetText("x gear"); gearText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other gearText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center gearText->SetHorizontalAlignment(HA_CENTER); gearText->SetVerticalAlignment(VA_CENTER); gearText->SetPosition(ui->GetRoot()->GetWidth()/8, ui->GetRoot()->GetHeight() / 6); }
void Player::CreateGUI() { UI* ui = GetSubsystem<UI>(); Text* scoreText = ui->GetRoot()->CreateChild<Text>(); scoreText->SetName("Score"); scoreTextName_ = scoreText->GetName(); scoreText->SetText(String(score_)); scoreText->SetFont(masterControl_->cache_->GetResource<Font>("Resources/Fonts/skirmishergrad.ttf"), 32); scoreText->SetColor(Color(0.5f, 0.95f, 1.0f, 0.666f)); scoreText->SetHorizontalAlignment(HA_CENTER); scoreText->SetVerticalAlignment(VA_CENTER); scoreText->SetPosition(0, ui->GetRoot()->GetHeight()/2.5f); //Setup 3D GUI elements guiNode_ = masterControl_->world.scene->CreateChild("GUI3D"); healthBarNode_ = guiNode_->CreateChild("HealthBar"); healthBarNode_->SetPosition(Vector3(0.0f, 1.0f, 21.0f)); healthBarNode_->SetScale(Vector3(health_, 1.0f, 1.0f)); healthBarModel_ = healthBarNode_->CreateComponent<StaticModel>(); healthBarModel_->SetModel(masterControl_->cache_->GetResource<Model>("Resources/Models/Bar.mdl")); healthBarModel_->SetMaterial(masterControl_->cache_->GetTempResource<Material>("Resources/Materials/GreenGlowEnvmap.xml")); shieldBarNode_ = guiNode_->CreateChild("HealthBar"); shieldBarNode_->SetPosition(Vector3(0.0f, 1.0f, 21.0f)); shieldBarNode_->SetScale(Vector3(health_, 0.9f, 0.9f)); shieldBarModel_ = shieldBarNode_->CreateComponent<StaticModel>(); shieldBarModel_->SetModel(masterControl_->cache_->GetResource<Model>("Resources/Models/Bar.mdl")); shieldBarModel_->SetMaterial(masterControl_->cache_->GetResource<Material>("Resources/Materials/BlueGlowEnvmap.xml")); Node* healthBarHolderNode = guiNode_->CreateChild("HealthBarHolder"); healthBarHolderNode->SetPosition(Vector3(0.0f, 1.0f, 21.0f)); StaticModel* healthBarHolderModel = healthBarHolderNode->CreateComponent<StaticModel>(); healthBarHolderModel->SetModel(masterControl_->cache_->GetResource<Model>("Resources/Models/BarHolder.mdl")); healthBarHolderModel->SetMaterial(masterControl_->cache_->GetResource<Material>("Resources/Materials/Metal.xml")); appleCounterRoot_ = guiNode_->CreateChild("AppleCounter"); for (int a = 0; a < 5; a++){ appleCounter_[a] = appleCounterRoot_->CreateChild(); appleCounter_[a]->SetEnabled(false); appleCounter_[a]->SetPosition(Vector3(-(a + 8.0f), 1.0f, 21.0f)); appleCounter_[a]->SetScale(0.333f); StaticModel* apple = appleCounter_[a]->CreateComponent<StaticModel>(); apple->SetModel(masterControl_->cache_->GetResource<Model>("Resources/Models/Apple.mdl")); apple->SetMaterial(masterControl_->cache_->GetTempResource<Material>("Resources/Materials/GoldEnvmap.xml")); } heartCounterRoot_ = guiNode_->CreateChild("HeartCounter"); for (int h = 0; h < 5; h++){ heartCounter_[h] = heartCounterRoot_->CreateChild(); heartCounter_[h]->SetEnabled(false); heartCounter_[h]->SetPosition(Vector3(h + 8.0f, 1.0f, 21.0f)); heartCounter_[h]->SetScale(0.333f); StaticModel* heart = heartCounter_[h]->CreateComponent<StaticModel>(); heart->SetModel(masterControl_->cache_->GetResource<Model>("Resources/Models/Heart.mdl")); heart->SetMaterial(masterControl_->cache_->GetTempResource<Material>("Resources/Materials/RedEnvmap.xml")); } }
void LightAnimation::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText("Use WASD keys and mouse/touch to move"); Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"); instructionText->SetFont(font, 15); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); // Animating text Text* text = ui->GetRoot()->CreateChild<Text>("animatingText"); text->SetFont(font, 15); text->SetHorizontalAlignment(HA_CENTER); text->SetVerticalAlignment(VA_CENTER); text->SetPosition(0, ui->GetRoot()->GetHeight() / 4 + 20); }
void TowerApp::CreateInstructions() { ResourceCache *cache = GetSubsystem<ResourceCache>(); UI *ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text *instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText("Use WASD keys to move, use PageUp PageDown keys to zoom."); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void PhoneModeAddressBook::AddContact(uint8_t Index) { uint16_t AddressMask = 1 << Index; if (this->AddressMask & AddressMask) return; string Name = sExe->ReadStringIndirect(VA_PHONE_ADDRMENU, Index, 0xC, 0x4); Text ContactText; ContactText.SetPosition(PHONE_WALLPAPER_X, BLUE_HEADER_POS_Y + BLUE_HEADER_HEIGHT + Contacts.size() * 20); ContactText.SetColor(Nsb::Color::BLACK); ContactText.SetCharacterSize(20); ContactText.CreateFromString(Name); Contacts.push_back({ ContactText, Index }); this->AddressMask |= AddressMask; }
void SignedDistanceFieldText::CreateInstructions() { ResourceCache* cache = GetContext()->m_ResourceCache.get(); UI* ui = GetContext()->m_UISystem.get(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText("Use WASD keys and mouse/touch to move"); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void Urho2DConstraints::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.\n Space to toggle debug geometry and joints - F5 to save the scene."); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); instructionText->SetTextAlignment(HA_CENTER); // Center rows in relation to each other // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void SkeletalAnimation::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys and mouse to move\n" "Space to toggle debug geometry" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void UIDrag::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText("Drag on the buttons to move them around.\n" "Touch input allows also multi-drag.\n" "Press SPACE to show/hide tagged UI elements."); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void MultipleViewports::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys and mouse/touch to move\n" "B to toggle bloom, F to toggle FXAA\n" "Space to toggle debug geometry\n" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
Slider* SoundEffects::CreateSlider(int x, int y, int xSize, int ySize, const String& text) { UIElement* root = GetSubsystem<UI>()->GetRoot(); ResourceCache* cache = GetSubsystem<ResourceCache>(); Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"); // Create text and slider below it Text* sliderText = root->CreateChild<Text>(); sliderText->SetPosition(x, y); sliderText->SetFont(font, 12); sliderText->SetText(text); Slider* slider = root->CreateChild<Slider>(); slider->SetStyleAuto(); slider->SetPosition(x, y + 20); slider->SetSize(xSize, ySize); // Use 0-1 range for controlling sound/music master volume slider->SetRange(1.0f); return slider; }
void CharacterDemo::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys and mouse/touch to move\n" "Space to jump, F to toggle 1st/3rd person\n" "F5 to save scene, F7 to load" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void PhysicsStressTest::CreateInstructions() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); // Construct new Text object, set string to display and font to use Text* instructionText = ui->GetRoot()->CreateChild<Text>(); instructionText->SetText( "Use WASD keys and mouse to move\n" "LMB to spawn physics objects\n" "F5 to save scene, F7 to load\n" "Space to toggle physics debug geometry" ); instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // The text has multiple rows. Center them in relation to each other instructionText->SetTextAlignment(HA_CENTER); // Position the text relative to the screen center instructionText->SetHorizontalAlignment(HA_CENTER); instructionText->SetVerticalAlignment(VA_CENTER); instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4); }
void UIDrag::HandleUpdate(StringHash eventType, VariantMap& eventData) { auto* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); auto* input = GetSubsystem<Input>(); unsigned n = input->GetNumTouches(); for (unsigned i = 0; i < n; i++) { Text* t = (Text*)root->GetChild("Touch " + String(i)); TouchState* ts = input->GetTouch(i); t->SetText("Touch " + String(ts->touchID_)); IntVector2 pos = ts->position_; pos.y_ -= 30; t->SetPosition(pos); t->SetVisible(true); } for (unsigned i = n; i < 10; i++) { Text* t = (Text*)root->GetChild("Touch " + String(i)); t->SetVisible(false); } if (input->GetKeyPress(KEY_SPACE)) { PODVector<UIElement*> elements; root->GetChildrenWithTag(elements, "SomeTag"); for (PODVector<UIElement*>::ConstIterator i = elements.Begin(); i != elements.End(); ++i) { UIElement* element = *i; element->SetVisible(!element->IsVisible()); } } }
void Urho2DPlatformer::HandleCollisionBegin(PhysicsWorld2D *, RigidBody2D *, RigidBody2D *, Node *nodeA, Node * nodeB, const std::vector<uint8_t> &, CollisionShape2D *, CollisionShape2D *) { // Get colliding node auto* hitNode = nodeA; if (hitNode->GetName() == "Imp") hitNode = nodeB; QString nodeName = hitNode->GetName(); Node* character2DNode = scene_->GetChild("Imp", true); // Handle ropes and ladders climbing if (nodeName == "Climb") { if (character2D_->isClimbing_) // If transition between rope and top of rope (as we are using split triggers) character2D_->climb2_ = true; else { character2D_->isClimbing_ = true; auto* body = character2DNode->GetComponent<RigidBody2D>(); body->SetGravityScale(0.0f); // Override gravity so that the character doesn't fall // Clear forces so that the character stops (should be performed by setting linear velocity to zero, but currently doesn't work) body->SetLinearVelocity(Vector2(0.0f, 0.0f)); body->SetAwake(false); body->SetAwake(true); } } if (nodeName == "CanJump") character2D_->aboveClimbable_ = true; // Handle coins picking if (nodeName == "Coin") { hitNode->Remove(); character2D_->remainingCoins_ -= 1; auto* ui = GetContext()->m_UISystem.get(); if (character2D_->remainingCoins_ == 0) { Text* instructions = static_cast<Text*>(ui->GetRoot()->GetChild("Instructions", true)); instructions->SetText("!!! Go to the Exit !!!"); } Text* coinsText = static_cast<Text*>(ui->GetRoot()->GetChild("CoinsText", true)); coinsText->SetText(QString(character2D_->remainingCoins_)); // Update coins UI counter sample2D_->PlaySoundEffect("Powerup.wav"); } // Handle interactions with enemies if (nodeName == "Enemy" || nodeName == "Orc") { auto* animatedSprite = character2DNode->GetComponent<AnimatedSprite2D>(); float deltaX = character2DNode->GetPosition().x_ - hitNode->GetPosition().x_; // Orc killed if character is fighting in its direction when the contact occurs (flowers are not destroyable) if (nodeName == "Orc" && animatedSprite->GetAnimation() == "attack" && (deltaX < 0 == animatedSprite->GetFlipX())) { static_cast<Mover*>(hitNode->GetComponent<Mover>())->emitTime_ = 1; if (!hitNode->GetChild("Emitter", true)) { hitNode->GetComponent("RigidBody2D")->Remove(); // Remove Orc's body sample2D_->SpawnEffect(hitNode); sample2D_->PlaySoundEffect("BigExplosion.wav"); } } // Player killed if not fighting in the direction of the Orc when the contact occurs, or when colliding with a flower else { if (!character2DNode->GetChild("Emitter", true)) { character2D_->wounded_ = true; if (nodeName == "Orc") { auto* orc = static_cast<Mover*>(hitNode->GetComponent<Mover>()); orc->fightTimer_ = 1; } sample2D_->SpawnEffect(character2DNode); sample2D_->PlaySoundEffect("BigExplosion.wav"); } } } // Handle exiting the level when all coins have been gathered if (nodeName == "Exit" && character2D_->remainingCoins_ == 0) { // Update UI auto* ui = GetContext()->m_UISystem.get(); Text* instructions = static_cast<Text*>(ui->GetRoot()->GetChild("Instructions", true)); instructions->SetText("!!! WELL DONE !!!"); instructions->SetPosition(IntVector2(0, 0)); // Put the character outside of the scene and magnify him character2DNode->SetPosition(Vector3(-20.0f, 0.0f, 0.0f)); character2DNode->SetScale(1.5f); } // Handle falling into lava if (nodeName == "Lava") { auto* body = character2DNode->GetComponent<RigidBody2D>(); body->ApplyForceToCenter(Vector2(0.0f, 1000.0f), true); if (!character2DNode->GetChild("Emitter", true)) { character2D_->wounded_ = true; sample2D_->SpawnEffect(character2DNode); sample2D_->PlaySoundEffect("BigExplosion.wav"); } } // Handle climbing a slope if (nodeName == "Slope") character2D_->onSlope_ = true; }