예제 #1
0
    void BlockControl()
    {
        Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);

        if (Input.GetMouseButtonDown(0))
        {
            if (hit.collider != null)
            {
                clickedGameObject = hit.collider.gameObject;
                clickedGOPosition = clickedGameObject.transform.position;


                // highlight
                if (highLightedGO != null)
                {
                    BlockHighLight(highLightedGO, false);
                    highLightedGO = null;
                }

                highLightedGO = clickedGameObject;
                BlockHighLight(highLightedGO, true);
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            if (hit.collider != null)
            {
                if (clickedGameObject != null && clickedGameObject == hit.collider.gameObject)
                {
                    // 현재 위치에 블럭이 없으면 그 자리에 위치, 있으면 원래 위치로 되돌림
                    bool       exist = false;
                    Coordinate pos   = new Coordinate((int)clickedGameObject.transform.position.x, (int)clickedGameObject.transform.position.y);
                    foreach (Block block in Block.BlockList)
                    {
                        if (block.Pos == pos)
                        {
                            exist = true;
                            break;
                        }
                    }

                    if (exist)
                    {
                        clickedGameObject.transform.position = clickedGOPosition;
                    }
                    else
                    {
                        clickedGameObject.GetComponent <Block>().Pos = pos;
                    }

                    clickedGameObject = null;
                }
            }
        }
        else if (Input.GetMouseButton(0))
        {
            if (clickedGameObject != null)
            {
                Vector3 vec = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                float   temp;
                temp = vec.x - (int)vec.x;
                if (temp < 0.5f)
                {
                    vec.x = (int)vec.x;
                }
                else
                {
                    vec.x = (int)(vec.x + 1);
                }

                temp = vec.y - (int)vec.y;
                if (temp < 0.5f)
                {
                    vec.y = (int)vec.y;
                }
                else
                {
                    vec.y = (int)(vec.y + 1);
                }

                vec.z = 0;
                clickedGameObject.transform.position = vec;
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            if (hit.collider != null)
            {
                if (highLightedGO != null)
                {
                    AddRelationBlock(highLightedGO, hit.collider.gameObject);
                }
            }
        }
        else if (Input.GetMouseButtonDown(2))
        {
            if (isSelectMode == false)
            {
                return;
            }

            if (hit.collider != null)
            {
                if (highLightedGO != null)
                {
                    BlockHighLight(highLightedGO, false);
                    highLightedGO = null;
                }

                createManager.SelectToggle(hit.collider.gameObject);
            }
        }
    }