diff options
-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 | ||||
-rw-r--r-- | frontend/GameController.cpp | 2 | ||||
-rw-r--r-- | frontend/GameData.cpp | 22 | ||||
-rw-r--r-- | frontend/GameData.h | 6 | ||||
-rw-r--r-- | frontend/cmd/cheat.cpp | 2 | ||||
-rw-r--r-- | frontend/cmd/go.cpp | 6 | ||||
-rw-r--r-- | frontend/cmd/query.cpp | 2 | ||||
-rw-r--r-- | frontend/generate_dungeon.cpp | 14 | ||||
-rw-r--r-- | frontend/load_dungeon.cpp | 19 |
12 files changed, 52 insertions, 50 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); } diff --git a/frontend/GameController.cpp b/frontend/GameController.cpp index e015348..81085ec 100644 --- a/frontend/GameController.cpp +++ b/frontend/GameController.cpp @@ -75,7 +75,7 @@ void GameController::gameloop() { lprtf("Wat is de naam van je karakter?\n"); player.name = rl().c_str(); player.set_location(dungeon->get_start_location()); - player.equip(static_cast<WeaponObject *>(gamedata.create_object("Dolk"))); + player.equip(static_cast<WeaponObject *>(gamedata.create_object("Dolk").release())); cmdset_default(); diff --git a/frontend/GameData.cpp b/frontend/GameData.cpp index 635073a..f281737 100644 --- a/frontend/GameData.cpp +++ b/frontend/GameData.cpp @@ -19,7 +19,7 @@ GameData & GameData::get_instance() { return instance; } -Enemy * GameData::create_enemy(const string & name) const { +unique_ptr<Enemy> GameData::create_enemy(const string & name) const { static DBStatement query = this->db.prepare(R"( select naam, @@ -43,18 +43,18 @@ Enemy * GameData::create_enemy(const string & name) const { auto enemy = unique_ptr<Enemy>{ EnemyFactory::create_enemy(row.col<const char *>(0), row.col<const char *>(1)) }; int object_count = RNG::get().rand_int(Range<int> { row.col<int>(2), row.col<int>(3) }); for (const string & name : this->random_objects(object_count)) - enemy->add_hidden_object(this->create_object(name)); + enemy->add_hidden_object(this->create_object(name).release()); enemy->set_health(row.col<int>(4)); enemy->set_attack(static_cast<float>(row.col<int>(5)) / 100); enemy->set_damage({ row.col<int>(6), row.col<int>(7) }); - return enemy.release(); + return enemy; } catch (Exception & e) { printf("Fout bij aanmaken van vijand: %s\n", e.what()); - return EnemyFactory::create_enemy(name.c_str()); + return unique_ptr<Enemy>{ EnemyFactory::create_enemy(name.c_str()) }; } } -Object * GameData::create_object(const string & name) const { +unique_ptr<Object> GameData::create_object(const string & name) const { static DBStatement query = this->db.prepare(R"( select omschrijving, @@ -72,19 +72,19 @@ Object * GameData::create_object(const string & name) const { try { auto row = query.row(); - return ObjectFactory::create_object({ + return unique_ptr<Object>(ObjectFactory::create_object({ .name = name.c_str(), .description = row.col<const char *>(0, nullptr), .type = row.col<const char *>(1, nullptr), .value = { row.col<int>(2), row.col<int>(3) }, .protection = row.col<unsigned int>(4), - }); + })); } catch (Exception & e) { - return ObjectFactory::create_object(name.c_str()); + return unique_ptr<Object>{ ObjectFactory::create_object(name.c_str()) }; } } -Location * GameData::create_location(const string & name) const{ +unique_ptr<Location> GameData::create_location(const string & name) const{ static DBStatement query = this->db.prepare(R"( select naam, @@ -99,9 +99,9 @@ Location * GameData::create_location(const string & name) const{ try { auto row = query.row(); - return LocationFactory::create_location(row.col<const char *>(0), row.col<const char *>(1)); + return unique_ptr<Location>{ LocationFactory::create_location(row.col<const char *>(0), row.col<const char *>(1)) }; } catch (...) { - return LocationFactory::create_location(name.c_str()); + return unique_ptr<Location>{ LocationFactory::create_location(name.c_str()) }; } } diff --git a/frontend/GameData.h b/frontend/GameData.h index 172f7d5..54d33c7 100644 --- a/frontend/GameData.h +++ b/frontend/GameData.h @@ -14,9 +14,9 @@ private: DB db { "kerkersendraken.db" }; public: - Enemy * create_enemy(const std::string & name) const; - Object * create_object(const std::string & name) const; - Location * create_location(const std::string & name) const; + std::unique_ptr<Enemy> create_enemy(const std::string & name) const; + std::unique_ptr<Object> create_object(const std::string & name) const; + std::unique_ptr<Location> create_location(const std::string & name) const; public: void leaderbord_add(const std::string & name, unsigned int gold); diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp index 981bca1..59fb13c 100644 --- a/frontend/cmd/cheat.cpp +++ b/frontend/cmd/cheat.cpp @@ -12,7 +12,7 @@ constexpr const char * cmd_give_key = "Manifesteer"; void GameController::cmd_give(string & item_name) { Player & player = this->dungeon->get_player(); GameData & gamedata = GameData::get_instance(); - auto object = unique_ptr<Object>(gamedata.create_object(item_name)); + auto object = gamedata.create_object(item_name); lprtf("Object aangemaakt: %s\n", object->get_displayname().c_str()); player.inventory.push_back(object.release()); } diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp index 6fb9105..0452897 100644 --- a/frontend/cmd/go.cpp +++ b/frontend/cmd/go.cpp @@ -22,13 +22,11 @@ void GameController::cmd_go(string & direction_str) { Player & player = this->dungeon->get_player(); Direction direction = direction_map.at(direction_str); - Location * next_location = player.get_location().get_exit(direction); - if (next_location == nullptr) - throw Exception("er is geen uitgang in deze richting"); + Location & next_location = player.get_location().get_exit(direction); this->dungeon->update(); if (!player.is_dead()) - player.set_location(*next_location); + player.set_location(next_location); } diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index 6de7f56..66f1bb2 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -39,7 +39,7 @@ void GameController::cmd_query(string &) { lprtf("Uitgangen: "); bool first = true; for (Direction direction : DIRECTIONS) { - if (location.get_exit(direction) == nullptr) continue; + if (!location.has_exit(direction)) continue; if (!first) lprtf(", "); lprtf("%s", direction_map.at(direction).c_str()); first = false; diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp index 6393abb..99b30a3 100644 --- a/frontend/generate_dungeon.cpp +++ b/frontend/generate_dungeon.cpp @@ -44,22 +44,22 @@ unique_ptr<Dungeon> generate_dungeon() { size_t index = 0; RNG & rng = RNG::get(); for (const string & name : locations) { - Location * location = gd.create_location(name); + auto location = gd.create_location(name); if (index % 3 == 0) { - location->add_enemy(gd.create_enemy(gd.random_enemy())); + location->add_enemy(gd.create_enemy(gd.random_enemy()).release()); } for (const string & object : gd.random_objects(rng.rand_int(Range<int> { 0, 3 }))) { - location->add_visible_object(gd.create_object(object)); + location->add_visible_object(gd.create_object(object).release()); } for (const string & object : gd.random_objects(rng.rand_int(Range<int> { 0, 2 }))) { - location->add_hidden_object(gd.create_object(object)); + location->add_hidden_object(gd.create_object(object).release()); } auto & temp = temp_map[index]; - temp.location = location; + temp.location = location.get(); for (Direction direction : shuffled_directions()) { // skip over already connected edges if (temp.edges[direction] >= 0) continue; @@ -75,7 +75,7 @@ unique_ptr<Dungeon> generate_dungeon() { if (rng.rand_double() < 0.8) break; } - dungeon->add_location(location); + dungeon->add_location(location.release()); index++; } @@ -83,7 +83,7 @@ unique_ptr<Dungeon> generate_dungeon() { for (Direction direction : DIRECTIONS) { unsigned id = temp.edges[direction]; if (temp.edges[direction] < 0) continue; - temp.location->set_exit(direction, temp_map[id].location); + temp.location->set_exit(direction, *temp_map[id].location); } } diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp index b19b541..667a151 100644 --- a/frontend/load_dungeon.cpp +++ b/frontend/load_dungeon.cpp @@ -47,30 +47,27 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) { map<unsigned, TempData> temp_map; for (xml_node & tag : locations) { - Location * location = LocationFactory::create_location( + auto location = unique_ptr<Location>(LocationFactory::create_location( tag.text().as_string(), tag.child("beschrijving").text().as_string() - ); + )); vector<string> objects_hidden = str_split(tag.attribute("objectenverborgen").as_string(), ";"); for (string & name : objects_hidden) { - Object * object = gamedata.create_object(name); - location->add_hidden_object(object); + location->add_hidden_object(gamedata.create_object(name).release()); } vector<string> objects_visible = str_split(tag.attribute("objectenzichtbaar").as_string(), ";"); for (string & name : objects_visible) { - Object * object = gamedata.create_object(name); - location->add_visible_object(object); + location->add_visible_object(gamedata.create_object(name).release()); } vector<string> enemies = str_split(tag.attribute("vijand").as_string(), ";"); for (string & name : enemies) { - Enemy * enemy = gamedata.create_enemy(name); - location->add_enemy(enemy); + location->add_enemy(gamedata.create_enemy(name).release()); } temp_map[tag.attribute("id").as_uint()] = { - .location = location, + .location = location.get(), .edges = { [Direction::NORTH] = tag.attribute("noord").as_uint(0), [Direction::EAST] = tag.attribute("oost").as_uint(0), @@ -78,7 +75,7 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) { [Direction::WEST] = tag.attribute("west").as_uint(0), }, }; - dungeon->add_location(location); + dungeon->add_location(location.release()); } // connect edges after creating all locations @@ -87,7 +84,7 @@ unique_ptr<Dungeon> load_dungeon(const string & filename) { unsigned id = temp.edges[direction]; if (temp.edges[direction] == 0) continue; if (!temp_map.contains(id)) continue; - temp.location->set_exit(direction, temp_map[id].location); + temp.location->set_exit(direction, *temp_map[id].location); } } |