Esempio n. 1
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)));
        }
Esempio n. 2
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);
        }
        static void ApplySizeTool(IPlatformLayerSpatialData platform, IPlatformLayerTiles tiles, ISizeData roomSize,
                                  SizePositionMod mod)
        {
            if (mod.SpecialCase)
            {
                mod.TileModifier = (mod.PosModifier * -1);
            }

            var sizeModifier   = mod.SizeModifier;
            var offsetModifier = mod.PosModifier;
            var tileModifier   = mod.TileModifier;

            var newSize = platform.TileDim + sizeModifier;
            var newPos  = platform.Position + offsetModifier;

            // level cannot begin before room begins, shrink size, add offset
            if (newPos.x < 0)
            {
                tileModifier += Vector2Int.right * newPos.x;
                newSize       = new Vector2Int(Mathf.Max(0, newSize.x + newPos.x), newSize.y);
                newPos        = new Vector2Int(0, newPos.y);
            }
            if (newPos.y < 0)
            {
                tileModifier += Vector2Int.up * newPos.y;
                newSize       = new Vector2Int(newSize.x, Mathf.Max(0, newSize.y + newPos.y));
                newPos        = new Vector2Int(newPos.x, 0);
            }

            if (ApplySizeTool_CheckLevelOutsideOfRoom(platform, tiles, roomSize, newPos, newSize, out var roomTileDim))
            {
                return;
            }

            CutIfOverflowing(newPos, ref newSize, roomTileDim);

            var newTiles = new ushort[newSize.x * newSize.y];

            //var newTiles = new PlatformLayerTilesArray() { Tiles_ = new PlatformLayerTilesBufferElement[newSize.x * newSize.y] };
            ApplySizeTool(platform, tiles, newSize, tileModifier, newTiles);
            platform.Position = newPos;
            platform.Size     = newSize - new Vector2Int(1, 1);
            tiles.SetTiles(newTiles);

            SceneView.RepaintAll();
        }
        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);
                }
            }
        }
Esempio n. 5
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);
        }
        public static void SizeToolsGUI(AnimBool showTools, IPlatformLayerSpatialData platform, IPlatformLayerTiles tiles, SizeData roomSize)
        {
            if (SizeToolsGUI(showTools, out var mod, false) != ChangeCheck.Changed)
            {
                return;
            }

            ApplySizeTool(platform, tiles, roomSize, mod);
        }
        static bool ApplySizeTool_CheckLevelOutsideOfRoom(IPlatformLayerSpatialData platform, IPlatformLayerTiles tiles,
                                                          ISizeData roomSize, Vector2Int newPos, Vector2Int newSize, out Vector2Int roomTileDim)
        {
            // level begins outside of room or a size-dimension is 0 -> empty level
            roomTileDim = roomSize.RoomTileDim();
            // ReSharper disable once ComplexConditionExpression
            if (newPos.x < roomTileDim.x && newPos.y < roomTileDim.y && newSize.x > 0 && newSize.y > 0)
            {
                return(false);
            }
            platform.Position = Vector2Int.zero;
            platform.Size     = Vector2Int.one;
            var newTiles = new ushort[platform.TileDim.x * platform.TileDim.y];

            tiles.SetTiles(newTiles);
            return(true);
        }