public static SpawnOrientation Shift(this SpawnOrientation spawnOrientation, int shiftBy)
        {
            int index = (int)spawnOrientation;

            index += shiftBy;

            index %= SpawnOrientationEnumLength;

            if (index < 0)
            {
                index += SpawnOrientationEnumLength;
            }

            return((SpawnOrientation)index);
        }
        public static float ToAngle(this SpawnOrientation spawnOrientation)
        {
            switch (spawnOrientation)
            {
            case SpawnOrientation.Front: return(0f);

            case SpawnOrientation.Right: return(90f);

            case SpawnOrientation.Back: return(180f);

            case SpawnOrientation.Left: return(270f);

            default:
                Debug.LogError($"Converting SpawnOrientation '{spawnOrientation}' to angle is not supported.");
                return(0f);
            }
        }
        public static SpawnOrientation Opposite(this SpawnOrientation spawnOrientation)
        {
            switch (spawnOrientation)
            {
            case SpawnOrientation.Back:
                return(SpawnOrientation.Front);

            case SpawnOrientation.Front:
                return(SpawnOrientation.Back);

            case SpawnOrientation.Left:
                return(SpawnOrientation.Right);

            case SpawnOrientation.Right:
                return(SpawnOrientation.Left);

            default:
                throw new ArgumentOutOfRangeException(nameof(SpawnOrientation), spawnOrientation, $"Getting opposite for SpawnOrientation '{spawnOrientation}' is not supported.");
            }
        }
        public static Direction ToDirection(this SpawnOrientation spawnOrientation)
        {
            switch (spawnOrientation)
            {
            case SpawnOrientation.Front:
                return(Direction.Front);

            case SpawnOrientation.Right:
                return(Direction.Right);

            case SpawnOrientation.Back:
                return(Direction.Back);

            case SpawnOrientation.Left:
                return(Direction.Left);

            default:
                throw new ArgumentOutOfRangeException(nameof(spawnOrientation), spawnOrientation, $"Converting SpawnOrientation '{spawnOrientation}' to Direction is not supported.");
            }
        }
 public static bool TowardsNegative(this SpawnOrientation spawnOrientation)
 {
     return(spawnOrientation == SpawnOrientation.Left || spawnOrientation == SpawnOrientation.Back);
 }
 public static bool IsSideways(this SpawnOrientation spawnOrientation)
 {
     return(spawnOrientation == SpawnOrientation.Left || spawnOrientation == SpawnOrientation.Right);
 }
 public static SpawnOrientation Shift(this SpawnOrientation spawnOrientation, bool clockwise)
 {
     return(clockwise ? spawnOrientation.Shift(1) : spawnOrientation.Shift(-1));
 }
Exemplo n.º 8
0
    // Sets the tiles for the tilemap
    void SetTilemap(bool useTileArray = false)
    {
        int numPacmanSpawnLocations = 0; // these are for averaging the positions

        int[] numGhostSpawnLocations = new int[4];
        dotLocations    = new List <Vector2>();
        bigDotLocations = new List <Vector2>();
        int numCherrySpawnLocations = 0; // for averaging the position

        cherryLocation = new Vector2();
        levelHasCherry = false;
        bool[] isGhostInLevel = new bool[4];

        if (useTileArray)
        {
            pacmanSpawnLocation    = Vector2Int.zero;
            ghostSpawnLocations    = new Vector2[4];
            ghostSpawnOrientations = new SpawnOrientation[] { SpawnOrientation.North, SpawnOrientation.North, SpawnOrientation.North, SpawnOrientation.North };
        }
        levelTilemap.ClearAllTiles();

        // for now just load a bunch of bools if they're tiles or not because that's pretty basic
        for (int y = 0; y < isWalkableArray.Count; y++)
        {
            for (int x = 0; x < isWalkableArray[0].Count; x++)
            {
                if (!useTileArray)
                {
                    if (isWalkableArray[y][x])
                    {
                        // this is the floor
                        levelTilemap.SetTile(new Vector3Int(x, y, 0), tiles[0]);
                    }
                    else
                    {
                        // this is the generic solid square wall
                        levelTilemap.SetTile(new Vector3Int(x, y, 0), tiles[1]);
                    }
                }
                else
                {
                    char currentTileChar = tileCharArray[y][x];
                    char upper           = char.ToUpper(currentTileChar);
                    // If Pacman
                    if (upper == 'S')
                    {
                        currentTileChar      = ' '; // make it a floor
                        pacmanSpawnLocation += new Vector2(x, y);
                        numPacmanSpawnLocations++;  // so we can average the position later
                        if (char.IsUpper(currentTileChar))
                        {
                            pacmanSpawnOrientation = SpawnOrientation.North;
                        }
                        else
                        {
                            pacmanSpawnOrientation = SpawnOrientation.South;
                        }
                        // If Ghost
                    }
                    else if (upper == 'I' || upper == 'B' || upper == 'P' || upper == 'C')
                    {
                        int currentGhost = 0;
                        if (upper == 'I')
                        {
                            currentGhost = 0;
                        }
                        else if (upper == 'B')
                        {
                            currentGhost = 1;
                        }
                        else if (upper == 'P')
                        {
                            currentGhost = 2;
                        }
                        else if (upper == 'C')
                        {
                            currentGhost = 3;
                        }
                        currentTileChar = ' ';                  // make it a floor
                        ghostSpawnLocations[currentGhost] += new Vector2(x, y);
                        numGhostSpawnLocations[currentGhost]++; // so we can average the position later
                        isGhostInLevel[currentGhost] = true;
                        if (char.IsUpper(currentTileChar))
                        {
                            ghostSpawnOrientations[currentGhost] = SpawnOrientation.North;
                        }
                        else
                        {
                            ghostSpawnOrientations[currentGhost] = SpawnOrientation.South;
                        }
                        // If Small Dot
                    }
                    else if (currentTileChar == '.')
                    {
                        dotLocations.Add(new Vector2(x, y));
                        currentTileChar = ' '; // make it a floor
                        // If Big Dot
                    }
                    else if (currentTileChar == '*')
                    {
                        bigDotLocations.Add(new Vector2(x, y));
                        currentTileChar = ' '; // make it a floor
                        // If Cherry
                    }
                    else if (currentTileChar == '&')
                    {
                        // get it? it's twisty like a cherry
                        numCherrySpawnLocations++;
                        levelHasCherry  = true;
                        cherryLocation += new Vector2(x, y);
                        currentTileChar = ' ';
                    }

                    // then use the char mapping to the index to the tile
                    int currTileIndex = tileKeys.FindIndex(u => u == currentTileChar);
                    if (currTileIndex >= 0 && currTileIndex < tiles.Count)
                    {
                        levelTilemap.SetTile(new Vector3Int(x, y, 0), tiles[currTileIndex]);
                    }
                    else
                    {
                        levelTilemap.SetTile(new Vector3Int(x, y, 0), tiles[0]);
                        Debug.LogError("CurrTileIndex not found");
                    }
                }
            }
        }

        // now average the spawn locations to get them located correctly:
        pacmanSpawnLocation /= numPacmanSpawnLocations;
        cherryLocation      /= numCherrySpawnLocations;
        for (int i = 0; i < ghostSpawnLocations.Length; i++)
        {
            if (isGhostInLevel[i])
            {
                ghostSpawnLocations[i] /= numGhostSpawnLocations[i];
            }
            else
            {
                ghostSpawnLocations[i] = -Vector2.one; // put it off the map so that the level knows not to spawn it in
            }
        }

        // set the camera orthographic size to encompas the width and height of the level
        float ratio = levelCamera.aspect;

        // Debug.Log("Aspect ratio: " + ratio);
        // the UIScreenCoveragePercent expands the camera even further so we can have UI on the sides, this ensures that it won't overlap with the level
        levelCamera.orthographicSize = Mathf.Max(levelHeight / 2f / (1 - UIScreenCoveragePercent.y), levelWidth / 2f / ratio / (1 - UIScreenCoveragePercent.x));

        // then set the camera position, focusing on the leve, offset by the yoffset to account for the UI
        levelCamera.transform.position = new Vector3(levelWidth / 2f, levelHeight / 2f - UIYOffset * levelCamera.orthographicSize * 2, -10);
    }