Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (paused)
        {
            return;
        }
        if (!turnController.isComputerTurn()) //human turn
        {
            if (Input.GetMouseButtonDown(0))  // On click, clear old selection and start a new selection.
            {
                isSelecting   = true;
                startMousePos = Input.mousePosition;
                foreach (GameObject gameObject in pieces.groups)
                {
                    Group g = gameObject.GetComponent <Group>();
                    if (g == null)
                    {
                        Debug.LogError("Something went wrong with selecting a group");
                    }
                    else if (g.isSelected)
                    {
                        //deselect group and its stones
                        g.isSelected = false;
                        foreach (GameObject go in g.stonesList)
                        {
                            Stone s = go.GetComponent <Stone>();
                            s.isSelected = false;
                        }
                    }
                }
            }
            if (isSelecting) //While mouse button is held down, select the group and stones.
            //Check each group is within the selection box and update the list of groupsInBounds accordingly.
            {
                foreach (GameObject g in pieces.groups)
                {
                    bool hit    = intersectsSelection(g);
                    bool inList = groupsInBounds.Contains(g);
                    if (hit && !inList) //The group is in selection box and not in list. Add it to the list.
                    {
                        groupsInBounds.Add(g);
                    }
                    else if (!hit && inList)  //The group is out of selection box but in list. Remove it from the list.
                    {
                        g.GetComponent <Group>().isSelected = false;
                        groupsInBounds.Remove(g);
                    }
                }
                //Highlight the group in groupsInBounds that was selected first, if it exists.
                bool found = false;
                for (int i = 0; i < groupsInBounds.Count && !found; i++)
                {
                    Group g = groupsInBounds[i].GetComponent <Group>();
                    if (groupsInBounds[i] != null && g.stonesList.Count > 0)
                    {
                        //find first group with active stones
                        selectedGroup            = g;
                        selectedGroup.isSelected = true;
                        found = true;
                    }
                }
            }
            if (Input.GetMouseButtonUp(0))  //On release, add the stones in the selected group to selectedList.
            {
                if (selectedGroup != null)
                {
                    selectedList = new List <GameObject>();
                    foreach (GameObject gameObject in selectedGroup.stonesList)
                    {
                        if (isWithinSelection(gameObject))
                        {
                            Stone s = gameObject.GetComponent <Stone>();
                            if (s != null)
                            {
                                s.isSelected = true;
                                selectedList.Add(gameObject);
                            }
                        }
                    }
                }
                isSelecting = false; //Selection is complete.
            }
        }
        else   //computer turn


        {
            if (!isSelecting) //

            {
                computerPlayer.ChooseStones();
                selectedGroup            = computerPlayer.selectedGroup;
                selectedGroup.isSelected = true;
                selectedList             = computerPlayer.selectedList;
                computerWaitTime         = Time.time;
                isSelecting = true;
                foreach (GameObject gameObject in selectedList)
                {
                    Stone s = gameObject.GetComponent <Stone>();
                    if (s != null)
                    {
                        s.isSelected = true;
                    }
                }
            }
            else if (isSelecting && (computerWaitTime + computerPlayer.waitTime < Time.time))
            {
                removeSelected();
            }
        }
    }