godot-cpp-utils
A collection of utility classes, functions and macros for use with Godot and GDExtension.
Loading...
Searching...
No Matches
game_root.hpp
1#ifndef UTILS_GAME_ROOT_HPP
2#define UTILS_GAME_ROOT_HPP
3
4#include "game_mode.hpp"
5#include "game_state.hpp"
6#include "level.hpp"
7#include <godot_cpp/classes/node.hpp>
8#include <godot_cpp/classes/packed_scene.hpp>
9#include <godot_cpp/classes/random_number_generator.hpp>
10#include <godot_cpp/templates/hash_map.hpp>
11#include <godot_cpp/templates/hash_set.hpp>
12#include <godot_cpp/templates/pair.hpp>
13#include <godot_cpp/templates/pair.hpp>
14#include <godot_cpp/templates/vector.hpp>
15
16namespace gd = godot;
17
18namespace utils {
19class PlayerInput;
20class IPlayer;
21class SpawnPoint3D;
22
28class GameRoot3D : public gd::Node {
29 GDCLASS(GameRoot3D, gd::Node);
30 static void _bind_methods();
31public:
33 static GameRoot3D *get_singleton();
35 static bool has_singleton();
36
37 virtual void _enter_tree() override;
38 virtual void _ready() override;
39 virtual void _exit_tree() override;
40
50 void remove_player(uint32_t player_id);
51 // calls remove_player for every used player input slot
52 void remove_all_players();
58 bool initialize_player(IPlayer *player, uint32_t id);
59
63 void reset_game_mode();
64
66 Level3D *load_level(gd::Ref<gd::PackedScene> level);
71 Level3D *load_level_at(gd::Ref<gd::PackedScene> level, gd::Transform3D at);
73 void unload_all_levels();
82 void replace_levels(gd::Ref<gd::PackedScene> level);
84 void register_spawn_point(SpawnPoint3D *spawn_point);
85 // remove a spawnpoint so it can't be used to spawn players
86 void unregister_spawn_point(SpawnPoint3D *spawn_point);
87 void place_player_at_spawnpoint(IPlayer *player);
88 void player_despawned(uint32_t id);
89
94 void set_game_mode(gd::Ref<gd::PackedScene> prototype);
96 GameMode *get_game_mode() const;
103 gd::HashMap<gd::StringName, Level3D *> &get_levels();
105 IPlayer *get_player(uint32_t id);
107 gd::Vector<IPlayer*> get_players();
108 void set_first_boot_level(gd::Ref<gd::PackedScene> level);
109 gd::Ref<gd::PackedScene> get_first_boot_level() const;
110 void set_game_state_prototype(gd::Ref<GameState> game_state);
111 gd::Ref<GameState> get_game_state_prototype() const;
112 gd::RandomNumberGenerator &get_rng();
113protected:
115 void grab_singleton();
120 void release_singleton();
122 uint32_t find_empty_player_slot() const;
124 IPlayer *spawn_player(uint32_t id);
126 void level_unloaded(gd::StringName scene_path);
128 static bool is_valid_level(gd::Ref<gd::PackedScene> &level);
129private:
130 static GameRoot3D *singleton_instance;
135 uint32_t next_player_id{1};
140 gd::HashMap<uint32_t, gd::Pair<PlayerInput*, IPlayer*>> players{};
145 gd::RandomNumberGenerator rng{};
152 gd::HashMap<gd::StringName, Level3D*> levels{};
155 gd::Vector<SpawnPoint3D*> spawn_points{};
169 gd::Ref<GameState> game_state{};
174 gd::Ref<gd::PackedScene> first_boot_level{};
181 gd::Ref<GameState> game_state_prototype{};
182};
183}
184
185#endif // !UTILS_GAME_ROOT_HPP
Stores session-relevant data.
Definition game_mode.hpp:16
The root of a game.
Definition game_root.hpp:28
void grab_singleton()
Attempt to make 'this' the current singleton instance.
Definition game_root.cpp:257
GameState * get_game_state() const
Get the current active game state.
Definition game_root.cpp:207
gd::Vector< SpawnPoint3D * > spawn_points
All currently available spawn points.
Definition game_root.hpp:155
static bool has_singleton()
returns true if there is currently a singleton active for GameRoot
Definition game_root.cpp:35
void register_spawn_point(SpawnPoint3D *spawn_point)
Register a spawnpoint for use when spawning players.
Definition game_root.cpp:148
IPlayer * get_player(uint32_t id)
Get the player instance associated with id.
Definition game_root.cpp:215
IPlayer * spawn_player(uint32_t id)
Spawn a player to be associated with id.
Definition game_root.cpp:283
void unload_all_levels()
Unload all currently loaded levels.
Definition game_root.cpp:135
void replace_levels(gd::Ref< gd::PackedScene > level)
Replace all currently loaded levels with a new level.
Definition game_root.cpp:143
gd::RandomNumberGenerator rng
Global random number generator.
Definition game_root.hpp:145
GameMode * game_mode
Current active gamemode.
Definition game_root.hpp:162
static bool is_valid_level(gd::Ref< gd::PackedScene > &level)
Check if a scene is a valid level.
Definition game_root.cpp:307
gd::HashMap< gd::StringName, Level3D * > levels
All currently active levels.
Definition game_root.hpp:152
void player_input_connected()
Instantiate a new PlayerInput.
Definition game_root.cpp:53
bool initialize_player(IPlayer *player, uint32_t id)
Initialize and register a player instance.
Definition game_root.cpp:84
GameMode * get_game_mode() const
get the current active game mode.
Definition game_root.cpp:203
void remove_player(uint32_t player_id)
Force-disconnect a player.
Definition game_root.cpp:60
void set_game_mode(gd::Ref< gd::PackedScene > prototype)
Override the current gamemode.
Definition game_root.cpp:177
gd::HashMap< gd::StringName, Level3D * > & get_levels()
Returns all currently active levels.
Definition game_root.cpp:211
uint32_t find_empty_player_slot() const
Find a Player Input device not yet associated with a player.
Definition game_root.cpp:274
gd::Ref< gd::PackedScene > first_boot_level
The level to boot into on startup.
Definition game_root.hpp:174
Level3D * load_level(gd::Ref< gd::PackedScene > level)
shorthand for load_level(level, Transform3D())
Definition game_root.cpp:102
gd::HashMap< uint32_t, gd::Pair< PlayerInput *, IPlayer * > > players
All players by id by input device.
Definition game_root.hpp:140
static GameRoot3D * get_singleton()
get the current active singleton instance of GameRoot
Definition game_root.cpp:31
Level3D * load_level_at(gd::Ref< gd::PackedScene > level, gd::Transform3D at)
Load a level, only works if 'level' is a valid scene where the root Node can cast to 'Level3D'.
Definition game_root.cpp:106
gd::Ref< GameState > game_state_prototype
The default game state data.
Definition game_root.hpp:181
void release_singleton()
Attempt to stop being the active singleton instance.
Definition game_root.cpp:266
void reset_game_mode()
Un-set game mode.
Definition game_root.cpp:98
gd::Vector< IPlayer * > get_players()
Get all players in a list.
Definition game_root.cpp:219
void level_unloaded(gd::StringName scene_path)
Callback for a level exiting the tree.
Definition game_root.cpp:303
uint32_t next_player_id
Next available player ID.
Definition game_root.hpp:135
gd::Ref< GameState > game_state
Active game state.
Definition game_root.hpp:169
Parent class for saved game state.
Definition game_state.hpp:11
Interface required for player nodes.
Definition player.hpp:19
3D level root to be used with GameRoot3D.
Definition level.hpp:14
A location in the game world that the player can spawn at.
Definition spawn_point.hpp:13