Exemple #1
0
        public override void OnMouseDown(int mouseButton, VoxelInfo voxelInfo)
        {
            if (mouseButton == 0)         // destroy a block with LMB
            {
                if (BuildMode)
                {
                    Voxel.DestroyBlock(voxelInfo);
                }
                else
                {
                    GameObject blockSelection = GameObject.Find("selected block graphics");
                    if (blockSelection != null)
                    {
                        blockSelection.GetComponent <WreckBlockLogic>().BeginWreck(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 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 void Update()
        {
            Chunk chunk = Engine.PositionToChunk(transform.position);

            if (chunk == null)
            {
                return;
            }

            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(gameObject, voxelInfo);
                }
                else // OnStay
                {
                    events.OnBlockStay(gameObject, voxelInfo);
                }
            }

            LastChunk = chunk;
            LastIndex = voxelIndex;

            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
            }

            }
        }
Exemple #5
0
        public static void DestroyBlockMultiplayer(VoxelInfo voxelInfo, NetworkPlayer sender)
        {
            // received from server, don't use directly
            GameObject obj = Instantiate(Engine.GetVoxelGameObject(voxelInfo.GetVoxel())) as GameObject;

            OnBlockDestroyMultiplayer(voxelInfo, obj, sender);

            RegisterWithChunk(voxelInfo, 0);
            Destroy(obj);
        }
Exemple #6
0
        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() == 10)
                {                                    // if open door
                    Voxel.ChangeBlock(voxelInfo, 7); // set to closed
                }

                else if (voxelInfo.GetVoxel() == 7)
                {                                     // if closed door
                    Voxel.ChangeBlock(voxelInfo, 10); // set to open
                }
            }
        }
Exemple #7
0
        // 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);
        }
        // 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);
        }
Exemple #9
0
        private void MouseCursorEvents()    // cursor position
        //Vector3 pos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10.0f);
        {
            VoxelInfo raycast = Engine.VoxelRaycast(Camera.main.ScreenPointToRay(Input.mousePosition), 9999.9f, false);

            if (raycast != null)
            {
                // create a local copy of the hit voxel so we can call functions on it
                GameObject voxelObject = Instantiate(Engine.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;
                }
                if (CrossHairs != null)
                {
                    CrossHairs.GetComponent <Image>().color = Color.white;
                }
            }
        }
        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);
            }
            }
        }
Exemple #11
0
        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);
            }
        }
Exemple #12
0
        private void CameraLookEvents()    // first person camera
        {
            VoxelInfo raycast = Engine.VoxelRaycast(Camera.main.transform.position, Camera.main.transform.forward, Range, false);

            if (raycast != null)
            {
                // create a local copy of the hit voxel so we can call functions on it
                GameObject voxelObject = Instantiate(Engine.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;
                }
            }
        }
Exemple #13
0
 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);
         }
     }
 }
Exemple #14
0
        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 obj = Instantiate(Engine.GetVoxelGameObject(voxelInfo.GetVoxel())) as GameObject;

                OnBlockDestroy(voxelInfo, obj);

                RegisterWithChunk(voxelInfo, 0);
                Destroy(obj);
            }
        }
Exemple #15
0
        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);
        }
Exemple #16
0
        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 = 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(voxelIndex) == false)
                {
                    events.OnBlockEnter(gameObject, voxelInfo);
                }

                // OnStay
                else
                {
                    events.OnBlockStay(gameObject, voxelInfo);
                }
            }

            lastChunk = chunk;

            Destroy(voxelObject);
        }
Exemple #17
0
        public override void OnMouseDown(int mouseButton, VoxelInfo voxelInfo)
        {
            VoxelInfo newInfo          = new VoxelInfo(voxelInfo.adjacentIndex, voxelInfo.chunk);    // use adjacentIndex to place the block
            var       absoluteBlockPos = newInfo.chunk.VoxelIndexToPosition(newInfo.index);

            if (mouseButton == 0)
            {             // destroy a block with LMB
                Debug.Log("absoluteBlockPos Y:" + absoluteBlockPos.y);

                if (absoluteBlockPos.y == Constants.Game.BOTTOM_BLOCK_Y)
                {
                    return;
                }

                Voxel.DestroyBlock(voxelInfo);
            }
            else if (mouseButton == 1)
            {
                // place a block with RMB
                var playerObject = GameObject.Find(Constants.Game.SINGLE_PLAYER_NAME);

                if (playerObject != null)
                {
                    var playerFeet = playerObject.transform.FindChild(Constants.Game.SINGLE_PLAYER_FEET_NAME);

                    if (playerFeet != null)
                    {
                        var playerVoxelInfo = Engine.PositionToVoxelInfo(playerFeet.transform.position);

                        if (playerVoxelInfo == null)
                        {
                            //NOTE: Doesn't build block because it looks like we've reach the top of the world...
                            Debug.Log("Reached top of the world...");
                            return;
                        }

                        var absolutePlayerPos = playerVoxelInfo.chunk.VoxelIndexToPosition(playerVoxelInfo.index);

                        var distance = DistanceFromBlock(absolutePlayerPos, absoluteBlockPos);

                        Debug.Log("Distance: " + distance);

                        if (absolutePlayerPos.x == absoluteBlockPos.x &&
                            absolutePlayerPos.y == (absoluteBlockPos.y - 1) &&
                            absolutePlayerPos.z == absoluteBlockPos.z || distance <= Constants.Game.MIN_BLOCK_DISTANCE)
                        {
                            return;
                        }
                    }
                }

                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
                    Voxel.PlaceBlock(newInfo, ExampleInventory.HeldBlock);
                }
            }
        }