static void PeriodicProcGenCellCheck() { var count = components.Count; if (count < 1) { return; } var array = new long[count]; components.Keys.CopyTo(array, 0); var entityId = array[random.Next(count)]; var coords = components[entityId].coords; var cellCoords = AsteroidGeneration.CellCoordsFromPosition(coords.x, coords.z); shipCells.TryGetValue(entityId, out var gridCoords);//gridCoords defaults if not found if (cellCoords.x != gridCoords.x || cellCoords.z != gridCoords.z) { shipCells[entityId] = cellCoords; SpatialOSConnectionSystem.requestPopulateGridCellOps.Enqueue( new CommandRequestOp <AsteroidSpawner.Commands.PopulateGridCell, PopulateGridCellRequest> { EntityId = new EntityId(1), //1 is the Spawner id according to the init. snapshot Request = new PopulateGridCellRequest(cellCoords.x, cellCoords.z), }); } }
static void ProcessCommandOps()//One request per frame to prevent overload { if (commandRequestOps.Count < 1) { return; } var request = commandRequestOps.Dequeue().Request; var offsets = AsteroidGeneration.GridOffsets(new GridCoords(request.cellX, request.cellZ)); var cells = new List <GridCoords>(); for (int j = 0; j < offsets.Length; j++) { var offset = offsets[j]; if (populatedCells.Add(offset))//check if cells are already populated { cells.Add(offset); } } if (cells.Count <= 0) { return; } var featurePoints = new Coordinates[cells.Count * 9]; var asteroidsCoords = new Coordinates[cells.Count * AsteroidGeneration.AsteroidNumber]; Parallel.For(0, cells.Count, i => { AsteroidGeneration.FeaturePoints(cells[i]).CopyTo(featurePoints, i * 9); AsteroidGeneration.RandomAsteroidsCoordinates(cells[i]).CopyTo(asteroidsCoords, i * AsteroidGeneration.AsteroidNumber); }); Parallel.For(0, asteroidsCoords.Length, j => { var points = new Coordinates[9]; //use Span<Coordinates> instead to avoid allocation Array.Copy(featurePoints, (j / AsteroidGeneration.AsteroidNumber) * 9, points, 0, 9); AsteroidGeneration.PushAwayFromFeaturePoints(ref asteroidsCoords[j], points); }); Parallel.For(0, asteroidsCoords.Length, j => { SpatialOSConnectionSystem.entitiesToCreate.Enqueue(EntityTemplates.DefaultAsteroid(asteroidsCoords[j])); }); }
private void Awake() { asteroidGeneration = GetComponent <AsteroidGeneration>(); }
public void Initialize(World world, int chunkSize, Vector3Int position) { asteroidGeneration = world.gameObject.GetComponent <AsteroidGeneration>(); this.chunkSize = chunkSize; this.position = position; _isolevel = world.isolevel; _densityGenerator = world.densityGenerator; int worldPosX = position.x; int worldPosY = position.y; int worldPosZ = position.z; midLength = chunkSize * asteroidGeneration.worldLength / 2; midWidth = chunkSize * asteroidGeneration.worldWidth / 2; midHeight = chunkSize * asteroidGeneration.worldHeight / 2; points = new VoxelPoint[chunkSize + 1, chunkSize + 1, chunkSize + 1]; asteroidGeneration = world.asteroidGeneration; _seed = asteroidGeneration.seed; _marchingCubes = new MarchingCubes(points, _isolevel, _seed); var asteroidLength = asteroidGeneration.radius * 2; var asteroidWidth = asteroidGeneration.radius * 2; var asteroidHeight = asteroidGeneration.radius * 2; // Fill array with density and material ID of 0 for (int x = 0; x < points.GetLength(0); x++) { for (int y = 0; y < points.GetLength(1); y++) { for (int z = 0; z < points.GetLength(2); z++) { points[x, y, z] = new VoxelPoint( new Vector3Int(x, y, z), 0.0f, 0 ); } } } for (int x = 0; x < points.GetLength(0); x++) { for (int y = 0; y < points.GetLength(1); y++) { for (int z = 0; z < points.GetLength(2); z++) { int currX = x + worldPosX; int currY = y + worldPosY; int currZ = z + worldPosZ; float density = GetVoxelDensity(world.asteroidData, currX, currY, currZ); SetDensity(density, x, y, z); int material = GetVoxelMaterial(world.asteroidData, currX, currY, currZ); SetMaterial(material, x, y, z); } } } }