From 9850d3c5dea463504e46b16cc2d403e663d186a4 Mon Sep 17 00:00:00 2001 From: Stephen Enders Date: Mon, 18 Jan 2021 08:01:21 -0500 Subject: Roll tweaks --- helper.cpp | 14 +++++++++---- helper.h | 2 +- ur.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/helper.cpp b/helper.cpp index d09318f..07c917d 100644 --- a/helper.cpp +++ b/helper.cpp @@ -153,10 +153,10 @@ createPlayer(sf::Texture& texture) return player; } -std::shared_ptr> +std::shared_ptr>> createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) { - auto dice = std::make_shared>(); + auto dice = std::make_shared>>(); // create dice, even 0 odds 1 // there are 8 dice results int total @@ -173,12 +173,18 @@ createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture) if (i % 2 == 0) { sf::Sprite s; s.setTexture(die0Texture); - struct dice_t die = { 0, true, s }; + auto die = std::make_shared(); + die->value = 0; + die->show = true; + die->sprite = s; dice->push_back(die); } else { sf::Sprite s; s.setTexture(die1Texture); - struct dice_t die = { 1, false, s }; + auto die = std::make_shared(); + die->value = 1; + die->show = false; + die->sprite = s; dice->push_back(die); } } diff --git a/helper.h b/helper.h index 950dbab..1d9159b 100644 --- a/helper.h +++ b/helper.h @@ -74,7 +74,7 @@ createPlayer(sf::Texture& pieceTexture); std::shared_ptr createPiece(int id, sf::Texture& texture); -std::shared_ptr> +std::shared_ptr>> createAllDice(sf::Texture& die0Texture, sf::Texture& die1Texture); std::shared_ptr>> diff --git a/ur.cpp b/ur.cpp index 1162c0b..a342db3 100644 --- a/ur.cpp +++ b/ur.cpp @@ -8,7 +8,7 @@ const float PAD = 32.f; const float PIECE_PAD = 8.f; const float TEXT_OFFSET = 8.f; const sf::Color BG_COLOR = sf::Color(66, 47, 81, 255); -const sf::Color GREY = sf::Color(255, 255, 255, 128); +const sf::Color SEMI_TRANSPARENT = sf::Color(255, 255, 255, 128); GameState state = GameState::WAITING; GameState prev_state = GameState::WAITING; @@ -23,6 +23,12 @@ change_state(GameState next) // p1 = false, p2 = true bool turn_tracker = true; +int rolling_frame = 0; +inline void +next(int* i, int max) +{ + (*i) = ((*i) + 1) % max; +} inline void next_turn() @@ -45,7 +51,7 @@ p2_turn() inline void render_dice( sf::RenderWindow* window, - std::shared_ptr> dice, + std::shared_ptr>> dice, std::shared_ptr>> roll_sprites) { @@ -68,10 +74,10 @@ render_dice( int r = dice_r, c = dice_c; for (int i = 0; i < 8; i++) { auto die = (*dice)[i]; - if (die.show) { - die.sprite.setPosition(pos(c, r)); - result += die.value; - window->draw(die.sprite); + if (die->show) { + die->sprite.setPosition(pos(c, r)); + result += die->value; + window->draw(die->sprite); if (i % 2 == 0) { c += 1; @@ -85,29 +91,41 @@ render_dice( } } } - /* - else if (state == GameState::ROLLING) - { + else if (state == GameState::ROLLING) { // animate the dice. This is attached to a timer // which will move between rolling and placing + int c = dice_c, r = dice_r; + // toggle dice + int i = 0; + for (auto die : (*dice)) { + if (!die->show) { + die->show = true; + continue; + } + die->sprite.setPosition(pos(c++, r)); + window->draw(die->sprite); + if (i++ == 1) { + c = dice_c; + r += 1; + } + die->show = false; + } + } - */ else { // draw initial values // draw the 0s int c = dice_c, r = dice_r; for (int i = 0; i < 8; i += 2) { auto die = (*dice)[i]; - die.sprite.setPosition(pos(c++, r)); - window->draw(die.sprite); + die->sprite.setPosition(pos(c++, r)); + window->draw(die->sprite); if (i == 2) { c = dice_c; r += 1; } } - // draw roll text - c = roll_c; - r = roll_r; + c = roll_c, r = roll_r; for (auto s : (*roll_sprites)) { s->setPosition(pos(c++, r)); window->draw(*s); @@ -132,7 +150,7 @@ main() const std::shared_ptr>> roll_sprites = createRollSprites((*textures)[ROLL_TILES[0]], (*textures)[ROLL_TILES[1]]); - const std::shared_ptr> dice = + const std::shared_ptr>> dice = createAllDice((*textures)[DIE_0], (*textures)[DIE_1]); sf::Sprite p1Score; @@ -169,15 +187,24 @@ main() for (auto s : (*roll_sprites)) { // zoom sprite bounds if (s->getGlobalBounds().contains(mPos)) { + // setup for rolling change_state(GameState::ROLLING); + + for (auto s : (*roll_sprites)) { + s->setColor(SEMI_TRANSPARENT); + } + + (*dice)[0]->show = false; + (*dice)[1]->show = true; + (*dice)[2]->show = true; + (*dice)[3]->show = false; + (*dice)[4]->show = true; + (*dice)[5]->show = false; + (*dice)[6]->show = false; + (*dice)[7]->show = true; break; } } - if (state == GameState::ROLLING) { - for (auto s : (*roll_sprites)) { - s->setColor(GREY); - } - } window.setView(window.getDefaultView()); } -- cgit v1.2.3-54-g00ecf