Example #1
0
    /* Coroutine that process the player's Turn. Check for end turn condition (currently set to after using any skill/attack).
     * While end turn condition is not met we get player input to determine what they can do. Options are: End the turn, move their character, or use a skill
     */
    public override IEnumerator PerformTurn(List <AbstractCreature> validTargetList)
    {
        bool   turnEnded        = false;
        bool   hasMoved         = false;
        bool   attackMade       = false;
        string skillDescription = "";


        //Potential values needed for multiattacks
        // Each AbstractCreature has UI elements associated with it, stored in a dictionary
        Dictionary <AbstractCreature, GameObject> targetsBeingAttacked = new Dictionary <AbstractCreature, GameObject>();

        if (lastTargets != null && lastTargets.Count != 0)
        {
            lastTargets.RemoveWhere((t) =>
            {
                return(t == null || t.IsDead());
            });

            foreach (var t in lastTargets)
            {
                targetsBeingAttacked.Add(t, playerUIController.drawCombatArrows(t));
            }
            string combatText = getTargetText(new HashSet <AbstractCreature>(targetsBeingAttacked.Keys)) + skillHandler.getSkillsText();
            this.ctc.updateText(combatText);
        }

        while (!turnEnded)
        {
            yield return(new WaitUntil(() => Input.anyKey));

            if (Input.GetKeyDown(KeyCode.E))
            {
                turnEnded = true;
            }

            if (Input.GetKeyDown(KeyCode.M) && !hasMoved)
            {
                ctc.updateText("Move action intiated. Click where you want to go!");
                yield return(new WaitUntil(() => Input.GetMouseButtonDown(0)));

                Camera  cam   = Camera.main;
                Vector2 start = transform.position;
                Vector2 end   = cam.ScreenToWorldPoint(Input.mousePosition);

                float dist = Vector2.Distance(start, end);
                if (dist > distance)
                {
                    playerUIController.PickupEvent("Unable to move there, distance too far");
                }
                else
                {
                    int     layerMask = LayerMask.GetMask("Default");
                    Vector2 dir       = (end - start);
                    dir.Normalize();
                    RaycastHit2D rayHit = Physics2D.Raycast(start, dir, dist, layerMask);
                    if (rayHit.collider == null)
                    {
                        hasMoved           = true;
                        transform.position = end;
                        this.ctc.updateText("Move completed");
                    }
                    else
                    {
                        playerUIController.PickupEvent("Unable to move there, something is blocking you");
                    }
                }
            }


            if (Input.GetMouseButtonDown(0))
            {
                AbstractCreature potentialTarget = ClickOnTarget();
                if (potentialTarget == null)
                {
                    foreach (var key in targetsBeingAttacked.Keys)
                    {
                        // Destroy all the UI objects pointing to the targets
                        Destroy(targetsBeingAttacked[key]);
                    }
                    targetsBeingAttacked.Clear();
                }
                else if (!validTargetList.Contains(potentialTarget))
                {
                    //Do nothing
                }
                else if (targetsBeingAttacked.ContainsKey(potentialTarget))
                {
                    Destroy(targetsBeingAttacked[potentialTarget]);
                    targetsBeingAttacked.Remove(potentialTarget);
                }
                else
                {
                    targetsBeingAttacked.Add(potentialTarget, playerUIController.drawCombatArrows(potentialTarget));
                }

                lastTargets = new HashSet <AbstractCreature>(targetsBeingAttacked.Keys);
                string combatText = getTargetText(new HashSet <AbstractCreature>(targetsBeingAttacked.Keys)) + skillHandler.getSkillsText();
                this.ctc.updateText(combatText);
            }

            if (Input.GetKeyDown(KeyCode.Alpha1) && !attackMade)
            {
                attackMade       = skillHandler.performSkillAtIndex(0, new List <AbstractCreature>(targetsBeingAttacked.Keys), data, this);
                skillDescription = skillHandler.getSkillDescriptionAtIndex(0);
            }

            if (Input.GetKeyDown(KeyCode.Alpha2) && !attackMade)
            {
                attackMade       = skillHandler.performSkillAtIndex(1, new List <AbstractCreature>(targetsBeingAttacked.Keys), data, this);
                skillDescription = skillHandler.getSkillDescriptionAtIndex(1);
            }

            if (Input.GetKeyDown(KeyCode.Alpha3) && !attackMade)
            {
                attackMade       = skillHandler.performSkillAtIndex(2, new List <AbstractCreature>(targetsBeingAttacked.Keys), data, this);
                skillDescription = skillHandler.getSkillDescriptionAtIndex(2);
            }

            if (Input.GetKeyDown(KeyCode.Alpha4) && !attackMade)
            {
                attackMade       = skillHandler.performSkillAtIndex(3, new List <AbstractCreature>(targetsBeingAttacked.Keys), data, this);
                skillDescription = skillHandler.getSkillDescriptionAtIndex(3);
            }

            if (attackMade)
            {
                skillDescription = "";
            }

            playerUIController.skillDescriptionText.text = skillDescription;

            if (attackMade)
            {
                turnEnded = true;
            }
        }

        foreach (var key in targetsBeingAttacked.Keys)
        {
            // Destroy any lingering ui elements
            Destroy(targetsBeingAttacked[key]);
        }
        yield return(null);
    }