public void Execute(ref ChunkMap chunkMap, ref Chunk chunk) //Entity entity, int index, { if (chunkMap.isBiome == 0 && chunkMap.dirty == 1) { chunkMap.dirty = 2; // get all the top voxels //int xzIndex = 0; for (int i = 0; i < chunk.Value.voxelDimensions.x; i++) { for (int j = 0; j < chunk.Value.voxelDimensions.z; j++) { byte topMostVoxel = 0; byte highestHeight = 0; int xzIndex = i + j * chunkMap.height; for (int k = (int)chunk.Value.voxelDimensions.y - 1; k >= 0; k--) { int xyzIndex = VoxelRaycastSystem.GetVoxelArrayIndex(new int3(i, k, j), chunk.Value.voxelDimensions); if (chunk.Value.voxels[xyzIndex] != 0) { topMostVoxel = chunk.Value.voxels[xyzIndex]; highestHeight = (byte)k; break; } } chunkMap.topVoxels[xzIndex] = topMostVoxel; chunkMap.heights[xzIndex] = highestHeight; //xzIndex++; } } } }
private int3 FindNewPosition(ref Chunk chunk) { //float3 chunkPosition = chunk.Value.chunkPosition; var voxelDimensions = chunk.Value.voxelDimensions; // int worldID = chunk.worldID; // find first y position of value not air int voxelIndex; int3 checkVoxelPosition; int randomPositionX = (int)math.floor(UnityEngine.Random.Range(0, voxelDimensions.x - 1)); int randomPositionZ = (int)math.floor(UnityEngine.Random.Range(0, voxelDimensions.z - 1)); for (int j = (int)chunk.Value.voxelDimensions.y - 1; j >= 0; j--) { // if not air, pick j + 1 checkVoxelPosition = new int3(randomPositionX, j, randomPositionZ); voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(checkVoxelPosition, voxelDimensions); if (chunk.Value.voxels[voxelIndex] != 0) { int3 newPosition = new int3(checkVoxelPosition.x, j + 1, checkVoxelPosition.z); //return chunk.GetVoxelPosition() + newPosition; return(newPosition); } } Debug.LogError("No solid ground found for character spawning at."); return(int3.Zero()); }
protected override void OnUpdate() { Entities.WithAll <CharacterRaycaster>().ForEach((Entity e, ref CharacterRaycaster raycaster) => { if (raycaster.commandID == 0) { if (UnityEngine.Time.time - raycaster.lastCasted >= 0.1f) { raycaster.lastCasted = UnityEngine.Time.time; if (voxelRaycastSystem != null) { Entity camera = raycaster.camera; var zoxID = World.EntityManager.GetComponentData <ZoxID>(camera).id; if (World.EntityManager.HasComponent <Controller>(e)) { Controller controller = World.EntityManager.GetComponentData <Controller>(e); WorldBound worldBound = World.EntityManager.GetComponentData <WorldBound>(e); if (controller.inputType == (byte)(DeviceType.KeyboardMouse)) { float2 screenPosition = cameraSystem.GetScreenPosition(zoxID); raycaster.commandID = voxelRaycastSystem.QueueRaycast(screenPosition, worldBound.world, camera); } else { float2 screenPosition = cameraSystem.GetScreenPosition(zoxID, new float2(Screen.width / 2f, Screen.height / 2f)); raycaster.commandID = voxelRaycastSystem.QueueRaycast(screenPosition, worldBound.world, camera); } } else { Debug.LogError("Raycasting not supported by npcs yet."); //raycaster.commandID = voxelRaycastSystem.QueueRaycast(screenPosition, raycaster.cameraID); } } } } else { if (VoxelRaycastSystem.commandOutputPositions.ContainsKey(raycaster.commandID)) { raycaster.voxelPosition = VoxelRaycastSystem.PullPosition(raycaster.commandID); raycaster.commandID = 0; } } if (raycaster.DidCast() == 1) { voxelPreviewSystem.SetPosition(raycaster.voxelPosition - new float3(0, 0.49f, 0)); // Bootstrap.instance.selectedVoxel.transform.position = ; } }); }
private byte GetVoxelType(Voxels.World world, WorldBound worldBound, int3 voxelPosition) { var chunkPosition = VoxelRaycastSystem.GetChunkPosition(voxelPosition, worldBound.voxelDimensions); Chunk chunk; if (GetChunk(world, chunkPosition, out chunk) && chunk.Value.voxels.Length > 0) { if (chunk.isGenerating == 1) { return(1); } int voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(voxelPosition - chunk.GetVoxelPosition(), worldBound.voxelDimensions); if (voxelIndex >= chunk.Value.voxels.Length) { return(1); } return(chunk.Value.voxels[voxelIndex]); } return(1); }
private void SetPlayerCharacter(Entity entity, int id, Entity world, int metaID, float3 position) { CharacterDatam characterDatam = meta[metaID]; var voxelDimensions = new int3(16, 64, 16); if (worldSpawnSystem != null) { voxelDimensions = World.EntityManager.GetComponentData <Voxels.World>(world).voxelDimensions; } Inventory inventory = new Inventory { }; inventory.InitializeItems(9, characterDatam.items); World.EntityManager.SetComponentData(entity, inventory); QuestLog questlog = new QuestLog { }; questlog.Initialize(characterDatam.quests); World.EntityManager.SetComponentData(entity, questlog); World.EntityManager.SetComponentData(entity, new ItemHitTaker { radius = characterDatam.itemPickupRadius }); StatbarSystem.SpawnPlayerStatbar(World.EntityManager, entity); ActionbarSystem.SpawnUI(World.EntityManager, entity); CrosshairSpawnSystem.SpawnUI(World.EntityManager, entity); World.EntityManager.SetComponentData(entity, new ChunkStreamPoint { world = world, voxelDimensions = voxelDimensions, didUpdate = 1, chunkPosition = VoxelRaycastSystem.GetChunkPosition(new int3(position), voxelDimensions) }); }
protected override void OnUpdate() { Entities.WithAll <WorldBound, Translation>().ForEach((Entity e, ref WorldBound worldBound, ref Translation translation) => { Entity worldEntity = worldBound.world; if (World.EntityManager.Exists(worldEntity) == false) { return; } float3 positionOfMe = translation.Value; if (worldBound.enabled == 0) { worldBound.worldTransform = World.EntityManager.GetComponentData <LocalToWorld>(worldEntity).Value; float3 newPosition2 = math.transform(math.inverse(worldBound.worldTransform), positionOfMe); var voxelPositionIn = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition2); Chunk chunk; var chunkPosition = VoxelRaycastSystem.GetChunkPosition(voxelPositionIn, worldBound.voxelDimensions); Voxels.World thisworld = World.EntityManager.GetComponentData <Voxels.World>(worldEntity); if (GetChunk(thisworld, chunkPosition, out chunk)) { if (chunk.isGenerating == 0) { worldBound.enabled = 1; } else { return; } } else { return; } } float3 positionOffset = -new float3(0, math.floor(worldBound.size.y), 0); // math.floor float3 newPosition = math.transform(math.inverse(worldBound.worldTransform), positionOfMe) + positionOffset; var voxelPosition = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition); worldBound.voxelPosition = voxelPosition; var voxelPositionLeft = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition + new float3(-worldBound.size.x, 0, 0)); var voxelPositionRight = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition + new float3(worldBound.size.x, 0, 0)); var voxelPositionForward = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition + new float3(0, 0, worldBound.size.z)); var voxelPositionBack = VoxelRaycastSystem.WorldPositionToVoxelPosition(newPosition + new float3(0, 0, -worldBound.size.z)); if (worldBound.voxelPositionLeft == voxelPositionLeft && worldBound.voxelPositionRight == voxelPositionRight && worldBound.voxelPositionForward == voxelPositionForward && worldBound.voxelPositionBack == voxelPositionBack) { return; } worldBound.voxelPositionLeft = voxelPositionLeft; worldBound.voxelPositionRight = voxelPositionRight; worldBound.voxelPositionForward = voxelPositionForward; worldBound.voxelPositionBack = voxelPositionBack; Voxels.World world = World.EntityManager.GetComponentData <Voxels.World>(worldEntity); worldBound.voxelTypeLeft = GetVoxelType(world, worldBound, voxelPositionLeft); worldBound.voxelTypeRight = GetVoxelType(world, worldBound, voxelPositionRight); worldBound.voxelTypeForward = GetVoxelType(world, worldBound, voxelPositionForward); worldBound.voxelTypeBack = GetVoxelType(world, worldBound, voxelPositionBack); worldBound.voxelTypeLeftBelow = GetVoxelType(world, worldBound, voxelPositionLeft + int3.Down()); // new float3(0, -1, 0)); worldBound.voxelTypeRightBelow = GetVoxelType(world, worldBound, voxelPositionRight + int3.Down()); // new float3(0, -1, 0)); worldBound.voxelTypeForwardBelow = GetVoxelType(world, worldBound, voxelPositionForward + int3.Down()); // new float3(0, -1, 0)); worldBound.voxelTypeBackBelow = GetVoxelType(world, worldBound, voxelPositionBack + int3.Down()); // new float3(0, -1, 0)); }); }