Wip: Working on Room Movement
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef int (*FUNCTION)(std::string s);
|
typedef int (*FUNCTION)(const std::string &s);
|
||||||
|
|
||||||
static std::vector<std::string> Verbs{"Quit", "Exit", "Go", "Move", "Look"};
|
static std::vector<std::string> Verbs{"Quit", "Exit", "Go", "Move", "Look"};
|
||||||
static std::vector<std::string> Nouns{"Object", "Door", "North", "South", "East",
|
static std::vector<std::string> Nouns{"Object", "Door", "North", "South", "East",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
static int curX, curY;
|
static int curX, curY;
|
||||||
|
|
||||||
extern bool isActive;
|
extern bool isActive;
|
||||||
|
extern std::vector<std::vector<Room>> rooms;
|
||||||
|
|
||||||
#define MAP_HEIGHT 5
|
#define MAP_HEIGHT 5
|
||||||
#define MAP_WIDTH 5
|
#define MAP_WIDTH 5
|
||||||
@@ -19,7 +20,6 @@ extern bool isActive;
|
|||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
class Game {
|
class Game {
|
||||||
private:
|
private:
|
||||||
std::vector<std::vector<Room>> rooms;
|
|
||||||
Player player;
|
Player player;
|
||||||
int progress;
|
int progress;
|
||||||
CommandParser parser;
|
CommandParser parser;
|
||||||
@@ -39,6 +39,7 @@ public:
|
|||||||
void Run();
|
void Run();
|
||||||
bool Save();
|
bool Save();
|
||||||
|
|
||||||
|
void ChangeRoom(Room r);
|
||||||
Room GetCurrentRoom() { return currentRoom; }
|
Room GetCurrentRoom() { return currentRoom; }
|
||||||
|
|
||||||
static void SetIsActive(const bool active) { isActive = active; }
|
static void SetIsActive(const bool active) { isActive = active; }
|
||||||
@@ -53,6 +54,6 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
static int Quit(std::string n);
|
static int Quit(const std::string &n);
|
||||||
static int Move(std::string c);
|
static int Move(const std::string &c);
|
||||||
static int Look(std::string c);
|
static int Look(const std::string &c);
|
||||||
|
|||||||
+15
-2
@@ -18,8 +18,8 @@ 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<std::string, 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"}};
|
static std::map<Direction, std::string> directionChar{{N, "N"}, {S, "S"}, {E, "E"}, {W, "W"}};
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
private:
|
private:
|
||||||
@@ -39,6 +39,19 @@ public:
|
|||||||
|
|
||||||
bool ValidDirection(Direction d);
|
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();
|
~Room();
|
||||||
|
|
||||||
friend void to_json(nlohmann::json &j, const Room &r);
|
friend void to_json(nlohmann::json &j, const Room &r);
|
||||||
|
|||||||
@@ -44,19 +44,21 @@ std::vector<std::string> CommandParser::Noun(std::stringstream &sentences) {
|
|||||||
return nouns;
|
return nouns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int CommandParser::Parse() {
|
int CommandParser::Parse() {
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
std::string word;
|
std::string word;
|
||||||
|
|
||||||
std::string noun;
|
std::string noun = "";
|
||||||
|
|
||||||
std::vector<std::string> verbs;
|
std::vector<std::string> verbs;
|
||||||
std::vector<std::string> nouns;
|
std::vector<std::string> nouns;
|
||||||
|
FUNCTION toRun;
|
||||||
|
|
||||||
|
|
||||||
printf("Please Enter a command \n");
|
printf("Please Enter a command \n");
|
||||||
printf("> ");
|
printf("> ");
|
||||||
getline(std::cin, cmd);
|
getline(std::cin, cmd);
|
||||||
|
std::cin.clear();
|
||||||
cmd[0] = toupper(cmd[0]);
|
cmd[0] = toupper(cmd[0]);
|
||||||
std::stringstream c(cmd);
|
std::stringstream c(cmd);
|
||||||
|
|
||||||
@@ -69,8 +71,19 @@ int CommandParser::Parse() {
|
|||||||
printf("Sorry kid, can't understand ya! \n");
|
printf("Sorry kid, can't understand ya! \n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
toRun = commands[verb];
|
||||||
commands[verb]("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (toRun) {
|
||||||
|
toRun(noun);
|
||||||
|
}
|
||||||
|
cmd.clear();
|
||||||
|
word.clear();
|
||||||
|
verbs.clear();
|
||||||
|
nouns.clear();
|
||||||
|
c.clear();
|
||||||
|
|
||||||
|
toRun = nullptr;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+42
-12
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
bool isActive;
|
bool isActive;
|
||||||
|
|
||||||
|
std::vector<std::vector<Room>> rooms;
|
||||||
|
|
||||||
Game::Game() {
|
Game::Game() {
|
||||||
curX = 0;
|
curX = 0;
|
||||||
curY = 0;
|
curY = 0;
|
||||||
@@ -46,20 +48,33 @@ void Game::Run() {
|
|||||||
|
|
||||||
bool Game::Save() { return false; }
|
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;
|
isActive = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Move(const std::string &c) {
|
||||||
|
|
||||||
int Move(std::string c) {
|
std::string dirInput = c;
|
||||||
|
|
||||||
std::string dirInput;
|
int tmpX = 0, tmpY = 0;
|
||||||
|
|
||||||
|
if (dirInput.empty()) {
|
||||||
printf("What Direction: ");
|
printf("What Direction: ");
|
||||||
std::cin >> dirInput;
|
std::cin >> dirInput;
|
||||||
dirInput[0] = toupper(dirInput[0]);
|
dirInput[0] = toupper(dirInput[0]);
|
||||||
|
}
|
||||||
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");
|
||||||
@@ -68,33 +83,48 @@ int Move(std::string c) {
|
|||||||
|
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case N:
|
case N:
|
||||||
curY += 1;
|
tmpY -= 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case S:
|
case S:
|
||||||
curY -= 1;
|
tmpY += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case E:
|
case E:
|
||||||
curX += 1;
|
tmpX += 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case W:
|
case W:
|
||||||
curX -= 1;
|
tmpX -= 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Incorrect direction \n");
|
printf("Incorrect direction \n");
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Look(const std::string &c) {
|
||||||
int Look(std::string c) {
|
|
||||||
Room cur = Game::instance().GetCurrentRoom();
|
Room cur = Game::instance().GetCurrentRoom();
|
||||||
cur.Print();
|
cur.Print();
|
||||||
|
printf("\n");
|
||||||
cur.PrintAvalibleExits();
|
cur.PrintAvalibleExits();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ 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] + '\n');
|
printf(directionChar[exit.dir].data(), "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user