Inital Commit

This commit is contained in:
Timothy O'Barr
2026-08-07 13:10:07 -05:00
commit 34f7d3f7bb
72 changed files with 31475 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 4.2)
project(Engine)
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)
add_library(Engine SHARED ${SOURCES})
target_include_directories(Engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/headers)
target_include_directories(Engine PUBLIC ${CMAKE_SOURCE_DIR}/Extern)
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <map>
#include <string>
#include <vector>
typedef int (*FUNCTION)(std::string s);
static std::vector<std::string> Verbs{"Quit", "Exit", "Go", "Move", "Look"};
static std::vector<std::string> Nouns{"Object", "Door", "North", "South", "East",
"West", "Up", "Down", "Left", "right"};
class CommandParser {
private:
// std::vector<std::string> commands;
std::map<std::string, FUNCTION> commands;
public:
CommandParser();
bool AddCommand(std::string c, FUNCTION f);
void RemoveCommand(std::string c);
std::vector<std::string> Verb(std::stringstream &sentences);
std::vector<std::string> Noun(std::stringstream &sentences);
static inline void CommandUnrecognized() { printf("I don't know how to do that.\n"); }
int Parse();
};
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
#include <vector>
enum DmgType {SINGLE, MULTI};
struct Stat
{
int statID;
std::string statName;
float stat;
};
class Creature
{
protected:
int id;
int level;
std::vector<Stat> stats;
std::string name;
std::string description;
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;
bool IsAlive() { return GetHealth() > 0; }
void PrintDescription() { printf("%s\n", description); }
void PrintName() { printf("%s\n", name); }
virtual ~Creature() = default;
};
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "CommandParser.h"
#include "game.h"
#include "json.hpp"
#include "player.h"
#include "room.h"
extern bool isActive;
#define MAP_HEIGHT 5
#define MAP_WIDTH 5
namespace fs = std::filesystem;
class Game {
private:
std::vector<std::vector<Room>> rooms;
Player player;
int progress;
CommandParser parser;
nlohmann::json json;
fs::path filePath = "Resources/Rooms.json";
std::ifstream stream;
public:
Game();
static bool GetIsActive() { return isActive; }
bool Init();
void ChangeRoom(Direction dir);
void Run();
bool Save();
static void SetIsActive(const bool active) { isActive = active; }
};
static int Exit(std::string n);
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include <iomanip>
#include <iostream>
#include <vector>
#include "json.hpp"
using json = nlohmann::json;
enum class ObjectType { ITEM, SCENERY, NONE };
class Object {
protected:
int id;
std::string name;
std::string description;
std::vector<std::string> keywords;
ObjectType type;
bool canTake;
int roomIdItBelongsTo;
public:
virtual void Interact() = 0;
std::string const GetName() { return name; }
std::string const GetDescription() { return description; }
ObjectType const GetObjectType() { return type; }
std::vector<std::string> const GetKeywords() { return keywords; }
int GetId() { return id; }
int GetRoomId() { return roomIdItBelongsTo; }
bool CanTake() { return canTake; }
virtual ~Object() = default;
};
// Global/Namespace scope serialization functions
inline void to_json(json &j, Object &obj) {
j = json{{"id", obj.GetId()},
{"name", obj.GetName()},
{"description", obj.GetDescription()},
{"keywords", obj.GetKeywords()},
{"type", obj.GetObjectType()},
{"canTake", obj.CanTake()},
{"roomIdItBelongsTo", obj.GetRoomId()}};
}
+22
View File
@@ -0,0 +1,22 @@
#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(int damageType, float damage);
float GetHealth();
};
+22
View File
@@ -0,0 +1,22 @@
#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();
};
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "json.hpp"
#include "object.h"
using json = nlohmann::json;
enum Direction { N, S, E, W };
struct Exit {
int exitID;
Direction dir;
bool isLocked;
friend void from_json(const json &j, Exit &e);
friend void to_json(json &j, const Exit &e);
};
class Room {
private:
int id;
std::string name;
std::string description;
std::vector<Exit> exits;
std::vector<std::unique_ptr<Object>> objects;
public:
Room();
Room(int id, std::string name, std::string desc, std::vector<Exit> e);
Room(const Room &r);
int AvalibleExits() { return exits.size(); }
void Print() const;
~Room();
friend void to_json(nlohmann::json &j, const Room &r);
friend void from_json(const nlohmann::json &j, Room &r);
};
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "object.h"
class Scenery : Object
{
private:
bool containsItem;
public:
Scenery();
void Examine();
void Interact();
~Scenery();
};
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "object.h"
class Scenery : Object
+70
View File
@@ -0,0 +1,70 @@
#include "CommandParser.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
CommandParser::CommandParser() {}
bool CommandParser::AddCommand(std::string c, FUNCTION f) {
commands.emplace(c, f);
return true;
}
void CommandParser::RemoveCommand(std::string c) { commands.erase(c); }
std::vector<std::string> CommandParser::Verb(std::stringstream &sentences) {
std::string word;
std::vector<std::string> verbs = {};
while (sentences >> word) {
if (std::find(Verbs.begin(), Verbs.end(), word) != Verbs.end()) {
verbs.push_back(word);
}
}
if (verbs.empty()) {
CommandUnrecognized();
}
return verbs;
}
std::vector<std::string> CommandParser::Noun(std::stringstream &sentences) {
std::string word;
std::vector<std::string> nouns = {};
while (sentences >> word) {
if (std::find(Nouns.begin(), Nouns.end(), word) != Nouns.end()) {
nouns.push_back(word);
}
}
return nouns;
}
int CommandParser::Parse() {
std::string cmd;
std::string word;
std::string noun;
std::vector<std::string> verbs;
std::vector<std::string> nouns;
printf("Please Enter a command \n");
printf("> ");
getline(std::cin, cmd);
cmd[0] = toupper(cmd[0]);
std::stringstream c(cmd);
verbs = Verb(c);
nouns = Noun(c);
for (const auto &verb: verbs) {
commands[verb]("");
}
return 0;
}
+57
View File
@@ -0,0 +1,57 @@
#include "game.h"
#include <fstream>
bool isActive;
Game::Game() {
progress = 0;
Init();
}
bool Game::Init() {
isActive = true;
rooms.resize(MAP_HEIGHT);
for (int i = 0; i < MAP_HEIGHT; i++) {
rooms[i].resize(MAP_WIDTH);
}
// Read rooms into buffer
stream.open(absolute(filePath));
json = nlohmann::json::parse(stream);
// rooms = json.get<std::vector<std::vector<Room>>>();
auto &roomData = json["rooms"];
for (const auto &roomCol: rooms) {
for (const auto &roomRow: roomCol) {
// rooms.push_back(roomRow.get<Room>());
}
}
player.Init();
parser = CommandParser();
FUNCTION exit = Exit;
parser.AddCommand("Exit", exit);
// Initalize Objects
return true;
}
void Game::ChangeRoom(Direction dir) {}
void Game::Run() {
while (isActive) {
parser.Parse();
// Process game logic in here
}
}
bool Game::Save() { return false; }
int Exit(std::string n) {
isActive = false;
return 0;
}
+7
View File
@@ -0,0 +1,7 @@
#include <iostream>
int main()
{
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
#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(int damageType, float damage)
{
// Ouch
}
float Player::GetHealth()
{
return 0.0f;
}
+22
View File
@@ -0,0 +1,22 @@
#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
}
+58
View File
@@ -0,0 +1,58 @@
#include "room.h"
Room::Room() {
id = -1;
name = "Nothing";
description = "There's nothing here";
exits.clear();
}
Room::Room(int id, std::string n, std::string desc, std::vector<Exit> e) {
id = id;
name = n;
description = desc;
exits = e;
}
Room::Room(const Room &r) {
id = r.id;
name = r.name;
description = r.description;
exits = r.exits;
}
Room::~Room() {
id = -1;
name = "";
description = "";
exits.clear();
}
void Room::Print() const {
printf("%s\n", name.c_str());
printf("%s\n", description.c_str());
}
inline void from_json(const json &j, Exit &e) {
j.at("exitID").get_to(e.exitID);
j.at("dir").get_to(e.dir);
j.at("isLocked").get_to(e.isLocked);
}
inline void to_json(json &j, const Exit &e) {
j = json{{"exitID", e.exitID}, {"dir", e.dir}, {"isLocked", e.isLocked}};
}
inline void from_json(const json &j, Room &r) {
j.at("id").get_to(r.id);
j.at("name").get_to(r.name);
j.at("description").get_to(r.description);
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}};
}
+29
View File
@@ -0,0 +1,29 @@
#include "scenery.h"
Scenery::Scenery()
{
type = ObjectType::SCENERY;
name = "Scenery";
containsItem = false;
}
void Scenery::Interact()
{
if (containsItem)
{
// Give Item
}
Examine();
}
void Scenery::Examine()
{
printf("%s\n", GetDescription());
}
Scenery::~Scenery()
{
type = ObjectType::NONE;
name = "";
keywords.clear();
}
+1
View File
@@ -0,0 +1 @@
#include "object.h"