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

This commit is contained in:
Zed A. Shaw 2024-10-29 23:39:03 -04:00
parent 04350cb51e
commit 2fdbd63f4c
4 changed files with 38 additions and 32 deletions

55
gui.cpp
View file

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