From ae46fdf51fe7ac3d71b137b909fac81083c8b5aa Mon Sep 17 00:00:00 2001 From: Timothy O'Barr Date: Fri, 21 Aug 2026 15:52:12 -0500 Subject: [PATCH] Wip: Working on Room Movement --- Engine/headers/CommandParser.h | 2 +- Engine/headers/game.h | 9 +++--- Engine/headers/room.h | 17 ++++++++-- Engine/src/CommandParser.cpp | 21 +++++++++--- Engine/src/game.cpp | 58 ++++++++++++++++++++++++++-------- Engine/src/room.cpp | 2 +- 6 files changed, 83 insertions(+), 26 deletions(-) diff --git a/Engine/headers/CommandParser.h b/Engine/headers/CommandParser.h index aa24a8d..01de049 100644 --- a/Engine/headers/CommandParser.h +++ b/Engine/headers/CommandParser.h @@ -3,7 +3,7 @@ #include #include -typedef int (*FUNCTION)(std::string s); +typedef int (*FUNCTION)(const std::string &s); static std::vector Verbs{"Quit", "Exit", "Go", "Move", "Look"}; static std::vector Nouns{"Object", "Door", "North", "South", "East", diff --git a/Engine/headers/game.h b/Engine/headers/game.h index b12c941..df62a90 100644 --- a/Engine/headers/game.h +++ b/Engine/headers/game.h @@ -12,6 +12,7 @@ static int curX, curY; extern bool isActive; +extern std::vector> rooms; #define MAP_HEIGHT 5 #define MAP_WIDTH 5 @@ -19,7 +20,6 @@ extern bool isActive; namespace fs = std::filesystem; class Game { private: - std::vector> rooms; Player player; int progress; CommandParser parser; @@ -39,6 +39,7 @@ public: void Run(); bool Save(); + void ChangeRoom(Room r); Room GetCurrentRoom() { return currentRoom; } static void SetIsActive(const bool active) { isActive = active; } @@ -53,6 +54,6 @@ public: }; // Commands -static int Quit(std::string n); -static int Move(std::string c); -static int Look(std::string c); +static int Quit(const std::string &n); +static int Move(const std::string &c); +static int Look(const std::string &c); diff --git a/Engine/headers/room.h b/Engine/headers/room.h index 18f4566..165bd2d 100644 --- a/Engine/headers/room.h +++ b/Engine/headers/room.h @@ -18,8 +18,8 @@ struct Exit { friend void to_json(json &j, const Exit &e); }; -static std::map directions{{"N", N}, {"S", S}, {"E", E}, {"W", W}}; -static std::map directionChar{{N, "N"}, {S, "S"}, {E, "E"}, {W, "W"}}; +static std::map directions{{"N", N}, {"S", S}, {"E", E}, {"W", W}}; +static std::map directionChar{{N, "N"}, {S, "S"}, {E, "E"}, {W, "W"}}; class Room { private: @@ -39,6 +39,19 @@ public: bool ValidDirection(Direction d); + inline bool operator==(const Room &rhs) const { + if (id == rhs.id) { + return true; + } + return false; + } + inline bool operator!=(const Room &rhs) const { + if (id != rhs.id) { + return true; + } + return false; + } + ~Room(); friend void to_json(nlohmann::json &j, const Room &r); diff --git a/Engine/src/CommandParser.cpp b/Engine/src/CommandParser.cpp index 2522086..771a657 100644 --- a/Engine/src/CommandParser.cpp +++ b/Engine/src/CommandParser.cpp @@ -44,19 +44,21 @@ std::vector CommandParser::Noun(std::stringstream &sentences) { return nouns; } - int CommandParser::Parse() { std::string cmd; std::string word; - std::string noun; + std::string noun = ""; std::vector verbs; std::vector nouns; + FUNCTION toRun; + printf("Please Enter a command \n"); printf("> "); getline(std::cin, cmd); + std::cin.clear(); cmd[0] = toupper(cmd[0]); std::stringstream c(cmd); @@ -69,8 +71,19 @@ int CommandParser::Parse() { printf("Sorry kid, can't understand ya! \n"); break; } - - commands[verb](""); + toRun = commands[verb]; } + + if (toRun) { + toRun(noun); + } + cmd.clear(); + word.clear(); + verbs.clear(); + nouns.clear(); + c.clear(); + + toRun = nullptr; + return 0; } diff --git a/Engine/src/game.cpp b/Engine/src/game.cpp index 8b5186a..50f2a44 100644 --- a/Engine/src/game.cpp +++ b/Engine/src/game.cpp @@ -3,6 +3,8 @@ bool isActive; +std::vector> rooms; + Game::Game() { curX = 0; curY = 0; @@ -46,20 +48,33 @@ void Game::Run() { bool Game::Save() { return false; } -int Quit(std::string n) { +void Game::ChangeRoom(Room r) { + if (currentRoom != r) { + currentRoom = r; + + currentRoom.Print(); + printf("\n"); + currentRoom.PrintAvalibleExits(); + } +} + +int Quit(const std::string &n) { isActive = false; return 0; } +int Move(const std::string &c) { -int Move(std::string c) { + std::string dirInput = c; - std::string dirInput; - printf("What Direction: "); - std::cin >> dirInput; - dirInput[0] = toupper(dirInput[0]); + int tmpX = 0, tmpY = 0; - Direction dir = directions[dirInput.data()]; + if (dirInput.empty()) { + printf("What Direction: "); + std::cin >> dirInput; + dirInput[0] = toupper(dirInput[0]); + } + const Direction dir = directions[dirInput.data()]; if (!Game::instance().GetCurrentRoom().ValidDirection(dir)) { printf("Sorry, can't move that direction kiddo \n"); @@ -68,33 +83,48 @@ int Move(std::string c) { switch (dir) { case N: - curY += 1; + tmpY -= 1; break; case S: - curY -= 1; + tmpY += 1; break; case E: - curX += 1; + tmpX += 1; break; case W: - curX -= 1; + tmpX -= 1; break; default: printf("Incorrect direction \n"); return 0; break; }; + + if (tmpX > 0 && tmpX < MAP_WIDTH) { + curX = tmpX; + } else { + printf("Ye hit a wall of some kind! \n"); + return -1; + } + + if (tmpY > 0 && tmpY < MAP_HEIGHT) { + curY = tmpY; + } else { + printf("Ye hit a wall of some kind! \n"); + return -1; + } + + Game::instance().ChangeRoom(rooms[curY][curX]); return 0; } - -int Look(std::string c) { +int Look(const std::string &c) { Room cur = Game::instance().GetCurrentRoom(); cur.Print(); + printf("\n"); cur.PrintAvalibleExits(); - return 0; } diff --git a/Engine/src/room.cpp b/Engine/src/room.cpp index 023d045..9fbd5b8 100644 --- a/Engine/src/room.cpp +++ b/Engine/src/room.cpp @@ -38,7 +38,7 @@ void Room::Print() const { void Room::PrintAvalibleExits() { printf("Available exits are: \n"); for (const auto &exit: exits) { - printf(directionChar[exit.dir] + '\n'); + printf(directionChar[exit.dir].data(), "\n"); } }