public IntVector2[] GetRandomUniquePositions(IntVector2 startTile, int maxDistanceFromCenter, int numTilesToGenerate) { IntVector2[] tiles = new IntVector2[numTilesToGenerate]; for (int i = 0; i < numTilesToGenerate; i++) { int xPosition = startTile.x + _randomProvider.GetRandomIntegerInRange(-maxDistanceFromCenter, maxDistanceFromCenter + 1); int yPosition = startTile.y + _randomProvider.GetRandomIntegerInRange(-maxDistanceFromCenter, maxDistanceFromCenter + 1); xPosition = System.Math.Min((int)_grid.NumTilesX - 1, System.Math.Max(0, xPosition)); yPosition = System.Math.Min((int)_grid.NumTilesY - 1, System.Math.Max(0, yPosition)); tiles[i] = IntVector2.Of(xPosition, yPosition); } return(tiles); }
public void LoadTiles() { if (_mapSectionData.Sprites == null || _mapSectionData.Sprites.Length == 0) { _logger.LogError(LoggedFeature.Map, "Map Data has no sprites"); return; } uint x = 0; uint y = 0; while (y < _mapSectionData.GridData.NumTilesY) { x = 0; uint miny = UInt32.MaxValue; while (x < _mapSectionData.GridData.NumTilesX) { Sprite sprite = _mapSectionData.Sprites[_randomProvider.GetRandomIntegerInRange(0, _mapSectionData.Sprites.Length)]; TileRendererBehaviour tileRendererBehaviour = _tileRendererPool.Spawn(sprite); tileRendererBehaviour.SpriteRenderer.flipX = _randomProvider.GetRandomIntegerInRange(0, 2) == 0; tileRendererBehaviour.SpriteRenderer.flipY = _randomProvider.GetRandomIntegerInRange(0, 2) == 0; tileRendererBehaviour.transform.position = _positionCalculator.GetTileOriginWorldPosition(IntVector2.Of(x, y)) + new Vector2(sprite.bounds.extents.x, sprite.bounds.extents.y); // Calculate how many units in X this unit generates (assume square for now). uint numXTiles = (uint)Mathf.CeilToInt(sprite.bounds.size.x / _mapSectionData.PixelsPerUnit); uint numYTiles = (uint)Mathf.CeilToInt(sprite.bounds.size.y / _mapSectionData.PixelsPerUnit); x += numXTiles; miny = System.Math.Min(numYTiles, miny); } y += miny; } }