public override void OnMouseDown(int mouseButton, BlockInfo voxelInfo) { if (mouseButton == 0) { Voxel.DestroyBlock(voxelInfo); // destroy with left click } else if (mouseButton == 1) // open/close with right click { if (voxelInfo.GetVoxel() == 70) // if open door { Voxel.ChangeBlock(voxelInfo, 7); // set to closed } else if (voxelInfo.GetVoxel() == 7) // if closed door { Voxel.ChangeBlock(voxelInfo, 70); // set to open } } }
// multiplayer public static void DestroyBlockMultiplayer(BlockInfo voxelInfo, NetworkPlayer sender) { // received from server, don't use directly GameObject voxelObject = Instantiate(getVoxelGameObject(voxelInfo.GetVoxel())) as GameObject; VoxelEvents events = voxelObject.GetComponent <VoxelEvents>(); if (events != null) { events.OnBlockDestroy(voxelInfo); events.OnBlockDestroyMultiplayer(voxelInfo, sender); } voxelInfo.chunk.SetVoxel(voxelInfo.index, 0, true); Destroy(voxelObject); }
public override void OnMouseDown(int mouseButton, BlockInfo voxelInfo) { if (mouseButton == 0) { Voxel.DestroyBlock(voxelInfo); return; } if (voxelInfo.GetVoxel() == 8) { Voxel.PlaceBlock(voxelInfo, ExampleInventory.HeldBlock); return; } BlockInfo newInfo = new BlockInfo(voxelInfo.adjacentIndex, voxelInfo.chunk); // use adjacentIndex to place the block Voxel.PlaceBlock(newInfo, ExampleInventory.HeldBlock); }
public static void DestroyBlock(BlockInfo voxelInfo) { // multiplayer - send change to server if (MasterEngine.getInstance().EnableMultiplayer) { MasterEngine.getInstance().UniblocksNetwork.GetComponent <UniblocksClient>().SendPlaceBlock(voxelInfo, 0); } // single player - apply change locally else { GameObject voxelObject = Instantiate(getVoxelGameObject(voxelInfo.GetVoxel())) as GameObject; if (voxelObject.GetComponent <VoxelEvents>() != null) { voxelObject.GetComponent <VoxelEvents>().OnBlockDestroy(voxelInfo); } voxelInfo.chunk.SetVoxel(voxelInfo.index, 0, true); Destroy(voxelObject); } }
public void Update() { // check if chunk is not null GameObject chunkObject = MasterEngine.getInstance().PositionToChunk(transform.position); if (chunkObject == null) { return; } // get the voxelInfo from the transform's position Chunk chunk = chunkObject.GetComponent <Chunk>(); Index voxelIndex = chunk.PositionToVoxelIndex(transform.position); BlockInfo voxelInfo = new BlockInfo(voxelIndex, chunk); // create a local copy of the collision voxel so we can call functions on it GameObject voxelObject = Instantiate(MasterEngine.getInstance().GetVoxelGameObject(voxelInfo.GetVoxel())) as GameObject; VoxelEvents events = voxelObject.GetComponent <VoxelEvents>(); if (events != null) { // OnEnter if (chunk != LastChunk || voxelIndex.IsEqual(LastIndex) == false) { events.OnBlockEnter(this.gameObject, voxelInfo); } // OnStay else { events.OnBlockStay(this.gameObject, voxelInfo); } } LastChunk = chunk; LastIndex = voxelIndex; Destroy(voxelObject); }
private void CameraLookEvents() { // first person camera var cam = Camera.main.transform; KeyValuePair <bool, RaycastHit> hitInfo = RaycastUtil.AABB(new Ray(cam.position, cam.forward), 3F); BlockInfo raycast = null; if (!hitInfo.Key) { return; } if (MasterEngine.getInstance().getVoxelEngine().raycastHasVoxel(hitInfo.Value)) { raycast = MasterEngine.getInstance().getVoxelEngine().getVoxelInfo(hitInfo.Value, 3f, false); } if (raycast != null) { // create a local copy of the hit voxel so we can call functions on it GameObject voxelObject = Instantiate(MasterEngine.getInstance().GetVoxelGameObject(raycast.GetVoxel())) as GameObject; // only execute this if the voxel actually has any events (either VoxelEvents component, or any component that inherits from it) if (voxelObject.GetComponent <VoxelEvents>() != null) { voxelObject.GetComponent <VoxelEvents>().OnLook(raycast); // for all mouse buttons, send events for (int i = 0; i < 3; i++) { if (Input.GetMouseButtonDown(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseDown(i, raycast); } if (Input.GetMouseButtonUp(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseUp(i, raycast); } if (Input.GetMouseButton(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseHold(i, raycast); } } } Destroy(voxelObject); } else { // disable selected block ui when no block is hit if (SelectedBlockGraphics != null) { SelectedBlockGraphics.GetComponent <Renderer>().enabled = false; } } }
private void MouseCursorEvents() { // cursor position KeyValuePair <bool, RaycastHit> hitInfo = RaycastUtil.AABB(Camera.main.ScreenPointToRay(Input.mousePosition), 2F); //Vector3 pos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10.0f); BlockInfo raycast = null; if (MasterEngine.getInstance().getVoxelEngine().raycastHasVoxel(hitInfo.Value)) { raycast = MasterEngine.getInstance().getVoxelEngine().getVoxelInfo(hitInfo.Value, 2F, false); } if (raycast != null) { // create a local copy of the hit voxel so we can call functions on it GameObject voxelObject = Instantiate(MasterEngine.getInstance().GetVoxelGameObject(raycast.GetVoxel())) as GameObject; // only execute this if the voxel actually has any events (either VoxelEvents component, or any component that inherits from it) if (voxelObject.GetComponent <VoxelEvents>() != null) { voxelObject.GetComponent <VoxelEvents>().OnLook(raycast); // for all mouse buttons, send events for (int i = 0; i < 3; i++) { if (Input.GetMouseButtonDown(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseDown(i, raycast); } if (Input.GetMouseButtonUp(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseUp(i, raycast); } if (Input.GetMouseButton(i)) { voxelObject.GetComponent <VoxelEvents>().OnMouseHold(i, raycast); } } } Destroy(voxelObject); } else { // disable selected block ui when no block is hit if (SelectedBlockGraphics != null) { SelectedBlockGraphics.GetComponent <Renderer>().enabled = false; } } }