public void Execute(ref ChunkStreamPoint streamer, ref Translation position) { if (streamer.didUpdate == 0) { // should do some transform stuff for the position var newChunkPosition = VoxelRaycastSystem.GetChunkPosition(new int3(position.Value), streamer.voxelDimensions); /*if (float.IsNaN(newChunkPosition.x) || (newChunkPosition.x >= -0.001f && newChunkPosition.x <= 0.001f)) * { * Debug.LogError("New Chunk Position.x is NaN"); * newChunkPosition.x = 0; * } * if (float.IsNaN(newChunkPosition.z) || (newChunkPosition.z >= -0.001f && newChunkPosition.z <= 0.001f)) * { * Debug.LogError("New Chunk Position.z is NaN"); * newChunkPosition.z = 0; * }*/ newChunkPosition.y = 0; // can limit bounds here, if out should teleport them back in (faling) if (newChunkPosition.x != streamer.chunkPosition.x || newChunkPosition.y != streamer.chunkPosition.y || newChunkPosition.z != streamer.chunkPosition.z) { streamer.didUpdate = 1; streamer.chunkPosition = newChunkPosition; } } }
void SpawnVoxel(int3 spawnPosition, int voxelID, Entity world) { var voxelDimensions = World.EntityManager.GetComponentData <World>(world).voxelDimensions; var chunkPosition = VoxelRaycastSystem.GetChunkPosition(spawnPosition, voxelDimensions);// new float3(spawnPosition.x / 16, spawnPosition.y / 16, spawnPosition.z / 16); var localPosition = VoxelRaycastSystem.GetLocalPosition(spawnPosition, chunkPosition, voxelDimensions); //Debug.LogError("Spawning voxel of Type: " + spawnType + " M: " + spawnPosition.ToString() + " C: " + chunkPosition.ToString() + " L:" + localPosition); // get chunk that position is within // get chunk // todo: store chunks in UniqueKey<(worldID, chunkPosition)> as keys) - 4 numbers to generate a unique key? Entity foundChunk = new Entity(); Chunk writeToChunk = new Chunk(); bool didFindChunk = false; foreach (Entity e in chunkSpawnSystem.chunks.Values) { Chunk chunk = World.EntityManager.GetComponentData <Chunk>(e); if (chunk.Value.chunkPosition.x == chunkPosition.x && chunk.Value.chunkPosition.y == chunkPosition.y && chunk.Value.chunkPosition.z == chunkPosition.z) { foundChunk = e; writeToChunk = chunk; didFindChunk = true; //return chunk.Value.voxels[GetVoxelArrayIndex(localChunkPosition)]; } } if (!didFindChunk) { //Debug.LogError("Could not find chunk: " + chunkPosition.ToString()); return; } // get index out of spawnPosition int index = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, voxelDimensions); //if (didFindChunk) { //Debug.LogError("Found Chunk: " + chunkPosition.ToString() + " with voxelIndex: " + index); } //Debug.LogError(" chunkPosition: " + chunkPosition.ToString() + " index at " + index); // get index from voxel list int voxelIndex = voxelIDs.IndexOf(voxelID) + 1; // add one for air writeToChunk.Value.voxels[index] = (byte)(voxelIndex); //writeToChunk.isDirty = 1; World.EntityManager.SetComponentData(foundChunk, writeToChunk); if (World.EntityManager.HasComponent <ChunkBuilder>(foundChunk)) { World.EntityManager.SetComponentData(foundChunk, new ChunkBuilder { }); } else { World.EntityManager.AddComponentData(foundChunk, new ChunkBuilder { }); } }
public void OnAddedStreamer(Entity player, Entity worldEntity) { Translation position = World.EntityManager.GetComponentData <Translation>(player); World world = World.EntityManager.GetComponentData <World>(worldEntity); int3 chunkPosition = VoxelRaycastSystem.GetChunkPosition( VoxelRaycastSystem.WorldPositionToVoxelPosition(position.Value), world.voxelDimensions); chunkPosition.y = 0; WorldStreamSystem.StreamChunksIn(World.EntityManager, chunkSpawnSystem, world.modelID != 0, worldEntity, ref world, chunkPosition, Bootstrap.GetRenderDistance(), Bootstrap.GetLoadDistance()); World.EntityManager.SetComponentData(worldEntity, world); }