Пример #1
0
        public void InicializarPagina()
        {
            // Carga los pokemons iniciales
            GlobalVar.friendCoach.pokemons.Add(rep.ObtenerPokemon(GlobalVar.friendCoach.user.pokemons[0].name));
            GlobalVar.enemyCoach.pokemons.Add(rep.ObtenerPokemon(GlobalVar.enemyCoach.user.pokemons[0].name));

            // Pone a cargar el resto de pokemons de forma secundaria
            CargarPokemonsThread.Start();

            // Genero y gestiono los grids de la página
            GlobalGrid.InicializarGrids();
            GlobalGrid.DimensionarGrids();
            GlobalGrid.EstructurarGrids();
        }
Пример #2
0
    /// <summary>
    ///  A delegate function that is called when FindPath function
    /// took too long to find a path.
    /// </summary>
    //public delegate void SearchFailedEvent();
    /// <summary>
    ///  Event called then FindPath function has completed a search.
    /// </summary>
    /// <param name="path">List of nodes forming a complete path.</param>
    /// <returns></returns>
    //public delegate List<GridNode> SearchCompleteEvent(List<GridNode> path);


    public List <INode> FindPath(Vector3 from, Vector3 to)
    {
        INode startNode  = GlobalGrid.GetNodeFromWorld(from);
        INode targetNode = GlobalGrid.GetNodeFromWorld(to);

        if (startNode == null)
        {
#if UNITY_EDITOR
            Debug.LogWarning("FindPath: startNode is null for pos: " + from);
#endif
            return(null);
        }

        List <INode> opened = new List <INode>();
        List <INode> closed = new List <INode>();
        opened.Add(startNode);

        float startTime     = Time.timeSinceLevelLoad;
        float searchingTime = 0;
        //|| opened.Count > 20
        while (opened.Count > 0 || searchingTime > 0)
        {
            if (opened.Count == 0)
            {
                break;
            }

            searchingTime = Time.timeSinceLevelLoad - startTime; //safety to avoid long pathsearch and infinite loop.
            INode stepNode = this.findCheapestNode(ref opened);
            opened.Remove(stepNode);
            closed.Add(stepNode);

            if (stepNode == targetNode)
            {
                var path = this.retracePath(startNode, targetNode);
                return(path);
            }

            List <INode> neighbours = stepNode.GetNeighbours();
            for (int n = 0; n < neighbours.Count; n++)
            {
                INode neighbour = neighbours[n];

                if (!neighbour.GetWalkable() || closed.Contains(neighbour))
                {
                    continue;
                }

                int fromToDistance = this.GetDistance(stepNode, neighbour);

                int newCost = stepNode.GetDistanceCost() + fromToDistance;
                if (closed.Contains(neighbour) && newCost < neighbour.GetDistanceCost())
                {
                    closed.Remove(neighbour);
                }

                if (!opened.Contains(neighbour) && !closed.Contains(neighbour))
                {
                    neighbour.SetDistanceCost(newCost);
                    opened.Add(neighbour);
                    int newHeuristic = this.GetDistance(neighbour, targetNode);
                    neighbour.SetHeuristicCost(newHeuristic);
                    neighbour.SetParent(stepNode);
                    this.retracePath(startNode, neighbour);

                    this.calculatedCosts.Add(neighbour);
                }
            } //for
        }     //while

        return(null);
    }//OnFindPath
    /// <summary>
    /// Generates a dungeon floor map
    /// </summary>
    /// <returns></returns>
    public void GenerateNewDungeon()
    {
        Init();

        bool criticalPathComplete = false;

        while (!criticalPathComplete)
        {
            criticalPathComplete = GenerateCriticalPath();
        }

        PopulateExtraRooms();
        //PrintLayout();

        for (int y = 0; y < layout.GetLength(1); y++)
        {
            for (int x = 0; x < layout.GetLength(0); x++)
            {
                if (layout[y, x] == null || layout[y, x].Equals("") || layout[y, x].Equals("X"))
                {
                    roomTypeData.Add(new RoomTypeData(x, y, RoomType.EMPTY));
                }
                else
                {
                    List <Vector2Int> validNeighboringRooms = GetValidNeighboringRooms(x, y);
                    switch (validNeighboringRooms.Count)
                    {
                    case 0:
                        Debug.LogWarning("Somehow the above if statement didn't catch this??");
                        roomTypeData.Add(new RoomTypeData(x, y, RoomType.EMPTY));
                        break;

                    case 1:
                        roomTypeData.Add(new RoomTypeData(x, y, RoomType.ONEDOOR));
                        break;

                    case 2:
                        roomTypeData.Add(new RoomTypeData(x, y, RoomType.TWODOOR));
                        break;

                    case 3:
                        roomTypeData.Add(new RoomTypeData(x, y, RoomType.THREEDOOR));
                        break;

                    case 4:
                        roomTypeData.Add(new RoomTypeData(x, y, RoomType.FOURDOOR));
                        break;

                    default:
                        Debug.LogError("Something went horribly wrong in populating RoomTypeData");
                        break;
                    }
                }
            }
        }

        // now we need to create a map using preconstructed room prefabs
        for (int i = 0; i < roomTypeData.Count; i++)
        {
            RoomTypeData thisRoomData      = roomTypeData[i];
            GameObject   roomPrefab        = GetRoomPrefab(thisRoomData.roomType);
            Vector3Int   roomGridPosition  = new Vector3Int(roomWidth * thisRoomData.x, roomHeight * thisRoomData.y, 0);
            Vector3      roomWorldPosition = GlobalGrid.GetGrid().CellToWorld(roomGridPosition);

            GameObject newRoom = Instantiate(roomPrefab, roomWorldPosition, Quaternion.identity);

            if (thisRoomData.roomType == RoomType.FOURDOOR)
            {
                // we don't have to align anything, since any orientation of a 4 door room will work.
                // give it a random rotation then move on
                newRoom.GetComponent <RoomDirection>().RotateRandom();
                continue;
            }
            else if (thisRoomData.roomType == RoomType.EMPTY)
            {
                // we don't care about the orientation of empty rooms
                continue;
            }

            Vector2[] adjacentRoomDirections = GetAdjacentRoomDirections(thisRoomData.x, thisRoomData.y);
            Vector2[] roomDoorDirections     = newRoom.GetComponent <RoomDirection>().GetDoorDirections();

            if (!Vector2ArraysAreEqual(adjacentRoomDirections, roomDoorDirections))
            {
                // we need to rotate the room up to 3 times
                bool rotationMatchFound = false;
                for (int j = 0; j < 3; j++)
                {
                    newRoom.GetComponent <RoomDirection>().RotateRoom90Degrees();
                    roomDoorDirections = newRoom.GetComponent <RoomDirection>().GetDoorDirections();
                    if (Vector2ArraysAreEqual(adjacentRoomDirections, roomDoorDirections))
                    {
                        rotationMatchFound = true;
                        break;
                    }
                }

                if (!rotationMatchFound && thisRoomData.roomType == RoomType.TWODOOR)
                {
                    Debug.Log("Resorting to alternate two door!");
                    // we need to try the alternate 2 door room
                    Destroy(newRoom);
                    roomPrefab         = twoDoorAlternatePrefabs[Random.Range(0, twoDoorAlternatePrefabs.Length)];
                    newRoom            = Instantiate(roomPrefab, roomWorldPosition, Quaternion.identity);
                    roomDoorDirections = newRoom.GetComponent <RoomDirection>().GetDoorDirections();

                    if (!Vector2ArraysAreEqual(adjacentRoomDirections, roomDoorDirections))
                    {
                        // we need to rotate the room up to 3 times
                        rotationMatchFound = false;
                        for (int k = 0; k < 3; k++)
                        {
                            newRoom.GetComponent <RoomDirection>().RotateRoom90Degrees();
                            roomDoorDirections = newRoom.GetComponent <RoomDirection>().GetDoorDirections();
                            if (Vector2ArraysAreEqual(adjacentRoomDirections, roomDoorDirections))
                            {
                                rotationMatchFound = true;
                                break;
                            }
                        }

                        if (!rotationMatchFound)
                        {
                            Debug.LogError("Could not figure out the rotation of this two door room after trying both types.");
                        }
                    }
                }
                else if (!rotationMatchFound)
                {
                    Debug.LogError("Serious problem here, we couldn't find a rotation that worked.");
                }
            }
        }
    }