コード例 #1
0
    public override void Update()
    {
        base.Update();
        SetMousePosition();

        if (screenState == ScreenState.setup)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                CloseScreen();
            }

            //Deselect
            if (Input.GetMouseButtonDown(1))
            {
                DeselectDesignData();
                if (PickedUpObject != null)
                {
                    PickedUpObject.transform.position = PickedUpOrigin;
                    PickedUpObject = null;
                }
            }

            //Delete placed objects
            if (Input.GetMouseButton(1))
            {
                Ray        ray;
                RaycastHit hit;

                ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    if (hit.collider.transform.root.tag == "Ship" && PlayerPlacedObjects.Contains(hit.collider.transform.root.gameObject))
                    {
                        RemovePlacedShip(hit.collider.transform.root.GetComponent <Ship>());
                    }
                }
            }
            //Pivot
            else if (DummyUnit != null && !ScreenElementsContains(mousePosition))
            {
                Ray        ray;
                RaycastHit hit;

                ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    if (!PivotSet)
                    {
                        DummyUnit.transform.position = new Vector3(hit.point.x, 1f, hit.point.z);
                    }
                    //Check if it hits deployment area
                    if (Input.GetMouseButtonDown(0) && CanAffordDesign(selectedDesign) && DeploymentAreaContains(hit.point))
                    {
                        PivotPoint = DummyUnit.transform.position;
                        PivotSet   = true;
                    }
                    else if (Input.GetMouseButton(0) && PivotSet)
                    {
                        Vector3 Direction = (new Vector3(hit.point.x, 1f, hit.point.z) - PivotPoint);
                        if (Direction.magnitude > DummyUnitRadius)
                        {
                            Quaternion Rotation = Quaternion.LookRotation(Direction);
                            DummyUnit.transform.rotation = Rotation;
                        }
                    }
                    else if (Input.GetMouseButtonUp(0) && PivotSet)
                    {
                        PlaceShip();
                        PivotSet = false;
                    }
                }
            }
            else if (PickedUpObject != null && !ScreenElementsContains(mousePosition))
            {
                Ray        ray;
                RaycastHit hit;
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    if (!PivotSet)
                    {
                        PickedUpObject.transform.position = new Vector3(hit.point.x, 1f, hit.point.z);
                    }
                    if (Input.GetMouseButtonDown(0) && DeploymentAreaContains(PickedUpObject.transform.position - new Vector3(0, 1, 0)))
                    {
                        PivotPoint = PickedUpObject.transform.position;
                        PivotSet   = true;
                    }
                    else if (Input.GetMouseButton(0) && PivotSet)
                    {
                        Vector3 Direction = (new Vector3(hit.point.x, 1f, hit.point.z) - PivotPoint);
                        if (Direction.magnitude > DummyUnitRadius)
                        {
                            Quaternion Rotation = Quaternion.LookRotation(Direction);
                            PickedUpObject.transform.rotation = Rotation;
                        }
                    }
                    else if (Input.GetMouseButtonUp(0) && PivotSet)
                    {
                        PickedUpObject = null;
                        PivotSet       = false;
                    }
                }
            }
            else if (miniMap.Contains(mousePosition))
            {
                if (Input.GetMouseButton(0))
                {
                    miniMap.GetWorldPosition(mousePosition);
                }
            }
            else if (DummyUnit == null)
            {
                if (Input.GetMouseButtonUp(0))
                {
                    Ray        ray;
                    RaycastHit hit;

                    ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                    //If we hit...
                    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                    {
                        if (hit.collider.transform.root.tag == "Ship" && PlayerPlacedObjects.Contains(hit.collider.transform.root.gameObject))
                        {
                            PickedUpObject = hit.collider.transform.root.gameObject;
                            PickedUpOrigin = hit.collider.transform.root.transform.position;
                        }
                    }
                }
            }
        }
        else if (screenState == ScreenState.play)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                screenState = ScreenState.paused;
                SetPauseShipManagers(true);
            }
            //Update the ships managers
            UpdateShipManagers();
            if (combatTimer.CountDown())
            {
                EndCombat();
                return;
            }
            //Check if Player defeated
            if (PlayerShipManager.NoUnitsLeft())
            {
                SummaryScrollList.SetResult("Defeat");
                EndCombat();
                return;
            }
            if (EnemyShipManager.NoUnitsLeft())
            {
                SummaryScrollList.SetResult("Victory");
                EndCombat();
                return;
            }

            Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                SetPauseShipManagers(true);
            }

            if (!ScreenElementsContains(mousePosition))
            {
                Ray        ray;
                RaycastHit hit;
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    //Highlight hovered units
                    if (hit.collider.transform.root.tag == "Ship")
                    {
                        hit.collider.transform.root.GetComponent <SpaceUnit>().Hovered();
                    }
                    else if (hit.collider.transform.root.tag == "Fighter")
                    {
                        hit.collider.transform.root.GetComponent <Fighter>().GetParentWing().Hovered();
                    }
                    else if (hit.collider.transform.root.tag == "FighterWing")
                    {
                        hit.collider.transform.root.GetComponent <FighterWing>().Hovered();
                    }
                    //Process abilities
                    if (PlayerShipManager.AbilityActivite())
                    {
                        if (hit.collider.transform.root.tag == "Ship")
                        {
                            Ship hoveredShip = hit.collider.transform.root.GetComponent <Ship>();
                            if (hoveredShip.ValidTransporterTarget(PlayerShipManager))
                            {
                                if (PlayerShipManager.TransporterTargetingTroop)
                                {
                                    //Cursor.SetCursor(ResourceManager.instance.GetIconTexture("Icon_TransportTroop"), Vector2.zero, CursorMode.Auto);
                                    if (Input.GetMouseButtonDown(0))
                                    {
                                        PlayerShipManager.CastActiveAbility(hoveredShip);
                                    }
                                }
                                else if (PlayerShipManager.TransporterTargetingCrew)
                                {
                                    //Cursor.SetCursor(ResourceManager.instance.GetIconTexture("Icon_TransportCrew"), Vector2.zero, CursorMode.Auto);
                                    if (Input.GetMouseButtonDown(0))
                                    {
                                        PlayerShipManager.CastActiveAbility(hoveredShip);
                                    }
                                }
                            }
                        }
                        if (Input.GetMouseButtonDown(1))
                        {
                            PlayerShipManager.ResetAbilities();
                        }
                    }
                    else //Normal movement and commands
                    {
                        if (Input.GetMouseButtonDown(0))
                        {
                            //Cancel movement order
                            if (PivotSet)
                            {
                                PivotSet = false;
                                UnityEngine.Object.Destroy(PivotObject);
                                foreach (GameObject marker in DirectionMarkers)
                                {
                                    UnityEngine.Object.Destroy(marker);
                                }
                                DirectionMarkers.Clear();
                            }
                            //Check if clicked a ship
                            if (hit.collider.transform.root.tag == "Ship")
                            {
                                Ship ship = hit.collider.transform.root.GetComponent <Ship>();
                                if (ship.GetShipManager() == PlayerShipManager)
                                {
                                    if (!Input.GetKey(KeyCode.LeftShift))
                                    {
                                        PlayerShipManager.DeselectShips();
                                    }
                                    PlayerShipManager.AddToSelection(ship);
                                }
                                else
                                {
                                    PlayerShipManager.SelectUnownedShip(ship);
                                }
                            }
                            else if (hit.collider.transform.root.tag == "FighterWing")
                            {
                                FighterWing fighterWing = hit.collider.transform.root.GetComponent <FighterWing>();
                                if (fighterWing.GetShipManager() == PlayerShipManager)
                                {
                                    if (!Input.GetKey(KeyCode.LeftShift))
                                    {
                                        PlayerShipManager.DeselectShips();
                                    }
                                    PlayerShipManager.AddToSelection(fighterWing);
                                }
                                else
                                {
                                    PlayerShipManager.SelectUnownedFighter(fighterWing);
                                }
                            }
                            else if (hit.collider.transform.root.tag == "Fighter")
                            {
                                Fighter fighter = hit.collider.transform.root.GetComponent <Fighter>();
                                if (fighter.GetShipManager() == PlayerShipManager)
                                {
                                    if (!Input.GetKey(KeyCode.LeftShift))
                                    {
                                        PlayerShipManager.DeselectShips();
                                    }
                                    PlayerShipManager.AddToSelection(fighter);
                                }
                                else
                                {
                                    PlayerShipManager.SelectUnownedFighter(fighter);
                                }
                            }
                            else if (hit.collider.gameObject.tag == "PlayArea")
                            {
                                if (!Input.GetKey(KeyCode.LeftShift))
                                {
                                    PlayerShipManager.DeselectShips();
                                }
                                shipDragSelectionBox.SetStart(Input.mousePosition);
                                shipDragSelectionBox.SetActive(true);
                            }
                        }
                        else if (Input.GetMouseButtonDown(1) && PlayerShipManager.HasSomethingSelected())
                        {
                            if (hit.collider.gameObject.tag == "PlayArea")
                            {
                                PivotSet    = true;
                                PivotObject = new GameObject();
                                PivotObject.transform.position = hit.point;
                                Vector3 shipGroupCenter;
                                Vector3 sum = Vector3.zero;
                                foreach (Ship ship in PlayerShipManager.GetSelectedShips())
                                {
                                    sum += ship.transform.position;
                                }
                                foreach (FighterWing fighterWing in PlayerShipManager.GetSelectedFighters())
                                {
                                    sum += fighterWing.transform.position;
                                }
                                shipGroupCenter = sum / PlayerShipManager.GetSelectedMobileUnitsCount();
                                Vector3    Direction = PivotObject.transform.position - shipGroupCenter;
                                Quaternion rotation  = Quaternion.LookRotation(Direction, Vector3.up);
                                PivotObject.transform.rotation = rotation;
                                foreach (Ship ship in PlayerShipManager.GetSelectedShips())
                                {
                                    GameObject marker = ResourceManager.CreateDirectionMarker(ship.transform.position + Direction);
                                    marker.transform.rotation = rotation;
                                    DirectionMarkers.Add(marker);
                                    marker.transform.parent = PivotObject.transform;
                                }
                                foreach (FighterWing fighterWing in PlayerShipManager.GetSelectedFighters())
                                {
                                    GameObject marker = ResourceManager.CreateDirectionMarker(fighterWing.transform.position + Direction);
                                    marker.transform.rotation = rotation;
                                    DirectionMarkers.Add(marker);
                                    marker.transform.parent = PivotObject.transform;
                                }
                            }
                            else if (hit.collider.transform.root.tag == "Ship")
                            {
                                Ship ship = hit.collider.transform.root.GetComponent <Ship>();
                                PlayerShipManager.SetTargetForSelectedShips(ship);
                            }
                            else if (hit.collider.transform.root.tag == "FighterWing")
                            {
                                FighterWing fighterWing = hit.collider.transform.root.GetComponent <FighterWing>();
                                PlayerShipManager.SetTargetForSelectedShips(fighterWing.GetFirstFighter());
                            }
                            else if (hit.collider.transform.root.tag == "Fighter")
                            {
                                Fighter fighter = hit.collider.transform.root.GetComponent <Fighter>();
                                PlayerShipManager.SetTargetForSelectedShips(fighter);
                            }
                        }
                        else if (Input.GetMouseButton(1) && PivotSet)
                        {
                            if (hit.collider.gameObject.tag == "PlayArea")
                            {
                                Vector3 Direction = hit.point - PivotObject.transform.position;
                                if (Direction.sqrMagnitude > 0)
                                {
                                    Quaternion rotation = Quaternion.LookRotation(Direction, Vector3.up);
                                    PivotObject.transform.rotation = rotation;
                                    foreach (GameObject marker in DirectionMarkers)
                                    {
                                        marker.transform.rotation = rotation;
                                    }
                                }
                            }
                        }
                        else if (PivotSet)
                        {
                            if (hit.collider.gameObject.tag == "PlayArea")
                            {
                                List <Vector3> points = new List <Vector3>();
                                foreach (GameObject marker in DirectionMarkers)
                                {
                                    points.Add(marker.transform.position);
                                }
                                PlayerShipManager.SetGoalPositionsForShips(points, PivotObject.transform.rotation);

                                PivotSet = false;
                                UnityEngine.Object.Destroy(PivotObject);
                                foreach (GameObject marker in DirectionMarkers)
                                {
                                    UnityEngine.Object.Destroy(marker);
                                }
                                DirectionMarkers.Clear();
                            }
                        }
                    }
                }

                //Drag left click
                if (Input.GetMouseButton(0))
                {
                    shipDragSelectionBox.SetEnd(Input.mousePosition);
                }
                else if (shipDragSelectionBox.isActive())
                {
                    //Deselect ships from before
                    if (!Input.GetKey(KeyCode.LeftShift))
                    {
                        PlayerShipManager.DeselectShips();
                    }
                    shipDragSelectionBox.SetActive(false);
                    shipDragSelectionBox.SelectShips(PlayerShipManager);
                }
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                CloseScreen();
            }
        }
        if (miniMap.Contains(mousePosition))
        {
            if (Input.GetMouseButton(0))
            {
                miniMap.GetWorldPosition(mousePosition);
            }
        }
    }