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