| 1 | /**************************************************************************/ |
| 2 | /* multiplayer_spawner.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef MULTIPLAYER_SPAWNER_H |
| 32 | #define MULTIPLAYER_SPAWNER_H |
| 33 | |
| 34 | #include "scene_replication_config.h" |
| 35 | |
| 36 | #include "core/templates/local_vector.h" |
| 37 | #include "core/variant/typed_array.h" |
| 38 | #include "scene/main/node.h" |
| 39 | #include "scene/resources/packed_scene.h" |
| 40 | |
| 41 | class MultiplayerSpawner : public Node { |
| 42 | GDCLASS(MultiplayerSpawner, Node); |
| 43 | |
| 44 | public: |
| 45 | enum { |
| 46 | INVALID_ID = 0xFF, |
| 47 | }; |
| 48 | |
| 49 | private: |
| 50 | struct SpawnableScene { |
| 51 | String path; |
| 52 | Ref<PackedScene> cache; |
| 53 | }; |
| 54 | |
| 55 | LocalVector<SpawnableScene> spawnable_scenes; |
| 56 | |
| 57 | HashSet<ResourceUID::ID> spawnable_ids; |
| 58 | NodePath spawn_path; |
| 59 | |
| 60 | struct SpawnInfo { |
| 61 | Variant args; |
| 62 | int id = INVALID_ID; |
| 63 | SpawnInfo(Variant p_args, int p_id) { |
| 64 | id = p_id; |
| 65 | args = p_args; |
| 66 | } |
| 67 | SpawnInfo() {} |
| 68 | }; |
| 69 | |
| 70 | ObjectID spawn_node; |
| 71 | HashMap<ObjectID, SpawnInfo> tracked_nodes; |
| 72 | uint32_t spawn_limit = 0; |
| 73 | Callable spawn_function; |
| 74 | |
| 75 | void _update_spawn_node(); |
| 76 | void _track(Node *p_node, const Variant &p_argument, int p_scene_id = INVALID_ID); |
| 77 | void _node_added(Node *p_node); |
| 78 | void _node_exit(ObjectID p_id); |
| 79 | void _spawn_notify(ObjectID p_id); |
| 80 | |
| 81 | Vector<String> _get_spawnable_scenes() const; |
| 82 | void _set_spawnable_scenes(const Vector<String> &p_scenes); |
| 83 | |
| 84 | protected: |
| 85 | static void _bind_methods(); |
| 86 | void _notification(int p_what); |
| 87 | |
| 88 | #ifdef TOOLS_ENABLED |
| 89 | bool _set(const StringName &p_name, const Variant &p_value); |
| 90 | bool _get(const StringName &p_name, Variant &r_ret) const; |
| 91 | void _get_property_list(List<PropertyInfo> *p_list) const; |
| 92 | #endif |
| 93 | public: |
| 94 | PackedStringArray get_configuration_warnings() const override; |
| 95 | |
| 96 | Node *get_spawn_node() const { |
| 97 | return spawn_node.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(spawn_node)) : nullptr; |
| 98 | } |
| 99 | |
| 100 | void add_spawnable_scene(const String &p_path); |
| 101 | int get_spawnable_scene_count() const; |
| 102 | String get_spawnable_scene(int p_idx) const; |
| 103 | void clear_spawnable_scenes(); |
| 104 | |
| 105 | NodePath get_spawn_path() const; |
| 106 | void set_spawn_path(const NodePath &p_path); |
| 107 | uint32_t get_spawn_limit() const { return spawn_limit; } |
| 108 | void set_spawn_limit(uint32_t p_limit) { spawn_limit = p_limit; } |
| 109 | void set_spawn_function(Callable p_spawn_function) { spawn_function = p_spawn_function; } |
| 110 | Callable get_spawn_function() const { return spawn_function; } |
| 111 | |
| 112 | const Variant get_spawn_argument(const ObjectID &p_id) const; |
| 113 | int find_spawnable_scene_index_from_object(const ObjectID &p_id) const; |
| 114 | int find_spawnable_scene_index_from_path(const String &p_path) const; |
| 115 | Node *spawn(const Variant &p_data = Variant()); |
| 116 | Node *instantiate_custom(const Variant &p_data); |
| 117 | Node *instantiate_scene(int p_idx); |
| 118 | |
| 119 | MultiplayerSpawner() {} |
| 120 | }; |
| 121 | |
| 122 | #endif // MULTIPLAYER_SPAWNER_H |
| 123 | |