More refactoring to get the GUI dumber.

This commit is contained in:
Zed A. Shaw 2024-10-30 02:13:31 -04:00
parent 2fdbd63f4c
commit 009b1e63a7
6 changed files with 86 additions and 75 deletions

59
gui.cpp
View file

@ -51,8 +51,7 @@ sf::Color GUI::color(Value val) {
return VALUES[size_t(val)];
}
GUI::GUI() :
$game_map(GAME_MAP_X, GAME_MAP_Y),
GUI::GUI(DinkyECS::World &world, Map& game_map) :
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
$screen(SCREEN_X, SCREEN_Y),
$map_screen(0,0),
@ -60,7 +59,9 @@ GUI::GUI() :
$map_font_size(BASE_MAP_FONT_SIZE),
$line_spacing(0),
$sounds("./assets"),
$log({{"Welcome to the game!"}})
$log({{"Welcome to the game!"}}),
$world(world),
$game_map(game_map)
{
// this needs a config file soon
$font.loadFromFile("./assets/text.otf");
@ -72,8 +73,6 @@ GUI::GUI() :
$ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setFillColor(color(Value::LIGHT_LIGHT));
$game_map.generate();
}
void GUI::create_renderer() {
@ -112,30 +111,28 @@ void GUI::create_renderer() {
void GUI::handle_world_events() {
auto player = $world.get_the<Player>();
while($world.has_event<GUIEvent>()) {
auto [evt, entity] = $world.recv<GUIEvent>();
while($world.has_event<Events::GUI>()) {
auto [evt, entity] = $world.recv<Events::GUI>();
switch(evt) {
case GUIEvent::HIT: {
case Events::GUI::HIT: {
auto combat = $world.get<Combat>(entity);
if(entity == player.entity) {
$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");
shake();
}
} break;
case GUIEvent::MISS:
case Events::GUI::MISS:
if(entity == player.entity) {
$log.log("You MISSED the enemy.");
} else {
$log.log("Enemy MISSED YOU.");
}
break;
case GUIEvent::DEAD:
case Events::GUI::DEAD:
$log.log("--- ENEMY DEAD!");
break;
default:
@ -305,43 +302,6 @@ void GUI::shake() {
}
}
void GUI::configure_world() {
// 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.");
// configure a player as a fact of the world
Player player{$world.entity()};
$world.set_the<Player>(player);
spatial_map collider;
$world.set_the<spatial_map>(collider);
$world.set<Position>(player.entity, {$game_map.place_entity(0)});
$world.set<Motion>(player.entity, {0, 0});
$world.set<Combat>(player.entity, {100, 10});
$world.set<Tile>(player.entity, {PLAYER_TILE});
auto enemy = $world.entity();
$world.set<Position>(enemy, {$game_map.place_entity(1)});
$world.set<Motion>(enemy, {0,0});
$world.set<Combat>(enemy, {20, 10});
$world.set<Tile>(enemy, {ENEMY_TILE});
auto enemy2 = $world.entity();
$world.set<Position>(enemy2, {$game_map.place_entity(2)});
$world.set<Motion>(enemy2, {0,0});
$world.set<Combat>(enemy2, {20, 10});
$world.set<Tile>(enemy2, {"*"});
auto gold = $world.entity();
$world.set<Position>(gold, {$game_map.place_entity($game_map.room_count() - 1)});
$world.set<Treasure>(gold, {100});
$world.set<Tile>(gold, {"$"});
System::init_positions($world);
}
void GUI::render_scene() {
$screen.Clear();
$map_screen.Clear();
@ -352,7 +312,6 @@ void GUI::render_scene() {
}
int GUI::main() {
configure_world();
create_renderer();
run_systems();