62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System;
|
|
using Continentis.MainGame.Character;
|
|
using SLSFramework.General;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
namespace Continentis.MainGame.Commands
|
|
{
|
|
public class Cmd_PlayAnimation : CommandBase
|
|
{
|
|
private readonly CombatCharacterViewBase characterView;
|
|
private readonly Animator animator;
|
|
private bool waitForFinish;
|
|
private float overrideDuration;
|
|
private string stateName;
|
|
private int layer;
|
|
|
|
public Cmd_PlayAnimation(CombatCharacterViewBase characterView, string stateName,
|
|
bool waitForFinish = false, float overrideDuration = -1, int layer = 0) : base(null)
|
|
{
|
|
this.characterView = characterView;
|
|
this.animator = characterView.animator;
|
|
this.stateName = stateName;
|
|
this.waitForFinish = waitForFinish;
|
|
this.overrideDuration = overrideDuration;
|
|
this.layer = layer;
|
|
}
|
|
|
|
protected override IObservable<Unit> OnExecute(CommandContext outerContext)
|
|
{
|
|
if (animator == null)
|
|
{
|
|
Debug.LogWarning("Animator or stateName is null or empty.");
|
|
return Observable.Return(Unit.Default);
|
|
}
|
|
|
|
if (!animator.HasState(layer, Animator.StringToHash(stateName)))
|
|
{
|
|
if (!animator.HasState(layer, Animator.StringToHash("Default")))
|
|
{
|
|
Debug.LogWarning($"Animator does not have state: {stateName}, and Default state is also missing.");
|
|
return Observable.Return(Unit.Default);
|
|
}
|
|
|
|
stateName = "Default"; // Fallback to Default state
|
|
Debug.Log($"Animator does not have state: {stateName}. Falling back to Default state.");
|
|
}
|
|
|
|
animator.CrossFade(stateName, 0f, layer, 0f);
|
|
if (waitForFinish)
|
|
{
|
|
float animationDuration = overrideDuration >= 0 ? overrideDuration : characterView.animationClips[stateName].length;
|
|
return Observable.Timer(TimeSpan.FromSeconds(animationDuration)).AsUnitObservable();
|
|
}
|
|
else
|
|
{
|
|
return Observable.Return(Unit.Default);
|
|
}
|
|
}
|
|
}
|
|
}
|