Beispiel #1
0
        public static ushort GetTile(IPlatformLayerSpatialData platform,
                                     IPlatformLayerTiles tiles,
                                     int x, int z,
                                     OutOfLevelBoundsAction action = OutOfLevelBoundsAction.Error)
        {
            //Debug.Log($"GetTile {z} * {platform.TileDim.x} + {x} = {z * platform.TileDim.x + x}");
            if (platform.IsInside(x, z))
            {
                return(tiles[z * platform.TileDim.x + x]);
            }
            switch (action)
            {
            case OutOfLevelBoundsAction.Error:
                Debug.LogError("tile is out of level bounds");
                break;

            case OutOfLevelBoundsAction.ExpandBounds:
                Debug.LogError("ExpandBounds not supported in this method");
                break;

            case OutOfLevelBoundsAction.IgnoreTile:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(action), action, null);
            }
            return(ushort.MaxValue);
        }
Beispiel #2
0
        public static void SetTile(IPlatformLayerSpatialData lvlDat, IPlatformLayerTiles tiles,
                                   int lvlXPos, int lvlZPos, ushort tile,
                                   ushort mask, ushort shift     = 0,
                                   OutOfLevelBoundsAction action = OutOfLevelBoundsAction.Error)
        {
            if (!lvlDat.IsInside(lvlXPos, lvlZPos))
            {
                switch (action)
                {
                case OutOfLevelBoundsAction.Error:
                    Debug.LogError("tile is out of level bounds");
                    break;

                case OutOfLevelBoundsAction.ExpandBounds:
                    Debug.LogError("ExpandBounds not supported in this method");
                    break;

                case OutOfLevelBoundsAction.IgnoreTile:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(action), action, null);
                }

                return;
            }
            var invertedMask = (ushort)~mask;
            var origTile     = tiles[lvlZPos * lvlDat.TileDim.x + lvlXPos];

            tiles[lvlZPos * lvlDat.TileDim.x + lvlXPos] = (ushort)((origTile & invertedMask) | (mask & (tile << shift)));
        }
Beispiel #3
0
        public static IEnumerable <Vector2Int> GetRegionTiles(IPlatformLayerSpatialData lvlDat, IPlatformLayerTiles tiles,
                                                              int startX, int startZ,
                                                              ushort compareMask)
        {
            var v2Tiles = new List <Vector2Int>();
            // mark tiles that have already been processed
            var mapFlags = new int[lvlDat.TileDim.x, lvlDat.TileDim.y];

            var tileType = (GetTile(lvlDat, tiles, startX, startZ) & compareMask);

            var queue = new Queue <Vector2Int>();

            queue.Enqueue(new Vector2Int(startX, startZ));
            mapFlags[startX, startZ] = 1;

            // spread-algorithm, select all adjacent tiles with same tileType
            while (queue.Count > 0)
            {
                var tile = queue.Dequeue();
                v2Tiles.Add(tile);

                for (var x = tile.x - 1; x <= tile.x + 1; ++x)
                {
                    for (var z = tile.y - 1; z <= tile.y + 1; ++z)
                    {
                        var adjacent = (z == tile.y || x == tile.x);
                        if (!adjacent)
                        {
                            continue;
                        }
                        if (z == tile.y && x == tile.x)
                        {
                            continue;
                        }
                        if (!lvlDat.IsInside(x, z))
                        {
                            continue;
                        }
                        if (mapFlags[x, z] != 0 || (GetTile(lvlDat, tiles, x, z) & compareMask) != tileType)
                        {
                            continue;
                        }

                        mapFlags[x, z] = 1;
                        queue.Enqueue(new Vector2Int(x, z));
                    }
                }
            }

            return(v2Tiles);
        }
        static void ApplySizeTool(IPlatformLayerSpatialData platform, IPlatformLayerTiles tiles, Vector2Int newSize,
                                  Vector2Int tileModifier, ushort[] newTiles)
        {
            for (var x = 0; x < newSize.x; ++x)
            {
                for (var z = 0; z < newSize.y; ++z)
                {
                    var oldX = x - tileModifier.x;
                    var oldZ = z - tileModifier.y;
                    var idx  = z * newSize.x + x;

                    if (!platform.IsInside(oldX, oldZ))
                    {
                        newTiles[idx] = 0;
                        continue;
                    }

                    newTiles[idx] = GetTile(platform, tiles, oldX, oldZ, OutOfLevelBoundsAction.IgnoreTile);
                }
            }
        }