Compare commits
2
Commits
707b88c736
...
66fd2b0951
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66fd2b0951 | ||
|
|
9b72d1588e |
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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})
|
||||
|
||||
@@ -11,7 +11,6 @@ static std::vector<std::string> Nouns{"Object", "Door", "North", "South", "East"
|
||||
|
||||
class CommandParser {
|
||||
private:
|
||||
// std::vector<std::string> commands;
|
||||
std::map<std::string, FUNCTION> commands;
|
||||
|
||||
public:
|
||||
|
||||
+35
-25
@@ -1,41 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum DmgType {SINGLE, MULTI};
|
||||
enum DmgType { SINGLE, MULTI };
|
||||
|
||||
struct Stat
|
||||
{
|
||||
int statID;
|
||||
std::string statName;
|
||||
float stat;
|
||||
struct Stat {
|
||||
int statID;
|
||||
float stat;
|
||||
};
|
||||
|
||||
class Creature
|
||||
{
|
||||
protected:
|
||||
int id;
|
||||
int level;
|
||||
std::vector<Stat> stats;
|
||||
std::string name;
|
||||
std::string description;
|
||||
struct Stats {
|
||||
float hp;
|
||||
float speed;
|
||||
float magic;
|
||||
float defense;
|
||||
};
|
||||
|
||||
public:
|
||||
virtual void Init() = 0;
|
||||
virtual void LevelUp() = 0;
|
||||
virtual void Die() = 0;
|
||||
virtual void TakeDamage(int damageType, float damage) = 0;
|
||||
virtual float GetHealth() = 0;
|
||||
struct RoomInfo {
|
||||
int roomID;
|
||||
int x, y;
|
||||
};
|
||||
|
||||
class Creature {
|
||||
protected:
|
||||
int id = -1;
|
||||
int level = 0;
|
||||
Stats stats = {};
|
||||
// std::map<std::string, Stat> stats;
|
||||
std::string name;
|
||||
std::string description;
|
||||
|
||||
bool IsAlive() { return GetHealth() > 0; }
|
||||
public:
|
||||
virtual void Init() = 0;
|
||||
virtual void LevelUp() = 0;
|
||||
virtual void Die() = 0;
|
||||
virtual void TakeDamage(int damageType, float damage) = 0;
|
||||
virtual float GetHealth() = 0;
|
||||
|
||||
void PrintDescription() { printf("%s\n", description); }
|
||||
void PrintName() { printf("%s\n", name); }
|
||||
|
||||
virtual ~Creature() = default;
|
||||
bool IsAlive() { return GetHealth() > 0; }
|
||||
|
||||
void PrintDescription() { printf("%s\n", description.c_str()); }
|
||||
void PrintName() { printf("%s\n", name.c_str()); }
|
||||
|
||||
virtual ~Creature() = default;
|
||||
};
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
+18
-3
@@ -9,13 +9,14 @@
|
||||
#include "player.h"
|
||||
#include "room.h"
|
||||
|
||||
static int curX, curY;
|
||||
|
||||
extern bool isActive;
|
||||
|
||||
#define MAP_HEIGHT 5
|
||||
#define MAP_WIDTH 5
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class Game {
|
||||
private:
|
||||
std::vector<std::vector<Room>> rooms;
|
||||
@@ -26,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;
|
||||
@@ -34,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_;
|
||||
}
|
||||
};
|
||||
|
||||
static int Exit(std::string n);
|
||||
// Commands
|
||||
static int Quit(std::string n);
|
||||
static int Move(std::string c);
|
||||
static int Look(std::string c);
|
||||
|
||||
+14
-11
@@ -1,22 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include "creature.h"
|
||||
|
||||
|
||||
class Player : Creature
|
||||
{
|
||||
class Player : Creature {
|
||||
private:
|
||||
// Inventory inventory;
|
||||
|
||||
private:
|
||||
// Inventory inventory;
|
||||
// Lets keep the room a loose reference to avoid to many hardcoded details and other info
|
||||
int curRoomID = -1;
|
||||
int prevRoomID = -1;
|
||||
|
||||
public:
|
||||
public:
|
||||
void Init() override;
|
||||
void LevelUp() override;
|
||||
void Die() override;
|
||||
void TakeDamage(int damageType, float damage) override;
|
||||
float GetHealth() override;
|
||||
|
||||
void Init();
|
||||
void LevelUp();
|
||||
void Die();
|
||||
void TakeDamage(int damageType, float damage);
|
||||
float GetHealth();
|
||||
void ChangeRoom(int roomID);
|
||||
};
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#include "creature.h"
|
||||
|
||||
|
||||
class Player : Creature
|
||||
{
|
||||
|
||||
private:
|
||||
// Inventory inventory;
|
||||
|
||||
public:
|
||||
|
||||
void Init();
|
||||
void LevelUp();
|
||||
void Die();
|
||||
void TakeDamage();
|
||||
|
||||
};
|
||||
@@ -18,6 +18,9 @@ struct Exit {
|
||||
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 {
|
||||
private:
|
||||
int id;
|
||||
@@ -30,8 +33,11 @@ public:
|
||||
Room(int id, std::string name, std::string desc, std::vector<Exit> e);
|
||||
Room(const Room &r);
|
||||
|
||||
int AvalibleExits() { return exits.size(); }
|
||||
void PrintAvalibleExits();
|
||||
void Print() const;
|
||||
int GetID() const { return id; }
|
||||
|
||||
bool ValidDirection(Direction d);
|
||||
|
||||
~Room();
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+57
-7
@@ -4,6 +4,8 @@
|
||||
bool isActive;
|
||||
|
||||
Game::Game() {
|
||||
curX = 0;
|
||||
curY = 0;
|
||||
progress = 0;
|
||||
Init();
|
||||
}
|
||||
@@ -24,17 +26,17 @@ bool Game::Init() {
|
||||
player.Init();
|
||||
parser = CommandParser();
|
||||
|
||||
FUNCTION exit = Exit;
|
||||
parser.AddCommand("Exit", Quit);
|
||||
parser.AddCommand("Move", Move);
|
||||
parser.AddCommand("Look", Look);
|
||||
|
||||
parser.AddCommand("Exit", exit);
|
||||
|
||||
// Initalize Objects
|
||||
currentRoom = rooms[curY][curX];
|
||||
player.ChangeRoom(currentRoom.GetID());
|
||||
currentRoom.Print();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Game::ChangeRoom(Direction dir) {}
|
||||
|
||||
void Game::Run() {
|
||||
while (isActive) {
|
||||
parser.Parse();
|
||||
@@ -44,7 +46,55 @@ void Game::Run() {
|
||||
|
||||
bool Game::Save() { return false; }
|
||||
|
||||
int Exit(std::string n) {
|
||||
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;
|
||||
}
|
||||
|
||||
+18
-15
@@ -1,28 +1,31 @@
|
||||
#include "player.h"
|
||||
|
||||
void Player::Init()
|
||||
{
|
||||
// initalize stats, room location, inventory and item data
|
||||
void Player::Init() {
|
||||
// initialize stats, room location, inventory and item data
|
||||
stats = Stats{100.0f, 50.00f, 150.00f, 50.00f};
|
||||
|
||||
name = "Player";
|
||||
}
|
||||
|
||||
void Player::LevelUp()
|
||||
{
|
||||
void Player::LevelUp() {}
|
||||
|
||||
void Player::Die() {
|
||||
// Now. You must, die!
|
||||
}
|
||||
|
||||
void Player::Die()
|
||||
{
|
||||
// Now. You must, die!
|
||||
void Player::TakeDamage(int damageType, float damage) {
|
||||
// Ouch
|
||||
}
|
||||
|
||||
void Player::TakeDamage(int damageType, float damage)
|
||||
{
|
||||
// Ouch
|
||||
}
|
||||
float Player::GetHealth() { return 0.0f; }
|
||||
|
||||
float Player::GetHealth()
|
||||
{
|
||||
void Player::ChangeRoom(int roomID) {
|
||||
if (prevRoomID == roomID) {
|
||||
curRoomID = prevRoomID;
|
||||
}
|
||||
|
||||
return 0.0f;
|
||||
else if (curRoomID != roomID) {
|
||||
prevRoomID = curRoomID;
|
||||
curRoomID = roomID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#include "player.h"
|
||||
|
||||
void Player::Init()
|
||||
{
|
||||
// initalize stats, room location, inventory and item data
|
||||
|
||||
}
|
||||
|
||||
void Player::LevelUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Player::Die()
|
||||
{
|
||||
// Now. You must, die!
|
||||
}
|
||||
|
||||
void Player::TakeDamage()
|
||||
{
|
||||
// Ouch
|
||||
}
|
||||
+17
-1
@@ -1,5 +1,7 @@
|
||||
#include "room.h"
|
||||
|
||||
#include "game.h"
|
||||
|
||||
Room::Room() {
|
||||
id = -1;
|
||||
name = "Nothing";
|
||||
@@ -33,6 +35,21 @@ 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);
|
||||
@@ -50,7 +67,6 @@ void from_json(const json &j, Room &r) {
|
||||
j.at("exits").get_to(r.exits);
|
||||
}
|
||||
|
||||
|
||||
void to_json(nlohmann::json &j, const Room &r) {
|
||||
j = json{{"id", r.id}, {"name", r.name}, {"description", r.description}, {"exits", r.exits}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user