| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  collision_object_2d.cpp                                              */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                    http://www.godotengine.org                         */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2017-01-01 22:01:57 +01:00
										 |  |  | /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							| 
									
										
										
										
											2017-04-08 00:11:42 +02:00
										 |  |  | /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | #include "collision_object_2d.h"
 | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | #include "scene/scene_string_names.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "servers/physics_2d_server.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::_notification(int p_what) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	switch (p_what) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 		case NOTIFICATION_ENTER_TREE: { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-14 10:31:38 -03:00
										 |  |  | 			if (area) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->area_set_transform(rid, get_global_transform()); | 
					
						
							| 
									
										
										
										
											2014-08-14 10:31:38 -03:00
										 |  |  | 			else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->body_set_state(rid, Physics2DServer::BODY_STATE_TRANSFORM, get_global_transform()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			RID space = get_world_2d()->get_space(); | 
					
						
							|  |  |  | 			if (area) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->area_set_space(rid, space); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			} else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->body_set_space(rid, space); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 			_update_pickable(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			//get space
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 		case NOTIFICATION_VISIBILITY_CHANGED: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			_update_pickable(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		case NOTIFICATION_TRANSFORM_CHANGED: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (area) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->area_set_transform(rid, get_global_transform()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->body_set_state(rid, Physics2DServer::BODY_STATE_TRANSFORM, get_global_transform()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 		case NOTIFICATION_EXIT_TREE: { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (area) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->area_set_space(rid, RID()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			} else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				Physics2DServer::get_singleton()->body_set_space(rid, RID()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | uint32_t CollisionObject2D::create_shape_owner(Object *p_owner) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	ShapeData sd; | 
					
						
							|  |  |  | 	uint32_t id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (shapes.size() == 0) { | 
					
						
							|  |  |  | 		id = 1; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		id = shapes.back()->key() + 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sd.owner = p_owner; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shapes[id] = sd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::remove_shape_owner(uint32_t owner) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(owner)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shape_owner_clear_shapes(owner); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shapes.erase(owner); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) { | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ShapeData &sd = shapes[p_owner]; | 
					
						
							|  |  |  | 	sd.disabled = p_disabled; | 
					
						
							|  |  |  | 	for (int i = 0; i < sd.shapes.size(); i++) { | 
					
						
							|  |  |  | 		if (area) { | 
					
						
							|  |  |  | 			Physics2DServer::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			Physics2DServer::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CollisionObject2D::is_shape_owner_disabled(uint32_t p_owner) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].disabled; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (area) | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 		return; //not for areas
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ShapeData &sd = shapes[p_owner]; | 
					
						
							|  |  |  | 	sd.one_way_collision = p_enable; | 
					
						
							|  |  |  | 	for (int i = 0; i < sd.shapes.size(); i++) { | 
					
						
							|  |  |  | 		Physics2DServer::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, p_enable); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CollisionObject2D::is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].one_way_collision; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::get_shape_owners(List<uint32_t> *r_owners) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		r_owners->push_back(E->key()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ShapeData &sd = shapes[p_owner]; | 
					
						
							|  |  |  | 	sd.xform = p_transform; | 
					
						
							|  |  |  | 	for (int i = 0; i < sd.shapes.size(); i++) { | 
					
						
							|  |  |  | 		if (area) { | 
					
						
							| 
									
										
										
										
											2017-06-24 12:33:52 -03:00
										 |  |  | 			Physics2DServer::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform); | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-06-24 12:33:52 -03:00
										 |  |  | 			Physics2DServer::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | Transform2D CollisionObject2D::shape_owner_get_transform(uint32_t p_owner) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), Transform2D()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	return shapes[p_owner].xform; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | Object *CollisionObject2D::shape_owner_get_owner(uint32_t p_owner) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].owner; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(p_shape.is_null()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ShapeData &sd = shapes[p_owner]; | 
					
						
							|  |  |  | 	ShapeData::Shape s; | 
					
						
							|  |  |  | 	s.index = total_subshapes; | 
					
						
							|  |  |  | 	s.shape = p_shape; | 
					
						
							|  |  |  | 	if (area) { | 
					
						
							|  |  |  | 		Physics2DServer::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		Physics2DServer::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sd.shapes.push_back(s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	total_subshapes++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | int CollisionObject2D::shape_owner_get_shape_count(uint32_t p_owner) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].shapes.size(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-30 19:01:20 +03:00
										 |  |  | Ref<Shape2D> CollisionObject2D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const { | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 19:01:20 +03:00
										 |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape2D>()); | 
					
						
							|  |  |  | 	ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape2D>()); | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].shapes[p_shape].shape; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | int CollisionObject2D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!shapes.has(p_owner), -1); | 
					
						
							|  |  |  | 	ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return shapes[p_owner].shapes[p_shape].index; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | void CollisionObject2D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							|  |  |  | 	ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int index_to_remove = shapes[p_owner].shapes[p_shape].index; | 
					
						
							|  |  |  | 	if (area) { | 
					
						
							|  |  |  | 		Physics2DServer::get_singleton()->area_remove_shape(rid, index_to_remove); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		Physics2DServer::get_singleton()->body_remove_shape(rid, index_to_remove); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shapes[p_owner].shapes.remove(p_shape); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		for (int i = 0; i < E->get().shapes.size(); i++) { | 
					
						
							|  |  |  | 			if (E->get().shapes[i].index > index_to_remove) { | 
					
						
							|  |  |  | 				E->get().shapes[i].index -= 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	total_subshapes--; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | void CollisionObject2D::shape_owner_clear_shapes(uint32_t p_owner) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	ERR_FAIL_COND(!shapes.has(p_owner)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	while (shape_owner_get_shape_count(p_owner) > 0) { | 
					
						
							|  |  |  | 		shape_owner_remove_shape(p_owner, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | uint32_t CollisionObject2D::shape_find_owner(int p_shape_index) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	for (const Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		for (int i = 0; i < E->get().shapes.size(); i++) { | 
					
						
							|  |  |  | 			if (E->get().shapes[i].index == p_shape_index) { | 
					
						
							|  |  |  | 				return E->key(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//in theory it should be unreachable
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | void CollisionObject2D::set_pickable(bool p_enabled) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (pickable == p_enabled) | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	pickable = p_enabled; | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 	_update_pickable(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CollisionObject2D::is_pickable() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return pickable; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 12:38:03 -03:00
										 |  |  | void CollisionObject2D::_input_event(Node *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape) { | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (get_script_instance()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		get_script_instance()->call(SceneStringNames::get_singleton()->_input_event, p_viewport, p_input_event, p_shape); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	emit_signal(SceneStringNames::get_singleton()->input_event, p_viewport, p_input_event, p_shape); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::_mouse_enter() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (get_script_instance()) { | 
					
						
							|  |  |  | 		get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 00:51:08 -03:00
										 |  |  | 	emit_signal(SceneStringNames::get_singleton()->mouse_entered); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::_mouse_exit() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (get_script_instance()) { | 
					
						
							|  |  |  | 		get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-12 00:51:08 -03:00
										 |  |  | 	emit_signal(SceneStringNames::get_singleton()->mouse_exited); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CollisionObject2D::_update_pickable() { | 
					
						
							|  |  |  | 	if (!is_inside_tree()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-01-13 10:45:50 -03:00
										 |  |  | 	bool pickable = this->pickable && is_inside_tree() && is_visible_in_tree(); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 	if (area) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Physics2DServer::get_singleton()->area_set_pickable(rid, pickable); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Physics2DServer::get_singleton()->body_set_pickable(rid, pickable); | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void CollisionObject2D::_bind_methods() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject2D::get_rid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_pickable", "enabled"), &CollisionObject2D::set_pickable); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_pickable"), &CollisionObject2D::is_pickable); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 12:38:03 -03:00
										 |  |  | 	BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "viewport"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::INT, "shape_idx"))); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 12:38:03 -03:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("input_event", PropertyInfo(Variant::OBJECT, "viewport"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::INT, "shape_idx"))); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("mouse_entered")); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("mouse_exited")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ADD_GROUP("Pickable", "input_"); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_pickable"), "set_pickable", "is_pickable"); | 
					
						
							|  |  |  | 	ADD_GROUP("", ""); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	rid = p_rid; | 
					
						
							|  |  |  | 	area = p_area; | 
					
						
							|  |  |  | 	pickable = true; | 
					
						
							| 
									
										
										
										
											2017-02-15 08:29:46 -03:00
										 |  |  | 	set_notify_transform(true); | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 	total_subshapes = 0; | 
					
						
							| 
									
										
										
										
											2017-02-15 08:29:46 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (p_area) { | 
					
						
							| 
									
										
										
										
											2015-03-22 01:46:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Physics2DServer::get_singleton()->area_attach_object_instance_ID(rid, get_instance_ID()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Physics2DServer::get_singleton()->body_attach_object_instance_ID(rid, get_instance_ID()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CollisionObject2D::CollisionObject2D() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//owner=
 | 
					
						
							| 
									
										
										
										
											2017-06-23 23:30:43 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-12 20:35:46 -03:00
										 |  |  | 	set_notify_transform(true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CollisionObject2D::~CollisionObject2D() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Physics2DServer::get_singleton()->free(rid); | 
					
						
							|  |  |  | } |