world_2d.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* world_2d.cpp */
  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. #include "world_2d.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/2d/visible_on_screen_notifier_2d.h"
  33. #include "servers/rendering_server.h"
  34. #ifndef NAVIGATION_2D_DISABLED
  35. #include "servers/navigation_server_2d.h"
  36. #endif // NAVIGATION_2D_DISABLED
  37. RID World2D::get_canvas() const {
  38. return canvas;
  39. }
  40. #ifndef NAVIGATION_2D_DISABLED
  41. RID World2D::get_navigation_map() const {
  42. if (navigation_map.is_null()) {
  43. navigation_map = NavigationServer2D::get_singleton()->map_create();
  44. NavigationServer2D::get_singleton()->map_set_active(navigation_map, true);
  45. NavigationServer2D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_GET("navigation/2d/default_cell_size"));
  46. NavigationServer2D::get_singleton()->map_set_merge_rasterizer_cell_scale(navigation_map, GLOBAL_GET("navigation/2d/merge_rasterizer_cell_scale"));
  47. NavigationServer2D::get_singleton()->map_set_use_edge_connections(navigation_map, GLOBAL_GET("navigation/2d/use_edge_connections"));
  48. NavigationServer2D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_GET("navigation/2d/default_edge_connection_margin"));
  49. NavigationServer2D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_GET("navigation/2d/default_link_connection_radius"));
  50. }
  51. return navigation_map;
  52. }
  53. #endif // NAVIGATION_2D_DISABLED
  54. #ifndef PHYSICS_2D_DISABLED
  55. RID World2D::get_space() const {
  56. if (space.is_null()) {
  57. space = PhysicsServer2D::get_singleton()->space_create();
  58. PhysicsServer2D::get_singleton()->space_set_active(space, true);
  59. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY, GLOBAL_GET("physics/2d/default_gravity"));
  60. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_GET("physics/2d/default_gravity_vector"));
  61. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_LINEAR_DAMP, GLOBAL_GET("physics/2d/default_linear_damp"));
  62. PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_GET("physics/2d/default_angular_damp"));
  63. }
  64. return space;
  65. }
  66. PhysicsDirectSpaceState2D *World2D::get_direct_space_state() {
  67. return PhysicsServer2D::get_singleton()->space_get_direct_state(get_space());
  68. }
  69. #endif // PHYSICS_2D_DISABLED
  70. void World2D::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("get_canvas"), &World2D::get_canvas);
  72. ADD_PROPERTY(PropertyInfo(Variant::RID, "canvas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_canvas");
  73. #ifndef NAVIGATION_2D_DISABLED
  74. ClassDB::bind_method(D_METHOD("get_navigation_map"), &World2D::get_navigation_map);
  75. ADD_PROPERTY(PropertyInfo(Variant::RID, "navigation_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_navigation_map");
  76. #endif // NAVIGATION_2D_DISABLED
  77. #ifndef PHYSICS_2D_DISABLED
  78. ClassDB::bind_method(D_METHOD("get_space"), &World2D::get_space);
  79. ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state);
  80. ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_space");
  81. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState2D", PROPERTY_USAGE_NONE), "", "get_direct_space_state");
  82. #endif // PHYSICS_2D_DISABLED
  83. }
  84. void World2D::register_viewport(Viewport *p_viewport) {
  85. viewports.insert(p_viewport);
  86. }
  87. void World2D::remove_viewport(Viewport *p_viewport) {
  88. viewports.erase(p_viewport);
  89. }
  90. World2D::World2D() {
  91. canvas = RenderingServer::get_singleton()->canvas_create();
  92. }
  93. World2D::~World2D() {
  94. ERR_FAIL_NULL(RenderingServer::get_singleton());
  95. RenderingServer::get_singleton()->free(canvas);
  96. #ifndef NAVIGATION_2D_DISABLED
  97. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  98. if (navigation_map.is_valid()) {
  99. NavigationServer2D::get_singleton()->free(navigation_map);
  100. }
  101. #endif // NAVIGATION_2D_DISABLED
  102. #ifndef PHYSICS_2D_DISABLED
  103. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  104. if (space.is_valid()) {
  105. PhysicsServer2D::get_singleton()->free(space);
  106. }
  107. #endif // PHYSICS_2D_DISABLED
  108. }