diff --git a/components.hpp b/components.hpp index f5c6a3d..e650364 100644 --- a/components.hpp +++ b/components.hpp @@ -1,5 +1,6 @@ #pragma once #include "dinkyecs.hpp" +#include namespace Components { struct Player { @@ -27,4 +28,15 @@ namespace Components { struct Tile { std::string chr = "!"; }; + + struct ActionLog { + std::deque messages; + + void log(std::string msg) { + messages.push_front(msg); + if(messages.size() > 20) { + messages.pop_back(); + } + } + }; } diff --git a/gui.cpp b/gui.cpp index c279663..48c1922 100644 --- a/gui.cpp +++ b/gui.cpp @@ -83,12 +83,24 @@ void GUI::create_renderer() { $document = Renderer([&, player]{ const auto& player_combat = $world.component(player.entity); + const auto& log = $world.get(); + + $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; + + std::vector log_list; + for(auto msg : log.messages) { + log_list.push_back(text(msg)); + } + auto log_box = vbox(log_list) | yflex_grow | border; + return hbox({ hflow( vbox( - text(format("HP: {}", player_combat.hp)) | border, - text($status_text) | border - ) | xflex_grow + text(format("HP: {: >3}", player_combat.hp)) | border, + text($status_text) | border, + separator(), + log_box + ) | flex_grow ), separator(), hbox(), @@ -262,6 +274,9 @@ void GUI::configure_world() { Player player{$world.entity()}; $world.set(player); + ActionLog log{{"Welcome to the game!"}}; + $world.set(log); + $world.assign(player.entity, {$game_map.place_entity(0)}); $world.assign(player.entity, {0, 0}); $world.assign(player.entity, {100, 10}); @@ -286,6 +301,8 @@ void GUI::configure_world() { } void GUI::render_scene() { + $screen.Clear(); + $map_screen.Clear(); Render($map_screen, $map_view->Render()); Render($screen, $document->Render()); diff --git a/scratchpad/collider.cpp b/scratchpad/collider.cpp index 6d63ad4..60c48ae 100644 --- a/scratchpad/collider.cpp +++ b/scratchpad/collider.cpp @@ -58,6 +58,8 @@ class SpatialHashTable { std::unordered_map, PointHash> table; }; + + int main() { SpatialHashTable hashTable; Object obj1 = {{5, 5}}; diff --git a/status.txt b/status.txt index 6bec22e..0646a82 100644 --- a/status.txt +++ b/status.txt @@ -1,8 +1,8 @@ TODO: -* Figure out how to render color fonts from FTUI. -* Dynamically determine the font vs. screensize to get an exact FTXUI screen size. * Write a test that generates a ton of maps then confirms there's a path from one room to every other room? -* If the player is trapped in a room the enemy just travles through walls. -* Add FLECS. * Lua integration? +* Text is not actually cleared when rendered either in FTXUI or SFML. + +* Work on collision detection with either a coordinate map or morton codes. +* Bring back sounds, check out SoLoud. diff --git a/systems.cpp b/systems.cpp index d09fe87..ae317b6 100644 --- a/systems.cpp +++ b/systems.cpp @@ -1,6 +1,8 @@ #include "systems.hpp" #include #include +#include +#include "rand.hpp" using std::string; using namespace fmt; @@ -33,8 +35,9 @@ void System::motion(DinkyECS::World &world, Map &game_map) { }; motion = {0,0}; // clear it after getting it - // avoid colision, but could this be a different system? - if(game_map.inmap(move_to.x, move_to.y) && !game_map.iswall(move_to.x,move_to.y)) { + if(game_map.inmap(move_to.x, move_to.y) && + !game_map.iswall(move_to.x,move_to.y)) + { position.location = move_to; } } @@ -44,12 +47,22 @@ void System::motion(DinkyECS::World &world, Map &game_map) { void System::combat(DinkyECS::World &world, Player &player) { const auto& player_position = world.component(player.entity); + auto& player_combat = world.component(player.entity); + auto& log = world.get(); world.system([&](const auto &ent, auto &pos, auto &combat) { - if(ent != player.entity && pos.location.x == player_position.location.x && - pos.location.y == player_position.location.y) - { - // need to sort out combat here + if(ent != player.entity) { + int dx = std::abs(int(pos.location.x) - int(player_position.location.x)); + int dy = std::abs(int(pos.location.y) - int(player_position.location.y)); + + if(dx <= 1 && dy <= 1) { + if(player_combat.hp > -1) { + int dmg = Random::uniform(1, combat.damage); + player_combat.hp -= dmg; + + log.log(format("HIT! You took {} damage.", dmg)); + } + } } }); };