diff options
Diffstat (limited to 'backend')
-rw-r--r-- | backend/CMakeLists.txt | 2 | ||||
-rw-r--r-- | backend/Dungeon.cpp | 4 | ||||
-rw-r--r-- | backend/Dungeon.h | 8 | ||||
-rw-r--r-- | backend/Location.cpp | 38 | ||||
-rw-r--r-- | backend/Location.h | 36 | ||||
-rw-r--r-- | backend/LocationFactory.cpp | 4 | ||||
-rw-r--r-- | backend/LocationFactory.h | 5 | ||||
-rw-r--r-- | backend/Object.cpp | 41 | ||||
-rw-r--r-- | backend/Object.h | 13 | ||||
-rw-r--r-- | backend/util.cpp | 14 | ||||
-rw-r--r-- | backend/util.h | 6 |
11 files changed, 154 insertions, 17 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 6d94517..9b1e251 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -4,5 +4,7 @@ target_sources(main PUBLIC Object.cpp ObjectFactory.cpp Dungeon.cpp + Location.cpp + util.cpp ) diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index 45158a6..55dccec 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -4,3 +4,7 @@ void Dungeon::update() { } +void Dungeon::add_location(Location * location) { + this->locations.push_back(location); +} + diff --git a/backend/Dungeon.h b/backend/Dungeon.h index 60faa09..88328c8 100644 --- a/backend/Dungeon.h +++ b/backend/Dungeon.h @@ -1,8 +1,16 @@ #pragma once +#include "List.hpp" + +class Location; + class Dungeon { public: void update(); + void add_location(Location *); + +private: + List<Location *> locations; }; diff --git a/backend/Location.cpp b/backend/Location.cpp new file mode 100644 index 0000000..8a79af5 --- /dev/null +++ b/backend/Location.cpp @@ -0,0 +1,38 @@ +#include <string.h> + +#include "Location.h" +#include "util.h" + +Location::Location(const char * name, const char * description) { + this->set_name(name); + this->set_description(description); +} + +Location::~Location() { + safe_free(this->name); + safe_free(this->description); +} + +void Location::set_name(const char * name) { + safe_free(this->name); + this->name = strdup(name); +} +const char * Location::get_name() { + return this->name; +} + +void Location::set_description(const char * description) { + safe_free(this->description); + this->description = strdup(description); +} +const char * Location::get_description() { + return this->description; +} + +void Location::set_exit(Direction dir, Location * location) { + this->edges[dir] = location; +} +Location * Location::get_exit(Direction dir) { + return this->edges[dir]; +} + diff --git a/backend/Location.h b/backend/Location.h index 1b23c78..6068a99 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -1,9 +1,41 @@ #pragma once +#include "List.hpp" + +class Enemy; +class Object; + +enum Direction { + NORTH = 0, + EAST = 1, + SOUTH = 2, + WEST = 3, +}; + class Location { +public: + void set_name(const char * name); + const char * get_name(); + void set_description(const char * description); + const char * get_description(); + void set_exit(Direction dir, Location * location = nullptr); + Location * get_exit(Direction dir); + protected: - Location() = default; - virtual ~Location() = default; + Location(const char * name = "", const char * description = ""); + virtual ~Location(); friend class LocationFactory; +private: + const char * name = nullptr; + const char * description = nullptr; + List<Enemy*> enemies = {}; + List<Object*> objects = {}; + + Location * edges[4] = { + nullptr, + nullptr, + nullptr, + nullptr, + }; }; diff --git a/backend/LocationFactory.cpp b/backend/LocationFactory.cpp index 98a0cd8..6e0b6b8 100644 --- a/backend/LocationFactory.cpp +++ b/backend/LocationFactory.cpp @@ -1,6 +1,6 @@ #include "LocationFactory.h" -Location * LocationFactory::create_location() { - return new Location(); +Location * LocationFactory::create_location(const char * name, const char * description) { + return new Location(name, description); } diff --git a/backend/LocationFactory.h b/backend/LocationFactory.h index e9b1bc1..a12bb0e 100644 --- a/backend/LocationFactory.h +++ b/backend/LocationFactory.h @@ -4,9 +4,6 @@ class LocationFactory { public: - static Location * create_location(); - -private: - LocationFactory() = delete; + Location * create_location(const char * name, const char * description); }; diff --git a/backend/Object.cpp b/backend/Object.cpp index 300e6ac..e14c780 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -1,15 +1,40 @@ +#include <string.h> #include <stdlib.h> #include "Object.h" +#include "util.h" + +Object::Object(const char * name, const char * description) { + this->set_name(name); + this->set_description(description); +} + Object::~Object() { - if (this->name != nullptr) { - free(const_cast<char *>(this->name)); - this->name = nullptr; - } - if (this->description != nullptr) { - free(const_cast<char *>(this->description)); - this->description = nullptr; - } + safe_free(this->name); + safe_free(this->description); +} + +void Object::set_name(const char * name) { + safe_free(this->name); + this->name = strdup(name); +} +const char * Object::get_name() { + return this->name; +} + +void Object::set_description(const char * description) { + safe_free(this->description); + this->description = strdup(description); +} +const char * Object::get_description() { + return this->description; +} + +void Object::set_hidden(bool hidden) { + this->hidden = hidden; +} +bool Object::get_hidden() { + return this->hidden; } diff --git a/backend/Object.h b/backend/Object.h index 789de25..92652c4 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -5,10 +5,21 @@ private: const char * name = nullptr; const char * description = nullptr; +public: + void set_name(const char * name); + const char * get_name(); + void set_description(const char * description); + const char * get_description(); + void set_hidden(bool hidden); + bool get_hidden(); + protected: - Object() = default; + Object(const char * name = "", const char * description = ""); virtual ~Object(); friend class ObjectFactory; +protected: + bool hidden = false; + }; diff --git a/backend/util.cpp b/backend/util.cpp new file mode 100644 index 0000000..4a55f63 --- /dev/null +++ b/backend/util.cpp @@ -0,0 +1,14 @@ +#include <stdlib.h> + +#include "util.h" + +void safe_free(void * & ptr) { + if (ptr == nullptr) return; + free(ptr); + ptr = nullptr; +} +void safe_free(const char * & ptr) { + auto x = static_cast<void *>(const_cast<char *>(ptr)); + safe_free(x); +} + diff --git a/backend/util.h b/backend/util.h new file mode 100644 index 0000000..1889698 --- /dev/null +++ b/backend/util.h @@ -0,0 +1,6 @@ +#pragma once + +void safe_free(void * & ptr); +void safe_free(const char * & ptr); + + |