Fixed several issues with Parser and enabled room navigation

This commit is contained in:
2026-08-21 20:02:11 -05:00
parent b1e87868c8
commit 3e50d46284
9 changed files with 91 additions and 43 deletions
+32
View File
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/Text-Adventure",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
+2 -2
View File
@@ -34,8 +34,8 @@ class Creature
bool IsAlive() { return GetHealth() > 0; } bool IsAlive() { return GetHealth() > 0; }
void PrintDescription() { printf("%s\n", description); } void PrintDescription() { printf("%s\n", description.data()); }
void PrintName() { printf("%s\n", name); } void PrintName() { printf("%s\n", name.data()); }
virtual ~Creature() = default; virtual ~Creature() = default;
}; };
+3 -3
View File
@@ -15,8 +15,8 @@ void PlayerEditor::Edit()
{ {
printf("Options: %s\n"); printf("Options: %s\n");
for (auto &command : commands.begin()) /*for (const auto &command : commands.begin())
{ {
printf("%s\n", command.c_str()); printf("%s\n", command);
} }*/
} }
+5 -2
View File
@@ -26,6 +26,8 @@ private:
nlohmann::json json; nlohmann::json json;
fs::path filePath = "Resources/Rooms.json"; fs::path filePath = "Resources/Rooms.json";
std::ifstream stream; std::ifstream stream;
inline static Game *game_;
// Likely need to make sure Player is ADDED to room // Likely need to make sure Player is ADDED to room
@@ -44,13 +46,14 @@ public:
static void SetIsActive(const bool active) { isActive = active; } static void SetIsActive(const bool active) { isActive = active; }
static Game &instance() { static Game &Instance() {
static Game *game_;
if (game_ == nullptr) { if (game_ == nullptr) {
game_ = new Game(); game_ = new Game();
} }
return *game_; return *game_;
} }
~Game() = default;
}; };
// Commands // Commands
-7
View File
@@ -1,7 +0,0 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "object.h"
class Scenery : Object
+1 -6
View File
@@ -1,6 +1,5 @@
#include "CommandParser.h" #include "CommandParser.h"
#include <algorithm> #include <algorithm>
#include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@@ -24,10 +23,6 @@ std::vector<std::string> CommandParser::Verb(std::stringstream &sentences) {
} }
} }
if (verbs.empty()) {
CommandUnrecognized();
}
return verbs; return verbs;
} }
@@ -68,7 +63,7 @@ int CommandParser::Parse() {
for (const auto &verb: verbs) { for (const auto &verb: verbs) {
if (!commands[verb]) { if (!commands[verb]) {
printf("Sorry kid, can't understand ya! \n"); CommandUnrecognized();
break; break;
} }
toRun = commands[verb]; toRun = commands[verb];
+44 -21
View File
@@ -1,4 +1,6 @@
#include "game.h" #include "game.h"
#include <cstdio>
#include <filesystem>
#include <fstream> #include <fstream>
bool isActive; bool isActive;
@@ -20,6 +22,17 @@ bool Game::Init() {
rooms[i].resize(MAP_WIDTH); rooms[i].resize(MAP_WIDTH);
} }
filePath = fs::absolute(filePath);
if (!fs::exists(filePath)) {
printf("File Read error! Could not find Json at: \n");
printf("%s", filePath.c_str());
isActive = false;
return false;
}
// Read rooms into buffer // Read rooms into buffer
stream.open(absolute(filePath)); stream.open(absolute(filePath));
json = nlohmann::json::parse(stream); json = nlohmann::json::parse(stream);
@@ -32,9 +45,7 @@ bool Game::Init() {
parser.AddCommand("Move", Move); parser.AddCommand("Move", Move);
parser.AddCommand("Look", Look); parser.AddCommand("Look", Look);
currentRoom = rooms[curY][curX]; ChangeRoom(rooms[0][0]);
player.ChangeRoom(currentRoom.GetID());
currentRoom.Print();
return true; return true;
} }
@@ -52,6 +63,8 @@ void Game::ChangeRoom(Room r) {
if (currentRoom != r) { if (currentRoom != r) {
currentRoom = r; currentRoom = r;
player.ChangeRoom(currentRoom.GetID());
currentRoom.Print(); currentRoom.Print();
printf("\n"); printf("\n");
currentRoom.PrintAvalibleExits(); currentRoom.PrintAvalibleExits();
@@ -67,7 +80,7 @@ int Move(const std::string &c) {
std::string dirInput = c; std::string dirInput = c;
int tmpX = 0, tmpY = 0; int tmpX = curX, tmpY = curY;
if (dirInput.empty()) { if (dirInput.empty()) {
printf("What Direction: "); printf("What Direction: ");
@@ -76,7 +89,7 @@ int Move(const std::string &c) {
} }
const Direction dir = directions[dirInput.data()]; const Direction dir = directions[dirInput.data()];
if (!Game::instance().GetCurrentRoom().ValidDirection(dir)) { if (!Game::Instance().GetCurrentRoom().ValidDirection(dir)) {
printf("Sorry, can't move that direction kiddo \n"); printf("Sorry, can't move that direction kiddo \n");
return 0; return 0;
} }
@@ -84,18 +97,42 @@ int Move(const std::string &c) {
switch (dir) { switch (dir) {
case N: case N:
tmpY -= 1; tmpY -= 1;
if (tmpY > 0 && tmpY < MAP_HEIGHT) {
curY = tmpY;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break; break;
case S: case S:
tmpY += 1; tmpY += 1;
if (tmpY > 0 && tmpY < MAP_HEIGHT) {
curY = tmpY;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break; break;
case E: case E:
tmpX += 1; tmpX += 1;
if (tmpX > 0 && tmpX < MAP_WIDTH) {
curX = tmpX;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break; break;
case W: case W:
tmpX -= 1; tmpX -= 1;
if (tmpX > 0 && tmpX < MAP_WIDTH) {
curX = tmpX;
} else {
printf("Ye hit a wall of some kind! \n");
return -1;
}
break; break;
default: default:
printf("Incorrect direction \n"); printf("Incorrect direction \n");
@@ -103,26 +140,12 @@ int Move(const std::string &c) {
break; break;
}; };
if (tmpX > 0 && tmpX < MAP_WIDTH) { Game::Instance().ChangeRoom(rooms[curY][curX]);
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; return 0;
} }
int Look(const std::string &c) { int Look(const std::string &c) {
Room cur = Game::instance().GetCurrentRoom(); Room cur = Game::Instance().GetCurrentRoom();
cur.Print(); cur.Print();
printf("\n"); printf("\n");
cur.PrintAvalibleExits(); cur.PrintAvalibleExits();
+3 -1
View File
@@ -1,4 +1,5 @@
#include "room.h" #include "room.h"
#include <cstdio>
#include "game.h" #include "game.h"
@@ -38,7 +39,8 @@ void Room::Print() const {
void Room::PrintAvalibleExits() { void Room::PrintAvalibleExits() {
printf("Available exits are: \n"); printf("Available exits are: \n");
for (const auto &exit: exits) { for (const auto &exit: exits) {
printf(directionChar[exit.dir].data(), "\n"); printf(directionChar[exit.dir].c_str());
printf("\n");
} }
} }
+1 -1
View File
@@ -18,7 +18,7 @@ void Scenery::Interact()
void Scenery::Examine() void Scenery::Examine()
{ {
printf("%s\n", GetDescription()); printf("%s\n", GetDescription().data());
} }
Scenery::~Scenery() Scenery::~Scenery()