示例#1
0
    IEnumerator RoomTransitionTimer(DoorPosition doorPosition, GameObject player)
    {
        yield return(new WaitForSeconds(0.5f));

        bool    hasTransitioned  = false;
        float   ratio            = 0.0f;
        Vector3 startingPosition = transform.position;
        Vector3 targetPosition   = GetTransitionPosition(doorPosition);

        while (!hasTransitioned)
        {
            if (ratio <= 1.0f)
            {
                transform.position = Vector3.Lerp(startingPosition, targetPosition, ratio);
                ratio += 0.01f;
                yield return(null);
            }
            else
            {
                PlayerEnterRoom(player, doorPosition);
                SpawnEnemies();
                hasTransitioned = true;
            }
        }
    }
示例#2
0
    public void AddDoor(Door door, DoorPosition position)
    {
        if (_doors == null)
        {
            _doors = new Door[] { door };
        }
        else
        {
            var list = new List <Door>(_doors);
            list.Add(door);
            _doors = list.ToArray();
        }


        if (_doorsPositions == null)
        {
            _doorsPositions = new DoorPosition[] { position };
        }
        else
        {
            var listPosition = new List <DoorPosition>(_doorsPositions);
            listPosition.Add(position);
            _doorsPositions = listPosition.ToArray();
        }
    }
示例#3
0
    private void PlayerEnterRoom(GameObject player, DoorPosition doorPosition)
    {
        player.SetActive(true);
        Vector2 playerDirection = Vector2.zero;

        switch (doorPosition)
        {
        case DoorPosition.Bottom:
            playerDirection = new Vector2(0, 1);
            break;

        case DoorPosition.Top:
            playerDirection = new Vector2(0, -1);
            break;

        case DoorPosition.Left:
            playerDirection = new Vector2(1, 0);
            break;

        case DoorPosition.Right:
            playerDirection = new Vector2(-1, 0);
            break;
        }
        player.transform.GetChild(0).GetComponent <LinkAnimationEvents>().WalkToRoom(playerDirection);
    }
示例#4
0
        private PotentialPosition ResolveDoorPosition(DoorPosition doorPosition)
        {
            Vector3 position  = transform.position;
            Vector3 rotation  = Vector3.zero;
            Vector3 direction = Vector3.zero;

            switch (doorPosition)
            {
            case DoorPosition.Up:
                position += new Vector3(0, 0, 4.5f);
                direction = Vector3.forward;
                break;

            case DoorPosition.Down:
                position += new Vector3(0, 0, -4.5f);
                rotation  = new Vector3(0, 180, 0);
                direction = Vector3.back;
                break;

            case DoorPosition.Left:
                position += new Vector3(-4.5f, 0, 0);
                rotation  = new Vector3(0, 270, 0);
                direction = Vector3.left;
                break;

            case DoorPosition.Right:
                position += new Vector3(4.5f, 0, 0);
                rotation  = new Vector3(0, 90, 0);
                direction = Vector3.right;
                break;
            }

            return(new PotentialPosition(position, Quaternion.Euler(rotation), direction));
        }
示例#5
0
 public Door(DoorPosition position, int levelToId)
 {
     this.Position = position;
     this.LevelToId = levelToId;
     this.SpriteSheet = this.GetSpriteSheet();
     this.IsOpen = false;
     this.x = this.GetX();
     this.y = this.GetY();
 }
示例#6
0
 public static DoorPosition GetOppositePosition(DoorPosition doorPos)
 {
     switch (doorPos)
     {
         case DoorPosition.Bottom:
             return DoorPosition.Top;
         case DoorPosition.Top:
             return DoorPosition.Bottom;
         case DoorPosition.Left:
             return DoorPosition.Right;
         case DoorPosition.Right:
             return DoorPosition.Left;
         default:
             throw new InvalidEnumArgumentException("No such door type");
     }
 }
示例#7
0
    private void AddDoor(Door prefab, int lineId, float positionOnTheLine, float offset, IRoom room)
    {
        Vector3    position;
        Quaternion rotation;

        GetPositionRotation(lineId, positionOnTheLine, offset, prefab.DefaultOrientation, out position, out rotation);

        var door = Instantiate(prefab.gameObject, position, rotation,
                               Holder.gameObject.transform).GetComponent <Door>();
        var doorPosition = new DoorPosition()
        {
            LineId = lineId, PartOfTheLine = positionOnTheLine
        };

        door.RoomTo = room;

        Holder.AddDoor(door, doorPosition);
    }
        /// <summary>
        /// Spawns a room at a given position, then spawns paths from each of the doors
        /// </summary>
        /// <param name="spawnPosition">position of where to spawn the room</param>
        private void SpawnRoomAndPath(Vector2 spawnPosition)
        {
            //Spawn the paren GameObject
            GameObject roomGO = new GameObject("Room");

            roomGO.transform.parent = _levelGO.transform;

            //Generate random room properties
            int roomHeight    = UnityEngine.Random.Range(_minRoomHeight, _maxRoomHeight + 1);
            int roomWidth     = UnityEngine.Random.Range(_minRoomWidth, _maxRoomWidth + 1);
            int numberOfDoors = UnityEngine.Random.Range(1, 4);

            //Prepare a list of door positions to then generate doors at random positions
            List <DoorPosition> doorPositionTypes = new List <DoorPosition>();
            Array values = Enum.GetValues(typeof(DoorPosition)); //Used for getting random value from enum

            //Generate doors at random positions
            for (int i = 0; i < numberOfDoors; i++)
            {
                //Get random value from enum
                System.Random random  = new System.Random();
                DoorPosition  doorPos = (DoorPosition)values.GetValue(UnityEngine.Random.Range(0, values.Length));
                //Add a random door
                doorPositionTypes.Add(doorPos);
            }

            //Create and instantiate the room
            Room room = new Room(roomHeight, roomWidth, doorPositionTypes);

            for (int i = 0; i < roomWidth; i++)
            {
                for (int j = 0; j < roomHeight; j++)
                {
                    SpawnSingleFloorTile(i + (int)spawnPosition.x, j + (int)spawnPosition.y, roomGO.transform);
                }
            }
            _currentNumberOfRooms++;

            //Spawn path from each of the room's doors
            SpawnPath(room);
        }
示例#9
0
        public void AddDoor(DoorPosition position, float width)
        {
            float horizontalWidth = Corner2.X - Corner1.X;
            float verticalWidth   = Corner2.Y - Corner1.Y;

            switch (position)
            {
            case DoorPosition.Down:
                this.Doors.Add(new Door(
                                   new PointF(Corner1.X + horizontalWidth / 2 - width / 2, Corner2.Y),
                                   new PointF(Corner1.X + horizontalWidth / 2 + width / 2, Corner2.Y)
                                   ));
                break;

            case DoorPosition.Up:
                this.Doors.Add(new Door(
                                   new PointF(Corner1.X + horizontalWidth / 2 - width / 2, Corner1.Y),
                                   new PointF(Corner1.X + horizontalWidth / 2 + width / 2, Corner1.Y)
                                   ));
                break;

            case DoorPosition.Right:
                this.Doors.Add(new Door(
                                   new PointF(Corner2.X, Corner1.Y + verticalWidth / 2 - width / 2),
                                   new PointF(Corner2.X, Corner1.Y + verticalWidth / 2 + width / 2)
                                   ));
                break;

            case DoorPosition.Left:
                this.Doors.Add(new Door(
                                   new PointF(Corner1.X, Corner1.Y + verticalWidth / 2 - width / 2),
                                   new PointF(Corner1.X, Corner1.Y + verticalWidth / 2 + width / 2)
                                   ));
                break;
            }
        }
示例#10
0
    private Vector3 GetTransitionPosition(DoorPosition doorPosition)
    {
        Vector3 transitionPosition = new Vector3();

        switch (doorPosition)
        {
        case DoorPosition.Bottom:
            transitionPosition = new Vector3(transform.position.x, transform.position.y + 11, transform.position.z);
            break;

        case DoorPosition.Top:
            transitionPosition = new Vector3(transform.position.x, transform.position.y - 11, transform.position.z);
            break;

        case DoorPosition.Left:
            transitionPosition = new Vector3(transform.position.x + 16, transform.position.y, transform.position.z);
            break;

        case DoorPosition.Right:
            transitionPosition = new Vector3(transform.position.x - 16, transform.position.y, transform.position.z);
            break;
        }
        return(transitionPosition);
    }
示例#11
0
        private DoorPosition ApplyRoomRotation(DoorPosition d)
        {
            var roomRotation = transform.eulerAngles.y;

            switch (d)
            {
            case DoorPosition.Up:
                if (Mathf.Approximately(roomRotation, 90f))
                {
                    return(DoorPosition.Right);
                }
                if (Mathf.Approximately(roomRotation, 180f))
                {
                    return(DoorPosition.Down);
                }
                if (Mathf.Approximately(roomRotation, 270f))
                {
                    return(DoorPosition.Left);
                }
                break;

            case DoorPosition.Down:
                if (Mathf.Approximately(roomRotation, 90f))
                {
                    return(DoorPosition.Left);
                }
                if (Mathf.Approximately(roomRotation, 180f))
                {
                    return(DoorPosition.Up);
                }
                if (Mathf.Approximately(roomRotation, 270f))
                {
                    return(DoorPosition.Right);
                }
                break;

            case DoorPosition.Left:
                if (Mathf.Approximately(roomRotation, 90f))
                {
                    return(DoorPosition.Up);
                }
                if (Mathf.Approximately(roomRotation, 180f))
                {
                    return(DoorPosition.Right);
                }
                if (Mathf.Approximately(roomRotation, 270f))
                {
                    return(DoorPosition.Down);
                }
                break;

            case DoorPosition.Right:
                if (Mathf.Approximately(roomRotation, 90f))
                {
                    return(DoorPosition.Down);
                }
                if (Mathf.Approximately(roomRotation, 180f))
                {
                    return(DoorPosition.Left);
                }
                if (Mathf.Approximately(roomRotation, 270f))
                {
                    return(DoorPosition.Up);
                }
                break;
            }
            return(d);
        }
示例#12
0
 public Door(DoorPosition position) : base()
 {
     Position = position;
 }
示例#13
0
 public TopDownDoor(int index, DoorPosition doorPosition) : base(index)
 {
     Name = "Door" + doorPosition;
     Tag  = "Door";
 }
示例#14
0
 public Door(DoorPosition doorPosition, int nextRoomId)
 {
     DoorPosition = doorPosition;
     NextRoomId = nextRoomId;
 }
示例#15
0
 public void AddDoor(int roomId, DoorPosition doorPosition)
 {
     Doors.Add(new Door(doorPosition, roomId));
 }
示例#16
0
    private void GenerateMap()
    {
        Debug.Log("Gerou um mapa");
        if (navigator && player)
        {
            Dictionary <Vector2, RoomInfo> map = new Dictionary <Vector2, RoomInfo>();
            Vector2        curPos           = Vector2.zero;
            Vector2        curDirection     = Vector2.up;
            List <Vector2> directionHistory = new List <Vector2>();
            int            curID            = 0;

            // Cria a primeira sala do mapa
            map.Add(curPos, new RoomInfo());
            map[curPos].SO.ID = curID;
            map[curPos].coord = curPos;
            // Adiciona uma porta para cima
            map[curPos].objects[DoorPosition.North] = Instantiate(door);
            Door curDoor = map[curPos].objects[DoorPosition.North] as Door;
            curDoor.targetDoorPosition = DoorPosition.South;
            // Incrementa o id
            curID++;
            curDoor.targetRoomID = curID;
            curPos += curDirection;
            directionHistory.Add(Vector2.down);

            while (curID < nRooms)
            {
                // A cada 3 salas coloca uma poção
                if (curID % 2 == 0)
                {
                    pendingPotions++;
                }
                // A cada 4 salas coloca um item
                if (curID % 3 == 0)
                {
                    pendingItems++;
                }

                // Cria a sala em questão
                map.Add(curPos, new RoomInfo());
                map[curPos].SO.ID = curID;
                map[curPos].coord = curPos;

                // Cria a porta de entrada
                DoorPosition entrancePos = GetEntrancePos(curDirection);
                map[curPos].objects[entrancePos] = Instantiate(door);
                curDoor = map[curPos].objects[entrancePos] as Door;
                curDoor.targetRoomID       = map[curPos - curDirection].SO.ID;
                curDoor.targetDoorPosition = GetExitPos(curDirection);

                // Se for a ultima sala não precisa criar uma saída ou colocar itens
                curID++;
                if (curID < nRooms)
                {
                    // Adiciona os inimigos
                    for (int i = 0; i < 3; i++)
                    {
                        Enemy newEnemy = Instantiate(enemies[Random.Range(0, enemies.Count)]);
                        if (newEnemy != null)
                        {
                            if (Mathf.Abs(curDirection.x) > Mathf.Abs(curDirection.y))
                            {
                                newEnemy.spawnPosition = new Vector3(0, -5 + (i * 5), 0);
                            }
                            else
                            {
                                newEnemy.spawnPosition = new Vector3(-5 + (i * 5), 0, 0);
                            }
                            map[curPos].SO.enemies.Add(newEnemy);
                        }
                    }

                    if (pendingPotions > 0)
                    {
                        if (map[curPos].AddObject(Instantiate(potionItem)))
                        {
                            pendingPotions--;
                        }
                    }
                    if (pendingItems > 0)
                    {
                        if (items.Count <= 0)
                        {
                            items.AddRange(possibleItems);
                        }
                        // Sorteia um item
                        int index = Random.Range(0, items.Count);
                        if (map[curPos].AddObject(Instantiate(items[index])))
                        {
                            // Remove da lista para evitar items duplicados
                            items.RemoveAt(index);
                            pendingItems--;
                        }
                    }

                    // Seleciona a posição da saída na sala atual
                    Vector2 nextDir = NextDirection(curDirection, curPos, map);
                    while (nextDir == Vector2.zero)
                    {
                        // Volta uma sala e recalcula a proxima direção até achar uma saida valida
                        curPos -= curDirection;
                        directionHistory.RemoveAt(directionHistory.Count - 1);
                        curDirection = directionHistory[directionHistory.Count - 1];
                        nextDir      = NextDirection(curDirection, curPos, map);
                    }

                    // Cria a porta de saída
                    DoorPosition exitPos = GetExitPos(nextDir);
                    map[curPos].objects[exitPos] = Instantiate(door);
                    curDoor = map[curPos].objects[exitPos] as Door;
                    curDoor.targetDoorPosition = GetEntrancePos(nextDir);
                    curDoor.targetRoomID       = curID;
                    curDirection = nextDir;
                    curPos      += curDirection;
                    directionHistory.Add(curDirection);
                }
                else
                {
                    // Spawna a ultima sala e a primeira sala
                    map[curPos].Spawn(lastRoom, navigator);
                    map[Vector2.zero].Spawn(firstRoom, navigator);
                }
            }

            foreach (RoomInfo info in map.Values)
            {
                GameObject selectedRoom = roomPrefabs[Random.Range(0, roomPrefabs.Count)];
                info.Spawn(selectedRoom, navigator);
            }

            player.transform.position = map[Vector2.zero].GetWorldPos();
            // player.transform.position = new Vector3(player.transform.position.x, player.transform.position.y, 10f);
            navigator.Camera.MoveToRoom(map[Vector2.zero].SO, true);

            map.Clear();
            navigator.StartNavigation();
        }
    }
示例#17
0
 public void RoomTransition(DoorPosition doorPosition, GameObject player)
 {
     StartCoroutine(RoomTransitionTimer(doorPosition, player));
 }
示例#18
0
 public Door(DoorPosition position, string pathTo, string sourceImage)
 {
     this.Position    = position;
     this.PathTo      = pathTo;
     this.SourceImage = sourceImage;
 }
示例#19
0
    void Start()
    {
        CollectableManagerObject = GameObject.Find("Collectable Manager");
        CollectableManagerScript = (CollectableManager)CollectableManagerObject.GetComponent(typeof(CollectableManager));


        if (FullRandom)
        {
            Random.InitState(System.DateTime.Now.Millisecond);
        }
        else
        {
            Random.InitState(RandomSeed);
        }

        switch (Algorithm)
        {
        case MazeGenerationAlgorithm.PureRecursive:
            mMazeGenerator = new RecursiveMazeGenerator(Rows, Columns);
            break;

        case MazeGenerationAlgorithm.RecursiveTree:
            mMazeGenerator = new RecursiveTreeMazeGenerator(Rows, Columns);
            break;

        case MazeGenerationAlgorithm.RandomTree:
            mMazeGenerator = new RandomTreeMazeGenerator(Rows, Columns);
            break;

        case MazeGenerationAlgorithm.OldestTree:
            mMazeGenerator = new OldestTreeMazeGenerator(Rows, Columns);
            break;

        case MazeGenerationAlgorithm.RecursiveDivision:
            mMazeGenerator = new DivisionMazeGenerator(Rows, Columns);
            break;
        }
        mMazeGenerator.GenerateMaze();
        for (int row = 0; row < Rows; row++)
        {
            for (int column = 0; column < Columns; column++)
            {
                float      x    = column * (CellWidth + (AddGaps ? .2f : 0));
                float      z    = row * (CellHeight + (AddGaps ? .2f : 0));
                MazeCell   cell = mMazeGenerator.GetMazeCell(row, column);
                GameObject tmp;
                tmp = Instantiate(Floor, new Vector3(x, 0, z), Quaternion.Euler(0, 0, 0)) as GameObject;
                tmp.transform.parent = transform;

                if (AddRoof)
                {
                    tmp = Instantiate(Roof, new Vector3(x, 4, z), Quaternion.Euler(-90, 0, 0)) as GameObject;
                    tmp.transform.parent = transform;
                }

                if (cell.WallRight)
                {
                    tmp = Instantiate(Wall, new Vector3(x + CellWidth / 2, 0, z) + Wall.transform.position, Quaternion.Euler(0, 90, 0)) as GameObject;        // right
                    tmp.transform.parent = transform;
                }
                else
                {
                    int neighborColumn = column + 1;
                    if (neighborColumn < Columns)
                    {
                        MazeCell neighborCell = mMazeGenerator.GetMazeCell(row, neighborColumn);
                        if (!neighborCell.WallLeft)
                        {
                            PossibleDoorPositions.Add(new DoorPosition(new Vector3(x + CellWidth / 2, 0, z) + Wall.transform.position, Quaternion.Euler(0, 90, 0)));
                        }
                    }
                }

                if (cell.WallFront)
                {
                    tmp = Instantiate(Wall, new Vector3(x, 0, z + CellHeight / 2) + Wall.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;        // front
                    tmp.transform.parent = transform;
                }
                else
                {
                    int neighborRow = row + 1;
                    if (neighborRow < Rows)
                    {
                        MazeCell neighborCell = mMazeGenerator.GetMazeCell(neighborRow, column);
                        if (!neighborCell.WallBack)
                        {
                            PossibleDoorPositions.Add(new DoorPosition(new Vector3(x, 0, z + CellHeight / 2) + Wall.transform.position, Quaternion.Euler(0, 0, 0)));
                        }
                    }
                }

                if (cell.WallLeft)
                {
                    tmp = Instantiate(Wall, new Vector3(x - CellWidth / 2, 0, z) + Wall.transform.position, Quaternion.Euler(0, 270, 0)) as GameObject;        // left
                    tmp.transform.parent = transform;
                }
                else
                {
                    int neighborColumn = column - 1;
                    if (neighborColumn >= 0)
                    {
                        MazeCell neighborCell = mMazeGenerator.GetMazeCell(row, neighborColumn);
                        if (!neighborCell.WallRight)
                        {
                            PossibleDoorPositions.Add(new DoorPosition(new Vector3(x - CellWidth / 2, 0, z) + Wall.transform.position, Quaternion.Euler(0, 270, 0)));
                        }
                    }
                }

                if (cell.WallBack)
                {
                    tmp = Instantiate(Wall, new Vector3(x, 0, z - CellHeight / 2) + Wall.transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;        // back
                    tmp.transform.parent = transform;
                }
                else
                {
                    int neighborRow = row - 1;
                    if (neighborRow >= 0)
                    {
                        MazeCell neighborCell = mMazeGenerator.GetMazeCell(neighborRow, column);
                        if (!neighborCell.WallFront)
                        {
                            PossibleDoorPositions.Add(new DoorPosition(new Vector3(x, 0, z - CellHeight / 2) + Wall.transform.position, Quaternion.Euler(0, 180, 0)));
                        }
                    }
                }

                if (cell.IsGoal)
                {
                    PossibleGoalCells.Add(new Vector3(x, 1.5f, z));
                }
                else if (x > 2 && z > 2)
                {
                    PossibleEnemyCells.Add(new Vector3(x, 1.5f, z));
                }
            }
        }
        if (Pillar != null)
        {
            for (int row = 0; row < Rows + 1; row++)
            {
                for (int column = 0; column < Columns + 1; column++)
                {
                    float      x   = column * (CellWidth + (AddGaps?.2f : 0));
                    float      z   = row * (CellHeight + (AddGaps?.2f : 0));
                    GameObject tmp = Instantiate(Pillar, new Vector3(x - CellWidth / 2, 0, z - CellHeight / 2), Quaternion.identity) as GameObject;
                    tmp.transform.parent = transform;
                }
            }
        }

        Random.InitState(System.DateTime.Now.Millisecond);

        for (int GoalCount = 0; GoalCount < 5; GoalCount++)
        {
            int     rand    = Random.Range(0, PossibleGoalCells.Count);
            Vector3 vector3 = PossibleGoalCells[rand];

            PossibleGoalCells.RemoveAt(rand);

            //Add Goals
            GameObject GoalPrefab;
            switch (GoalCount)
            {
            case 0:
                GoalPrefab = CollectablePrefab1;
                break;

            case 1:
                GoalPrefab = CollectablePrefab2;
                break;

            case 2:
                GoalPrefab = CollectablePrefab3;
                break;

            case 3:
                GoalPrefab = CollectablePrefab4;
                break;

            case 4:
                GoalPrefab = CollectablePrefab5;
                break;

            default:
                GoalPrefab = CollectablePrefab1;
                break;
            }

            GameObject tmp = Instantiate(GoalPrefab, vector3, Quaternion.Euler(0, 0, 0)) as GameObject;
            tmp.transform.parent = transform;

            CollectableManagerScript.Add(tmp);
        }

        if (DoorCount > 0)
        {
            for (int i = 0; i < DoorCount; i++)
            {
                int          rand         = Random.Range(0, PossibleDoorPositions.Count);
                DoorPosition doorPosition = PossibleDoorPositions[rand];

                PossibleDoorPositions.RemoveAt(rand);

                GameObject tmp = Instantiate(DoorPrefab, doorPosition.Position, doorPosition.Rotation) as GameObject;
                tmp.transform.parent = transform;
            }
        }

        if (EnemyCount > 0)
        {
            for (int i = 0; i < EnemyCount; i++)
            {
                int     rand    = Random.Range(0, PossibleEnemyCells.Count);
                Vector3 vector3 = PossibleEnemyCells[rand];

                PossibleEnemyCells.RemoveAt(rand);

                GameObject tmp = Instantiate(EnemyPrefab, vector3, Quaternion.Euler(0, 0, 0)) as GameObject;
                tmp.transform.parent = transform;
            }
        }

        ParticleSystem fireWork1 = Instantiate(FireWorksParticleSystem, new Vector3(0, 0, 0), Quaternion.Euler(-90, 0, 0)) as ParticleSystem;

        fireWork1.transform.parent = transform;
        ParticleSystem fireWork2 = Instantiate(FireWorksParticleSystem, new Vector3(Rows * CellWidth, 0, 0), Quaternion.Euler(-90, 0, 0));

        fireWork2.transform.parent = transform;
        ParticleSystem fireWork3 = Instantiate(FireWorksParticleSystem, new Vector3(0, 0, Columns * CellHeight), Quaternion.Euler(-90, 0, 0));

        fireWork3.transform.parent = transform;
        ParticleSystem fireWork4 = Instantiate(FireWorksParticleSystem, new Vector3(Rows * CellWidth, 0, Columns * CellHeight), Quaternion.Euler(-90, 0, 0));

        fireWork4.transform.parent = transform;

        FireWorks.Add(fireWork1);
        FireWorks.Add(fireWork2);
        FireWorks.Add(fireWork3);
        FireWorks.Add(fireWork4);
    }