multiplayer_spawner.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef MULTIPLAYER_SPAWNER_H
  31. #define MULTIPLAYER_SPAWNER_H
  32. #include "scene_replication_config.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/variant/typed_array.h"
  35. #include "scene/main/node.h"
  36. #include "scene/resources/packed_scene.h"
  37. class MultiplayerSpawner : public Node {
  38. GDCLASS(MultiplayerSpawner, Node);
  39. public:
  40. enum {
  41. INVALID_ID = 0xFF,
  42. };
  43. private:
  44. struct SpawnableScene {
  45. String path;
  46. Ref<PackedScene> cache;
  47. };
  48. LocalVector<SpawnableScene> spawnable_scenes;
  49. HashSet<ResourceUID::ID> spawnable_ids;
  50. NodePath spawn_path;
  51. struct SpawnInfo {
  52. Variant args;
  53. int id = INVALID_ID;
  54. SpawnInfo(Variant p_args, int p_id) {
  55. id = p_id;
  56. args = p_args;
  57. }
  58. SpawnInfo() {}
  59. };
  60. ObjectID spawn_node;
  61. HashMap<ObjectID, SpawnInfo> tracked_nodes;
  62. uint32_t spawn_limit = 0;
  63. Callable spawn_function;
  64. void _update_spawn_node();
  65. void _track(Node *p_node, const Variant &p_argument, int p_scene_id = INVALID_ID);
  66. void _node_added(Node *p_node);
  67. void _node_exit(ObjectID p_id);
  68. void _spawn_notify(ObjectID p_id);
  69. Vector<String> _get_spawnable_scenes() const;
  70. void _set_spawnable_scenes(const Vector<String> &p_scenes);
  71. protected:
  72. static void _bind_methods();
  73. void _notification(int p_what);
  74. #ifdef TOOLS_ENABLED
  75. bool _set(const StringName &p_name, const Variant &p_value);
  76. bool _get(const StringName &p_name, Variant &r_ret) const;
  77. void _get_property_list(List<PropertyInfo> *p_list) const;
  78. #endif
  79. public:
  80. PackedStringArray get_configuration_warnings() const override;
  81. Node *get_spawn_node() const {
  82. return spawn_node.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(spawn_node)) : nullptr;
  83. }
  84. void add_spawnable_scene(const String &p_path);
  85. int get_spawnable_scene_count() const;
  86. String get_spawnable_scene(int p_idx) const;
  87. void clear_spawnable_scenes();
  88. NodePath get_spawn_path() const;
  89. void set_spawn_path(const NodePath &p_path);
  90. uint32_t get_spawn_limit() const { return spawn_limit; }
  91. void set_spawn_limit(uint32_t p_limit) { spawn_limit = p_limit; }
  92. void set_spawn_function(Callable p_spawn_function) { spawn_function = p_spawn_function; }
  93. Callable get_spawn_function() const { return spawn_function; }
  94. const Variant get_spawn_argument(const ObjectID &p_id) const;
  95. int find_spawnable_scene_index_from_object(const ObjectID &p_id) const;
  96. int find_spawnable_scene_index_from_path(const String &p_path) const;
  97. Node *spawn(const Variant &p_data = Variant());
  98. Node *instantiate_custom(const Variant &p_data);
  99. Node *instantiate_scene(int p_idx);
  100. MultiplayerSpawner() {}
  101. };
  102. #endif // MULTIPLAYER_SPAWNER_H