From 2e8abbaf5e7c9b17379767037fae2bdfc9f9ac06 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 30 Sep 2024 00:04:58 -0400 Subject: [PATCH] Our hero can walk around the world and won't go through walls. --- main.cpp | 66 +++++++++++++++++++++++++++++++++++--------------------- map.cpp | 4 ++++ map.hpp | 4 ++++ 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/main.cpp b/main.cpp index ed3cdcd..9e698ea 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,6 @@ // Use of this source code is governed by the MIT license that can be found in // the LICENSE file. #include // for operator""s, chrono_literals -#include // for Full, Screen #include // for cout, ostream #include // for allocator, shared_ptr #include // for string, operator<< @@ -12,7 +11,9 @@ #include // for Render #include // for ftxui #include +#include #include +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include #include "map.hpp" @@ -30,34 +31,25 @@ int main() { Map game_map(50, 27); game_map.generate(); + Matrix &walls = game_map.walls(); + Room &start = game_map.room(0); + Point me = {.x=start.x+1, .y=start.y+1}; auto map = Renderer([&] { - Matrix &result = game_map.paths(); - Matrix &walls = game_map.walls(); - - for(size_t x = 0; x < result[0].size(); ++x) { - for(size_t y = 0; y < result.size(); ++y) { - auto path = result[y][x]; - if(path == 1000 || walls[y][x] == 1) { - // it's a wall or unreachable, use the wall_map - const string tile = walls[y][x] == 1 ? "#" : "."; - c.DrawText(x*2, y*4, tile, Color::GrayDark); - } else { - // it's a path number, show it - const string tile = format("{}", path); - auto color = Color::Palette16(path % 7 + 1); - c.DrawText(x*2, y*4, tile, color); - } + for(size_t x = 0; x < walls[0].size(); ++x) { + for(size_t y = 0; y < walls.size(); ++y) { + const string tile = walls[y][x] == 1 ? "#" : "."; + c.DrawText(x*2, y*4, tile, Color::GrayDark); } } - // draw the character - //c.DrawText(10*2, 10*4, "@", Color::Blue); + c.DrawText(me.x*2, me.y*4, "@", Color::Blue); return canvas(c); }); - auto document = hbox({ + auto document = Renderer([&]{ + return hbox({ hflow( vbox( gauge(0.5) | border, @@ -67,13 +59,37 @@ int main() { separator(), hbox(map->Render()), }) | border; + }); - auto screen = Screen::Create(Dimension::Full()); - Render(screen, document); + document |= CatchEvent([&](Event e) -> bool { + size_t x = me.x; + size_t y = me.y; - std::cout << reset_position; - screen.Print(); - reset_position = screen.ResetPosition(); + if(e == Event::ArrowLeft) { + x -= 1; + } else if(e == Event::ArrowRight) { + x += 1; + } else if(e == Event::ArrowDown) { + y += 1; + } else if(e == Event::ArrowUp) { + y -= 1; + } else { + return false; + } + + if(game_map.inmap(x,y) && !game_map.iswall(x,y)) { + me.x = x; + me.y = y; + } + return true; + }); + + auto screen = ScreenInteractive::Fullscreen(); + Loop loop(&screen, document); + + while(!loop.HasQuitted()) { + loop.RunOnceBlocking(); + } return 0; } diff --git a/map.cpp b/map.cpp index 6ca098e..048ac13 100644 --- a/map.cpp +++ b/map.cpp @@ -292,6 +292,10 @@ void Map::generate() { } } +bool Map::iswall(size_t x, size_t y) { + return m_walls[y][x] == WALL_VALUE; +} + void Map::dump() { dump_map("PATHS", m_paths); diff --git a/map.hpp b/map.hpp index df066d1..c791db2 100644 --- a/map.hpp +++ b/map.hpp @@ -59,11 +59,15 @@ public: int limit() { return m_limit; } size_t width() { return m_walls[0].size(); } size_t height() { return m_walls.size(); } + Room &room(size_t at) { + return m_rooms[at]; + } void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height); void add_door(Room &room); bool inmap(size_t x, size_t y); + bool iswall(size_t x, size_t y); bool neighbors(Point &out, bool up); void generate(); void place_rooms(Room &root);