111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using System.Collections;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Utility;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
/// <summary>
|
|
/// 角色基类
|
|
/// 有Sprite实现和Animator实现
|
|
/// </summary>
|
|
public abstract class AbsActor : MonoBehaviour
|
|
{
|
|
public string ActorName { get; private set; }
|
|
public string SlotName { get; private set; }
|
|
protected SpriteRenderer spriteRenderer;
|
|
|
|
public virtual void Init(string actorName, ActorSlot slot)
|
|
{
|
|
ActorName = actorName;
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
SetSlot(slot);
|
|
}
|
|
|
|
public virtual void Show()
|
|
{
|
|
spriteRenderer.SetAlpha(1);
|
|
}
|
|
|
|
public virtual void Hide()
|
|
{
|
|
spriteRenderer.SetAlpha(0);
|
|
}
|
|
|
|
public void SetSlot(ActorSlot slot)
|
|
{
|
|
transform.position = slot.Position;
|
|
SlotName = slot.slotName;
|
|
spriteRenderer.sortingOrder = slot.sortingOrder;
|
|
spriteRenderer.sortingLayerID = slot.sortingLayer;
|
|
}
|
|
|
|
public virtual IEnumerator FadeInAsync(float duration)
|
|
{
|
|
yield return spriteRenderer.DOFade(1, duration).WaitForCompletion();
|
|
}
|
|
|
|
public virtual IEnumerator FadeOutAsync(float duration)
|
|
{
|
|
yield return spriteRenderer.DOFade(0, duration).WaitForCompletion();
|
|
}
|
|
|
|
public abstract void ChangeState(string stateName);
|
|
|
|
public virtual IEnumerator ChangeStateAsync(string stateName)
|
|
{
|
|
ChangeState(stateName);
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
// 工厂类
|
|
public class ActorFactory
|
|
{
|
|
private readonly Transform _actorRoot;
|
|
private readonly GameObject _actorAnimaPrefab;
|
|
private readonly GameObject _spriteActorPrefab;
|
|
|
|
public ActorFactory(Transform actorRoot)
|
|
{
|
|
_actorRoot = actorRoot;
|
|
_actorAnimaPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.ActorPrefabName);
|
|
_spriteActorPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.SpriteActorPrefabName);
|
|
}
|
|
|
|
public AbsActor CreateActor(string actorName, ActorSlot slot, ActorType actorType)
|
|
{
|
|
return actorType switch
|
|
{
|
|
ActorType.Sprite => CreateSpriteActor(actorName, slot),
|
|
ActorType.Anima => CreateActorAnima(actorName, slot),
|
|
_ => null
|
|
};
|
|
}
|
|
|
|
private AbsActor CreateSpriteActor(string actorName, ActorSlot slot)
|
|
{
|
|
var actor = Object.Instantiate(_spriteActorPrefab, _actorRoot);
|
|
actor.name = actorName;
|
|
var actorAnima = actor.GetComponent<AbsActor>();
|
|
actorAnima.Init(actorName, slot);
|
|
return actorAnima;
|
|
}
|
|
|
|
private AbsActor CreateActorAnima(string actorName, ActorSlot slot)
|
|
{
|
|
var actor = Object.Instantiate(_actorAnimaPrefab, _actorRoot);
|
|
actor.name = actorName;
|
|
var actorAnima = actor.GetComponent<AbsActor>();
|
|
actorAnima.Init(actorName, slot);
|
|
return actorAnima;
|
|
}
|
|
}
|
|
|
|
public enum ActorType
|
|
{
|
|
Sprite,
|
|
Anima
|
|
}
|
|
} |