コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        // Camera panning
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A) || Input.GetKeyDown(KeyCode.A))
        {
            MainCamera.transform.Translate(Vector3.left * PanSpeed);
        }
        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKey(KeyCode.D) || Input.GetKeyDown(KeyCode.D))
        {
            MainCamera.transform.Translate(Vector3.right * PanSpeed);
        }
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) || Input.GetKeyDown(KeyCode.W))
        {
            MainCamera.transform.Translate(Vector3.up * PanSpeed);
        }
        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || Input.GetKeyDown(KeyCode.S))
        {
            MainCamera.transform.Translate(Vector3.down * PanSpeed);
        }

        // limit camera
        Vector3 v3 = MainCamera.transform.position;

        v3.x = Mathf.Clamp(v3.x, CameraMinX, CameraMaxX);
        v3.y = Mathf.Clamp(v3.y, CameraMinY, CameraMaxY);
        MainCamera.transform.position = v3;

        /**************************
        * Right click mouse actions
        **************************/

        // Mouse pointer section
        if (Input.GetMouseButtonDown(0) && SelectedUnit == null && !CastleMenuUI.activeSelf)
        {
            // Position of mouse pointer
            Vector3 pos = MainCamera.ScreenToWorldPoint(Input.mousePosition);
            pos.x = Mathf.Round(pos.x);
            pos.y = Mathf.Round(pos.y);
            pos.z = 0;

            RaycastHit2D[] hits = Physics2D.RaycastAll(pos, Vector2.zero);
            foreach (RaycastHit2D h in hits)
            {
                Stack curStack = h.transform.GetComponent <Stack>();
                if (curStack != null && curStack.StackData.Owner == CurrentGameData.CurrentPlayer)
                {
                    SelectedUnit = h.collider.gameObject;
                    BottomUI.GetComponent <BottomUI>().SetSelectedStack(curStack);
                }
            }
        }

        // Handle stack interactions
        if (Input.GetMouseButtonDown(0) && SelectedUnit != null && !CastleMenuUI.activeSelf)
        {
            // Position of mouse pointer
            Vector3 mousePosClick = MainCamera.ScreenToWorldPoint(Input.mousePosition);
            mousePosClick.x = Mathf.Round(mousePosClick.x);
            mousePosClick.y = Mathf.Round(mousePosClick.y);
            mousePosClick.z = 0;


            Stack curStack = SelectedUnit.GetComponent <Stack>();
            // check if selected object is a stack
            if (curStack != null)
            {
                // Handle stack merge
                if (mousePosClick == curStack.transform.position)
                {
                    // if there is more stacks on the same field and the stack is selected, merge them
                    RaycastHit2D[] hits = Physics2D.RaycastAll(mousePosClick, Vector2.zero);
                    var            possibleMergeStack = new List <Stack>();
                    foreach (RaycastHit2D h in hits)
                    {
                        Stack hitStack = h.transform.GetComponent <Stack>();
                        if (curStack != hitStack)
                        {
                            possibleMergeStack.Add(hitStack);
                        }
                    }
                    // merge the stacks
                    foreach (var stack in possibleMergeStack)
                    {
                        curStack.StackData.Units.InsertRange(stack.StackData.Units.ToEnumerable().ToList());
                        KillStack(stack);
                    }
                }

                if (CheckMovementPossible(curStack, CurrentGameData.TerrainTiles[(int)mousePosClick.x, (int)mousePosClick.y]))
                {
                    // assume the stack will move, unless something is hit
                    bool setMove = true;
                    // hit all objects, incase of castle beneath stack
                    // TODO handle multiple stacked stacks
                    RaycastHit2D[] hits = Physics2D.RaycastAll(mousePosClick, Vector2.zero);
                    foreach (RaycastHit2D h in hits)
                    {
                        // check hit componenent
                        Castle cas      = h.collider.gameObject.GetComponent <Castle>();
                        Stack  hitStack = h.collider.gameObject.GetComponent <Stack>();
                        // check if the castle is hostile then do battle
                        if (cas != null && cas.Owner != curStack.StackData.Owner && (Vector3.Distance(cas.transform.position, curStack.transform.position) < 3f))
                        {
                            // Handle battle against castles
                            bool win = true;
                            // Fight the entire garrison
                            //TODO handle fights in groups, adding all the stack together in a great stack, such that bonuses stack
                            foreach (Stack st in cas.Garrison)
                            {
                                if (curStack.Battle(st))
                                {
                                    Debug.Log("Kill hit stack");
                                    KillStack(st);
                                }
                                else
                                {
                                    win = false;
                                    break;
                                }
                            }
                            if (!win)
                            {
                                Debug.Log("Kill cur stack");
                                KillStack(curStack);
                                SelectedUnit = null;
                            }
                            else
                            {
                                cas.ChangeOwner(curStack.StackData.Owner);
                            }
                            break;
                        }
                        else if (hitStack != null && curStack != null && curStack.StackData.Owner != hitStack.StackData.Owner)
                        {
                            // Check distance, if next to target, do battle
                            if (Vector3.Distance(hitStack.transform.position, curStack.transform.position) < 2f)
                            {
                                if (curStack.Battle(hitStack))
                                {
                                    KillStack(hitStack);
                                }
                                else
                                {
                                    KillStack(curStack);
                                    SelectedUnit = null;
                                    setMove      = false;
                                }
                            }
                        }
                    }
                    if (setMove)
                    {
                        // Set blocked paths depending on the castles and stacks
                        bool[,] obstructed = generateObstructedFields(curStack, CurrentGameData.AllStacks, AllCastles);

                        // Set stack movement path
                        curStack.Path = AStar.ShortestPath(CurrentGameData.TerrainTiles, obstructed, (int)SelectedUnit.transform.position.x,
                                                           (int)SelectedUnit.transform.position.y, (int)mousePosClick.x, (int)mousePosClick.y);
                    }
                    // update the UI, to deal with dead
                    if (SelectedUnit != null)
                    {
                        BottomUI.GetComponent <BottomUI>().SetSelectedStack(SelectedUnit.GetComponent <Stack>());
                    }
                    else if (SelectedUnit == null)
                    {
                        BottomUI.GetComponent <BottomUI>().ClearSelectedStack();
                    }
                }
            }
        }


        /**************************
        * Left click mouse actions
        **************************/
        if (Input.GetMouseButtonDown(1))
        {
            // TODO fix for better handling of right click
            Vector3 pos = MainCamera.ScreenToWorldPoint(Input.mousePosition);
            pos.x = Mathf.Round(pos.x);
            pos.y = Mathf.Round(pos.y);
            pos.z = 0;
            RaycastHit2D[] hits = Physics2D.RaycastAll(pos, Vector2.zero);
            foreach (RaycastHit2D hit in hits)
            {
                Castle hitCas = hit.transform.gameObject.GetComponent <Castle>();
                if (hitCas != null && hitCas.Owner != null)
                {
                    CastleMenuUI.SetActive(true);
                    CastleMenuUI.GetComponent <CastleMenu>().SetCastle(hitCas);
                }
            }
        }

        /************************************
         * Hotkey actions
         ***********************************/
        // Next turn. Set all stacks movement. Should be called from else where
        if (Input.GetKeyDown(KeyCode.Return))
        {
            NextTurn();
        }

        // Deselect all
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SelectedUnit = null;
            BottomUI.GetComponent <BottomUI>().ClearSelectedStack();
        }
    }