コード例 #1
0
    public static GroundPlatform[] DivideToPlatforms(List <GroundPlatform.Tile> tiles)
    {
        // Divide to platforms
        int[] platNums = new int[tiles.Count];
        for (int i = 0; i < tiles.Count; i++)
        {
            platNums[i] = -1;
        }

        int platIndex = 0;

        for (int i = 0; i < tiles.Count; i++)
        {
            var openList = new List <GroundPlatform.Tile>();

            if (platNums[i] == -1)
            {
                openList.Add(tiles[i]);
                platNums[i] = platIndex;

                while (openList.Count > 0)
                {
                    // Get connected neighbors

                    var   current = openList[0];
                    short ends    = GroundPlatform.GetPlatformOpenings(current);

                    //Debug.Log("Tile: " + new Vector2Int(current.pos.x, current.pos.y) + " type: " + current.type + " rot: " + current.rotation + " direction flags: " + (ends & 8) + " " + (ends & 4) + " " + (ends & 2) + " " + (ends & 1));
                    if ((ends & 1) == 1)
                    {
                        int neighborIndex = tiles.FindIndex(t => t.pos == new Vector2Int(current.pos.x + 1, current.pos.y));
                        if (neighborIndex > -1)
                        {
                            if (platNums[neighborIndex] == -1)
                            {
                                openList.Add(tiles[neighborIndex]);
                                platNums[neighborIndex] = platIndex;
                            }
                            else if (platNums[neighborIndex] != platIndex)
                            {
                                Debug.LogWarning("Platform index collision 1!");
                            }
                        }
                        else
                        {
                            Debug.Log("Couldn't find a 1 tile");
                        }
                    }

                    if ((ends & 2) == 2)
                    {
                        int neighborIndex = tiles.FindIndex(t => t.pos == new Vector2Int(current.pos.x, current.pos.y - 1));
                        if (neighborIndex > -1)
                        {
                            if (platNums[neighborIndex] == -1)
                            {
                                openList.Add(tiles[neighborIndex]);
                                platNums[neighborIndex] = platIndex;
                            }
                            else if (platNums[neighborIndex] != platIndex)
                            {
                                Debug.LogWarning("Platform index collision 2!");
                            }
                        }
                        else
                        {
                            Debug.Log("Couldn't find a 2 tile");
                        }
                    }

                    if ((ends & 4) == 4)
                    {
                        int neighborIndex = tiles.FindIndex(t => t.pos == new Vector2Int(current.pos.x - 1, current.pos.y));
                        if (neighborIndex > -1)
                        {
                            if (platNums[neighborIndex] == -1)
                            {
                                openList.Add(tiles[neighborIndex]);
                                platNums[neighborIndex] = platIndex;
                            }
                            else if (platNums[neighborIndex] != platIndex)
                            {
                                Debug.LogWarning("Platform index collision 4!");
                            }
                        }
                        else
                        {
                            Debug.Log("Couldn't find a 4 tile");
                        }
                    }

                    if ((ends & 8) == 8)
                    {
                        int neighborIndex = tiles.FindIndex(t => t.pos == new Vector2Int(current.pos.x, current.pos.y + 1));
                        if (neighborIndex > -1)
                        {
                            if (platNums[neighborIndex] == -1)
                            {
                                openList.Add(tiles[neighborIndex]);
                                platNums[neighborIndex] = platIndex;
                            }
                            else if (platNums[neighborIndex] != platIndex)
                            {
                                Debug.LogWarning("Platform index collision 8!");
                            }
                        }
                        else
                        {
                            Debug.Log("Couldn't find an 8 tile");
                        }
                    }

                    openList.RemoveAt(0);
                }

                platIndex++;
            }
        }

        var platforms = new List <GroundPlatform>();

        for (int i = 0; i < platIndex; i++)
        {
            var platTiles = new List <GroundPlatform.Tile>();
            for (int j = 0; j < platNums.Length; j++)
            {
                if (platNums[j] == i)
                {
                    platTiles.Add(tiles[j]);
                }
            }

            platforms.Add(new GroundPlatform(platTiles.ToArray()));
        }

        return(platforms.ToArray());
    }
コード例 #2
0
    public void LoadSector(Sector sector)
    {
        searchQueue.Clear();
        for (int i = 0; i < AIData.entities.Count; i++)
        {
            searchQueue.Enqueue(AIData.entities[i]);
        }

        Vector2 center = new Vector2(sector.bounds.x + sector.bounds.w / 2, sector.bounds.y - sector.bounds.h / 2);

        if (sector.platform) // Old data
        {
            BuildTiles(sector.platform, center);
        }
        else if (sector.platformData.Length > 0)
        {
            GameObject[] prefabs = new GameObject[prefabNames.Length];
            for (int i = 0; i < prefabNames.Length; i++)
            {
                prefabs[i] = ResourceManager.GetAsset <GameObject>(prefabNames[i]);
            }

            tileSize = prefabs[0].GetComponent <SpriteRenderer>().bounds.size.x;

            var cols = sector.bounds.w / (int)tileSize;
            var rows = sector.bounds.h / (int)tileSize;

            Offset = new Vector2
            {
                x = center.x - tileSize * (cols - 1) / 2F,
                y = center.y + tileSize * (rows - 1) / 2F
            };

            Size = new Vector2Int
            {
                x = cols,
                y = rows
            };

            directionMap = new short[cols, rows];
            for (int i = 0; i < cols; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    directionMap[i, j] = -1;
                }
            }

            // Assemble platforms from tile prefabs
            sector.platforms = new GroundPlatform[sector.platformData.Length];
            for (int i = 0; i < sector.platformData.Length; i++)
            {
                var plat = new GroundPlatform(sector.platformData[i], prefabs, this);
                sector.platforms[i] = plat;
            }

            groundPlatforms = sector.platforms;

            // Create the direction map
            for (int i = 0; i < groundPlatforms.Length; i++)
            {
                for (int j = 0; j < groundPlatforms[i].tiles.Count; j++)
                {
                    GroundPlatform.Tile t = groundPlatforms[i].tiles[j];
                    if (t.pos.x >= cols || t.pos.y >= rows || t.pos.x < 0 || t.pos.y < 0)
                    {
                        Debug.LogWarning($"Invalid tile position: { t.pos } Bounds: {cols}, {rows}");
                        continue;
                    }
                    directionMap[t.pos.x, t.pos.y] = GroundPlatform.GetPlatformOpenings(t);
                }
            }


            // Direction map debug
            string str = "";
            for (int j = 0; j < rows; j++)
            {
                for (int i = 0; i < cols; i++)
                {
                    str += directionMap[i, j].ToString().PadLeft(2, '0') + ' ';
                }
                str += '\n';
            }
        }
    }