예제 #1
0
        public void HandleUnitMovement()
        {
            if (Input.GetMouseButtonDown(0))
            {
                mousePos = Input.mousePosition;

                //Create a ray
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                //Check if we hit something
                if (Physics.Raycast(ray, out hit, 100, interactableLayer))
                {
                    if (AddedUnit(hit.transform, Input.GetKey(KeyCode.LeftShift)))
                    {
                        // be able to do stuff with units
                    }
                    else if (AddedBuilding(hit.transform))
                    {
                        // be able to do stuff with buildings
                    }
                }
                else
                {
                    isDragging = true;
                    DeselectUnits();
                }
            }

            if (Input.GetMouseButtonUp(0))
            {
                foreach (Transform child in Player.PlayerManager.instance.playerUnits)
                {
                    foreach (Transform unit in child)
                    {
                        if (IsWithinSelectionBounds(unit))
                        {
                            AddedUnit(unit, true);
                        }
                    }
                }
                isDragging = false;
            }

            if (Input.GetMouseButtonDown(1) && HaveSelectedUnits())
            {
                //Create a ray
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                //Check if we hit something
                if (Physics.Raycast(ray, out hit))
                {
                    //If we do, then do something with that data
                    LayerMask layerHit = hit.transform.gameObject.layer;

                    switch (layerHit.value)
                    {
                    case 8:     //Interactables Layer
                        //Determine which object was right clicked here and based on the unit clicking do a certain action
                        //for now, just move to whatever interactable object is there.
                        foreach (Transform unit in selectedUnits)
                        {
                            PlayerUnit playerUnit = unit.gameObject.GetComponent <PlayerUnit>();
                            playerUnit.MoveUnit(hit.point);
                        }
                        break;

                    case 9:     //Enemy Units Layer
                        //Attack / set as target
                        foreach (Transform unit in selectedUnits)
                        {
                            PlayerUnit playerUnit = unit.gameObject.GetComponent <PlayerUnit>();
                            playerUnit.HandleUnitAttack(hit.transform);
                        }
                        break;

                    default:
                        //isDragging = true;
                        foreach (Transform unit in selectedUnits)
                        {
                            PlayerUnit playerUnit = unit.gameObject.GetComponent <PlayerUnit>();
                            playerUnit.MoveUnit(hit.point);
                        }
                        break;
                    }
                }
            }
        }