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 /frontend | |
parent | ed78baff64fe45479ca6c480d985ce0f9c0c9515 (diff) |
remove more pointers from frontend
Diffstat (limited to 'frontend')
-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 |
8 files changed, 34 insertions, 39 deletions
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); } } |