From f9bf8f06ea47d9631ad5741f0453673178bc0a10 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 4 Oct 2024 18:23:14 -0400 Subject: [PATCH] Some jank test visual effects are working. --- gui.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- gui.hpp | 7 ++++++- map.cpp | 23 ++++++++++------------- meson.build | 2 ++ 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/gui.cpp b/gui.cpp index b3f378a..75040ca 100644 --- a/gui.cpp +++ b/gui.cpp @@ -17,6 +17,7 @@ #include #include "dbc.hpp" #include "gui.hpp" +#include "rand.hpp" using std::string; using namespace fmt; @@ -36,6 +37,10 @@ std::array VALUES{ sf::Color::Transparent, // white }; +sf::Color GUI::color(int val) { + return VALUES[size_t(val)]; +} + sf::Color GUI::color(Value val) { return VALUES[size_t(val)]; } @@ -128,6 +133,7 @@ void GUI::handle_events() { $game_map.clear_target($player.location); $player.move({x, y}); } else { + $shake_it = true; $hit_sound.play(); } @@ -139,6 +145,7 @@ void GUI::handle_events() { if($enemy.location.x == $player.location.x && $enemy.location.y == $player.location.y) { $player.event(EntityEvent::HIT); + $burn_baby_burn = true; } else if($goal.x == $player.location.x && $goal.y == $player.location.y) { $status_text = "YOU WIN!"; } @@ -146,16 +153,53 @@ void GUI::handle_events() { } } +void GUI::burn() { + for(int i = 0; i < 20; ++i) { + $text.setFillColor(color(i % VALUES.size())); + int x = Random::rand_int(-10,10); + int y = Random::rand_int(-10,10); + $text.setPosition({(float)x,(float)y}); + $window.draw($text); + $window.display(); + std::this_thread::sleep_for(2ms); + } + + $text.setFillColor(color(Value::LIGHT_DARK)); +} + +inline void draw_screen(sf::RenderWindow &window, sf::Text &text, float x=0, float y=0) { + text.setPosition({x,y}); + window.clear(); + window.draw(text); + window.display(); +} + +void GUI::shake() { + for(int i = 0; i < 10; ++i) { + int x = Random::rand_int(-10,10); + int y = Random::rand_int(-10,10); + draw_screen($window, $text, (float)x, (float)y); + std::this_thread::sleep_for(1ms); + } +} + void GUI::render_scene() { Render($screen, $document->Render()); std::string $screenout = $screen.ToString(); std::wstring utf8 = $converter.from_bytes($screenout); $text.setString(utf8); - $text.setPosition({0,0}); - $window.clear(); - $window.draw($text); - $window.display(); + if($shake_it) { + shake(); + $shake_it = false; + } + + if($burn_baby_burn) { + burn(); + $burn_baby_burn = false; + } + + draw_screen($window, $text, 0, 0); } int GUI::main() { diff --git a/gui.hpp b/gui.hpp index ef1c196..c440b96 100644 --- a/gui.hpp +++ b/gui.hpp @@ -30,7 +30,6 @@ enum class Value { }; class GUI { - sf::Color color(Value val); Map $game_map; sf::SoundBuffer $hit_buf; sf::Sound $hit_sound; @@ -44,6 +43,8 @@ class GUI { Canvas $canvas; sf::Font $font; sf::Text $text; + bool $shake_it = false; + bool $burn_baby_burn = false; std::wstring_convert> $converter; sf::RenderWindow $window; Screen $screen; @@ -53,9 +54,13 @@ public: // disable copying GUI(GUI &gui) = delete; + sf::Color color(Value val); + sf::Color color(int val); void create_renderer(); void render_scene(); void handle_events(); + void shake(); + void burn(); int main(); }; diff --git a/map.cpp b/map.cpp index 69821c5..90138ec 100644 --- a/map.cpp +++ b/map.cpp @@ -2,11 +2,9 @@ #include "dbc.hpp" #include #include -#include +#include "rand.hpp" #include -std::random_device $RNG; -std::mt19937 $GENERATOR($RNG()); using std::vector, std::pair; using namespace fmt; @@ -116,8 +114,8 @@ inline int make_split(Room &cur, bool horiz) { size_t dimension = horiz ? cur.height : cur.width; int min = dimension / 4; int max = dimension - min; - std::uniform_int_distribution rand_dim(min, max); - return rand_dim($GENERATOR); + + return Random::rand_int(min, max); } void Map::partition_map(Room &cur, int depth) { @@ -212,26 +210,25 @@ void Map::set_door(Room &room, int value) { } void rand_side(Room &room, Point &door) { - std::uniform_int_distribution rand_side(0, 3); - std::uniform_int_distribution rand_x(0, room.width - 1); - std::uniform_int_distribution rand_y(0, room.height - 1); + int rand_x = Random::rand_int(0, room.width - 1); + int rand_y = Random::rand_int(0, room.height - 1); - switch(rand_side($GENERATOR)) { + switch(Random::rand_int(0,3)) { case 0: // north - door.x = room.x + rand_x($GENERATOR); + door.x = room.x + rand_x; door.y = room.y-1; break; case 1: // south - door.x = room.x + rand_x($GENERATOR); + door.x = room.x + rand_x; door.y = room.y + room.height; break; case 2: // east door.x = room.x + room.width; - door.y = room.y + rand_y($GENERATOR); + door.y = room.y + rand_y; break; case 3: // west door.x = room.x - 1; - door.y = room.y + rand_y($GENERATOR); + door.y = room.y + rand_y; break; default: dbc::sentinel("impossible side"); diff --git a/meson.build b/meson.build index 8c56172..9055988 100644 --- a/meson.build +++ b/meson.build @@ -17,6 +17,7 @@ runtests = executable('runtests', [ 'dbc.cpp', 'map.cpp', 'entity.cpp', + 'rand.cpp', 'tests/fsm.cpp', 'tests/dbc.cpp', 'tests/map.cpp', @@ -28,6 +29,7 @@ roguish = executable('roguish', [ 'main.cpp', 'map.cpp', 'gui.cpp', + 'rand.cpp', 'entity.cpp', ], dependencies: dependencies)