Cleaning up and sorting out how to use the new events best.
This commit is contained in:
parent
04350cb51e
commit
2fdbd63f4c
4 changed files with 38 additions and 32 deletions
55
gui.cpp
55
gui.cpp
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
6
gui.hpp
6
gui.hpp
|
@ -12,6 +12,7 @@
|
|||
#include <string>
|
||||
#include "map.hpp"
|
||||
#include "dinkyecs.hpp"
|
||||
#include "components.hpp"
|
||||
#include "sound.hpp"
|
||||
|
||||
using std::string;
|
||||
|
@ -54,6 +55,8 @@ class GUI {
|
|||
int $map_font_size;
|
||||
sf::Glyph $base_glyph;
|
||||
float $line_spacing;
|
||||
SoundManager $sounds;
|
||||
Components::ActionLog $log;
|
||||
|
||||
public:
|
||||
GUI();
|
||||
|
@ -64,7 +67,8 @@ public:
|
|||
sf::Color color(int val);
|
||||
void create_renderer();
|
||||
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 shake();
|
||||
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));
|
||||
|
||||
// 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;
|
||||
|
||||
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) {
|
||||
dbc::check($sounds.contains(name), format("sound {} is not loaded in map", name));
|
||||
// get the sound from the sound map
|
||||
SoundPair *pair = $sounds.at(name);
|
||||
auto pair = $sounds.at(name);
|
||||
// play it
|
||||
pair->sound.play();
|
||||
}
|
||||
|
||||
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.play();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <SFML/Audio.hpp>
|
||||
|
||||
|
@ -11,7 +12,7 @@ struct SoundPair {
|
|||
|
||||
struct SoundManager {
|
||||
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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue