diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 19:59:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 19:59:38 +0100 |
commit | 6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (patch) | |
tree | c2505409c68d554b1e776cdb0c8104af54d375bf /backend/Location.cpp | |
parent | c1d43cddee94dd370078f755d33147c9a8181852 (diff) |
create backend string class
Diffstat (limited to 'backend/Location.cpp')
-rw-r--r-- | backend/Location.cpp | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/backend/Location.cpp b/backend/Location.cpp index 31df1e3..d3227cf 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -1,43 +1,40 @@ -#include <string.h> - +#include "RNG.h" #include "Location.h" #include "ListIterator.h" #include "Enemy.h" #include "Object.h" #include "util.h" -Location::Location(const char * name, const char * description) { - this->set_name(name); - this->set_description(description); +Direction random_direction() { + return DIRECTIONS[RNG::get().rand_int(4)]; +} + +Direction random_direction(const Location & location) { + List<Direction> valid = {}; + for (Direction direction : DIRECTIONS) { + if (location.get_exit(direction) == nullptr) continue; + valid.push_back(direction); + } + return valid[RNG::get().rand_int(valid.size())]; } +Location::Location(const String & name, const String & description) : name(name), description(description) { } + Location::~Location() { - safe_free(this->name); - safe_free(this->description); safe_free(this->enemies); safe_free(this->objects); } -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_name(const String & name) { this->name = name; } +const String & Location::get_name() const { 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_description(const String & description) { this->description = description; } +const String & Location::get_description() const { return this->description; } void Location::set_exit(Direction dir, Location * location) { this->edges[dir] = location; } -Location * Location::get_exit(Direction dir) { +Location * Location::get_exit(Direction dir) const { return this->edges[dir]; } @@ -47,14 +44,17 @@ void Location::add_object(Object * object) { void Location::remove_object(Object * object) { this->objects.remove(object); } -ListRange<Object *> Location::get_objects() { +ListRange<Object *> Location::get_objects() const { return this->objects.range(); } void Location::add_enemy(Enemy * enemy) { this->enemies.push_back(enemy); } -ListRange<Enemy *> Location::get_enemies() { +void Location::remove_enemy(Enemy * enemy) { + this->enemies.remove(enemy); +} +ListRange<Enemy *> Location::get_enemies() const { return this->enemies.range(); } |