bool Enemy::Update(Level* level, LightObject* spotlight, bool inLight)
{
	if (path.size() > 0)
	{
		XMFLOAT3 nextPos = position;
		if (!inLight)
		{
			nextPos.x += direction.x / SPEED;
			nextPos.z += direction.z / SPEED;
		}
		else
		{
			float pushSpeed = SPEED / 2;
			nextPos.x += spotlight->getDirection().x / pushSpeed;
			nextPos.z += spotlight->getDirection().z / pushSpeed;
		}

		float radius = 0.5f;
		bool result = false;

		Tile* currentTile = level->getTile(tileCoord);
		if (currentTile != nullptr)
		{
			Coord nextTileCoord = Coord((int)(abs(nextPos.x)), (int)(abs(nextPos.z)));
			for (int x = nextTileCoord.x - 1; x <= nextTileCoord.x + 1; x++)
			{
				for (int y = nextTileCoord.y - 1; y <= nextTileCoord.y + 1; y++)
				{
					Coord iteratorTileCoord = Coord(x, y);
					Tile* iteratorTile = level->getTile(iteratorTileCoord);
					if (!iteratorTile->IsWalkableAI())
					{
						nextPos = NextPositionFromCollision(result, nextPos, radius, iteratorTileCoord);

						if (result && iteratorTile) 
						{
							Container* shadowContainer = iteratorTile->getShadowContainer();
							if (shadowContainer && !shadowContainer->GetIsActivated()) 
							{
								shadowContainer->ActivateContainer();
								return false;
							}
						}
					}
				}
			}
		}

		float xRem = -(nextPos.x - (int)nextPos.x);
		float zRem = -(nextPos.z - (int)nextPos.z);


		if (tileCoord == next && xRem >= 0.45f && zRem >= 0.45f)
		{
			XMINT2 p = path.at(path.size() - 1);
			this->path.pop_back();

			next = Coord(p.x, p.y);
			direction = XMFLOAT3((-position.x - (next.x + (TILE_SIZE / 2))), 0.0f, -position.z - (next.y + (TILE_SIZE / 2)));
			//desiredRotation = -atan2(direction.z, direction.x) * 180 / XM_PI;
			rotation.y = -atan2(direction.z, direction.x) * 180 / XM_PI;
			SetRotationDeg(rotation);
		}

		//float difference = desiredRotation - rotation.y;
		//cout << difference << endl;

		//if (difference < 180)
		//	rotation.y += difference / 10;
		//else
		//	rotation.y -= difference / 10;


		//SetRotationDeg(rotation);

		SetPosition(nextPos);
		moved = 1;
	}
	else
	{
		moved = 0;
	}

	return true;
}