コード例 #1
0
    void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            destroyModeOn = false;
            buildModeOn   = !buildModeOn;
        }

        if (Input.GetKeyDown("r"))
        {
            buildModeOn   = false;
            destroyModeOn = !destroyModeOn;
        }

        if (!destroyModeOn && !buildModeOn)
        {
            editModeOn = true;
        }
        else
        {
            editModeOn = false;
        }

        if (buildModeOn)
        {
            RaycastHit buildPosHit;
            Ray        ray = playerCamera.ScreenPointToRay(Input.mousePosition);

            BlockTypeSelector();
            if (Physics.Raycast(ray, out buildPosHit, Mathf.Infinity, LayerList.cubesLayer))
            {
                parentBlock = buildPosHit.collider.gameObject;
                Vector3 point = buildPosHit.collider.transform.localPosition;
                buildQuaternion = buildPosHit.transform.rotation;
                BlockBase currentBlock = null;

                if (currentTemplateBlock != null)
                {
                    BoxCollider currentBoxCollider = currentTemplateBlock.GetComponent <BoxCollider>();
                    currentBlock = currentTemplateBlock.gameObject.GetComponent <BlockBase>();
                    if (currentBoxCollider != null)
                    {
                        var v = plane.transform.InverseTransformVector(buildPosHit.normal);

                        var normalColliderRotation = Quaternion.FromToRotation(Vector3.up, new Vector3(Math.Abs(v.x), Math.Abs(v.y), Math.Abs(v.z)));
                        var t = normalColliderRotation * (currentBoxCollider.size + currentBoxCollider.center);
                        Debug.Log($"1 = {Vector3.Scale(t,v)} 2 = {v}");

                        point += 0.5f * v + 0.5f * Vector3.Scale(normalColliderRotation *
                                                                 (currentBoxCollider.size + currentBoxCollider.center), v);
                    }
                    else
                    {
                        point += plane.transform.InverseTransformVector(buildPosHit.normal);
                    }
                    //                    point += Vector3.Scale(plane.transform.InverseTransformVector(buildPosHit.normal)*0.5f, currentTemplateBlock.GetComponent<Collider>().bounds.size * 0.5f);
                    //                    point += 0.5f * plane.transform.InverseTransformVector(buildPosHit.normal);
                }
                else
                {
                }

                if (currentBlock != null && currentBlock.CanBePlacedChild(buildPosHit.collider.gameObject, buildPosHit.normal))
                {
                    isBlockAllowed = true;
                }
                else
                {
                    isBlockAllowed = false;
                }
                buildPos = point;
                canBuild = true;
            }
            else if (Physics.Raycast(ray, out buildPosHit, Mathf.Infinity, LayerList.groundLayer))
            {
                var     local = plane.transform.InverseTransformPoint(buildPosHit.point);
                Vector3 point = new Vector3(Mathf.Round(local.x), Mathf.Round(local.y), Mathf.Round(local.z)) + buildPosHit.normal * 1f;
                buildQuaternion = buildPosHit.transform.rotation;

                buildPos       = point;
                canBuild       = true;
                isBlockAllowed = true;
                parentBlock    = buildPosHit.collider.gameObject;
            }
            else
            {
                if (currentTemplateBlock != null)
                {
                    Destroy(currentTemplateBlock);
                }
                canBuild       = false;
                isBlockAllowed = false;
                parentBlock    = null;
            }
        }

        if (!buildModeOn && currentTemplateBlock != null)
        {
            Destroy(currentTemplateBlock.gameObject);
            canBuild = false;
        }

        if (canBuild && currentTemplateBlock == null && blockTemplatePrefab != null)
        {
            currentTemplateBlock = Instantiate(blockTemplatePrefab, plane.transform);
            var rBody = currentTemplateBlock.GetComponent <Rigidbody>();
            if (rBody != null)
            {
//                rBody.isKinematic = true;
//                rBody.detectCollisions = false;
            }

            currentTemplateBlock.layer = LayerList.LayerMaskToLayerNumber(LayerList.ignoreRaycastLayer);
            currentTemplateBlock.transform.localPosition = buildPos;
            currentTemplateBlock.GetComponent <MeshRenderer>().material = templateMaterial;
        }



        if (canBuild && currentTemplateBlock != null)
        {
            currentTemplateBlock.transform.localPosition = buildPos;
            ChangeGameObjectRotation(currentTemplateBlock.GetComponent <BlockBase>());
//            currentTemplateBlock.transform.rotation = grid.transform.rotation;

//            Debug.DrawLine(plane.transform.position + buildPos, plane.transform.position + buildPos + Vector3.up*2, Color.magenta, 0.01f);

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    Debug.DrawLine(buildPos, new Vector3(buildPos.x + j, buildPos.y, buildPos.z + i), Color.cyan, 0.1f);
                }
            }

            if (isBlockAllowed == false)
            {
                currentTemplateBlock.GetComponent <MeshRenderer>().material = isNotAllowedMaterial;
            }
            else
            {
                if (blockTemplatePrefab != null)
                {
                    currentTemplateBlock.GetComponent <MeshRenderer>().material =
                        blockTemplatePrefab.GetComponent <MeshRenderer>().sharedMaterial;
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (isBlockAllowed)
                {
                    PlaceBlock();
                }
            }
        }
        DestroySystem();
        SelectSystem();
    }