Exemple #1
0
        public IEnumerator MoveByAction(Action action, float duration)
        {
            var path = action?.Path;

            if (path != null)
            {
                associatedUnit.IsMoving = true;
                associatedUnit.UnitAnimator?.PlayMoveAnimation();
                Tile finalTile = null;
                var  pathCount = path.Count;
                for (int i = 0; i < pathCount; i++)
                {
                    if (path[i] != null)
                    {
                        finalTile = path[i];
                    }
                    float counter = 0;

                    if (finalTile != null)
                    {
                        if (path.IndexOf(finalTile) != pathCount - 1)
                        {
                            associatedUnit.MovesLeft -= finalTile.CostToMove;
                        }
                        var startPos = associatedUnit.Transform.position;
                        LookAt(finalTile.WorldPosition);

                        while (counter < duration)
                        {
                            counter += Time.deltaTime;

                            associatedUnit.Transform.position =
                                Vector3.Lerp(startPos, finalTile.WorldPosition, counter / duration);
                            yield return(null);
                        }

                        if (associatedUnit.MovesLeft < 0 && path.IndexOf(finalTile) != pathCount - 1)
                        {
                            i = pathCount;
                        }
                    }
                }

                associatedUnit.OnUnitMove.Publish(associatedUnit);

                associatedUnit.CurrentTile = finalTile;
                if (associatedUnit.CurrentTile != null)
                {
                    associatedUnit.Transform.position = associatedUnit.CurrentTile.WorldPosition;
                }
                associatedUnit.IsMoving = false;
                associatedUnit.UnitAnimator?.StopMoveAnimation();
            }

            if (action != null)
            {
                if (action.ActionType != ActionType.Nothing)
                {
                    if (action.ActionType == ActionType.Attack && action.Target != null)
                    {
                        associatedUnit.OnAttack.Publish(associatedUnit);
                        if (associatedUnit.TargetIsInRange(action.Target))
                        {
                            Harmony.Finder.LevelController.BattleOngoing = true;
                            yield return(associatedUnit.Attack(action.Target));

                            if (action.Target is Unit)
                            {
                                if (!Harmony.Finder.LevelController.CinematicController.IsPlayingACinematic)
                                {
                                    uiController.LaunchBattleReport(associatedUnit.IsEnemy);
                                    while (uiController.IsBattleReportActive)
                                    {
                                        yield return(null);
                                    }
                                }
                            }
                            Harmony.Finder.LevelController.BattleOngoing = false;
                        }
                        else
                        {
                            associatedUnit.Rest();
                        }
                    }
                    else if (action.ActionType == ActionType.Recruit && action.Target != null)
                    {
                        associatedUnit.UnitAnimator?.PlayAttackAnimation();
                        if (action.Target.GetType() == typeof(Unit) && !associatedUnit.RecruitUnit((Unit)action.Target))
                        {
                            associatedUnit.Rest();
                        }
                        associatedUnit.UnitAnimator?.StopAttackAnimation();
                    }
                    else if (action.ActionType == ActionType.Heal && action.Target != null)
                    {
                        associatedUnit.UnitAnimator?.PlayAttackAnimation();
                        if (action.Target.GetType() == typeof(Unit) && !associatedUnit.HealUnit((Unit)action.Target))
                        {
                            associatedUnit.Rest();
                        }
                        associatedUnit.UnitAnimator?.StopAttackAnimation();
                    }
                    else
                    {
                        associatedUnit.Rest();
                    }
                }
            }
            else
            {
                associatedUnit.Rest();
            }
        }