public void Update() { // check if chunk is not null GameObject chunkObject = Engine.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); VoxelInfo voxelInfo = new VoxelInfo (voxelIndex, chunk); // create a local copy of the collision voxel so we can call functions on it GameObject voxelObject = Instantiate ( Engine.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); }
public override void OnBlockDestroy( VoxelInfo voxelInfo ) { // if the block above is tall grass, destroy it Index indexAbove = new Index (voxelInfo.index.x, voxelInfo.index.y+1, voxelInfo.index.z); if ( voxelInfo.chunk.GetVoxel(indexAbove) == 8 ) { voxelInfo.chunk.SetVoxel(indexAbove, 0, true); } }
public override void OnBlockPlace( VoxelInfo voxelInfo ) { // if the block below is grass, change it to dirt Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); if ( voxelInfo.GetVoxelType ().VTransparency == Transparency.solid && voxelInfo.chunk.GetVoxel(indexBelow) == 2) { voxelInfo.chunk.SetVoxel(indexBelow, 1, true); } }
public override void OnLook( VoxelInfo voxelInfo ) { // move the selected block ui to the block that's being looked at (convert the index of the hit voxel to absolute world position) GameObject blockSelection = GameObject.Find ("selected block graphics"); if (blockSelection != null) { blockSelection.transform.position = voxelInfo.chunk.VoxelIndexToPosition (voxelInfo.index); blockSelection.GetComponent<Renderer>().enabled = true; blockSelection.transform.rotation = voxelInfo.chunk.transform.rotation; } }
// multiplayer public static void DestroyBlockMultiplayer( VoxelInfo voxelInfo, NetworkPlayer sender ) { // received from server, don't use directly GameObject voxelObject = Instantiate ( Engine.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 static void ChangeBlockMultiplayer( VoxelInfo voxelInfo, ushort data, NetworkPlayer sender ) { // received from server, don't use directly voxelInfo.chunk.SetVoxel (voxelInfo.index, data, true); GameObject voxelObject = Instantiate ( Engine.GetVoxelGameObject (data) ) as GameObject; VoxelEvents events = voxelObject.GetComponent<VoxelEvents>(); if (events != null) { events.OnBlockChange(voxelInfo); events.OnBlockChangeMultiplayer(voxelInfo, sender); } Destroy (voxelObject); }
public void SendChangeBlock( VoxelInfo info, ushort data ) { // convert to ints int chunkx = info.chunk.ChunkIndex.x; int chunky = info.chunk.ChunkIndex.y; int chunkz = info.chunk.ChunkIndex.z; // send to server if (Network.isServer) { GetComponent<UniblocksServer>().ServerChangeBlock (Network.player, info.index.x, info.index.y, info.index.z, chunkx,chunky,chunkz, (int)data); } else { GetComponent<NetworkView>().RPC ("ServerChangeBlock", RPCMode.Server, Network.player, info.index.x, info.index.y, info.index.z, chunkx,chunky,chunkz, (int)data); } }
public void ReceiveChangeBlock( NetworkPlayer sender, int x, int y, int z, int chunkx, int chunky, int chunkz, int data ) { // receives a change sent by other client or server GameObject chunkObject = ChunkManager.GetChunk (chunkx,chunky,chunkz); if (chunkObject != null) { // convert back to VoxelInfo Index voxelIndex = new Index (x,y,z); VoxelInfo info = new VoxelInfo (voxelIndex, chunkObject.GetComponent<Chunk>()); // apply change Voxel.ChangeBlockMultiplayer (info, (ushort)data, sender); } }
public override void OnMouseDown( int mouseButton, VoxelInfo voxelInfo ) { if ( mouseButton == 0 ) { // destroy a block with LMB Voxel.DestroyBlock (voxelInfo); } else if ( mouseButton == 1 ) { // place a block with RMB if ( voxelInfo.GetVoxel() == 8 ) { // if we're looking at a tall grass block, replace it with the held block Voxel.PlaceBlock (voxelInfo, ExampleInventory.HeldBlock); } else { // else put the block next to the one we're looking at VoxelInfo newInfo = new VoxelInfo (voxelInfo.adjacentIndex, voxelInfo.chunk); // use adjacentIndex to place the block Voxel.PlaceBlock (newInfo, ExampleInventory.HeldBlock); } } }
public override void OnBlockPlace( VoxelInfo voxelInfo ) { // switch to dirt if the block above isn't 0 Index adjacentIndex = voxelInfo.chunk.GetAdjacentIndex (voxelInfo.index, Direction.up); if ( voxelInfo.chunk.GetVoxel(adjacentIndex) != 0 ) { voxelInfo.chunk.SetVoxel(voxelInfo.index, 1, true); } // if the block below is grass, change it to dirt Index indexBelow = new Index (voxelInfo.index.x, voxelInfo.index.y-1, voxelInfo.index.z); if ( voxelInfo.GetVoxelType ().VTransparency == Transparency.solid && voxelInfo.chunk.GetVoxel(indexBelow) == 2) { voxelInfo.chunk.SetVoxel(indexBelow, 1, true); } }
public static void DestroyBlock( VoxelInfo voxelInfo ) { // multiplayer - send change to server if (Engine.EnableMultiplayer) { Engine.UniblocksNetwork.GetComponent<UniblocksClient>().SendPlaceBlock ( voxelInfo, 0 ); } // single player - apply change locally else { GameObject voxelObject = Instantiate ( Engine.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 override void OnMouseDown(int mouseButton, VoxelInfo 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 } } }
public static void ChangeBlock( VoxelInfo voxelInfo, ushort data ) { // multiplayer - send change to server if (Engine.EnableMultiplayer) { Engine.UniblocksNetwork.GetComponent<UniblocksClient>().SendChangeBlock ( voxelInfo, data ); } // single player - apply change locally else { voxelInfo.chunk.SetVoxel (voxelInfo.index, data, true); GameObject voxelObject = Instantiate ( Engine.GetVoxelGameObject (data) ) as GameObject; if (voxelObject.GetComponent<VoxelEvents>() != null) { voxelObject.GetComponent<VoxelEvents>().OnBlockChange(voxelInfo); } Destroy (voxelObject); } }
public virtual void OnBlockPlaceMultiplayer( VoxelInfo voxelInfo, NetworkPlayer sender ) { }
public virtual void OnBlockPlace( VoxelInfo voxelInfo ) { }
public virtual void OnBlockEnter( GameObject enteringObject, VoxelInfo voxelInfo ) { }
public virtual void OnBlockDestroy( VoxelInfo voxelInfo ) { }
public virtual void OnBlockDestroy(VoxelInfo voxelInfo) { }
public virtual void OnBlockChangeMultiplayer(VoxelInfo voxelInfo, NetworkPlayer sender) { }
public virtual void OnMouseUp( int mouseButton, VoxelInfo voxelInfo ) { }
public static Vector3 VoxelInfoToPosition( VoxelInfo voxelInfo ) { return voxelInfo.chunk.GetComponent<Chunk>().VoxelIndexToPosition (voxelInfo.index); }
public virtual void OnLook(VoxelInfo voxelInfo) { }
public void SendPlaceBlock( VoxelInfo info, ushort data ) { // sends a voxel change to the server, which then redistributes it to other clients // convert to ints int chunkx = info.chunk.ChunkIndex.x; int chunky = info.chunk.ChunkIndex.y; int chunkz = info.chunk.ChunkIndex.z; // send to server if (Network.isServer) { GetComponent<UniblocksServer>().ServerPlaceBlock (Network.player, info.index.x, info.index.y, info.index.z, chunkx,chunky,chunkz, (int)data); } else { GetComponent<NetworkView>().RPC ("ServerPlaceBlock", RPCMode.Server, Network.player, info.index.x, info.index.y, info.index.z, chunkx,chunky,chunkz, (int)data); } }
public virtual void OnBlockStay(GameObject stayingObject, VoxelInfo voxelInfo) { }
public virtual void OnBlockEnter(GameObject enteringObject, VoxelInfo voxelInfo) { }
public virtual void OnBlockStay( GameObject stayingObject, VoxelInfo voxelInfo ) { }
public override void OnBlockEnter( GameObject enteringObject, VoxelInfo voxelInfo ) { Debug.Log ("OnBlockEnter at " + voxelInfo.chunk.ChunkIndex.ToString() + " / " + voxelInfo.index.ToString()); }
public virtual void OnLook( VoxelInfo voxelInfo ) { }
public virtual void OnBlockDestroyMultiplayer(VoxelInfo voxelInfo, NetworkPlayer sender) { }
public override void OnBlockEnter(GameObject enteringObject, VoxelInfo voxelInfo) { Debug.Log("OnBlockEnter at " + voxelInfo.chunk.ChunkIndex.ToString() + " / " + voxelInfo.index.ToString()); }
public virtual void OnBlockChange(VoxelInfo voxelInfo) { }
public static Vector3 VoxelInfoToPosition(VoxelInfo voxelInfo) { return(voxelInfo.chunk.GetComponent <Chunk>().VoxelIndexToPosition(voxelInfo.index)); }
public virtual void OnMouseHold(int mouseButton, VoxelInfo voxelInfo) { }
public virtual void OnBlockChange( VoxelInfo voxelInfo ) { }
public virtual void OnBlockPlace(VoxelInfo voxelInfo) { }