diff options
author | Stephen Enders <smenders@gmail.com> | 2021-01-20 22:53:14 -0500 |
---|---|---|
committer | Stephen Enders <smenders@gmail.com> | 2021-01-21 20:56:16 -0500 |
commit | bb25b2bc17ca4cada09da38d209be57a55020d31 (patch) | |
tree | 04fbdaf27804dcf6991172e11ed44ae9b71eb4fe | |
parent | eccb6df404b9ae052b9d13750f54dc69241668b4 (diff) |
Add rolling animation
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | random.cpp | 27 | ||||
-rw-r--r-- | random.hpp | 19 | ||||
-rw-r--r-- | ur.cpp | 61 |
4 files changed, 83 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 814eacb..683ead2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(EXECUTABLE_NAME ur) set(SFML_LIBRARIES sfml-system sfml-window sfml-graphics) find_package(SFML 2.5 REQUIRED COMPONENTS system window graphics) -add_executable(${EXECUTABLE_NAME} ur.cpp helper.cpp timedLatch.cpp) +add_executable(${EXECUTABLE_NAME} ur.cpp helper.cpp timedLatch.cpp random.cpp) target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES}) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/random.cpp b/random.cpp new file mode 100644 index 0000000..e94b191 --- /dev/null +++ b/random.cpp @@ -0,0 +1,27 @@ +#include "random.hpp" +#include <chrono> +#include <cmath> + +namespace ur { + +Random::Random(int min, int max) +{ + this->min = min; + this->max = max; + // setup the random stuff + unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); + this->engine = std::default_random_engine(seed); + + // setup distribution + float range = float(max - min); + this->distribution = + std::normal_distribution<float>(range / 2.f, range / 4.f); +} + +int +Random::next() +{ + return std::round(this->distribution(this->engine)); +} + +} diff --git a/random.hpp b/random.hpp new file mode 100644 index 0000000..e74267a --- /dev/null +++ b/random.hpp @@ -0,0 +1,19 @@ +#ifndef UR_RANDOM_H +#define UR_RANDOM_H + +#include <random> +namespace ur { +class Random +{ +public: + Random(int min, int max); + int next(); + +private: + int min; + int max; + std::default_random_engine engine; + std::normal_distribution<float> distribution; +}; +} +#endif @@ -1,4 +1,5 @@ #include "helper.hpp" +#include "random.hpp" #include "timedLatch.hpp" #include <SFML/Graphics.hpp> #include <SFML/System.hpp> @@ -12,6 +13,7 @@ const float TEXT_OFFSET = 8.f; const sf::Color BG_COLOR = sf::Color(66, 47, 81, 255); const sf::Color SEMI_TRANSPARENT = sf::Color(255, 255, 255, 128); +ur::Random dice_rand(0, 1); // 50/50 random GameState state = GameState::WAITING; GameState prev_state = GameState::WAITING; @@ -57,7 +59,8 @@ p2_turn() inline void render_dice(sf::RenderWindow* window, std::shared_ptr<std::vector<struct dice_t>> dice, - std::shared_ptr<std::vector<sf::Sprite>> roll_sprites) + std::shared_ptr<std::vector<sf::Sprite>> roll_sprites, + ur::TimedLatch* animation_frame_timer) { // draw dice @@ -96,23 +99,35 @@ render_dice(sf::RenderWindow* window, } } } else if (state == GameState::ROLLING) { - // animate the dice. This is attached to a timer - // which will move between rolling and placing + // if completed update dice sprites + if (animation_frame_timer->is_completed()) { + // iterate over each pair of dice sprites + // and show whichever matches the roll + for (int i = 0; i < 8; i+=2) + { + int result = dice_rand.next(); + (*dice)[i].show = result == 0; + (*dice)[i+1].show = result == 1; + } + } 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; + for (auto& die : (*dice)) + { + if (die.show) { + die.sprite.setPosition(pos(c++, r)); + window->draw(die.sprite); + if (++i == 2) { + c = dice_c; + r += 1; + } } - die.show = false; + } + // make sure we're started! + // note - this should come after the completed check otherwise we'll always restart it + if (!animation_frame_timer->is_running()) + { + animation_frame_timer->start(); } } else { @@ -173,6 +188,7 @@ main() view.setCenter(view.getSize() / 2.f); ur::TimedLatch rolling_animation_timer(sf::seconds(3)); + ur::TimedLatch rolling_animation_frame_pause_timer(sf::milliseconds(100)); while (window.isOpen()) { @@ -200,15 +216,10 @@ main() for (auto& rs : (*roll_sprites)) { rs.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; + for (int i = 0; i < 8; i++) + { + (*dice)[i].show = i % 2 == 0; // only show the 0s + } break; } } @@ -239,7 +250,7 @@ main() window.draw(p.sprite); } - render_dice(&window, dice, roll_sprites); + render_dice(&window, dice, roll_sprites, &rolling_animation_frame_pause_timer); for (auto& s : (*roll_sprites)) { window.draw(s); } |