Пример #1
0
    private void CommonDrag()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (!CurrentDrag)
            {
                {
                    // Drag items in bag
                    Ray ray = UIManager.Instance.UICamera.ScreenPointToRay(Input.mousePosition);
                    Physics.Raycast(ray, out RaycastHit hit, 1000f, GameManager.Instance.LayerMask_ComponentHitBox);
                    if (hit.collider)
                    {
                        BagItem bagItem = hit.collider.gameObject.GetComponentInParent <BagItem>();
                        if (bagItem)
                        {
                            CurrentDrag          = bagItem.gameObject.GetComponent <Draggable>();
                            CurrentDrag.IsOnDrag = true;
                        }
                        else
                        {
                            CancelCurrentDrag();
                        }
                    }
                    else
                    {
                        CancelCurrentDrag();
                    }
                }

                //Drag components in scene
                if (!CurrentDrag)
                {
                    Ray ray = GameManager.Instance.MainCamera.ScreenPointToRay(Input.mousePosition);
                    Physics.Raycast(ray, out RaycastHit hit, 1000f, GameManager.Instance.LayerMask_ComponentHitBox);
                    if (hit.collider)
                    {
                        HitBox hitBox = hit.collider.gameObject.GetComponent <HitBox>();
                        if (hitBox)
                        {
                            CurrentDrag_MechaComponentBase = hitBox.ParentHitBoxRoot.MechaComponentBase;
                            CurrentDrag          = CurrentDrag_MechaComponentBase.gameObject.GetComponent <Draggable>();
                            CurrentDrag.IsOnDrag = true;
                        }
                        else
                        {
                            CancelCurrentDrag();
                        }
                    }
                    else
                    {
                        CancelCurrentDrag();
                    }
                }

                // Drag items dropped
                if (!CurrentDrag)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        Ray ray = GameManager.Instance.MainCamera.ScreenPointToRay(Input.mousePosition);
                        Physics.Raycast(ray, out RaycastHit hit, 1000f, GameManager.Instance.LayerMask_ItemDropped);
                        if (hit.collider)
                        {
                            MechaComponentDropSprite mcds = hit.collider.GetComponentInParent <MechaComponentDropSprite>();
                            if (mcds)
                            {
                                MechaComponentBase mcb = MechaComponentBase.BaseInitialize(mcds.MechaComponentInfo.Clone(), BattleManager.Instance.PlayerMecha);
                                GridPos            gp  = GridPos.GetGridPosByMousePos(BattleManager.Instance.PlayerMecha.transform, Vector3.up, GameManager.GridSize);
                                mcb.SetGridPosition(gp);
                                BattleManager.Instance.PlayerMecha.AddMechaComponent(mcb);
                                CurrentDrag          = mcb.GetComponent <Draggable>();
                                CurrentDrag.IsOnDrag = true;
                                mcds.PoolRecycle();
                            }
                        }
                    }
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            CancelCurrentDrag();
        }
    }