From 9283e1eb66d6ff96b02f317e28cb6ff060953cdf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 29 Oct 2024 20:01:27 +0100 Subject: WIP load XML --- backend/CMakeLists.txt | 2 ++ backend/Dungeon.cpp | 4 ++++ backend/Dungeon.h | 8 ++++++++ backend/Location.cpp | 38 ++++++++++++++++++++++++++++++++++++++ backend/Location.h | 36 ++++++++++++++++++++++++++++++++++-- backend/LocationFactory.cpp | 4 ++-- backend/LocationFactory.h | 5 +---- backend/Object.cpp | 41 +++++++++++++++++++++++++++++++++-------- backend/Object.h | 13 ++++++++++++- backend/util.cpp | 14 ++++++++++++++ backend/util.h | 6 ++++++ 11 files changed, 154 insertions(+), 17 deletions(-) create mode 100644 backend/Location.cpp create mode 100644 backend/util.cpp create mode 100644 backend/util.h (limited to 'backend') 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 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 + +#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 enemies = {}; + List 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 #include #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(this->name)); - this->name = nullptr; - } - if (this->description != nullptr) { - free(const_cast(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 + +#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(const_cast(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); + + -- cgit v1.2.3