Exemplo n.º 1
0
    //TODO clean this up to reduce raycasts
    void Update()
    {
        //if (GetComponent<FirstPersonControllerCustom>().inputLocked)
        //return;

        //Block selector
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))
        {
            if (!blockSelector.gameObject.activeSelf)
            {
                blockSelector.gameObject.SetActive(true);
            }
            hit.point += (-hit.normal * 0.1f); //Smudging in a bit to fix edge case
            Vector3Int pos = EditTerrain.GetBlockPos(hit);
            blockSelector.position = pos.ToVector3();
        }
        else
        {
            if (blockSelector.gameObject.activeSelf)
            {
                blockSelector.gameObject.SetActive(false);
            }
        }

        if (currentBlockPlaceCooldown > 0f)
        {
            currentBlockPlaceCooldown -= Time.deltaTime;
            return;
        }

        //Break block
        if (PlayerInputManager.input.Attack.IsPressed)
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) {
            {
                EditTerrain.BreakBlock(hit);
                currentBlockPlaceCooldown = blockPlaceCooldown;
            }
        }

        //Place block
        if (PlayerInputManager.input.Use.IsPressed)
        {
//            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) {
            {
                if (GetComponent <PlayerInventory>().CurrentActiveItem != null && GetComponent <PlayerInventory>().CurrentActiveItem.placeable)
                {
                    hit.point += hit.normal;
                    Vector3Int pos  = EditTerrain.GetBlockPos(hit);
                    Collider[] cols = Physics.OverlapBox(pos.ToVector3(), Vector3.one * 0.45f, Quaternion.identity, LayerMask.GetMask("Default", "Blocks"), QueryTriggerInteraction.Ignore);
                    if (cols.Length > 0)
                    {
                        //Something in way
                        foreach (Collider col in cols)
                        {
                            if (col.GetComponentInChildren <Renderer>() != null)
                            {
                                StartCoroutine(TempFlashRed(col.GetComponentInChildren <Renderer>()));
                            }
                        }
                    }
                    else
                    {
                        //                    RaycastHit hit2;
                        //                    if (Physics.BoxCast(pos.ToVector3(), Vector3.one * 0.35f, Vector3.up * 0.001f, out hit2))
                        //                    {
                        //                        Debug.LogError(pos.ToVector3() + " - " + hit2.collider.gameObject.name + " - " + hit2.distance);
                        //                        ExtDebug.DrawBoxCastOnHit(pos.ToVector3(), Vector3.one * 0.35f, Quaternion.identity, Vector3.up * 0.001f, 0f, Color.red);
                        //                    }

                        //TODO fix
                        EditTerrain.PlaceBlock(hit, FindObjectOfType <PlayerInventory>().CurrentActiveItem.placeableBlockID);
                        GetComponent <PlayerInventory>().ConsumeCurrentItem();
                        currentBlockPlaceCooldown = blockPlaceCooldown;
                    }
                }
            }
        }
    }