diff options
| author | AndreaCatania | 2017-10-21 13:02:06 +0200 |
|---|---|---|
| committer | AndreaCatania | 2017-11-04 03:25:51 +0100 |
| commit | 7a9ca08f16c500aa0caccc21a8e42564f962971a (patch) | |
| tree | 3274c444cab28252757a1dde47995264ac2d2c66 /servers/physics_2d_server.cpp | |
| parent | f52ab8d86418a67ddee247ed7765e72935b0c57f (diff) | |
| download | godot-7a9ca08f16c500aa0caccc21a8e42564f962971a.tar.gz godot-7a9ca08f16c500aa0caccc21a8e42564f962971a.tar.zst godot-7a9ca08f16c500aa0caccc21a8e42564f962971a.zip | |
Diffstat (limited to 'servers/physics_2d_server.cpp')
| -rw-r--r-- | servers/physics_2d_server.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/servers/physics_2d_server.cpp b/servers/physics_2d_server.cpp index 671c31e6a..a27f2bb40 100644 --- a/servers/physics_2d_server.cpp +++ b/servers/physics_2d_server.cpp @@ -28,7 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "physics_2d_server.h" +#include "core/project_settings.h" #include "print_string.h" + Physics2DServer *Physics2DServer::singleton = NULL; void Physics2DDirectBodyState::integrate_forces() { @@ -691,3 +693,68 @@ Physics2DServer::~Physics2DServer() { singleton = NULL; } + +Vector<Physics2DServerManager::ClassInfo> Physics2DServerManager::physics_2d_servers; +int Physics2DServerManager::default_server_id = -1; +int Physics2DServerManager::default_server_priority = -1; +const String Physics2DServerManager::setting_property_name("physics/2d/physics_engine"); + +void Physics2DServerManager::on_servers_changed() { + + String physics_servers("DEFAULT"); + for (int i = get_servers_count() - 1; 0 <= i; --i) { + physics_servers += "," + get_server_name(i); + } + ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers)); +} + +void Physics2DServerManager::register_server(const String &p_name, CreatePhysics2DServerCallback p_creat_callback) { + + ERR_FAIL_COND(!p_creat_callback); + ERR_FAIL_COND(find_server_id(p_name) != -1); + physics_2d_servers.push_back(ClassInfo(p_name, p_creat_callback)); + on_servers_changed(); +} + +void Physics2DServerManager::set_default_server(const String &p_name, int p_priority) { + + const int id = find_server_id(p_name); + ERR_FAIL_COND(id == -1); // Not found + if (default_server_priority < p_priority) { + default_server_id = id; + default_server_priority = p_priority; + } +} + +int Physics2DServerManager::find_server_id(const String &p_name) { + + for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) { + if (p_name == physics_2d_servers[i].name) { + return i; + } + } + return -1; +} + +int Physics2DServerManager::get_servers_count() { + return physics_2d_servers.size(); +} + +String Physics2DServerManager::get_server_name(int p_id) { + ERR_FAIL_INDEX_V(p_id, get_servers_count(), ""); + return physics_2d_servers[p_id].name; +} + +Physics2DServer *Physics2DServerManager::new_default_server() { + ERR_FAIL_COND_V(default_server_id == -1, NULL); + return physics_2d_servers[default_server_id].create_callback(); +} + +Physics2DServer *Physics2DServerManager::new_server(const String &p_name) { + int id = find_server_id(p_name); + if (id == -1) { + return NULL; + } else { + return physics_2d_servers[id].create_callback(); + } +} |
