Now systems.cpp is disconnected from levelmanager. That leaves the GUIs and then to completely remove it and clean up the api.

master
Zed A. Shaw 10 months ago
parent 81e25f73bb
commit d5ff57e025
  1. 28
      gui/fsm.cpp
  2. 1
      gui/fsm.hpp
  3. 6
      gui/loot_ui.cpp
  4. 2
      gui/map_view.cpp
  5. 6
      gui/status_ui.cpp
  6. 2
      raycaster.cpp
  7. 103
      systems.cpp
  8. 45
      systems.hpp
  9. 2
      tests/map.cpp

@ -73,7 +73,7 @@ namespace gui {
void FSM::MOVING(Event ) { void FSM::MOVING(Event ) {
// this should be an optional that returns a point // this should be an optional that returns a point
if(auto move_to = $main_ui.play_move()) { if(auto move_to = $main_ui.play_move()) {
System::plan_motion(Game::current(), *move_to); System::plan_motion(*move_to);
run_systems(); run_systems();
$main_ui.dirty(); $main_ui.dirty();
state(State::IDLE); state(State::IDLE);
@ -84,7 +84,7 @@ namespace gui {
using enum Event; using enum Event;
switch(ev) { switch(ev) {
case TICK: { case TICK: {
System::combat(Game::current(), $temp_attack_id); System::combat($temp_attack_id);
run_systems(); run_systems();
state(State::IN_COMBAT); state(State::IN_COMBAT);
} break; } break;
@ -195,7 +195,7 @@ namespace gui {
auto gui_id = std::any_cast<guecs::Entity>(data); auto gui_id = std::any_cast<guecs::Entity>(data);
auto& slot_name = $status_ui.$gui.name_for(gui_id); auto& slot_name = $status_ui.$gui.name_for(gui_id);
if(System::use_item(Game::current(), slot_name)) { if(System::use_item(slot_name)) {
$status_ui.update(); $status_ui.update();
} }
} break; } break;
@ -207,7 +207,7 @@ namespace gui {
mouse_action({1 << guecs::ModBit::hover}); mouse_action({1 << guecs::ModBit::hover});
} break; } break;
case AIM_CLICK: case AIM_CLICK:
System::pickup(Game::current()); System::pickup();
break; break;
default: default:
break; // ignore everything else break; // ignore everything else
@ -362,9 +362,8 @@ namespace gui {
event(Event::LOOT_OPEN); event(Event::LOOT_OPEN);
break; break;
case KEY::Z: { case KEY::Z: {
auto& level = Game::current();
auto& player_pos = Game::player_position(); auto& player_pos = Game::player_position();
System::distribute_loot(level, {player_pos.aiming_at}); System::distribute_loot({player_pos.aiming_at});
} break; } break;
case KEY::X: case KEY::X:
event(Event::STAIRS_DOWN); event(Event::STAIRS_DOWN);
@ -422,14 +421,13 @@ namespace gui {
} }
void FSM::run_systems() { void FSM::run_systems() {
auto& level = Game::current(); System::generate_paths();
System::generate_paths(level); System::enemy_ai_initialize();
System::enemy_ai_initialize(level); System::enemy_pathing();
System::enemy_pathing(level); System::collision();
System::collision(level); System::motion();
System::motion(level); System::lighting();
System::lighting(level); System::death();
System::death(level);
} }
bool FSM::active() { bool FSM::active() {
@ -504,7 +502,7 @@ namespace gui {
event(Event::LOOT_OPEN); event(Event::LOOT_OPEN);
} break; } break;
case eGUI::HP_STATUS: case eGUI::HP_STATUS:
System::player_status(Game::current()); System::player_status();
break; break;
case eGUI::NEW_RITUAL: case eGUI::NEW_RITUAL:
$combat_ui.init(); $combat_ui.init();

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "constants.hpp" #include "constants.hpp"
#include "levelmanager.hpp"
#include "simplefsm.hpp" #include "simplefsm.hpp"
#include "gui/debug_ui.hpp" #include "gui/debug_ui.hpp"
#include "gui/main_ui.hpp" #include "gui/main_ui.hpp"

@ -128,7 +128,7 @@ namespace gui {
} }
void LootUI::drop_item(DinkyECS::Entity item_id) { void LootUI::drop_item(DinkyECS::Entity item_id) {
System::drop_item($level, item_id); System::drop_item(item_id);
update(); update();
} }
@ -137,14 +137,14 @@ namespace gui {
} }
bool LootUI::occupied(guecs::Entity slot) { bool LootUI::occupied(guecs::Entity slot) {
return System::inventory_occupied($level, $target, $gui.name_for(slot)); return System::inventory_occupied($target, $gui.name_for(slot));
} }
void LootUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) { void LootUI::swap(guecs::Entity gui_a, guecs::Entity gui_b) {
if(gui_a != gui_b) { if(gui_a != gui_b) {
auto& a_name = $gui.name_for(gui_a); auto& a_name = $gui.name_for(gui_a);
auto& b_name = $gui.name_for(gui_b); auto& b_name = $gui.name_for(gui_b);
System::inventory_swap($level, $target, a_name, b_name); System::inventory_swap($target, a_name, b_name);
} }
update(); update();

@ -51,7 +51,7 @@ namespace gui {
void MapViewUI::render(sf::RenderWindow &window, int compass_dir) { void MapViewUI::render(sf::RenderWindow &window, int compass_dir) {
$gui.render(window); $gui.render(window);
System::draw_map($level, $map_tiles, $entity_map); System::draw_map($map_tiles, $entity_map);
System::render_map($map_tiles, $entity_map, *$map_render, compass_dir, $player_display); System::render_map($map_tiles, $entity_map, *$map_render, compass_dir, $player_display);
$map_sprite.setTexture($map_render->getTexture(), true); $map_sprite.setTexture($map_render->getTexture(), true);
window.draw($map_sprite); window.draw($map_sprite);

@ -127,7 +127,7 @@ namespace gui {
} }
void StatusUI::drop_item(DinkyECS::Entity item_id) { void StatusUI::drop_item(DinkyECS::Entity item_id) {
System::drop_item($level, item_id); System::drop_item(item_id);
update(); update();
} }
@ -142,7 +142,7 @@ namespace gui {
if(gui_a != gui_b) { if(gui_a != gui_b) {
auto& a_name = $gui.name_for(gui_a); auto& a_name = $gui.name_for(gui_a);
auto& b_name = $gui.name_for(gui_b); auto& b_name = $gui.name_for(gui_b);
System::inventory_swap($level, $level.player, a_name, b_name); System::inventory_swap($level.player, a_name, b_name);
} }
update(); update();
@ -150,6 +150,6 @@ namespace gui {
bool StatusUI::occupied(guecs::Entity slot) { bool StatusUI::occupied(guecs::Entity slot) {
auto player = $level.world->get_the<components::Player>(); auto player = $level.world->get_the<components::Player>();
return System::inventory_occupied($level, player.entity, $gui.name_for(slot)); return System::inventory_occupied(player.entity, $gui.name_for(slot));
} }
} }

@ -196,7 +196,7 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) {
float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT; float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT;
shared_ptr<sf::Shader> effect = System::sprite_effect($level, rec.entity); shared_ptr<sf::Shader> effect = System::sprite_effect(rec.entity);
if(effect) { if(effect) {
apply_sprite_effect(effect, sprite_width, sprite_height); apply_sprite_effect(effect, sprite_width, sprite_height);

@ -17,6 +17,7 @@
#include "shaders.hpp" #include "shaders.hpp"
#include "inventory.hpp" #include "inventory.hpp"
#include "game_level.hpp" #include "game_level.hpp"
#include "levelmanager.hpp"
using std::string; using std::string;
using namespace fmt; using namespace fmt;
@ -32,10 +33,11 @@ void System::set_position(World& world, SpatialMap& collision, Entity entity, Po
collision.insert(pos.location, entity, has_collision); collision.insert(pos.location, entity, has_collision);
} }
void System::lighting(GameLevel &level) { void System::lighting() {
auto &light = *level.lights; auto& level = Game::current();
auto &world = *level.world; auto& light = *level.lights;
auto &map = *level.map; auto& world = *level.world;
auto& map = *level.map;
light.reset_light(); light.reset_light();
@ -55,16 +57,18 @@ void System::lighting(GameLevel &level) {
} }
void System::generate_paths(GameLevel &level) { void System::generate_paths() {
auto& level = Game::current();
const auto &player_pos = Game::player_position(); const auto &player_pos = Game::player_position();
level.map->set_target(player_pos.location); level.map->set_target(player_pos.location);
level.map->make_paths(); level.map->make_paths();
} }
void System::enemy_ai_initialize(GameLevel &level) { void System::enemy_ai_initialize() {
auto &world = *level.world; auto& level = Game::current();
auto &map = *level.map; auto& world = *level.world;
auto& map = *level.map;
world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) { world.query<Position, EnemyConfig>([&](const auto ent, auto& pos, auto& config) {
if(world.has<ai::EntityAI>(ent)) { if(world.has<ai::EntityAI>(ent)) {
@ -89,9 +93,10 @@ void System::enemy_ai_initialize(GameLevel &level) {
}); });
} }
void System::enemy_pathing(GameLevel &level) { void System::enemy_pathing() {
auto &world = *level.world; auto& level = Game::current();
auto &map = *level.map; auto& world = *level.world;
auto& map = *level.map;
const auto &player_pos = Game::player_position(); const auto &player_pos = Game::player_position();
world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) { world.query<Position, Motion>([&](auto ent, auto &position, auto &motion) {
@ -130,7 +135,8 @@ inline void move_entity(SpatialMap &collider, Map &game_map, Position &position,
position.location = move_to; position.location = move_to;
} }
void System::motion(GameLevel &level) { void System::motion() {
auto& level = Game::current();
level.world->query<Position, Motion>( level.world->query<Position, Motion>(
[&](auto ent, auto &position, auto &motion) { [&](auto ent, auto &position, auto &motion) {
// don't process entities that don't move // don't process entities that don't move
@ -140,7 +146,8 @@ void System::motion(GameLevel &level) {
}); });
} }
void System::distribute_loot(GameLevel &level, Position target_pos) { void System::distribute_loot(Position target_pos) {
auto& level = Game::current();
auto& world = *level.world; auto& world = *level.world;
auto& config = world.get_the<GameConfig>(); auto& config = world.get_the<GameConfig>();
int inventory_count = Random::uniform(0, 3); int inventory_count = Random::uniform(0, 3);
@ -164,8 +171,9 @@ void System::distribute_loot(GameLevel &level, Position target_pos) {
level.world->send<Events::GUI>(Events::GUI::ENTITY_SPAWN, loot_entity, {}); level.world->send<Events::GUI>(Events::GUI::ENTITY_SPAWN, loot_entity, {});
} }
void System::death(GameLevel &level) { void System::death() {
auto &world = *level.world; auto& level = Game::current();
auto& world = *level.world;
auto player = world.get_the<Player>(); auto player = world.get_the<Player>();
std::vector<Entity> dead_things; std::vector<Entity> dead_things;
@ -204,7 +212,7 @@ void System::death(GameLevel &level) {
level.collision->remove(pos.location, ent); level.collision->remove(pos.location, ent);
// distribute_loot is then responsible for putting something there // distribute_loot is then responsible for putting something there
System::distribute_loot(level, pos); System::distribute_loot(pos);
world.destroy(ent); world.destroy(ent);
} }
@ -221,9 +229,10 @@ inline void animate_entity(World &world, Entity entity) {
} }
} }
void System::combat(GameLevel &level, int attack_id) { void System::combat(int attack_id) {
auto &collider = *level.collision; auto& level = Game::current();
auto &world = *level.world; auto& collider = *level.collision;
auto& world = *level.world;
auto& the_belt = world.get_the<ritual::Belt>(); auto& the_belt = world.get_the<ritual::Belt>();
if(!the_belt.has(attack_id)) return; if(!the_belt.has(attack_id)) return;
@ -277,9 +286,10 @@ void System::combat(GameLevel &level, int attack_id) {
} }
void System::collision(GameLevel &level) { void System::collision() {
auto &collider = *level.collision; auto& level = Game::current();
auto &world = *level.world; auto& collider = *level.collision;
auto& world = *level.world;
const auto& player_pos = Game::player_position(); const auto& player_pos = Game::player_position();
// this is guaranteed to not return the given position // this is guaranteed to not return the given position
@ -309,7 +319,8 @@ void System::collision(GameLevel &level) {
* This isn't for destroying something, but just removing it * This isn't for destroying something, but just removing it
* from the world for say, putting into a container or inventory. * from the world for say, putting into a container or inventory.
*/ */
void System::remove_from_world(GameLevel &level, Entity entity) { void System::remove_from_world(Entity entity) {
auto& level = Game::current();
auto& item_pos = level.world->get<Position>(entity); auto& item_pos = level.world->get<Position>(entity);
level.collision->remove(item_pos.location, entity); level.collision->remove(item_pos.location, entity);
// if you don't do this you get the bug that you can pickup // if you don't do this you get the bug that you can pickup
@ -317,9 +328,10 @@ void System::remove_from_world(GameLevel &level, Entity entity) {
level.world->remove<Position>(entity); level.world->remove<Position>(entity);
} }
void System::pickup(GameLevel &level) { void System::pickup() {
auto &world = *level.world; auto& level = Game::current();
auto &collision = *level.collision; auto& world = *level.world;
auto& collision = *level.collision;
auto pos = Game::player_position(); auto pos = Game::player_position();
if(!collision.something_there(pos.aiming_at)) return; if(!collision.something_there(pos.aiming_at)) return;
@ -337,7 +349,7 @@ void System::pickup(GameLevel &level) {
// use spatial find to find an item with inventory... // use spatial find to find an item with inventory...
if(world.has<InventoryItem>(entity)) { if(world.has<InventoryItem>(entity)) {
// NOTE: this might need to be a separate system so that people can leave stuff alone // NOTE: this might need to be a separate system so that people can leave stuff alone
remove_from_world(level, entity); remove_from_world(entity);
if(world.has<ritual::JunkPile>(entity)) { if(world.has<ritual::JunkPile>(entity)) {
auto& pile = world.get<ritual::JunkPile>(entity); auto& pile = world.get<ritual::JunkPile>(entity);
@ -381,7 +393,8 @@ void System::device(World &world, Entity actor, Entity item) {
} }
} }
void System::plan_motion(GameLevel& level, Position move_to) { void System::plan_motion(Position move_to) {
auto& level = Game::current();
auto& player_pos = Game::player_position(); auto& player_pos = Game::player_position();
player_pos.aiming_at = move_to.aiming_at; player_pos.aiming_at = move_to.aiming_at;
@ -392,7 +405,8 @@ void System::plan_motion(GameLevel& level, Position move_to) {
} }
void System::player_status(GameLevel &level) { void System::player_status() {
auto& level = Game::current();
auto& combat = level.world->get<Combat>(level.player); auto& combat = level.world->get<Combat>(level.player);
float percent = float(combat.hp) / float(combat.max_hp); float percent = float(combat.hp) / float(combat.max_hp);
@ -409,15 +423,16 @@ void System::player_status(GameLevel &level) {
} }
} }
std::shared_ptr<sf::Shader> System::sprite_effect(GameLevel &level, Entity entity) { std::shared_ptr<sf::Shader> System::sprite_effect(Entity entity) {
if(level.world->has<SpriteEffect>(entity)) { auto world = Game::current_world();
auto& se = level.world->get<SpriteEffect>(entity); if(world->has<SpriteEffect>(entity)) {
auto& se = world->get<SpriteEffect>(entity);
if(se.frames > 0) { if(se.frames > 0) {
se.frames--; se.frames--;
return se.effect; return se.effect;
} else { } else {
level.world->remove<SpriteEffect>(entity); world->remove<SpriteEffect>(entity);
return nullptr; return nullptr;
} }
} else { } else {
@ -435,11 +450,11 @@ Entity System::spawn_item(World& world, const std::string& name) {
return item_id; return item_id;
} }
void System::drop_item(GameLevel& level, Entity item) { void System::drop_item(Entity item) {
auto& level = Game::current();
auto& world = *level.world; auto& world = *level.world;
auto& map = *level.map; auto& map = *level.map;
auto player_pos = Game::player_position();
auto player_pos = world.get<Position>(level.player);
dbc::check(map.can_move(player_pos.location), "impossible, the player can't be in a wall"); dbc::check(map.can_move(player_pos.location), "impossible, the player can't be in a wall");
@ -481,7 +496,8 @@ void System::remove_from_container(World& world, Entity cont_id, const std::stri
} }
void System::inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name) { void System::inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name) {
auto& level = Game::current();
dbc::check(a_name != b_name, "Attempt to inventory swap the same slot, you should check this and avoid calling me."); dbc::check(a_name != b_name, "Attempt to inventory swap the same slot, you should check this and avoid calling me.");
auto& inventory = level.world->get<inventory::Model>(container_id); auto& inventory = level.world->get<inventory::Model>(container_id);
@ -491,14 +507,16 @@ void System::inventory_swap(GameLevel &level, Entity container_id, const std::st
inventory.swap(a_ent, b_ent); inventory.swap(a_ent, b_ent);
} }
bool System::inventory_occupied(GameLevel& level, Entity container_id, const std::string& name) { bool System::inventory_occupied(Entity container_id, const std::string& name) {
auto& inventory = level.world->get<inventory::Model>(container_id); auto world = Game::current_world();
auto& inventory = world->get<inventory::Model>(container_id);
return inventory.has(name); return inventory.has(name);
} }
void System::draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map) { void System::draw_map(Matrix& grid, EntityGrid& entity_map) {
World &world = *level.world; auto& level = Game::current();
auto& world = *level.world;
Map &map = *level.map; Map &map = *level.map;
Matrix &fow = level.lights->$fow; Matrix &fow = level.lights->$fow;
size_t view_x = matrix::width(grid) - 1; size_t view_x = matrix::width(grid) - 1;
@ -586,7 +604,8 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture
render.display(); render.display();
} }
bool System::use_item(GameLevel& level, const string& slot_name) { bool System::use_item(const string& slot_name) {
auto& level = Game::current();
auto& world = *level.world; auto& world = *level.world;
auto& inventory = world.get<inventory::Model>(level.player); auto& inventory = world.get<inventory::Model>(level.player);
auto& player_combat = world.get<Combat>(level.player); auto& player_combat = world.get<Combat>(level.player);

@ -1,45 +1,46 @@
#pragma once #pragma once
#include "components.hpp" #include "components.hpp"
#include "levelmanager.hpp"
#include <SFML/Graphics/RenderTexture.hpp> #include <SFML/Graphics/RenderTexture.hpp>
#include "map.hpp"
#include "spatialmap.hpp"
namespace System { namespace System {
using namespace components; using namespace components;
using namespace DinkyECS; using namespace DinkyECS;
using std::string; using std::string, matrix::Matrix;
void lighting(GameLevel &level); void lighting();
void motion(GameLevel &level); void motion();
void collision(GameLevel &level); void collision();
void death(GameLevel &level); void death();
void generate_paths(GameLevel &level); void generate_paths();
void enemy_pathing(GameLevel &level); void enemy_pathing();
void enemy_ai_initialize(GameLevel &level); void enemy_ai_initialize();
void device(World &world, Entity actor, Entity item); void device(World &world, Entity actor, Entity item);
void plan_motion(GameLevel& level, Position move_to); void plan_motion(Position move_to);
Entity spawn_item(World& world, const string& name); Entity spawn_item(World& world, const string& name);
void drop_item(GameLevel& level, Entity item); void drop_item(Entity item);
void enemy_ai(GameLevel &level); void enemy_ai();
void combat(GameLevel& level, int attack_id); void combat(int attack_id);
std::shared_ptr<sf::Shader> sprite_effect(GameLevel &level, Entity entity); std::shared_ptr<sf::Shader> sprite_effect(Entity entity);
void player_status(GameLevel &level); void player_status();
void distribute_loot(GameLevel &level, Position target_pos); void distribute_loot(Position target_pos);
void pickup(GameLevel &level); void pickup();
bool place_in_container(World& world, Entity cont_id, const string& name, Entity world_entity); bool place_in_container(World& world, Entity cont_id, const string& name, Entity world_entity);
void remove_from_container(World& world, Entity cont_id, const std::string& name); void remove_from_container(World& world, Entity cont_id, const std::string& name);
void remove_from_world(GameLevel &level, Entity entity); void remove_from_world(Entity entity);
void inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name); void inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name);
bool inventory_occupied(GameLevel& level, Entity container_id, const std::string& name); bool inventory_occupied(Entity container_id, const std::string& name);
void draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map); void draw_map(Matrix& grid, EntityGrid& entity_map);
void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display); void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display);
void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos); void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos);
bool use_item(GameLevel& level, const std::string& slot_name); bool use_item(const std::string& slot_name);
} }

@ -99,7 +99,7 @@ TEST_CASE("map image test", "[map]") {
for(matrix::each_row it{level.map->walls()}; it.next();) { for(matrix::each_row it{level.map->walls()}; it.next();) {
player_pos.location.x = it.x; player_pos.location.x = it.x;
player_pos.location.y = it.y; player_pos.location.y = it.y;
System::draw_map(level, map_tiles, entity_map); System::draw_map(map_tiles, entity_map);
System::render_map(map_tiles, entity_map, *render, 2, player_display); System::render_map(map_tiles, entity_map, *render, 2, player_display);
#ifdef TEST_RENDER #ifdef TEST_RENDER