Exemplo n.º 1
0
    private IEnumerator TargetingCoroutine()
    {
        //Wait 1 frame
        yield return(null);

        //Começar no monstro mais a direita -> o mais perto da party
//        var currentIndex = TargetGroups.FindLastIndex(group => group.Any(battler => battler is MonsterBattler));
        var currentIndex = 0;

        BattleCanvas.CleanTargetArrows();

        foreach (var battler in TargetGroups[currentIndex])
        {
            BattleCanvas.BindTargetArrow(battler.RectTransform);
        }

        void DisplayTargets()
        {
            BattleCanvas.CleanTargetArrows();
            var names = new List <string>();

            foreach (var battler in TargetGroups[currentIndex])
            {
                BattleCanvas.BindTargetArrow(battler.RectTransform);
                names.Add($"Lv. {battler.Level} {battler.BattlerName}");
            }

            var targetName = string.Join(", ", names);

            BattleCanvas.battleInfoPanel.ShowInfo(TargetGroups[currentIndex].Length > 1 ? "Multiple Targets" : TargetGroups[currentIndex][0].BattlerName);
            // if (TargetGroups[currentIndex].Length == 1)
            // {
            //     var battler = TargetGroups[currentIndex][0];
            //     BattleCanvas.BindActionArrow(battler.RectTransform);
            //     BattleCanvas.battleInfoPanel.ShowInfo($"Lv. {battler.Level} {battler.BattlerName}");
            // }
            // else
            // {
            //     foreach (var battler in TargetGroups[currentIndex]) BattleCanvas.BindActionArrow(battler.RectTransform);
            //     BattleCanvas.battleInfoPanel.ShowInfo("Multiple Targets");
            // }
        };

        DisplayTargets();

        while (!TargetChosen)
        {
            //Handle Input
            if (Input.GetButtonDown("Horizontal"))
            {
                var hInput = (int)Input.GetAxisRaw("Horizontal");

                if (hInput != 0)
                {
                    currentIndex += hInput;
                    if (currentIndex == -1)
                    {
                        currentIndex = TargetGroups.Count - 1;
                    }
                    if (currentIndex == TargetGroups.Count)
                    {
                        currentIndex = 0;
                    }

                    DisplayTargets();
                }
            }

            if (Input.GetButtonDown("Submit"))
            {
                ChooseTargets(TargetGroups[currentIndex]);
            }

            //Handle click
            if (Input.GetMouseButtonDown(0))
            {
                if (TargetGroups.Count >= 2)
                {
                    var mousePosition = Input.mousePosition;

                    int   index         = 0;
                    float indexDistance = float.PositiveInfinity;

                    Func <Battler[], float> GetDistance = (group) =>
                    {
                        var groupPositions =
                            group.Select(target => target.RectTransform.position);

                        var totalPosition = new Vector3(0, 0, 0);

                        foreach (var position in groupPositions)
                        {
                            totalPosition += position;
                        }

                        totalPosition = new Vector3(totalPosition.x, totalPosition.y, 0);

                        var averagePosition = totalPosition / group.Length;
                        var deltaPosition   = averagePosition - mousePosition;
                        var distance        = deltaPosition.magnitude;

                        return(distance);
                    };

                    for (int i = 0; i < TargetGroups.Count; i++)
                    {
                        var distance = GetDistance(TargetGroups[i]);

                        if (distance < indexDistance)
                        {
                            index         = i;
                            indexDistance = distance;
                        }
                    }

                    if (index != currentIndex)
                    {
                        currentIndex = index;
                        DisplayTargets();
                    }
                    else
                    {
                        ChooseTargets(TargetGroups[index]);
                    }
                }
                else
                {
                    ChooseTargets(TargetGroups.First());
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                CancelTarget();
            }

            yield return(null);
        }

        //Cleanup
        BattleCanvas.battleInfoPanel.HideInfo();
    }