Added several commands
This commit is contained in:
@@ -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/game.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/player.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/player.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/scenery.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/scenery.cpp
|
||||||
src/CommandParser.cpp)
|
${CMAKE_CURRENT_SOURCE_DIR}/src/CommandParser.cpp)
|
||||||
|
|
||||||
|
|
||||||
add_library(Engine SHARED ${SOURCES})
|
add_library(Engine SHARED ${SOURCES})
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ static std::vector<std::string> Nouns{"Object", "Door", "North", "South", "East"
|
|||||||
|
|
||||||
class CommandParser {
|
class CommandParser {
|
||||||
private:
|
private:
|
||||||
// std::vector<std::string> commands;
|
|
||||||
std::map<std::string, FUNCTION> commands;
|
std::map<std::string, FUNCTION> commands;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
+17
-5
@@ -9,16 +9,14 @@
|
|||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "room.h"
|
#include "room.h"
|
||||||
|
|
||||||
|
static int curX, curY;
|
||||||
|
|
||||||
extern bool isActive;
|
extern bool isActive;
|
||||||
|
|
||||||
extern int curX, curY;
|
|
||||||
|
|
||||||
|
|
||||||
#define MAP_HEIGHT 5
|
#define MAP_HEIGHT 5
|
||||||
#define MAP_WIDTH 5
|
#define MAP_WIDTH 5
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
private:
|
private:
|
||||||
std::vector<std::vector<Room>> rooms;
|
std::vector<std::vector<Room>> rooms;
|
||||||
@@ -29,6 +27,7 @@ private:
|
|||||||
fs::path filePath = "Resources/Rooms.json";
|
fs::path filePath = "Resources/Rooms.json";
|
||||||
std::ifstream stream;
|
std::ifstream stream;
|
||||||
|
|
||||||
|
|
||||||
// Likely need to make sure Player is ADDED to room
|
// Likely need to make sure Player is ADDED to room
|
||||||
// We want player and objects to exist IN room
|
// We want player and objects to exist IN room
|
||||||
Room currentRoom;
|
Room currentRoom;
|
||||||
@@ -37,10 +36,23 @@ public:
|
|||||||
Game();
|
Game();
|
||||||
static bool GetIsActive() { return isActive; }
|
static bool GetIsActive() { return isActive; }
|
||||||
bool Init();
|
bool Init();
|
||||||
void ChangeRoom(Direction dir);
|
|
||||||
void Run();
|
void Run();
|
||||||
bool Save();
|
bool Save();
|
||||||
|
|
||||||
|
Room GetCurrentRoom() { return currentRoom; }
|
||||||
|
|
||||||
static void SetIsActive(const bool active) { isActive = active; }
|
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 Quit(std::string n);
|
||||||
|
static int Move(std::string c);
|
||||||
|
static int Look(std::string c);
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ struct Exit {
|
|||||||
friend void to_json(json &j, const Exit &e);
|
friend void to_json(json &j, const Exit &e);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static std::map<char *, Direction> directions{{"N", N}, {"S", S}, {"E", E}, {"W", W}};
|
||||||
|
static std::map<Direction, const char *> directionChar{{N, "N"}, {S, "S"}, {E, "E"}, {W, "W"}};
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
private:
|
private:
|
||||||
int id;
|
int id;
|
||||||
@@ -30,15 +33,14 @@ public:
|
|||||||
Room(int id, std::string name, std::string desc, std::vector<Exit> e);
|
Room(int id, std::string name, std::string desc, std::vector<Exit> e);
|
||||||
Room(const Room &r);
|
Room(const Room &r);
|
||||||
|
|
||||||
int AvalibleExits() const { return exits.size(); }
|
void PrintAvalibleExits();
|
||||||
void Print() const;
|
void Print() const;
|
||||||
|
|
||||||
int GetID() const { return id; }
|
int GetID() const { return id; }
|
||||||
|
|
||||||
|
bool ValidDirection(Direction d);
|
||||||
|
|
||||||
~Room();
|
~Room();
|
||||||
|
|
||||||
friend void to_json(nlohmann::json &j, const Room &r);
|
friend void to_json(nlohmann::json &j, const Room &r);
|
||||||
friend void from_json(const nlohmann::json &j, Room &r);
|
friend void from_json(const nlohmann::json &j, Room &r);
|
||||||
};
|
};
|
||||||
|
|
||||||
static int Move(std::string c);
|
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ int CommandParser::Parse() {
|
|||||||
nouns = Noun(c);
|
nouns = Noun(c);
|
||||||
|
|
||||||
for (const auto &verb: verbs) {
|
for (const auto &verb: verbs) {
|
||||||
|
|
||||||
|
if (!commands[verb]) {
|
||||||
|
printf("Sorry kid, can't understand ya! \n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
commands[verb]("");
|
commands[verb]("");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+55
-6
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
bool isActive;
|
bool isActive;
|
||||||
|
|
||||||
int curX, curY;
|
|
||||||
|
|
||||||
Game::Game() {
|
Game::Game() {
|
||||||
|
curX = 0;
|
||||||
|
curY = 0;
|
||||||
progress = 0;
|
progress = 0;
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
@@ -25,17 +25,18 @@ bool Game::Init() {
|
|||||||
rooms = json.get<std::vector<std::vector<Room>>>();
|
rooms = json.get<std::vector<std::vector<Room>>>();
|
||||||
player.Init();
|
player.Init();
|
||||||
parser = CommandParser();
|
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());
|
player.ChangeRoom(currentRoom.GetID());
|
||||||
currentRoom.Print();
|
currentRoom.Print();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::ChangeRoom(Direction dir) { currentRoom.Print(); }
|
|
||||||
|
|
||||||
void Game::Run() {
|
void Game::Run() {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
parser.Parse();
|
parser.Parse();
|
||||||
@@ -49,3 +50,51 @@ int Quit(std::string n) {
|
|||||||
isActive = false;
|
isActive = false;
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
+16
-30
@@ -35,6 +35,22 @@ void Room::Print() const {
|
|||||||
printf("%s\n", description.c_str());
|
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) {
|
void from_json(const json &j, Exit &e) {
|
||||||
j.at("exitID").get_to(e.exitID);
|
j.at("exitID").get_to(e.exitID);
|
||||||
j.at("dir").get_to(e.dir);
|
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) {
|
void to_json(nlohmann::json &j, const Room &r) {
|
||||||
j = json{{"id", r.id}, {"name", r.name}, {"description", r.description}, {"exits", r.exits}};
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user