public static void IntXZ(IntVector2 from, IntVector2 to, int step, Action<int, int> callback) { for (int x = from.x; x <= to.x; x += step) { for (int z = from.z; z <= to.z; z += step) { callback(x, z); } } }
public void ApplyToGrid() { var upperBounds = new IntVector2(cells.GetLength(0) - 1, cells.GetLength(1) - 1); var topLeftCorner = new IntVector3(64, 64, 64); LoopHelper.IntXZ(IntVector2.zero, upperBounds, 1, (x, z) => { var absPosition = topLeftCorner + new IntVector3(x, 0, upperBounds.z - z); var cell = RegionManager.Instance.GetCellAt(absPosition); /* If cell was empty and we don't want anything there, return */ if (this.cells[x, z] == CellType.EMPTY && cell == null) { return; } /* Otherwise, create a new cell if we need to add something here */ if (cell == null) { cell = RegionManager.Instance.CreateCellAt(absPosition); } switch (this.cells[x, z]) { case CellType.WALL: cell.CreateWall(); break; case CellType.FLOOR: cell.CreateFloor(); break; case CellType.DOOR: cell.CreateObject(new DoorObject()); break; /* This is a special case! If something was here previously, * we need to destroy the whole cell. */ case CellType.EMPTY: cell.Destroy(); break; } }); }
private void DrawRoomAction() { if (Input.GetMouseButton(0)) { if (mouseData.HasMoved()) { temporarySize.x = Mathf.Abs(mouseData.currentPosition.x - mouseData.originPosition.x); temporarySize.z = Mathf.Abs(mouseData.currentPosition.z - mouseData.originPosition.z); this.temporaryCells = new CellType[temporarySize.x, temporarySize.z]; for (int x = 0; x < temporarySize.x; x++) { for (int z = 0; z < temporarySize.z; z++) { var isWall = (x == 0 || x == temporarySize.x - 1 || z == 0 || z == temporarySize.z - 1); this.temporaryCells[x, z] = isWall ? CellType.WALL : CellType.FLOOR; } } this.temporaryOrigin = mouseData.originPosition; /* Make origin adjustments for negative room sizes */ if (mouseData.currentPosition.x < mouseData.originPosition.x) { this.temporaryOrigin.x = mouseData.currentPosition.x; } if (mouseData.currentPosition.z < mouseData.originPosition.z) { this.temporaryOrigin.z = mouseData.currentPosition.z; } mouseData.lastPosition = mouseData.currentPosition; } } else if (mouseData.wasPressed && this.temporaryCells != null) { if (temporarySize.x != 0 && temporarySize.z != 0) { SetCells(this.temporaryOrigin, this.temporaryCells); } mouseData.wasPressed = false; this.temporaryCells = null; } }
private void SetCells(IntVector2 origin, CellType[,] cells) { for (int x = 0; x < cells.GetLength(0); x++) { for (int z = 0; z < cells.GetLength(1); z++) { var spacePosition = new IntVector2(origin.x + x, origin.z + z); if (spacePosition.x >= 0 && spacePosition.x < AreaSize && spacePosition.z >= 0 && spacePosition.z < AreaSize) { this.cells[spacePosition.x, spacePosition.z] = cells[x, z]; } } } }
/** * Converts screen space to tile grid coordinates. * * @param original - Screen space coordinates */ private IntVector2 ConvertScreenToTileCoords(Vector3 original) { var coordinates = new IntVector2(original.x, original.y); coordinates.x = coordinates.x / TileSize; coordinates.z = (Screen.height - coordinates.z) / TileSize; return coordinates; }