Ejemplo n.º 1
0
	Grid* World::searchDestinationGrid(Object * object, bool isInternal)
	{
		Layer* belongLayer = nullptr;
		int nextIndex = 0;
		int belongIndex = 0;

		auto aabb = object->GetAABB();

		auto center = aabb.GetPosition() + aabb.GetSize() / 2;
		auto size = aabb.GetSize();

		for (int currentResolution = 0; currentResolution <= resolution; ++currentResolution)
		{

			auto range = layers[currentResolution]->GetGrids()[nextIndex]->GetGridRange();

			//グリッドの縦横がそれぞれ円の直径を上回っていないか調べる。
			if (range.GetIsContainingPoint(center) && range.Height >= size.Y && range.Width >= size.X)
			{
				belongLayer = layers[currentResolution];
				belongIndex = nextIndex;
			}
			else
			{
				break;
			}

			if (currentResolution >= layers.size() - 1)
			{
				continue;
			}

			//次に遷移するレイヤーにおけるグリッドの添字を調べる。
			auto children = Grid::GetChildrenIndices(belongIndex, currentResolution);
			auto nextLayer = layers[currentResolution + 1];
			nextIndex = -1;
			for (auto gridIndex : children)
			{
				RectF gridRange = nextLayer->GetGrids()[gridIndex]->GetGridRange();
				if (gridRange.GetIsContainingPoint(center))
				{
					nextIndex = gridIndex;
					break;
				}
			}

			if (nextIndex == -1)
			{
				break;
			}

		}

		{
			upperLeft_.X = Min(upperLeft_.X, aabb.X);
			upperLeft_.Y = Min(upperLeft_.Y, aabb.Y);
			lowerRight_.X = Max(lowerRight_.X, aabb.X + aabb.Width);
			lowerRight_.Y = Max(lowerRight_.Y, aabb.Y + aabb.Height);
			minRadius_ = Min(minRadius_, Min(aabb.Width, aabb.Height));
		}

		if (belongLayer == nullptr)
		{
			if (object->GetIsInWorld())
			{
				++outOfRangeObjectsCount;
			}

			object->SetIsInWorld(false);

			return layers[0]->GetGrids()[0];
		}
		else
		{
			if (!object->GetIsInWorld())
			{
				--outOfRangeObjectsCount;
			}

			object->SetIsInWorld(true);

			return belongLayer->GetGrids()[belongIndex];
		}
	}