public override void OnShowRegion(int regionX, int regionY) { FiniteGrid region = grid.GetRegion(regionX, regionY); FiniteComponentGrid regionTriangle = triangles.GetRegion(regionX, regionY); PositionRegionRenderer renderer = GetRegionRenderer(regionX, regionY); ClearRenderers(); renderer.mesh.PrepareUV(); int bx = regionX * Grid.REGION_SIZE; int by = regionY * Grid.REGION_SIZE; for (int i = 0; i < Grid.REGION_SIZE; i++) { for (int j = 0; j < Grid.REGION_SIZE; j++) { Tile tile = region.Get(i, j); if (tile != null && tile.id != 0) { if (regionTriangle == null) { _OnSet(null, renderer, i + bx, j + by, tile); } else { _OnSet(regionTriangle.Get(i, j) as GridTriangle, renderer, i + bx, j + by, tile); } } } } renderer.mesh.ApplyUV(); ApplyRenderers(); }
/// <summary> /// Saves the region data if saving is enabled. Ignores otherwise. /// </summary> /// <param name="regionX">The X coordinate of the region.</param> /// <param name="regionY">The Y coordinate of the region.</param> /// <param name="tiles">The data for the region.</param> public override void SaveTiles(int regionX, int regionY, FiniteGrid tiles) { if (useSave) { gridSerializer.SaveGrid(tiles); } }
void Serialize(FiniteGrid tiles) { BinaryFormatter bf = new BinaryFormatter(); Directory.CreateDirectory(RootPath()); FileStream file = File.Create(RegionPath(tiles.regionX, tiles.regionY)); bf.Serialize(file, tiles); file.Close(); }
void _SaveGrid(FiniteGrid tiles) { Point point = new Point(tiles.regionX, tiles.regionY); if (!_manifest.regionPositions.Contains(point)) { _manifest.regionPositions.Add(point); } ThreadManager.CreateJob(() => { Serialize(tiles); return(0); }, (fail, res) => { }); }
/// <summary> /// Sets the tile information for an entire region. Reuses provided FiniteGrid, do not use after this point. /// </summary> /// <param name="X">The X coordinate of the region.</param> /// <param name="Y">The Y coordinate of the region.</param> public void SetRegion(int X, int Y, FiniteGrid newRegion) { FiniteGrid region = GetRegion(X, Y); if (region == null) { regions.Add(newRegion); region = newRegion; } }
public override void OnSet(int x, int y, int width, int height) { int minRegionX = Mathf.FloorToInt(x / (float)Grid.REGION_SIZE); int minRegionY = Mathf.FloorToInt(y / (float)Grid.REGION_SIZE); int maxRegionX = Mathf.FloorToInt((x + width) / (float)Grid.REGION_SIZE); int maxRegionY = Mathf.FloorToInt((y + height) / (float)Grid.REGION_SIZE); for (int regionX = minRegionX; regionX <= maxRegionX; regionX++) { for (int regionY = minRegionY; regionY <= maxRegionY; regionY++) { FiniteGrid region = grid.GetRegion(regionX, regionY); if (region.presented) { PositionRegionRenderer renderer = GetRegionRenderer(regionX, regionY); FiniteComponentGrid triangleRegion = triangles.GetRegion(regionX, regionY); int startX = regionX * Grid.REGION_SIZE; int startY = regionY * Grid.REGION_SIZE; int minX = Mathf.Max(x, startX); int minY = Mathf.Max(y, startY); int maxX = Mathf.Min(x + width, (regionX + 1) * Grid.REGION_SIZE); int maxY = Mathf.Min(y + height, (regionY + 1) * Grid.REGION_SIZE); renderer.mesh.PrepareUV(); for (int i = minX; i < maxX; i++) { for (int j = minY; j < maxY; j++) { Tile tile = region.Get(i - startX, j - startY); if (tile != null) { if (triangleRegion != null) { _OnSet(triangleRegion.Get(x - startX, y - startY) as GridTriangle, renderer, i, j, tile); } else { _OnSet(null, renderer, i, j, tile); } } } } renderer.mesh.ApplyUV(); } } } }
/// <summary> /// Gets the specified region and creates it if there is none yet. /// </summary> /// <returns>The or create region.</returns> /// <param name="X">The X region coordinate.</param> /// <param name="Y">The Y region coordinate.</param> public FiniteGrid GetOrCreateRegion(int X, int Y) { FiniteGrid region = GetRegion(X, Y); if (region == null) { region = new FiniteGrid(X, Y, _regionSize); regions.Add(region); } return(region); }
/// <summary> /// Sets the tile at the specified coordinates. /// </summary> /// <param name="x">The x tile coordinate.</param> /// <param name="y">The y tile coordinate.</param> /// <param name="value">The new value.</param> public FiniteGrid Set(int x, int y, Tile value) { FiniteGrid region = GetContainingRegion(x, y); if (region == null) { int X = Mathf.FloorToInt(((float)x) / _regionSize); int Y = Mathf.FloorToInt(((float)y) / _regionSize); region = new FiniteGrid(X, Y, _regionSize); regions.Add(region); } region.Set(x - region.regionX * _regionSize, y - region.regionY * _regionSize, value); return(region); }
/// <summary> /// Called when a square of tile's information is set. /// </summary> /// <param name="x">The bottom left x coordinate of the square.</param> /// <param name="y">The bottom left y coordinate of the square.</param> /// <param name="width">The width of the square.</param> /// <param name="height">The height of the square.</param> public virtual void OnSet(int x, int y, int width, int height) { int minRegionX = Mathf.FloorToInt(x / (float)Grid.REGION_SIZE); int minRegionY = Mathf.FloorToInt(y / (float)Grid.REGION_SIZE); int maxRegionX = Mathf.FloorToInt((x + width) / (float)Grid.REGION_SIZE); int maxRegionY = Mathf.FloorToInt((y + height) / (float)Grid.REGION_SIZE); for (int regionX = minRegionX; regionX <= maxRegionX; regionX++) { for (int regionY = minRegionY; regionY <= maxRegionY; regionY++) { FiniteGrid region = grid.GetRegion(regionX, regionY); if (region.presented) { int startX = regionX * Grid.REGION_SIZE; int startY = regionY * Grid.REGION_SIZE; int minX = Mathf.Max(x, startX); int minY = Mathf.Max(y, startY); int maxX = Mathf.Min(x + width, (regionX + 1) * Grid.REGION_SIZE); int maxY = Mathf.Min(y + height, (regionY + 1) * Grid.REGION_SIZE); for (int i = minX; i < maxX; i++) { for (int j = minY; j < maxY; j++) { Tile tile = region.Get(i - startX, j - startY); if (tile != null) { OnSet(i, j, tile); } } } } } } }
/// <summary> /// Called when a region is presented. /// </summary> /// <param name="regionX">The X coordinate of the region.</param> /// <param name="regionY">The Y coordinate of the region.</param> public virtual void OnShowRegion(int regionX, int regionY) { FiniteGrid region = grid.GetRegion(regionX, regionY); int startX = regionX * Grid.REGION_SIZE; int endX = (regionX + 1) * Grid.REGION_SIZE; int startY = regionY * Grid.REGION_SIZE; int endY = (regionY + 1) * Grid.REGION_SIZE; for (int i = startX; i < endX; i++) { for (int j = startY; j < endY; j++) { Tile tile = region.Get(i - startX, j - startY); if (tile != null) { OnSet(i, j, tile); } } } }
/// <summary> /// Asynchronously returns the data for a region through the callback. The tiles are either loaded if loading is enabled or generated if loading /// is not used or that region has not yet been loaded. /// </summary> /// <param name="regionX">The X coordinate of the region.</param> /// <param name="regionY">The Y coordinate of the region.</param> /// <param name="callback">The callback to call once the region is loaded.</param> public override void LoadTiles(int regionX, int regionY, Action <FiniteGrid> callback) { if (useSave && gridSerializer.IsRegionSaved(regionX, regionY)) { gridSerializer.LoadGrid(regionX, regionY, callback); return; } LargeRegion largeRegion = GetRegions(regionX, regionY); if (largeRegion == null) { int largeRegionX = Mathf.FloorToInt(regionX / (float)generationRegionWidth) * generationRegionWidth; int largeRegionY = Mathf.FloorToInt(regionY / (float)generationRegionHeight) * generationRegionHeight; largeRegion = new LargeRegion { regionX = largeRegionX, regionY = largeRegionY, regionWidth = generationRegionWidth, regionHeight = generationRegionHeight, tiles = algorithm.GenerateTiles( largeRegionX * Grid.REGION_SIZE, largeRegionY * Grid.REGION_SIZE, generationRegionWidth * Grid.REGION_SIZE, generationRegionHeight * Grid.REGION_SIZE ) }; largeRegions.Add(largeRegion); if (largeRegions.Count > 15) { largeRegions.RemoveRange(0, 7); } } int xOffset = (regionX - largeRegion.regionX) * Grid.REGION_SIZE; int yOffset = (regionY - largeRegion.regionY) * Grid.REGION_SIZE; FiniteGrid region2 = new FiniteGrid(regionX, regionY, Grid.REGION_SIZE); bool empty = true; for (int i = 0; i < Grid.REGION_SIZE; i++) { for (int j = 0; j < Grid.REGION_SIZE; j++) { Tile tile = largeRegion.tiles [xOffset + i, yOffset + j]; if (tile.id > 0) { empty = false; } region2.Set(i, j, tile); } } if (empty) { callback(null); } else { callback(region2); } }
/// <summary> /// Save a region. Uses threading. /// </summary> /// <param name="tiles">The region tiles.</param> public void SaveGrid(FiniteGrid tiles) { _SaveGrid(tiles); SaveManifest(); }
/// <summary> /// Saves the region data /// </summary> /// <param name="regionX">The X coordinate of the region.</param> /// <param name="regionY">The Y coordinate of the region.</param> /// <param name="tiles">The data for the region.</param> public abstract void SaveTiles(int regionX, int regionY, FiniteGrid tiles);
public override void OnShowRegion(int regionX, int regionY) { int bx = regionX * Grid.REGION_SIZE; int by = regionY * Grid.REGION_SIZE; FiniteGrid region = grid.GetRegion(regionX, regionY); FiniteComponentGrid regionComponents = components.GetOrCreateRegion(regionX, regionY); for (int y = 0; y < Grid.REGION_SIZE; y++) { GridColliderPart currentWrapper = components.Get(bx - 1, y + by) as GridColliderPart; for (int x = 0; x < Grid.REGION_SIZE - 1; x++) { Tile tile = region.Get(x, y); if (tile != null) { TileInfo info = grid.atlas [tile.id]; if (currentWrapper != null && currentWrapper.Compatible(info) && !info.isVertical && !currentWrapper.isVertical) { currentWrapper.width++; } else { if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); } if (info.shape != TileShape.EMPTY && !info.isVertical) { currentWrapper = GridColliderPart.CreateColliderPart(containerGO, grid, info, bx + x, by + y, 1, 1); } else { currentWrapper = null; } } regionComponents.Set(x, y, currentWrapper); } else { if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); currentWrapper = null; } regionComponents.Set(x, y, null); } } if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); } Tile edgeTile = region.Get(Grid.REGION_SIZE - 1, y); if (edgeTile != null) { OnSet(bx + Grid.REGION_SIZE - 1, by + y, edgeTile); } } for (int x = 0; x < Grid.REGION_SIZE; x++) { GridColliderPart currentWrapper = components.Get(bx + x, by - 1) as GridColliderPart; for (int y = 0; y < Grid.REGION_SIZE - 1; y++) { Tile tile = region.Get(x, y); if (tile != null) { TileInfo info = grid.atlas [tile.id]; if (currentWrapper != null && currentWrapper.Compatible(info) && info.isVertical && currentWrapper.isVertical) { currentWrapper.height++; regionComponents.Set(x, y, currentWrapper); } else { if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); } if (info.shape != TileShape.EMPTY && info.isVertical) { currentWrapper = GridColliderPart.CreateColliderPart(containerGO, grid, info, bx + x, by + y, 1, 1); regionComponents.Set(x, y, currentWrapper); } else { currentWrapper = null; } } } else { if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); currentWrapper = null; } } } if (currentWrapper != null) { currentWrapper.ResetSizeAndPosition(grid); } Tile edgeTile = region.Get(x, Grid.REGION_SIZE - 1); if (edgeTile != null && grid.atlas[edgeTile.id].isVertical) { OnSet(bx + x, by + Grid.REGION_SIZE - 1, edgeTile); } } }
/// <summary> /// Gets the tile at the specified coordinates and the containing region. /// </summary> /// <param name="x">The x tile coordinate.</param> /// <param name="y">The y tile coordinate.</param> public Tile Get(int x, int y, out FiniteGrid region) { region = GetContainingRegion(x, y); return(region != null?region.Get(x - region.regionX *_regionSize, y - region.regionY *_regionSize) : null); }