void Update() { bool leftButton = Input.GetMouseButton(0); bool rightButton = Input.GetMouseButton(1); // Switch between grids when space key is pressed if (Input.GetKeyDown(KeyCode.Space)) { largeSystemActive = !largeSystemActive; } // Respond to mouse event? if (leftButton || rightButton) { // Find mouse position in 3D space Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); TileIndex index = _currentSystem.ClosestTileIndexFromRay(mouseRay); if (leftButton && _lastPainted != index) { // Ignore if this tile was painted last! _lastPainted = index; // Paint with left mouse button _brush.Paint(_currentSystem, index.row, index.column); _currentSystem.RefreshSurroundingTiles(index.row, index.column); } else if (rightButton) { // Ignore last painted state _lastPainted = new TileIndex(-1, -1); // Erase with right mouse button _currentSystem.EraseTile(index.row, index.column); _currentSystem.RefreshSurroundingTiles(index.row, index.column); } } }
/// <summary> /// Erase all out-of-bound tiles. /// </summary> /// <param name="system">Tile system.</param> /// <param name="newRows">New number of rows.</param> /// <param name="newColumns">New number of columns.</param> /// <param name="rowOffset">Number of rows of tiles to offset by.</param> /// <param name="columnOffset">Number of columns of tiles to offset by.</param> private void EraseOutOfBoundTiles(TileSystem system, int newRows, int newColumns, int rowOffset, int columnOffset) { if (system.Chunks == null) { return; } rowOffset = -rowOffset; columnOffset = -columnOffset; int offsetEndRow = rowOffset + newRows; int offsetEndColumn = columnOffset + newColumns; system.BeginBulkEdit(); for (int row = 0; row < system.RowCount; ++row) { for (int column = 0; column < system.ColumnCount; ++column) { var tile = system.GetTile(row, column); if (tile == null) { continue; } // Is tile out-of-bounds? if (row < rowOffset || row >= offsetEndRow || column < columnOffset || column >= offsetEndColumn) { system.EraseTile(row, column); system.RefreshSurroundingTiles(row, column); } } } system.EndBulkEdit(); }
public static bool ClearTile(this TileSystem map, int x, int y) { return(map.EraseTile(y, x)); }