From 66fd2b09518ccdcdb666aa915ec208382e9b99fd Mon Sep 17 00:00:00 2001 From: Timothy O'Barr Date: Fri, 21 Aug 2026 08:02:22 -0500 Subject: [PATCH] Added several commands --- Engine/CMakeLists.txt | 2 +- Engine/headers/CommandParser.h | 1 - Engine/headers/game.h | 22 +++++++++--- Engine/headers/room.h | 10 +++--- Engine/src/CommandParser.cpp | 6 ++++ Engine/src/game.cpp | 61 ++++++++++++++++++++++++++++++---- Engine/src/room.cpp | 46 +++++++++---------------- 7 files changed, 101 insertions(+), 47 deletions(-) diff --git a/Engine/CMakeLists.txt b/Engine/CMakeLists.txt index 106c744..ac8a4ab 100644 --- a/Engine/CMakeLists.txt +++ b/Engine/CMakeLists.txt @@ -6,7 +6,7 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/room.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/game.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/player.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/scenery.cpp - src/CommandParser.cpp) + ${CMAKE_CURRENT_SOURCE_DIR}/src/CommandParser.cpp) add_library(Engine SHARED ${SOURCES}) diff --git a/Engine/headers/CommandParser.h b/Engine/headers/CommandParser.h index 898a69b..aa24a8d 100644 --- a/Engine/headers/CommandParser.h +++ b/Engine/headers/CommandParser.h @@ -11,7 +11,6 @@ static std::vector Nouns{"Object", "Door", "North", "South", "East" class CommandParser { private: - // std::vector commands; std::map commands; public: diff --git a/Engine/headers/game.h b/Engine/headers/game.h index b6a007d..b12c941 100644 --- a/Engine/headers/game.h +++ b/Engine/headers/game.h @@ -9,16 +9,14 @@ #include "player.h" #include "room.h" +static int curX, curY; + extern bool isActive; -extern int curX, curY; - - #define MAP_HEIGHT 5 #define MAP_WIDTH 5 namespace fs = std::filesystem; - class Game { private: std::vector> rooms; @@ -29,6 +27,7 @@ private: fs::path filePath = "Resources/Rooms.json"; std::ifstream stream; + // Likely need to make sure Player is ADDED to room // We want player and objects to exist IN room Room currentRoom; @@ -37,10 +36,23 @@ public: Game(); static bool GetIsActive() { return isActive; } bool Init(); - void ChangeRoom(Direction dir); void Run(); bool Save(); + + Room GetCurrentRoom() { return currentRoom; } + static void SetIsActive(const bool active) { isActive = active; } + + static Game &instance() { + static Game *game_; + if (game_ == nullptr) { + game_ = new Game(); + } + return *game_; + } }; +// Commands static int Quit(std::string n); +static int Move(std::string c); +static int Look(std::string c); diff --git a/Engine/headers/room.h b/Engine/headers/room.h index b2ad8eb..18f4566 100644 --- a/Engine/headers/room.h +++ b/Engine/headers/room.h @@ -18,6 +18,9 @@ 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"}}; + class Room { private: int id; @@ -30,15 +33,14 @@ public: Room(int id, std::string name, std::string desc, std::vector e); Room(const Room &r); - int AvalibleExits() const { return exits.size(); } + void PrintAvalibleExits(); void Print() const; - int GetID() const { return id; } + bool ValidDirection(Direction d); + ~Room(); friend void to_json(nlohmann::json &j, const Room &r); friend void from_json(const nlohmann::json &j, Room &r); }; - -static int Move(std::string c); diff --git a/Engine/src/CommandParser.cpp b/Engine/src/CommandParser.cpp index 9e3e432..2522086 100644 --- a/Engine/src/CommandParser.cpp +++ b/Engine/src/CommandParser.cpp @@ -64,6 +64,12 @@ int CommandParser::Parse() { nouns = Noun(c); for (const auto &verb: verbs) { + + if (!commands[verb]) { + printf("Sorry kid, can't understand ya! \n"); + break; + } + commands[verb](""); } return 0; diff --git a/Engine/src/game.cpp b/Engine/src/game.cpp index 48ef43a..8b5186a 100644 --- a/Engine/src/game.cpp +++ b/Engine/src/game.cpp @@ -3,9 +3,9 @@ bool isActive; -int curX, curY; - Game::Game() { + curX = 0; + curY = 0; progress = 0; Init(); } @@ -25,17 +25,18 @@ bool Game::Init() { rooms = json.get>>(); player.Init(); parser = CommandParser(); - parser.AddCommand("Exit", Quit); - currentRoom = rooms[0][0]; + parser.AddCommand("Exit", Quit); + parser.AddCommand("Move", Move); + parser.AddCommand("Look", Look); + + currentRoom = rooms[curY][curX]; player.ChangeRoom(currentRoom.GetID()); currentRoom.Print(); return true; } -void Game::ChangeRoom(Direction dir) { currentRoom.Print(); } - void Game::Run() { while (isActive) { parser.Parse(); @@ -49,3 +50,51 @@ int Quit(std::string n) { isActive = false; return 0; } + + +int Move(std::string c) { + + std::string dirInput; + printf("What Direction: "); + std::cin >> dirInput; + dirInput[0] = toupper(dirInput[0]); + + Direction dir = directions[dirInput.data()]; + + if (!Game::instance().GetCurrentRoom().ValidDirection(dir)) { + printf("Sorry, can't move that direction kiddo \n"); + return 0; + } + + switch (dir) { + case N: + curY += 1; + break; + + case S: + curY -= 1; + break; + + case E: + curX += 1; + break; + + case W: + curX -= 1; + break; + default: + printf("Incorrect direction \n"); + return 0; + break; + }; + return 0; +} + + +int Look(std::string c) { + Room cur = Game::instance().GetCurrentRoom(); + cur.Print(); + cur.PrintAvalibleExits(); + + return 0; +} diff --git a/Engine/src/room.cpp b/Engine/src/room.cpp index 7013aae..023d045 100644 --- a/Engine/src/room.cpp +++ b/Engine/src/room.cpp @@ -35,6 +35,22 @@ void Room::Print() const { printf("%s\n", description.c_str()); } +void Room::PrintAvalibleExits() { + printf("Available exits are: \n"); + for (const auto &exit: exits) { + printf(directionChar[exit.dir] + '\n'); + } +} + +bool Room::ValidDirection(Direction d) { + for (const auto &exit: exits) { + if (exit.dir == d) { + return true; + } + } + return false; +} + void from_json(const json &j, Exit &e) { j.at("exitID").get_to(e.exitID); j.at("dir").get_to(e.dir); @@ -54,33 +70,3 @@ void from_json(const json &j, Room &r) { void to_json(nlohmann::json &j, const Room &r) { j = json{{"id", r.id}, {"name", r.name}, {"description", r.description}, {"exits", r.exits}}; } - -int Move(std::string c) { - - char dir; - printf("What Direction: "); - std::cin >> dir; - - - switch (dir) { - case 'N': - curY += 1; - break; - - case 'S': - curY -= 1; - break; - - case 'E': - curX += 1; - break; - - case 'W': - curX -= 1; - break; - default: - return 0; - break; - }; - return 0; -}