| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | #include "..\e2dmanager.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dnode.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | e2d::CollisionManager * e2d::CollisionManager::getInstance() | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | 	static CollisionManager instance; | 
					
						
							|  |  |  |  | 	return &instance; | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | e2d::CollisionManager::CollisionManager() | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | 	: _collisionEnabled(false) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | e2d::CollisionManager::~CollisionManager() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | void e2d::CollisionManager::__addCollider(Collider * collider) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 	_colliders.push_back(collider); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | void e2d::CollisionManager::__removeCollider(Collider * collider) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 	auto iter = std::find(_colliders.begin(), _colliders.end(), collider); | 
					
						
							|  |  |  |  | 	if (iter != _colliders.end()) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 		_colliders.erase(iter); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | void e2d::CollisionManager::__updateCollider(Collider* active) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 17:46:37 +08:00
										 |  |  |  | 	if (!_collisionEnabled || | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 		Game::getInstance()->isTransitioning()) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 	auto currScene = Game::getInstance()->getCurrentScene(); | 
					
						
							|  |  |  |  | 	if (active->getNode()->getParentScene() != currScene) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 15:38:02 +08:00
										 |  |  |  | 	std::vector<Collider*> currColliders; | 
					
						
							| 
									
										
										
										
											2018-08-12 20:19:47 +08:00
										 |  |  |  | 	currColliders.reserve(_colliders.size()); | 
					
						
							| 
									
										
										
										
											2018-08-12 15:38:02 +08:00
										 |  |  |  | 	std::copy_if( | 
					
						
							|  |  |  |  | 		_colliders.begin(), | 
					
						
							|  |  |  |  | 		_colliders.end(), | 
					
						
							|  |  |  |  | 		std::back_inserter(currColliders), | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 		[this, active, currScene](Collider* passive) -> bool | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 			return active != passive && | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 				passive->getNode()->isVisible() && | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 				passive->getNode()->getParentScene() == currScene && | 
					
						
							|  |  |  |  | 				this->isCollidable(active->getNode(), passive->getNode()); | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-12 15:38:02 +08:00
										 |  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 15:38:02 +08:00
										 |  |  |  | 	for (const auto& passive : currColliders) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 		// <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD>彻<EFBFBD><E5BDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 		Collider::Relation relation = active->getRelationWith(passive); | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 		// <20><><EFBFBD><EFBFBD> UNKNOWN <20><> DISJOIN <20><><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		if (relation != Collider::Relation::Unknown && | 
					
						
							|  |  |  |  | 			relation != Collider::Relation::Disjoin) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 			auto activeNode = active->getNode(); | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 			auto passiveNode = passive->getNode(); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:08:03 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD>¼<EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-08-12 15:38:02 +08:00
										 |  |  |  | 			Collision activeCollision(passiveNode, relation); | 
					
						
							| 
									
										
										
										
											2018-08-19 17:46:37 +08:00
										 |  |  |  | 			if (dynamic_cast<CollisionHandler*>(activeNode)) | 
					
						
							|  |  |  |  | 				dynamic_cast<CollisionHandler*>(activeNode)->handle(activeCollision); | 
					
						
							| 
									
										
										
										
											2018-07-17 14:08:03 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 			Collision passiveCollision(activeNode, passive->getRelationWith(active)); | 
					
						
							| 
									
										
										
										
											2018-08-19 17:46:37 +08:00
										 |  |  |  | 			if (dynamic_cast<CollisionHandler*>(passiveNode)) | 
					
						
							|  |  |  |  | 				dynamic_cast<CollisionHandler*>(passiveNode)->handle(passiveCollision); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | void e2d::CollisionManager::setCollisionEnabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	_collisionEnabled = enabled; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | void e2d::CollisionManager::addName(const String & name1, const String & name2) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 	if (!name1.isEmpty() && !name2.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | 		_collisionList.insert(std::make_pair(name1.hash(), name2.hash())); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::CollisionManager::addName(const std::vector<std::pair<String, String> >& names) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  |  | 	for (const auto& name : names) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 		if (!name.first.isEmpty() && !name.second.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | 			_collisionList.insert(std::make_pair(name.first.hash(), name.second.hash())); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::CollisionManager::isCollidable(Node * node1, Node * node2) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 	return CollisionManager::isCollidable(node1->getName(), node2->getName()); | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::CollisionManager::isCollidable(const String & name1, const String & name2) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 15:11:20 +08:00
										 |  |  |  | 	size_t hashName1 = name1.hash(),  | 
					
						
							|  |  |  |  | 		hashName2 = name2.hash(); | 
					
						
							| 
									
										
										
										
											2018-07-13 00:45:39 +08:00
										 |  |  |  | 	auto pair1 = std::make_pair(hashName1, hashName2),  | 
					
						
							|  |  |  |  | 		pair2 = std::make_pair(hashName2, hashName1); | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  |  | 	for (const auto& pair : _collisionList) | 
					
						
							| 
									
										
										
										
											2018-07-08 16:57:12 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (pair == pair1 || pair == pair2) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false; | 
					
						
							|  |  |  |  | } |