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))); }
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); }
internal static void DrawPlatformBounds(IPlatformLayerSpatialData platform, Vector3Int posOffset, float gridSize, float gridYSize, Color drawCol) { var bounds = GetBounds(platform, posOffset, gridSize, gridYSize); CustomHandleDraw.DrawBounds(bounds, drawCol); }
public static Bounds GetBounds(IPlatformLayerSpatialData platform, Vector3Int posOffset, float gridSize, float gridYSize) { var size = gridSize * platform.Size.Vector3() + new Vector3(0, gridYSize, 0); var bounds = new Bounds(posOffset.Vector3() + platform.Position.Vector3() + (size / 2), size); return(bounds); }
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); }
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 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); }
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); } } }
public static bool IsInsideWithInset(this IPlatformLayerSpatialData lvl, int x, int z, int inset) => x >= inset && x < lvl.TileDim.x - inset && z >= inset && z < lvl.TileDim.y - inset;
public static bool IsInsideWithoutEdge(this IPlatformLayerSpatialData lvl, int x, int z) => lvl.IsInsideWithInset(x, z, 1);
public static bool IsInside(this IPlatformLayerSpatialData lvl, int x, int z) => x >= 0 && x < lvl.TileDim.x && z >= 0 && z < lvl.TileDim.y;