IEnumerator Walk() { Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position); //Direction newLeft = TurnLeft(_direction); newLeft = TurnLeft(_direction); Vector2Int leftPosition = VectorTransformer.DirectionToVector2Int(position, newLeft); Vector2Int frontPosition = VectorTransformer.DirectionToVector2Int(position, _direction); Debug.Log("Iniciou: " + _direction + '-' + newLeft); //Verificar se pode ir para esquerda. Se sim vira e anda if (_gridController.CellIsEmpty(leftPosition)) { _direction = newLeft; _gridController.MoveObject(position, leftPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(leftPosition); Debug.Log("Go Left: " + _direction + "-" + newLeft); } //Se não anda para frente else if (_gridController.CellIsEmpty(frontPosition)) { _gridController.MoveObject(position, frontPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(frontPosition); Debug.Log("Go Ahead: " + _direction + "-" + newLeft); } //Senão puder andar para esquerda nem para frente apenas vira a direita else { _direction = TurnRight(_direction); Debug.Log("Turn Right: " + _direction + "-" + newLeft); } yield return(new WaitForSeconds(0.5f)); _coroutineControl = true; }
IEnumerator Walk() { Vector2Int position = VectorTransformer.Vector3ToVector2Int(transform.position); IsCharacterNeighbor(position); Direction nextDirection = Direction.None; int nextNeighborPoints = -200; int actualNeighborPoints; List <Vector2Int> isGluedAt = VerifyWhereIsGlued(position); Vector2Int actualNeighborPosition = position + Vector2Int.left; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Right) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Left; } } actualNeighborPosition = position + Vector2Int.up; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Down) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Up; } } actualNeighborPosition = position + Vector2Int.right; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Left) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Right; } } actualNeighborPosition = position + Vector2Int.down; if (_gridController.CellIsEmptyWithoutCharacter(actualNeighborPosition)) { actualNeighborPoints = _gridController.VerifyNeighbor(actualNeighborPosition); if (_directionFrom == Direction.Up) { actualNeighborPoints = actualNeighborPoints - 100; } int isGluedAtPoints = IsInSameWall(actualNeighborPosition, isGluedAt); int wasGluedAtPoints = IsInSameWall(actualNeighborPosition, _wasGluedAt); actualNeighborPoints = actualNeighborPoints + isGluedAtPoints + wasGluedAtPoints; if (actualNeighborPoints > nextNeighborPoints) { nextNeighborPoints = actualNeighborPoints; nextDirection = Direction.Down; } } Vector2Int toPosition = VectorTransformer.DirectionToVector2Int(position, nextDirection); _gridController.MoveObject(position, toPosition); transform.position = VectorTransformer.Vector2IntToVector3Int(toPosition); _directionFrom = nextDirection; _wasGluedAt = isGluedAt; yield return(new WaitForSeconds(0.5f)); _coroutineControl = true; }