From 7bdb5e70eb1138c898f4f7382d2906ad6528090f Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 23 May 2024 15:55:47 +0200 Subject: [PATCH] feat: added get_players and redefined how spawnpoints are stored --- game_root.cpp | 17 +++++++++++++++-- game_root.hpp | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/game_root.cpp b/game_root.cpp index bfb3e7d..0348042 100644 --- a/game_root.cpp +++ b/game_root.cpp @@ -147,7 +147,8 @@ void GameRoot3D::register_spawn_point(SpawnPoint3D *spawn_point) { UtilityFunctions::push_error("Duplicate attempt to register spawnpoint '", spawn_point->get_path(), "'"); return; } - this->spawn_points.insert(spawn_point); + if(!this->spawn_points.has(spawn_point)) + this->spawn_points.push_back(spawn_point); } void GameRoot3D::unregister_spawn_point(SpawnPoint3D *spawn_point) { @@ -160,7 +161,7 @@ void GameRoot3D::unregister_spawn_point(SpawnPoint3D *spawn_point) { void GameRoot3D::place_player_at_spawnpoint(IPlayer *player) { if(this->spawn_points.is_empty()) return; - SpawnPoint3D *spawn_point = *this->spawn_points.begin(); + SpawnPoint3D *spawn_point = this->spawn_points[rng.randi() % this->spawn_points.size()]; player->spawn_at_position(spawn_point->get_global_transform()); } @@ -226,6 +227,18 @@ HashMap &GameRoot3D::get_levels() { return this->levels; } +IPlayer *GameRoot3D::get_player(uint32_t id) { + return this->players[id].second; +} + +Vector GameRoot3D::get_players() { + Vector players{}; + for(KeyValue> pair : this->players) { + players.push_back(pair.value.second); + } + return players; +} + void GameRoot3D::grab_singleton() { if(GameRoot3D::has_singleton()) { this->set_process_mode(PROCESS_MODE_DISABLED); diff --git a/game_root.hpp b/game_root.hpp index 79c795a..529397d 100644 --- a/game_root.hpp +++ b/game_root.hpp @@ -2,6 +2,7 @@ #define GAME_ROOT_HPP #include "game_mode.hpp" +#include "godot_cpp/classes/random_number_generator.hpp" #include "level.hpp" #include #include @@ -68,6 +69,8 @@ public: void set_first_boot_level(Ref level); Ref get_first_boot_level() const; HashMap &get_levels(); + IPlayer *get_player(uint32_t id); + Vector get_players(); protected: // attempt to make 'this' the current singleton instance void grab_singleton(); @@ -85,8 +88,9 @@ protected: HashMap> players{}; Ref game_mode{}; private: + RandomNumberGenerator rng{}; HashMap levels{}; - HashSet spawn_points{}; + Vector spawn_points{}; Ref first_boot_level{}; };