More refactoring to get the GUI dumber.

main
Zed A. Shaw 3 weeks ago
parent 2fdbd63f4c
commit 009b1e63a7
  1. 11
      components.hpp
  2. 8
      events.hpp
  3. 59
      gui.cpp
  4. 20
      gui.hpp
  5. 53
      main.cpp
  6. 10
      systems.cpp

@ -25,15 +25,4 @@ namespace Components {
struct Tile {
std::string chr = "!";
};
struct ActionLog {
std::deque<std::string> messages;
void log(std::string msg) {
messages.push_front(msg);
if(messages.size() > 20) {
messages.pop_back();
}
}
};
}

@ -1,5 +1,7 @@
#pragma once
enum GUIEvent {
START, HIT, MISS, DEAD
};
namespace Events {
enum GUI {
START, HIT, MISS, DEAD
};
}

@ -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();

@ -36,8 +36,18 @@ enum class Value {
LIGHT_LIGHT, WHITE, TRANSPARENT
};
struct ActionLog {
std::deque<std::string> messages;
void log(std::string msg) {
messages.push_front(msg);
if(messages.size() > 20) {
messages.pop_back();
}
}
};
class GUI {
Map $game_map;
string $status_text = "NOT DEAD";
Component $document;
Component $map_view;
@ -48,7 +58,6 @@ class GUI {
sf::RenderWindow $window;
Screen $screen;
Screen $map_screen;
DinkyECS::World $world;
sf::Texture $font_texture;
std::unordered_map<wchar_t, sf::Sprite> $sprites;
Point $view_port;
@ -56,10 +65,12 @@ class GUI {
sf::Glyph $base_glyph;
float $line_spacing;
SoundManager $sounds;
Components::ActionLog $log;
ActionLog $log;
DinkyECS::World& $world;
Map& $game_map;
public:
GUI();
GUI(DinkyECS::World& world, Map& game_map);
// disable copying
GUI(GUI &gui) = delete;
@ -71,7 +82,6 @@ public:
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();
void run_systems();
void resize_map(int new_size);
sf::Sprite &get_text_sprite(wchar_t tile);

@ -1,6 +1,57 @@
#include "gui.hpp"
#include "dinkyecs.hpp"
#include "systems.hpp"
#include "events.hpp"
#include "components.hpp"
#include "dbc.hpp"
#include "collider.hpp"
/*
* This needs to be turned into a real world generator
* system.
*/
void configure_world(DinkyECS::World &world, Map &game_map) {
// this sets up the gui event system
world.set_the<Events::GUI>(Events::GUI::START);
// 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, {"$"});
}
int main() {
GUI gui;
DinkyECS::World world;
Map game_map(GAME_MAP_X, GAME_MAP_Y);
game_map.generate();
configure_world(world, game_map);
System::init_positions(world);
GUI gui(world, game_map);
return gui.main();
}

@ -98,21 +98,21 @@ void System::combat(DinkyECS::World &world, Player &player) {
int player_dmg = player_combat.attack(enemy_combat);
if(player_dmg > 0) {
world.send<GUIEvent>(GUIEvent::HIT, entity);
world.send<Events::GUI>(Events::GUI::HIT, entity);
} else {
world.send<GUIEvent>(GUIEvent::MISS, entity);
world.send<Events::GUI>(Events::GUI::MISS, entity);
}
if(enemy_combat.hp > 0) {
int enemy_dmg = enemy_combat.attack(player_combat);
if(enemy_dmg > 0) {
world.send<GUIEvent>(GUIEvent::HIT, player.entity);
world.send<Events::GUI>(Events::GUI::HIT, player.entity);
} else {
world.send<GUIEvent>(GUIEvent::MISS, player.entity);
world.send<Events::GUI>(Events::GUI::MISS, player.entity);
}
} else {
world.send<GUIEvent>(GUIEvent::DEAD, entity);
world.send<Events::GUI>(Events::GUI::DEAD, entity);
}
}
}

Loading…
Cancel
Save