diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-03 12:43:18 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-03 12:43:18 +0100 |
commit | 5f5bf3ac7a85f8cde512ec44e3d55b93fb4354ba (patch) | |
tree | 517b955e4e2ecddf185dfd13dcdc4bcf57f4588a /backend | |
parent | ed78baff64fe45479ca6c480d985ce0f9c0c9515 (diff) |
remove more pointers from frontend
Diffstat (limited to 'backend')
-rw-r--r-- | backend/Dungeon.cpp | 6 | ||||
-rw-r--r-- | backend/Location.cpp | 16 | ||||
-rw-r--r-- | backend/Location.h | 5 | ||||
-rw-r--r-- | backend/TeleportConsumableObject.cpp | 2 |
4 files changed, 18 insertions, 11 deletions
diff --git a/backend/Dungeon.cpp b/backend/Dungeon.cpp index ce9ba9f..3fe9a58 100644 --- a/backend/Dungeon.cpp +++ b/backend/Dungeon.cpp @@ -44,9 +44,9 @@ void Dungeon::update_movement() { lprtf(":: De vijanden bewegen ::\n"); Direction direction = random_direction(*location); location->remove_enemy(enemy); - Location * new_location = location->get_exit(direction); - new_location->add_enemy(enemy); - if (&this->player.get_location() == new_location) + Location & new_location = location->get_exit(direction); + new_location.add_enemy(enemy); + if (&this->player.get_location() == &new_location) lprtf("%s komt de huidige locatie binnen\n", enemy->get_name().c_str()); moved = true; } diff --git a/backend/Location.cpp b/backend/Location.cpp index 2177d8a..2de4676 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -3,6 +3,7 @@ #include "ListIterator.h" #include "Enemy.h" #include "Object.h" +#include "Exception.h" Direction operator - (const Direction & rhs) { return static_cast<Direction>((rhs + 2) % 4); @@ -15,7 +16,7 @@ Direction random_direction() { Direction random_direction(const Location & location) { List<Direction> valid = {}; for (Direction direction : DIRECTIONS) { - if (location.get_exit(direction) == nullptr) continue; + if (!location.has_exit(direction)) continue; valid.push_back(direction); } return valid[RNG::get().rand_int(valid.size())]; @@ -29,11 +30,16 @@ const String & Location::get_name() const { return this->name; } 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; +void Location::set_exit(Direction dir, Location & location) { + this->edges[dir] = &location; } -Location * Location::get_exit(Direction dir) const { - return this->edges[dir]; +bool Location::has_exit(Direction dir) const { + return this->edges[dir] != nullptr; +} +Location & Location::get_exit(Direction dir) const { + if (!this->has_exit(dir)) + throw Exception("er is geen uitgang in deze richting"); + return *this->edges[dir]; } void Location::add_visible_object(Object * object) { diff --git a/backend/Location.h b/backend/Location.h index 6f701ad..1eab055 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -28,8 +28,9 @@ public: void set_description(const String & description); const String & get_description() const; - void set_exit(Direction dir, Location * location = nullptr); - Location * get_exit(Direction dir) const; + void set_exit(Direction dir, Location & location); + Location & get_exit(Direction dir) const; + bool has_exit(Direction dir) const; void add_visible_object(Object *); void remove_visible_object(Object *); diff --git a/backend/TeleportConsumableObject.cpp b/backend/TeleportConsumableObject.cpp index 7d1803f..9ae03fb 100644 --- a/backend/TeleportConsumableObject.cpp +++ b/backend/TeleportConsumableObject.cpp @@ -7,7 +7,7 @@ void TeleportConsumableObject::consume(Player & player) { unsigned hops = RNG::get().rand_int(this->get_potency()); Location * location = &player.get_location(); for (size_t i = 0; i < hops; i++) { - location = location->get_exit(random_direction(*location)); + location = &location->get_exit(random_direction(*location)); } player.set_location(*location); } |