public void ApplyMask(TileMask mask) { this.tileMask = mask; mask.transform.position = transform.position; mask.transform.parent = transform.parent; mask.maskedTile = this; }
public void UpdateNeighborTiles(Vector3 position, float width, LayerMask layer, Vector3 pivotOffset) { List <Collider2D> neighborColliders = GetNeighbors(position, width, layer); for (int i = 0; i < neighborColliders.Count; i++) { TileMask mask = GetTileMask(neighborColliders[i].transform.position - pivotOffset, width, layer); int index = GetTilePrefab(mask); GameObject obj = (GameObject)PrefabUtility.InstantiatePrefab(tiles[index]) as GameObject; obj.transform.position = neighborColliders[i].transform.position; obj.transform.rotation = neighborColliders[i].transform.rotation; obj.transform.parent = neighborColliders[i].transform.parent; obj.transform.localScale = new Vector3(width, width, width); obj.name = mask.ToString(); if (neighborColliders[i].GetComponent <SpriteRenderer>() != null && obj.GetComponent <SpriteRenderer>() != null) { obj.GetComponent <SpriteRenderer>().color = neighborColliders[i].GetComponent <SpriteRenderer>().color; } if (obj.GetComponent <BoxCollider2D>() == null) { obj.AddComponent <BoxCollider2D>(); } GameObject.DestroyImmediate(neighborColliders[i].gameObject); } }
public static TileSet loadTileSet(RabbitPlatform game, string setFileName) { TileSetInfo info; TileSet ts; TileSetFile file = new TileSetFile(setFileName); TileMask[] masks; if (file.ReadMagic() != "TS") { throw new FileLoadException("Wrong file format!", file.path); } info = new TileSetInfo(file.ReadUInt32(), file.ReadUInt32(), file.ReadByte()); masks = new TileMask[info.Width * info.Height]; Texture2D texture = TileGraphicsFilePart.fromStream(game, file.BaseStream); for (int y = 0; y < info.Height; y++) { for (int x = 0; x < info.Width; x++) { masks[x + y * info.Width] = TileMaskFilePart.fromStream(file.BaseStream); } } ts = TileSet.loadFromTexture(texture, info, masks); return(ts); }
public void ApplyTileMask(Tile tile, TileMaskType maskType) { TileMask tileMask = TileMaskUtil.Instance.CreateTileMask(maskType); tile.ApplyMask(tileMask); m_liveMasks.Add(tileMask); tileMask.notifyTileMaskDestroy += OnTileMaskDestory; }
public TileMask GetTileMask(Vector3 pos, float width, LayerMask layer) { TileMask mask = 0; RaycastHit2D leftHit = Utility.GetFrontmostRaycastHit(pos + new Vector3(-width, 0, 0), tag, layer); if (leftHit.collider != null) { mask = mask | TileMask.Left; } RaycastHit2D rightHit = Utility.GetFrontmostRaycastHit(pos + new Vector3(width, 0, 0), tag, layer); if (rightHit.collider != null) { mask = mask | TileMask.Right; } RaycastHit2D topHit = Utility.GetFrontmostRaycastHit(pos + new Vector3(0, width, 0), tag, layer); if (topHit.collider != null) { mask = mask | TileMask.Top; } RaycastHit2D bottomHit = Utility.GetFrontmostRaycastHit(pos + new Vector3(0, -width, 0), tag, layer); if (bottomHit.collider != null) { mask = mask | TileMask.Bottom; } return(mask); }
private void isDirty(TileMask obj) { obj.GetBitmap(); counter++; if (counter == selection.Count) { counter = 0; buildSelectionBox(); Refresh(); } }
public GameObject GetTile(Vector3 position, float width, Color color, GameObject parent) { if (tiles == null) { return(null); } GameObject newTile; TileMask mask = GetTileMask(position, width, layerMask); int index = GetTilePrefab(mask); RaycastHit2D hit = Utility.GetFrontmostRaycastHit(position, tag, layerMask); if (hit.collider != null) { newTile = hit.collider.gameObject; } else { newTile = (GameObject)PrefabUtility.InstantiatePrefab(tiles[index]); } newTile.transform.localScale = new Vector3(width, width, width); newTile.transform.position = position; newTile.name = mask.ToString(); newTile.transform.parent = parent.transform; Vector3 pivotOffset = Vector3.zero; if (newTile.GetComponent <SpriteRenderer>() != null) { // newTile.GetComponent<SpriteRenderer>().color = color; Sprite sprite = newTile.GetComponent <SpriteRenderer>().sprite; Vector2 pivot = sprite.pivot; pivotOffset = new Vector3(pivot.x / sprite.rect.width - .5f, pivot.y / sprite.rect.height - .5f, 0); position += pivotOffset; newTile.transform.position += pivotOffset; } if (newTile.GetComponent <BoxCollider2D>() == null) { newTile.AddComponent <BoxCollider2D>(); } UpdateNeighborTiles(position, width, layerMask, pivotOffset); return(newTile); }
private int GetPrefabHelper(TileMask mask) { string maskStr = mask.ToString(); var result = System.Convert.ToString((int)mask, 2).PadLeft(4, '0'); int index = 0; for (int i = 0; i < tiles.Count; i++) { if (tiles[i].name == result) { index = i; } } return(index); }
public static void toStream(RabbitPlatform game, Stream stream, Texture2D texture) { RenderTarget2D target = new RenderTarget2D(game.GraphicsDevice, Tile.STANDARD_GTILE_WIDTH, Tile.STANDARD_GTILE_HEIGHT); SpriteBatch sb = new SpriteBatch(game.GraphicsDevice); int w = texture.Width / Tile.STANDARD_GTILE_WIDTH, h = texture.Height / Tile.STANDARD_GTILE_HEIGHT; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { game.GraphicsDevice.SetRenderTarget(target); game.GraphicsDevice.Clear(Color.White); sb.Begin(); sb.Draw(texture, new Rectangle(0, 0, Tile.STANDARD_GTILE_WIDTH, Tile.STANDARD_GTILE_HEIGHT), new Rectangle(x * Tile.STANDARD_GTILE_WIDTH, y * Tile.STANDARD_GTILE_HEIGHT, Tile.STANDARD_GTILE_WIDTH, Tile.STANDARD_GTILE_HEIGHT), Color.White); sb.End(); game.GraphicsDevice.SetRenderTarget(null); toStream(stream, TileMask.fromTexture(target)); } } }
public static TileMask fromStream(Stream stream) { BinaryReader reader = new BinaryReader(stream); byte[] compressed = reader.ReadBytes((int)Math.Ceiling(Tile.STANDARD_GTILE_WIDTH / 8d) * Tile.STANDARD_GTILE_HEIGHT), data = new byte[Tile.STANDARD_GTILE_WIDTH * Tile.STANDARD_GTILE_WIDTH]; for (int y = 0; y < Tile.STANDARD_GTILE_HEIGHT; y++) { for (int x = 0; x < Math.Ceiling(Tile.STANDARD_GTILE_WIDTH / 8d); x++) { for (int i = 0; i < 8; i++) { if ((byte)(compressed[(int)(x + y * Math.Ceiling(Tile.STANDARD_GTILE_WIDTH / 8d))] & (1 << i)) == (1 << i)) { data[x * 8 + y * Tile.STANDARD_GTILE_WIDTH + i] = 1; } } } } return(TileMask.fromData(data)); }
public TileMask[] GetBitmapsFromSelectedTiles(bool flipX, bool flipY, TilePriority priority) { if (selectedTiles == null) { return(null); } TileMask[] tms = new TileMask[selectedTiles.GetLength(0) * selectedTiles.GetLength(1)]; int w = selectedTiles.GetLength(0) - 1; int h = selectedTiles.GetLength(1) - 1; int k = 0; int x, y; char c1, c2; c1 = selectedTiles[0, 0].Code[2]; c2 = selectedTiles[w, 0].Code[2]; bool even1 = (c1 % 2 == 0 && c1 > '9') || (c1 % 2 == 1 && c1 <= '9'); bool even2 = (c2 % 2 == 0 && c2 > '9') || (c2 % 2 == 1 && c2 <= '9'); bool oddX = even1 != even2; c1 = selectedTiles[0, 0].Code[1]; c2 = selectedTiles[0, h].Code[1]; even1 = (c1 % 2 == 0 && c1 > '9') || (c1 % 2 == 1 && c1 <= '9'); even2 = (c2 % 2 == 0 && c2 > '9') || (c2 % 2 == 1 && c2 <= '9'); bool oddY = even1 != even2; x = 0; int tileSize = 16; if (selectedTiles[0, 0].Size == 8) { tileSize = 8; } int zsiz = tileSize * TileZoom; for (int i = 0; i < selectedTiles.GetLength(0); i++, x += zsiz) { if (tileSize == 16 && oddX && i == w) { x -= (zsiz / 2); } y = 0; for (int j = 0; j < selectedTiles.GetLength(1); j++, y += zsiz) { if (tileSize == 16 && oddY && j == h) { y -= (zsiz / 2); } tms[k] = new TileMask(sp, selectedTiles[i, j], TileZoom, flipX, flipY) { Priority = priority, XDisp = x, YDisp = y }; k++; } } return(tms); }
public void OnTileMaskDestory(TileMask tileMask) { tileMask.maskedTile.tileMask = null; m_liveMasks.Remove(tileMask); }
private int GetTilePrefab(TileMask mask) { int index = 0; TileMask testMask = TileMask.Top | TileMask.Bottom | TileMask.Right; if (mask == testMask) { index = GetPrefabHelper(TileMask.Left); } testMask = TileMask.Top | TileMask.Left | TileMask.Right; if (mask == testMask) { index = GetPrefabHelper(TileMask.Bottom); } testMask = TileMask.Bottom | TileMask.Left | TileMask.Right; if (mask == testMask) { index = GetPrefabHelper(TileMask.Top); } testMask = TileMask.Bottom | TileMask.Left | TileMask.Top; if (mask == testMask) { index = GetPrefabHelper(TileMask.Right); } testMask = TileMask.Right | TileMask.Bottom; if (mask == testMask) { index = GetPrefabHelper(TileMask.Left | TileMask.Top); } testMask = TileMask.Top | TileMask.Right; if (mask == testMask) { index = GetPrefabHelper(TileMask.Left | TileMask.Bottom); } testMask = TileMask.Left | TileMask.Top; if (mask == testMask) { index = GetPrefabHelper(TileMask.Right | TileMask.Bottom); } testMask = TileMask.Left | TileMask.Bottom; if (mask == testMask) { index = GetPrefabHelper(TileMask.Top | TileMask.Right); } testMask = TileMask.Left | TileMask.Right; if (mask == testMask) { index = GetPrefabHelper(TileMask.Top | TileMask.Bottom); } testMask = TileMask.Top | TileMask.Bottom; if (mask == testMask) { index = GetPrefabHelper(TileMask.Right | TileMask.Left); } if (mask == TileMask.Left) { index = GetPrefabHelper(TileMask.Top | TileMask.Right | TileMask.Bottom); } if (mask == TileMask.Bottom) { index = GetPrefabHelper(TileMask.Top | TileMask.Right | TileMask.Left); } if (mask == TileMask.Right) { index = GetPrefabHelper(TileMask.Top | TileMask.Bottom | TileMask.Left); } if (mask == TileMask.Top) { index = GetPrefabHelper(TileMask.Right | TileMask.Bottom | TileMask.Left); } return(index); }
private void lst_SelectedIndexChanged(object sender, System.EventArgs e) { Mask = lst.SelectedItem as TileMask; }
//Removes moves made invalid by the king being in check or king moving into check private void RemoveInvalidCheckMoves() { TileIndex wKing = board.wKingIndex; TileIndex bKing = board.bKingIndex; //Debug.Log("White King Position" + wKing.row + ", " + wKing.col); //Debug.Log("Black King Position" + bKing.row + ", " + bKing.col); isWInCheck = allThreatenedTileMaskB[wKing]; isBInCheck = allThreatenedTileMaskW[bKing]; if (isWInCheck && isBInCheck) { Debug.LogException(new Exception("Both kings flagged as in check")); } Debug.Log("Check State, White: " + isWInCheck + ", Black:" + isBInCheck); //In check logic if (isWInCheck) { FilterCheckResolvingMoves(wKing); } if (isBInCheck) { FilterCheckResolvingMoves(bKing); } PreventMoveIntoCheck(bKing, allThreatenedTileMaskW); PreventMoveIntoCheck(wKing, allThreatenedTileMaskB); //Prevent king moving into check by removing moves to threatened tiles void PreventMoveIntoCheck(TileIndex king, TileMask <bool> threats) { for (int i = allMoves[king].Count - 1; i >= 0; i--) { if (threats[allMoves[king][i]]) { allMoves[king].RemoveAt(i); } } } //Removes all moves that do not resolve check from Valid move lists void FilterCheckResolvingMoves(TileIndex king) { if (piecesAttackingKing.Count == 0) { Debug.LogException(new Exception("Attempted to resolve check where there is no attacking piece.")); } var kingProperties = board.GetTilePiecePropertiesAt(king); bool isOneThreat = piecesAttackingKing.Count == 1; //Calculate Mask for valid moves TileMask <bool> validMovesMask = new TileMask <bool>(); //-declare //-If the number of attacking pieces is 1, allow blocking or taking threat if (isOneThreat) { //Debug.Log("One King Threat Detected"); TileIndex threat = piecesAttackingKing[0]; var threatInfo = board.GetTilePiecePropertiesAt(threat); //For all single threats the attacking piece can be taken validMovesMask[threat] = true; //For Bishop queen and rook points between their line of sight are still valid if (threatInfo.Type == PieceType.Bishop || threatInfo.Type == PieceType.Rook || threatInfo.Type == PieceType.Queen) { foreach (TileIndex point in GetPointsBetween(king, threat)) { validMovesMask[point] = true; } ; //Remove king move to square behind but still inline with bishop/rook/queen TileIndex toRemove = GetPointAfter(threat, king); allMoves[king].Remove(toRemove); } //Apply mask to all team pieces other than king } for (int i = 0; i < allMoves.Length; i++) { if (Utils.IndexToTileIndex(i) != king) { ChessPieceProperties piece = board.GetTilePiecePropertiesAt(Utils.IndexToTileIndex(i)); if (piece != null) { if (piece.Team == kingProperties.Team) { if (isOneThreat) { //For one threat Apply mask to all team pieces other than king for (int j = allMoves[i].Count - 1; j >= 0; j--) { if (!validMovesMask[allMoves[i][j]]) { allMoves[i].RemoveAt(j); } } } else { //More than one threat, force move king allMoves[i].Clear(); } } } } } } }
//Adds castling moves if king and rooks in their starting positions on the row and are not blocked private void AddRowCastleMoves(int row) { TileIndex kingStartIndex = new TileIndex(row, 4); var king = board.GetTilePiecePropertiesAt(kingStartIndex); if (king == null) { return; } TileMask <bool> threats = king.Team == Team.White ? allThreatenedTileMaskB : allThreatenedTileMaskW; //Debug.Log("King has moved: " + king.isHasMoved); if (king.Type == PieceType.King && king.isHasMoved == false) { //King is on starting square and hasn't moved var kingSideRook = board.GetPieceProperties(king.Team, PieceID.KingSideRook); var queenSideRook = board.GetPieceProperties(king.Team, PieceID.QueenSideRook); if (queenSideRook != null) { //Debug.Log("QueenSideRook has moved: " + queenSideRook.isHasMoved); if (!queenSideRook.isHasMoved) { //Debug.Log("Checking Queen-side castle rule"); //Left Rook on starting square and hasn't moved //Check no piece at other squares; bool isClear = true; for (int i = 1; i < 4; i++) { if (null != board.GetTilePiecePropertiesAt(row, i)) { isClear = false; break; } } //Block if in check or moving through check for (int i = 2; i <= 4; i++) { if (threats[new TileIndex(row, i)] != false) { isClear = false; break; } } if (isClear) { //No piece Obstructions or threats allMoves[kingStartIndex].Add(new TileIndex(row, 2)); } } } if (kingSideRook != null) { if (!kingSideRook.isHasMoved) { //Debug.Log("Checking King-side castle rule"); //Right Rook on starting square and hasn't moved //Check no piece at other squares; bool isClear = true; for (int i = 6; i > 4; i--) { if (null != board.GetTilePiecePropertiesAt(row, i)) { isClear = false; break; } } //Block if in check or moving through check for (int i = 6; i >= 4; i--) { if (threats[new TileIndex(row, i)] != false) { isClear = false; break; } } if (isClear) { //No piece Obstructions or threats allMoves[kingStartIndex].Add(new TileIndex(row, 6)); } } } } }