// TODO: move this to Ground platform?
    public GroundPlatform.Tile?GetNearestTile(GroundPlatform platform, Vector2 pos, float maxDist = 100000f)
    {
        Vector2 relativePos = (pos - instance.Offset) / Instance.tileSize;

        relativePos.y = -relativePos.y;

        Vector2Int intPos = new Vector2Int(Mathf.RoundToInt(relativePos.x), Mathf.RoundToInt(relativePos.y));

        GroundPlatform.Tile?tileUnderPos = platform.GetTile(intPos);
        if (tileUnderPos.HasValue)
        {
            //Debug.Log($"Tile of {platform.offset} platform under start pos returned at {relativePos}");
            return(tileUnderPos);
        }

        float scaledDistance = maxDist / Instance.tileSize;

        float minDist = scaledDistance * scaledDistance;

        GroundPlatform.Tile?tile = null;  // Returns null if all tiles are outside max range

        for (int i = 0; i < platform.tiles.Count; i++)
        {
            float d = (relativePos - platform.tiles[i].pos).sqrMagnitude;
            if (d < minDist)
            {
                minDist = d;
                tile    = platform.tiles[i];
            }
        }

        return(tile);
    }
示例#2
0
    static void DrawSkeletonGizmo(GroundPlatform target, UnityEditor.GizmoType gizmoType)
    {
        if (!UnityEditor.SceneView.currentDrawingSceneView)
        {
            return;
        }

        if (target.targetRoot.localScale != Vector3.one || target.targetRoot.lossyScale != Vector3.one)
        {
            UnityEditor.Handles.color = Color.red;

            for (int i = 0; i < 16; i++)
            {
                UnityEditor.Handles.DrawDottedLine(target.transform.position, target.transform.position + Random.onUnitSphere, 3.0f);
            }
        }

        UnityEditor.SceneView.currentDrawingSceneView.Repaint();
    }
示例#3
0
    GroundPlatform.Tile?GetNearestTile(GroundPlatform platform, Vector2 pos, float maxDist = float.MaxValue)
    {
        Vector2 relativePos = (pos - instance.Offset) / Instance.tileSize;

        relativePos.y = -relativePos.y;

        float minDist = maxDist * maxDist;

        GroundPlatform.Tile?tile = null;  // When would this return null? Max distance parameter?

        for (int i = 0; i < platform.tiles.Count; i++)
        {
            float d = (relativePos - platform.tiles[i].pos).sqrMagnitude;
            if (d < minDist)
            {
                minDist = d;
                tile    = platform.tiles[i];
            }
        }
        return(tile);
    }
示例#4
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];
                    int ends    = GroundPlatform.GetPlatformEnds(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());
    }
    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';
            }
        }
    }