summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Enders <smenders@gmail.com>2021-01-20 21:05:44 -0500
committerStephen Enders <smenders@gmail.com>2021-01-20 21:05:44 -0500
commiteccb6df404b9ae052b9d13750f54dc69241668b4 (patch)
treeec4c52c55c021a4550723f2fe8fd6dce8c118ac2
parent1f8159534f30d3e46f53cebb788c91153103edcb (diff)
Create timer and use it to end the rolling phase
This timer can be used to determine when to end rolling
-rw-r--r--CMakeLists.txt2
-rw-r--r--timedLatch.cpp37
-rw-r--r--timedLatch.hpp22
-rw-r--r--ur.cpp22
4 files changed, 75 insertions, 8 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f7a56e2..814eacb 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.hpp helper.cpp)
+add_executable(${EXECUTABLE_NAME} ur.cpp helper.cpp timedLatch.cpp)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
diff --git a/timedLatch.cpp b/timedLatch.cpp
new file mode 100644
index 0000000..0629ac8
--- /dev/null
+++ b/timedLatch.cpp
@@ -0,0 +1,37 @@
+#include "timedLatch.hpp"
+
+namespace ur {
+
+TimedLatch::TimedLatch(sf::Time duration)
+{
+ this->duration = duration;
+ this->clock = sf::Clock();
+};
+
+void
+TimedLatch::start()
+{
+ this->clock.restart();
+ this->isStarted = true;
+};
+
+bool
+TimedLatch::is_running()
+{
+ return this->isStarted && this->clock.getElapsedTime() < duration;
+};
+
+bool
+TimedLatch::is_completed()
+{
+
+ return this->isStarted && this->clock.getElapsedTime() >= duration;
+};
+
+void
+TimedLatch::reset()
+{
+ this->isStarted = false;
+};
+
+}
diff --git a/timedLatch.hpp b/timedLatch.hpp
new file mode 100644
index 0000000..77c7d49
--- /dev/null
+++ b/timedLatch.hpp
@@ -0,0 +1,22 @@
+#ifndef UR_TIMEDLATCH_H
+#define UR_TIMEDLATCH_H
+#include <SFML/System.hpp>
+
+namespace ur {
+class TimedLatch
+{
+public:
+ TimedLatch(sf::Time duration);
+ void start();
+ void reset();
+ bool is_running();
+ bool is_completed();
+
+private:
+ bool isStarted;
+ sf::Time duration;
+ sf::Clock clock; // internal sfml clock to manage time
+};
+}
+
+#endif
diff --git a/ur.cpp b/ur.cpp
index eb68ad7..faec4d5 100644
--- a/ur.cpp
+++ b/ur.cpp
@@ -1,5 +1,7 @@
#include "helper.hpp"
+#include "timedLatch.hpp"
#include <SFML/Graphics.hpp>
+#include <SFML/System.hpp>
#include <iostream>
#include <string>
@@ -8,7 +10,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 SEMI_TRANSPARENT = sf::Color(0xff, 0x0, 0xff, 255);
+const sf::Color SEMI_TRANSPARENT = sf::Color(255, 255, 255, 128);
GameState state = GameState::WAITING;
GameState prev_state = GameState::WAITING;
@@ -170,6 +172,8 @@ main()
view.setSize(view.getSize() * ZOOM);
view.setCenter(view.getSize() / 2.f);
+ ur::TimedLatch rolling_animation_timer(sf::seconds(3));
+
while (window.isOpen()) {
sf::Event event;
@@ -191,6 +195,7 @@ main()
if (state == GameState::WAITING) {
std::cout << "Roll!" << std::endl;
// setup for rolling
+ rolling_animation_timer.start();
change_state(GameState::ROLLING);
for (auto& rs : (*roll_sprites)) {
rs.setColor(SEMI_TRANSPARENT);
@@ -205,12 +210,6 @@ main()
(*dice)[6].show = false;
(*dice)[7].show = true;
break;
- } else if (state == GameState::ROLLING) {
- std::cout << "Change turn" << std::endl;
- for (auto& rs : (*roll_sprites)) {
- rs.setColor(sf::Color::White);
- }
- next_turn();
}
}
}
@@ -249,6 +248,15 @@ main()
window.draw(p2Score);
window.setView(window.getDefaultView());
window.display();
+
+ // test timer logic - change turn after timer completes
+ if (rolling_animation_timer.is_completed()) {
+ rolling_animation_timer.reset();
+ for (auto& rs : (*roll_sprites)) {
+ rs.setColor(sf::Color::White);
+ }
+ next_turn();
+ }
}
return EXIT_SUCCESS;