diff --git a/combat.cpp b/combat.cpp index 6333a1a..6b4fea2 100644 --- a/combat.cpp +++ b/combat.cpp @@ -1,7 +1,7 @@ #include "combat.hpp" #include "rand.hpp" -int Combat::fight(Combat &target) { +int Combat::attack(Combat &target) { int attack = Random::uniform(0,1); int my_dmg = 0; diff --git a/combat.hpp b/combat.hpp index ef30add..a5ffc30 100644 --- a/combat.hpp +++ b/combat.hpp @@ -7,5 +7,5 @@ struct Combat { int damage; bool dead; - int fight(Combat &target); + int attack(Combat &target); }; diff --git a/dinkyecs.hpp b/dinkyecs.hpp index f5630e5..70e2d37 100644 --- a/dinkyecs.hpp +++ b/dinkyecs.hpp @@ -33,25 +33,25 @@ namespace DinkyECS { } template - void set(Comp val) { + void set_the(Comp val) { $facts[std::type_index(typeid(Comp))] = val; } template - Comp &get() { + Comp &get_the() { // use .at to get std::out_of_range if fact not set std::any &res = $facts.at(std::type_index(typeid(Comp))); return std::any_cast(res); } template - void assign(Entity ent, Comp val) { + void set(Entity ent, Comp val) { EntityMap &map = entity_map_for(); map[ent] = val; } template - Comp &component(Entity ent) { + Comp &get(Entity ent) { EntityMap &map = entity_map_for(); // use .at for bounds checking std::any &res = map.at(ent); @@ -59,7 +59,7 @@ namespace DinkyECS { } template - void system(std::function cb) { + void query(std::function cb) { EntityMap &map = entity_map_for(); for(auto& [entity, any_comp] : map) { Comp &res = std::any_cast(any_comp); @@ -68,31 +68,17 @@ namespace DinkyECS { } template - void system(std::function cb) { + void query(std::function cb) { EntityMap &map_a = entity_map_for(); EntityMap &map_b = entity_map_for(); for(auto& [entity, any_a] : map_a) { if(map_b.contains(entity)) { CompA &res_a = std::any_cast(any_a); - CompB &res_b = component(entity); + CompB &res_b = get(entity); cb(entity, res_a, res_b); } } } - - template - std::function runner(std::function cb) { - return [&]{ - system(cb); - }; - } - - template - std::function runner(std::function cb) { - return [&]{ - system(cb); - }; - } }; } diff --git a/gui.cpp b/gui.cpp index 0f44784..b471e32 100644 --- a/gui.cpp +++ b/gui.cpp @@ -72,7 +72,7 @@ GUI::GUI() : } void GUI::create_renderer() { - auto player = $world.get(); + auto player = $world.get_the(); $map_view = Renderer([&] { System::draw_map($world, $game_map, $canvas, $view_port.x, $view_port.y); @@ -80,8 +80,8 @@ void GUI::create_renderer() { }); $document = Renderer([&, player]{ - const auto& player_combat = $world.component(player.entity); - const auto& log = $world.get(); + const auto& player_combat = $world.get(player.entity); + const auto& log = $world.get_the(); $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; @@ -114,8 +114,8 @@ bool GUI::handle_events() { if(event.type == sf::Event::Closed) { $window.close(); } else if(event.type == sf::Event::KeyPressed) { - auto player = $world.get(); - auto& player_motion = $world.component(player.entity); + auto player = $world.get_the(); + auto& player_motion = $world.get(player.entity); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { player_motion.dx = -1; @@ -157,7 +157,7 @@ sf::Sprite &GUI::get_text_sprite(wchar_t tile) { } void GUI::run_systems() { - auto player = $world.get(); + auto player = $world.get_the(); System::enemy_pathing($world, $game_map, player); System::motion($world, $game_map); System::combat($world, player); @@ -268,38 +268,42 @@ void GUI::shake() { } void GUI::configure_world() { + SoundManager sounds("./assets"); + sounds.load("hit", "hit.wav"); + $world.set_the(sounds); + 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(player); + $world.set_the(player); ActionLog log{{"Welcome to the game!"}}; - $world.set(log); + $world.set_the(log); spatial_map collider; - $world.set(collider); + $world.set_the(collider); - $world.assign(player.entity, {$game_map.place_entity(0)}); - $world.assign(player.entity, {0, 0}); - $world.assign(player.entity, {100, 10}); - $world.assign(player.entity, {PLAYER_TILE}); + $world.set(player.entity, {$game_map.place_entity(0)}); + $world.set(player.entity, {0, 0}); + $world.set(player.entity, {100, 10}); + $world.set(player.entity, {PLAYER_TILE}); auto enemy = $world.entity(); - $world.assign(enemy, {$game_map.place_entity(1)}); - $world.assign(enemy, {0,0}); - $world.assign(enemy, {20, 10}); - $world.assign(enemy, {ENEMY_TILE}); + $world.set(enemy, {$game_map.place_entity(1)}); + $world.set(enemy, {0,0}); + $world.set(enemy, {20, 10}); + $world.set(enemy, {ENEMY_TILE}); auto enemy2 = $world.entity(); - $world.assign(enemy2, {$game_map.place_entity(2)}); - $world.assign(enemy2, {0,0}); - $world.assign(enemy2, {20, 10}); - $world.assign(enemy2, {"*"}); + $world.set(enemy2, {$game_map.place_entity(2)}); + $world.set(enemy2, {0,0}); + $world.set(enemy2, {20, 10}); + $world.set(enemy2, {"*"}); auto gold = $world.entity(); - $world.assign(gold, {$game_map.place_entity($game_map.room_count() - 1)}); - $world.assign(gold, {100}); - $world.assign(gold, {"$"}); + $world.set(gold, {$game_map.place_entity($game_map.room_count() - 1)}); + $world.set(gold, {100}); + $world.set(gold, {"$"}); System::init_positions($world); } @@ -314,10 +318,6 @@ void GUI::render_scene() { } int GUI::main() { - SoundManager sounds("./assets"); - sounds.load("hit", "hit.wav"); - $world.set(sounds); - configure_world(); create_renderer(); run_systems(); diff --git a/scratchpad/myecstest.cpp b/scratchpad/myecstest.cpp index 53162f3..3176e4a 100644 --- a/scratchpad/myecstest.cpp +++ b/scratchpad/myecstest.cpp @@ -31,28 +31,28 @@ void configure(DinkyECS::World &world, Entity &test) { println("---Configuring the base system."); Entity test2 = world.entity(); - world.assign(test, {10,20}); - world.assign(test, {1,2}); + world.set(test, {10,20}); + world.set(test, {1,2}); - world.assign(test2, {1,1}); - world.assign(test2, {10,20}); + world.set(test2, {1,1}); + world.set(test2, {10,20}); println("---- Setting up the player as a fact in the system."); auto player_eid = world.entity(); Player player_info{"Zed", player_eid}; // just set some player info as a fact with the entity id - world.set(player_info); + world.set_the(player_info); - world.assign(player_eid, {0,0}); - world.assign(player_eid, {0,0}); + world.set(player_eid, {0,0}); + world.set(player_eid, {0,0}); auto enemy = world.entity(); - world.assign(enemy, {0,0}); - world.assign(enemy, {0,0}); + world.set(enemy, {0,0}); + world.set(enemy, {0,0}); println("--- Creating facts (singletons)"); - world.set({0.9}); + world.set_the({0.9}); } int main() { @@ -61,31 +61,31 @@ int main() { configure(world, test); - Position &pos = world.component(test); + Position &pos = world.get(test); println("GOT POS x={}, y={}", pos.x, pos.y); - Velocity &vel = world.component(test); + Velocity &vel = world.get(test); println("GOT VELOCITY x={}, y={}", vel.x, vel.y); println("--- Position only system:"); - world.system([](const auto &ent, auto &pos) { + world.query([](const auto &ent, auto &pos) { println("entity={}, pos.x={}, pos.y={}", ent, pos.x, pos.y); }); println("--- Velocity only system:"); - world.system([](const auto &, auto &vel) { + world.query([](const auto &, auto &vel) { println("vel.x={}, vel.y={}", vel.x, vel.y); }); println("--- Manually get the velocity in position system:"); - world.system([&](const auto &ent, auto &pos) { - Velocity &vel = world.component(ent); + world.query([&](const auto &ent, auto &pos) { + Velocity &vel = world.get(ent); println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y); }); println("--- Query only entities with Position and Velocity:"); - world.system([&](const auto &ent, auto &pos, auto &vel) { - Gravity &grav = world.get(); + world.query([&](const auto &ent, auto &pos, auto &vel) { + Gravity &grav = world.get_the(); println("grav={}, entity={}, vel.x, vel.y, pos.x={}, pos.y={}", grav.level, ent, vel.x, vel.y, pos.x, pos.y); }); @@ -93,25 +93,15 @@ int main() { world.remove(test); println("--- After remove test, should only result in test2:"); - world.system([&](const auto &ent, auto &pos, auto &vel) { + world.query([&](const auto &ent, auto &pos, auto &vel) { println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y); }); - println("--- Create a stored system you can save for later."); - auto movementSystem = world.runner([&](const auto &ent, auto &pos, auto &vel) { - println("entity={}, vel.x, vel.y, pos.x={}, pos.y={}", ent, vel.x, vel.y, pos.x, pos.y); - }); - - movementSystem(); - - // how to create an identified entity like the player - - // to avoid repeatedly getting the player just make a closure with it // QUESTION: could I just capture it and not have the double function wrapping? auto playerVsEnemies = [&]() { - auto& player = world.get(); // grabbed it - world.system([&](const auto &ent, auto &pos) { + auto& player = world.get_the(); // grabbed it + world.query([&](const auto &ent, auto &pos) { if(player.eid != ent) { println("{} is enemy attacking player {}", ent, player.name); } else { diff --git a/sound.cpp b/sound.cpp index cf4531a..fe4e029 100644 --- a/sound.cpp +++ b/sound.cpp @@ -24,7 +24,8 @@ void SoundManager::load(const std::string name, const std::string sound_path) { // set it on the sound and keep in the sound map pair->sound.setBuffer(pair->buffer); - pair->sound.setRelativeToListener(true); + pair->sound.setRelativeToListener(false); + pair->sound.setPosition(0.0f, 0.0f, 1.0f); } void SoundManager::play(const std::string name) { diff --git a/systems.cpp b/systems.cpp index 303fe3d..234505c 100644 --- a/systems.cpp +++ b/systems.cpp @@ -14,11 +14,11 @@ using namespace Components; #define HEARING_DISTANCE 8 void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) { - const auto &player_position = world.component(player.entity); + const auto &player_position = world.get(player.entity); game_map.set_target(player_position.location); game_map.make_paths(); - world.system([&](const auto &ent, auto &position, auto &motion) { + world.query([&](const auto &ent, auto &position, auto &motion) { if(ent != player.entity) { Point out = position.location; // copy if(game_map.distance(out) < HEARING_DISTANCE) { @@ -31,9 +31,9 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player } void System::init_positions(DinkyECS::World &world) { - auto &collider = world.get(); + auto &collider = world.get_the(); - world.system([&](const auto &ent, auto &pos) { + world.query([&](const auto &ent, auto &pos) { collider.insert(pos.location, ent); }); } @@ -55,13 +55,13 @@ inline void move_entity(spatial_map &collider, Map &game_map, Position &position } void System::motion(DinkyECS::World &world, Map &game_map) { - auto &collider = world.get(); - auto &player = world.get(); - auto &player_position = world.component(player.entity); - auto &player_motion = world.component(player.entity); + auto &collider = world.get_the(); + auto &player = world.get_the(); + auto &player_position = world.get(player.entity); + auto &player_motion = world.get(player.entity); move_entity(collider, game_map, player_position, player_motion, player.entity); - world.system([&](const auto &ent, auto &position, auto &motion) { + world.query([&](const auto &ent, auto &position, auto &motion) { // don't process entities that don't move if(motion.dx != 0 || motion.dy != 0) { move_entity(collider, game_map, position, motion, ent); @@ -70,9 +70,9 @@ void System::motion(DinkyECS::World &world, Map &game_map) { } void System::death(DinkyECS::World &world) { - auto &collider = world.get(); + auto &collider = world.get_the(); - world.system([&](const auto &ent, auto &position, auto &combat) { + world.query([&](const auto &ent, auto &position, auto &combat) { // bring out yer dead if(combat.hp <= 0 && !combat.dead) { combat.dead = true; @@ -87,19 +87,19 @@ void System::death(DinkyECS::World &world) { void System::combat(DinkyECS::World &world, Player &player) { - auto& collider = world.get(); - const auto& player_position = world.component(player.entity); - auto& player_combat = world.component(player.entity); - auto& log = world.get(); - auto& sounds = world.get(); + auto& collider = world.get_the(); + const auto& player_position = world.get(player.entity); + auto& player_combat = world.get(player.entity); + auto& log = world.get_the(); + auto& sounds = world.get_the(); // this is guaranteed to not return the given position auto [found, nearby] = collider.neighbors(player_position.location); if(found) { for(auto entity : nearby) { - auto& enemy_combat = world.component(entity); - int player_dmg = player_combat.fight(enemy_combat); + auto& enemy_combat = world.get(entity); + int player_dmg = player_combat.attack(enemy_combat); if(player_dmg > 0) { log.log(format("You HIT for {} damage, HP left {}.", player_dmg, enemy_combat.hp)); @@ -109,7 +109,7 @@ void System::combat(DinkyECS::World &world, Player &player) { } if(enemy_combat.hp > 0) { - int enemy_dmg = enemy_combat.fight(player_combat); + int enemy_dmg = enemy_combat.attack(player_combat); if(enemy_dmg > 0) { sounds.play("hit"); @@ -125,7 +125,7 @@ void System::combat(DinkyECS::World &world, Player &player) { }; void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) { - world.system([&](const auto &ent, auto &pos, auto &tile) { + world.query([&](const auto &ent, auto &pos, auto &tile) { if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x && pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) { Point loc = game_map.map_to_camera(pos.location, cam_orig); @@ -136,8 +136,8 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas } void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) { - const auto& player = world.get(); - const auto& player_position = world.component(player.entity); + const auto& player = world.get_the(); + const auto& player_position = world.get(player.entity); Point start = game_map.center_camera(player_position.location, view_x, view_y); Matrix &walls = game_map.walls();