Example #1
0
        // --------------------------------------------------------------------------------------------
        public void UseSkill(EFacing faceTowards, IntVector2 targetCoord, Action onComplete)
        {
            if (!CanUseSkill)
            {
                Debug.LogError($"Unit {id} cannot use its skill");
                return;
            }

            HasUsedSkill = true;

            if (faceTowards != Facing)
            {
                SetFacing(faceTowards, false);
            }

            int numTimesSkillUsedOnTarget          = 0;
            int numTimesSkillUsedOnTargetCompleted = 0;

            void SkillUsedOnTargetCallback()
            {
                numTimesSkillUsedOnTargetCompleted++;
                if (numTimesSkillUsedOnTargetCompleted >= numTimesSkillUsedOnTarget)
                {
                    onComplete?.Invoke();
                }
            }

            if (Skill.Target == SkillData.ETarget.None)
            {
                numTimesSkillUsedOnTarget++;
                UseSkillNoTarget(SkillUsedOnTargetCallback);
            }
            else
            {
                List <BoardTile> targetTiles = Skill.GetAffectedTiles(faceTowards, targetCoord);
                foreach (BoardTile boardTile in targetTiles)
                {
                    if (Skill.Target == SkillData.ETarget.Tile)
                    {
                        numTimesSkillUsedOnTarget++;
                        UseSkillOnBoardTile(boardTile, SkillUsedOnTargetCallback);
                    }
                    else
                    {
                        switch (Skill.Target)
                        {
                        case SkillData.ETarget.Ally:
                            if (IsAllyOf(boardTile.Occupant))
                            {
                                numTimesSkillUsedOnTarget++;
                                UseSkillOnUnit(boardTile.Occupant, SkillUsedOnTargetCallback);
                            }
                            break;

                        case SkillData.ETarget.Enemy:
                            if (!IsAllyOf(boardTile.Occupant))
                            {
                                numTimesSkillUsedOnTarget++;
                                UseSkillOnUnit(boardTile.Occupant, SkillUsedOnTargetCallback);
                            }
                            break;

                        case SkillData.ETarget.Self:
                            if (boardTile.Occupant == this)
                            {
                                numTimesSkillUsedOnTarget++;
                                UseSkillOnUnit(boardTile.Occupant, SkillUsedOnTargetCallback);
                            }
                            break;
                        }
                    }
                }
            }
        }