| 
									
										
										
										
											2018-04-21 21:24:46 +08:00
										 |  |  |  | #include "..\e2dcollider.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dmanager.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dnode.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | e2d::Collider::Collider(Node * parent) | 
					
						
							| 
									
										
										
										
											2018-07-07 01:48:39 +08:00
										 |  |  |  | 	: _visible(true) | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 	, _color(Color::Blue, 0.7) | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	, _parentNode(parent) | 
					
						
							| 
									
										
										
										
											2018-07-04 17:23:47 +08:00
										 |  |  |  | 	, _geometry(nullptr) | 
					
						
							| 
									
										
										
										
											2018-07-04 17:00:21 +08:00
										 |  |  |  | 	, _enabled(true) | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | 	, _type(Collider::Type::None) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	_type = Game::getInstance()->getConfig()->getDefaultColliderType(); | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | e2d::Collider::~Collider() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-04 17:23:47 +08:00
										 |  |  |  | 	SafeRelease(_geometry); | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 13:15:57 +08:00
										 |  |  |  | e2d::Color e2d::Collider::getColor() const | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	return _color; | 
					
						
							| 
									
										
										
										
											2018-04-22 13:15:57 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | ID2D1Geometry * e2d::Collider::getGeometry() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	return _geometry; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-04 17:00:21 +08:00
										 |  |  |  | void e2d::Collider::setEnabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-04 17:00:21 +08:00
										 |  |  |  | 	_enabled = enabled; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 01:48:39 +08:00
										 |  |  |  | void e2d::Collider::setVisible(bool visible) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-07 01:48:39 +08:00
										 |  |  |  | 	_visible = visible; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 13:15:57 +08:00
										 |  |  |  | void e2d::Collider::setColor(Color color) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	_color = color; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Collider::_render() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 	if (_geometry && _enabled) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-03 18:16:26 +08:00
										 |  |  |  | 		auto renderer = Renderer::getInstance(); | 
					
						
							| 
									
										
										
										
											2018-04-03 11:12:30 +08:00
										 |  |  |  | 		// <20><>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB>ˢ
 | 
					
						
							| 
									
										
										
										
											2018-07-05 22:09:21 +08:00
										 |  |  |  | 		ID2D1SolidColorBrush * brush = renderer->getSolidColorBrush(); | 
					
						
							| 
									
										
										
										
											2018-04-03 11:12:30 +08:00
										 |  |  |  | 		// <20><><EFBFBD>û<EFBFBD>ˢ<EFBFBD><CBA2>ɫ<EFBFBD><C9AB><EFBFBD><CDB8><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-07-05 22:09:21 +08:00
										 |  |  |  | 		brush->SetColor(_color.toD2DColorF()); | 
					
						
							|  |  |  |  | 		brush->SetOpacity(1.f); | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 		// <20><><EFBFBD>Ƽ<EFBFBD><C6BC><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
 | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 		renderer->getRenderTarget()->DrawGeometry(_geometry, brush); | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | e2d::Collider::Relation e2d::Collider::getRelationWith(Collider * collider) const | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 	if (_geometry && collider->_geometry) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 		if (_enabled && collider->_enabled) | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			D2D1_GEOMETRY_RELATION relation; | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 			_geometry->CompareWithGeometry( | 
					
						
							|  |  |  |  | 				collider->_geometry, | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 				D2D1::Matrix3x2F::Identity(), | 
					
						
							|  |  |  |  | 				&relation | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 21:22:34 +08:00
										 |  |  |  | 			return Relation(relation); | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-24 20:37:34 +08:00
										 |  |  |  | 	return Relation::Unknown; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | void e2d::Collider::_recreate() | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	SafeRelease(_geometry); | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 	if (_type == Type::None) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	switch (_type) | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	case Type::Rect: | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		ID2D1RectangleGeometry* rectangle = nullptr; | 
					
						
							|  |  |  |  | 		Renderer::getFactory()->CreateRectangleGeometry( | 
					
						
							|  |  |  |  | 			D2D1::RectF( | 
					
						
							|  |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				float(_parentNode->getRealWidth()), | 
					
						
							|  |  |  |  | 				float(_parentNode->getRealHeight())), | 
					
						
							|  |  |  |  | 			&rectangle | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 		_geometry = rectangle; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	break; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	case Type::Circle: | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		double minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ID2D1EllipseGeometry* circle = nullptr; | 
					
						
							|  |  |  |  | 		Renderer::getFactory()->CreateEllipseGeometry( | 
					
						
							|  |  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  |  | 					float(_parentNode->getRealWidth() / 2), | 
					
						
							|  |  |  |  | 					float(_parentNode->getRealHeight() / 2) | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | 				), | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 				float(minSide / 2), | 
					
						
							|  |  |  |  | 				float(minSide / 2) | 
					
						
							|  |  |  |  | 			), | 
					
						
							|  |  |  |  | 			&circle | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 		_geometry = circle; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	break; | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 15:12:18 +08:00
										 |  |  |  | 	case Type::Ellipse: | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		float halfWidth = float(_parentNode->getWidth() / 2), | 
					
						
							|  |  |  |  | 			halfHeight = float(_parentNode->getHeight() / 2); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ID2D1EllipseGeometry* ellipse = nullptr; | 
					
						
							|  |  |  |  | 		Renderer::getFactory()->CreateEllipseGeometry( | 
					
						
							|  |  |  |  | 			D2D1::Ellipse( | 
					
						
							|  |  |  |  | 				D2D1::Point2F( | 
					
						
							|  |  |  |  | 					halfWidth, | 
					
						
							|  |  |  |  | 					halfHeight), | 
					
						
							|  |  |  |  | 				halfWidth, | 
					
						
							|  |  |  |  | 				halfHeight), | 
					
						
							|  |  |  |  | 			&ellipse | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 		_geometry = ellipse; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	break; | 
					
						
							| 
									
										
										
										
											2018-07-05 00:20:53 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 18:04:18 +08:00
										 |  |  |  | 	ID2D1TransformedGeometry * _transformed; | 
					
						
							|  |  |  |  | 	Renderer::getFactory()->CreateTransformedGeometry( | 
					
						
							|  |  |  |  | 		_geometry, | 
					
						
							|  |  |  |  | 		_parentNode->_finalMatri, | 
					
						
							|  |  |  |  | 		&_transformed | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 	SafeRelease(_geometry); | 
					
						
							|  |  |  |  | 	_geometry = _transformed; | 
					
						
							| 
									
										
										
										
											2018-04-02 23:01:38 +08:00
										 |  |  |  | } |