Cleaning up and sorting out how to use the new events best.

main
Zed A. Shaw 4 weeks ago
parent 04350cb51e
commit 2fdbd63f4c
  1. 55
      gui.cpp
  2. 6
      gui.hpp
  3. 6
      sound.cpp
  4. 3
      sound.hpp

@ -20,7 +20,6 @@
#include "dbc.hpp" #include "dbc.hpp"
#include "gui.hpp" #include "gui.hpp"
#include "rand.hpp" #include "rand.hpp"
#include "components.hpp"
#include "systems.hpp" #include "systems.hpp"
#include "collider.hpp" #include "collider.hpp"
#include "events.hpp" #include "events.hpp"
@ -59,11 +58,16 @@ GUI::GUI() :
$map_screen(0,0), $map_screen(0,0),
$view_port{0,0}, $view_port{0,0},
$map_font_size(BASE_MAP_FONT_SIZE), $map_font_size(BASE_MAP_FONT_SIZE),
$line_spacing(0) $line_spacing(0),
$sounds("./assets"),
$log({{"Welcome to the game!"}})
{ {
// this needs a config file soon
$font.loadFromFile("./assets/text.otf"); $font.loadFromFile("./assets/text.otf");
resize_map(BASE_MAP_FONT_SIZE); resize_map(BASE_MAP_FONT_SIZE);
$sounds.load("hit", "hit.wav");
$ui_text.setFont($font); $ui_text.setFont($font);
$ui_text.setPosition(0,0); $ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setCharacterSize(UI_FONT_SIZE);
@ -82,12 +86,10 @@ void GUI::create_renderer() {
$document = Renderer([&, player]{ $document = Renderer([&, player]{
const auto& player_combat = $world.get<Combat>(player.entity); const auto& player_combat = $world.get<Combat>(player.entity);
const auto& log = $world.get_the<ActionLog>();
$status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!";
std::vector<Element> log_list; std::vector<Element> log_list;
for(auto msg : log.messages) { for(auto msg : $log.messages) {
log_list.push_back(text(msg)); log_list.push_back(text(msg));
} }
auto log_box = vbox(log_list) | yflex_grow | border; auto log_box = vbox(log_list) | yflex_grow | border;
@ -107,12 +109,8 @@ void GUI::create_renderer() {
}); });
} }
bool GUI::handle_events() { void GUI::handle_world_events() {
sf::Event event;
bool event_happened = false;
auto& log = $world.get_the<ActionLog>();
auto player = $world.get_the<Player>(); auto player = $world.get_the<Player>();
auto sounds = $world.get_the<SoundManager>();
while($world.has_event<GUIEvent>()) { while($world.has_event<GUIEvent>()) {
auto [evt, entity] = $world.recv<GUIEvent>(); auto [evt, entity] = $world.recv<GUIEvent>();
@ -121,30 +119,36 @@ bool GUI::handle_events() {
auto combat = $world.get<Combat>(entity); auto combat = $world.get<Combat>(entity);
if(entity == player.entity) { if(entity == player.entity) {
log.log(format("Enemy HIT YOU, you have {} HP!", combat.hp)); $log.log(format("Enemy HIT YOU, you have {} HP!", combat.hp));
sounds.play("hit"); $sounds.play("hit");
shake(); shake();
} else { } else {
log.log(format("You HIT enemy, they have {} HP!", combat.hp)); $log.log(format("You HIT enemy, they have {} HP!", combat.hp));
sounds.play("hit"); $sounds.play("hit");
shake(); shake();
} }
} break; } break;
case GUIEvent::MISS: case GUIEvent::MISS:
if(entity == player.entity) { if(entity == player.entity) {
log.log("You MISSED the enemy."); $log.log("You MISSED the enemy.");
} else { } else {
log.log("Enemy MISSED YOU."); $log.log("Enemy MISSED YOU.");
} }
break; break;
case GUIEvent::DEAD: case GUIEvent::DEAD:
log.log("--- ENEMY DEAD!"); $log.log("--- ENEMY DEAD!");
break; break;
default: default:
log.log(format("INVALID EVENT! {},{}", evt, entity)); $log.log(format("INVALID EVENT! {},{}", evt, entity));
}
} }
} }
bool GUI::handle_ui_events() {
sf::Event event;
bool event_happened = false;
auto player = $world.get_the<Player>();
while($window.pollEvent(event)) { while($window.pollEvent(event)) {
if(event.type == sf::Event::Closed) { if(event.type == sf::Event::Closed) {
$window.close(); $window.close();
@ -293,8 +297,8 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) {
void GUI::shake() { void GUI::shake() {
for(int i = 0; i < 10; ++i) { for(int i = 0; i < 10; ++i) {
int x = Random::uniform<int>(-20,20); int x = Random::uniform<int>(-10,10);
int y = Random::uniform<int>(-20,20); int y = Random::uniform<int>(-10,10);
// add x/y back to draw screen // add x/y back to draw screen
draw_screen(true, x, y); draw_screen(true, x, y);
std::this_thread::sleep_for(1ms); std::this_thread::sleep_for(1ms);
@ -302,9 +306,7 @@ void GUI::shake() {
} }
void GUI::configure_world() { void GUI::configure_world() {
SoundManager sounds("./assets"); // this sets up the gui event system
sounds.load("hit", "hit.wav");
$world.set_the<SoundManager>(sounds);
$world.set_the<GUIEvent>(GUIEvent::START); $world.set_the<GUIEvent>(GUIEvent::START);
dbc::check($game_map.room_count() > 1, "not enough rooms in map."); dbc::check($game_map.room_count() > 1, "not enough rooms in map.");
@ -312,9 +314,6 @@ void GUI::configure_world() {
Player player{$world.entity()}; Player player{$world.entity()};
$world.set_the<Player>(player); $world.set_the<Player>(player);
ActionLog log{{"Welcome to the game!"}};
$world.set_the<ActionLog>(log);
spatial_map collider; spatial_map collider;
$world.set_the<spatial_map>(collider); $world.set_the<spatial_map>(collider);
@ -360,9 +359,11 @@ int GUI::main() {
while($window.isOpen()) { while($window.isOpen()) {
render_scene(); render_scene();
if(handle_events()) { if(handle_ui_events()) {
run_systems(); run_systems();
} }
handle_world_events();
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
} }

@ -12,6 +12,7 @@
#include <string> #include <string>
#include "map.hpp" #include "map.hpp"
#include "dinkyecs.hpp" #include "dinkyecs.hpp"
#include "components.hpp"
#include "sound.hpp" #include "sound.hpp"
using std::string; using std::string;
@ -54,6 +55,8 @@ class GUI {
int $map_font_size; int $map_font_size;
sf::Glyph $base_glyph; sf::Glyph $base_glyph;
float $line_spacing; float $line_spacing;
SoundManager $sounds;
Components::ActionLog $log;
public: public:
GUI(); GUI();
@ -64,7 +67,8 @@ public:
sf::Color color(int val); sf::Color color(int val);
void create_renderer(); void create_renderer();
void render_scene(); void render_scene();
bool handle_events(); bool handle_ui_events();
void handle_world_events();
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
void shake(); void shake();
void configure_world(); void configure_world();

@ -16,7 +16,7 @@ void SoundManager::load(const std::string name, const std::string sound_path) {
dbc::check(fs::exists(full_path), format("sound file {} does not exist", sound_path)); dbc::check(fs::exists(full_path), format("sound file {} does not exist", sound_path));
// create the buffer and keep in the buffer map // create the buffer and keep in the buffer map
SoundPair *pair = new SoundPair(); std::shared_ptr<SoundPair> pair = std::make_shared<SoundPair>();
$sounds[name] = pair; $sounds[name] = pair;
bool good = pair->buffer.loadFromFile(full_path.string()); bool good = pair->buffer.loadFromFile(full_path.string());
@ -31,13 +31,13 @@ void SoundManager::load(const std::string name, const std::string sound_path) {
void SoundManager::play(const std::string name) { void SoundManager::play(const std::string name) {
dbc::check($sounds.contains(name), format("sound {} is not loaded in map", name)); dbc::check($sounds.contains(name), format("sound {} is not loaded in map", name));
// get the sound from the sound map // get the sound from the sound map
SoundPair *pair = $sounds.at(name); auto pair = $sounds.at(name);
// play it // play it
pair->sound.play(); pair->sound.play();
} }
void SoundManager::playAt(const std::string name, float x, float y, float z) { void SoundManager::playAt(const std::string name, float x, float y, float z) {
SoundPair *pair = $sounds.at(name); auto pair = $sounds.at(name);
pair->sound.setPosition(x, y, z); pair->sound.setPosition(x, y, z);
pair->sound.play(); pair->sound.play();
} }

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <filesystem> #include <filesystem>
#include <memory>
#include <unordered_map> #include <unordered_map>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
@ -11,7 +12,7 @@ struct SoundPair {
struct SoundManager { struct SoundManager {
std::filesystem::path $base_path; std::filesystem::path $base_path;
std::unordered_map<std::string, SoundPair*> $sounds; std::unordered_map<std::string, std::shared_ptr<SoundPair> > $sounds;
SoundManager(std::string base_path); SoundManager(std::string base_path);

Loading…
Cancel
Save