using cfg.CharacterCfg;
using cfg.ShowCfg;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Emoji;
using Gameplay.Utils;
using PhxhSDK;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
namespace Gameplay.Show
{
public sealed class RaffleShowController : IShow
{
private RaffleShowComponent showComponent;
///
/// 角色动画
///
private readonly string[] animationNames = { "standready001", "squatready001", "standready002" };
///
/// 表情管理
///
private readonly List listCacheEmoji = new();
private List listShowPoint = new();
///
/// 已经加载的资源集合
///
private readonly HashSet setAssetToUnload = new();
///
/// 角色模型字典
///
private readonly Dictionary dicCharacterModel = new();
public E_ShowSceneType ShowSceneType => showComponent.ShowSceneTyp;
public void SetInfo(RaffleShowComponent raffleShowComponent)
{
showComponent = raffleShowComponent;
#region 获取演出点
showComponent.GetShowPoint(ref listShowPoint);
#endregion
}
public void PlayeCameraAnim(Action callback, params string[] args)
{
}
public async UniTask RefreshShow(object data = null)
{
if (data is not List listCharacterAttris)
return;
HashSet setShowRoleId = new();
List listCfg = new(listCharacterAttris);
// 星高的排前面
listCfg.Sort((a, b) =>
{
if (null == a)
return 0;
if (null == b)
return 0;
return a.StartStar.CompareTo(b.StartStar);
});
#region 先禁用之前的模型
foreach (GameObject go in dicCharacterModel.Values)
{
go.SetActive(false);
}
#endregion
#region 设置要显示的模型位置
int i = 0;
foreach (DataCharacterAttri cfg in listCfg)
{
if (cfg == null)
continue;
GameObject goModel = await GetModel(cfg);
if (goModel == null)
continue;
if (setShowRoleId.Contains(cfg.RoleID)) // 重复角色不重复显示
continue;
setShowRoleId.Add(cfg.RoleID);
Transform tsPoint = listShowPoint[i];
goModel.transform.SetPositionAndRotation(tsPoint.position, tsPoint.rotation);
goModel.SetActive(true); // 先激活再设置动画
SetAnim(goModel);
++i;
}
#endregion
}
public void Destory()
{
foreach (string path in setAssetToUnload)
{
AssetManager.Instance.Unload(path);
}
foreach (var unitEmojiManager in listCacheEmoji)
{
unitEmojiManager.Dispose();
}
}
///
/// 初始化表情
///
///
///
///
private UnitEmojiManager InitEmoji(GameObject go, DataCharacterAttri cfg)
{
DataCharacterSkin skinData = TableManager.Instance.Tables.SkinCfg.Get(Framework.Constants.DEFULT_SKINID, cfg.RoleID);
if (skinData == null)
{
DebugUtil.LogError($"找不到皮肤数据, skinID:{Framework.Constants.DEFULT_SKINID}, roleID:{cfg.RoleID}");
return null;
}
string eyebowsType = string.IsNullOrEmpty(cfg.EmojiEyeBowsType) ? "1" : cfg.EmojiEyeBowsType;
string eyesColor = string.IsNullOrEmpty(cfg.EmojiEyesColor) ? "#ff0000" : cfg.EmojiEyesColor;
string eyebowsColor = string.IsNullOrEmpty(cfg.EmojiEyeBowsColor) ? "#ff0000" : cfg.EmojiEyeBowsColor;
UnitEmojiManager unitEmojiManager = new(go, eyesColor, eyebowsType, eyebowsColor);
unitEmojiManager.SwitchEmoji(cfg.DefaultDisplayEmojiId);
return unitEmojiManager;
}
///
/// 异步加载资源
///
///
///
///
private async UniTask LoadAssetAsync(string path) where T : UnityEngine.Object
{
T res = await AssetManager.Instance.LoadAssetAsync(path);
if (res != null)
setAssetToUnload.Add(path);
return res;
}
///
/// 获取角色模型
///
///
///
private async UniTask GetModel(DataCharacterAttri cfg)
{
if (!dicCharacterModel.TryGetValue(cfg.RoleID, out GameObject goModel))
{
string prefabPath = PathEx.GetCharacterDisplayModelPath(cfg.NameID);
goModel = await LoadAssetAsync(prefabPath);
if (goModel == null)
{
DebugUtil.LogError($"找不到模型, path:{prefabPath}");
return null;
}
goModel = UnityEngine.Object.Instantiate(goModel, showComponent.TsCharacterRoot);
if (goModel.TryGetComponent(out PlayableDirector playableDirector))
playableDirector.enabled = false;
goModel.SetLayerEx(Framework.Constants.Layer.UIVIEW);
goModel.transform.localScale = new Vector3(8f, 8f, 8f);
UnitEmojiManager unitEmojiManager = InitEmoji(goModel, cfg);
if (unitEmojiManager != null)
listCacheEmoji.Add(unitEmojiManager); // 缓存起来
dicCharacterModel.Add(cfg.RoleID, goModel);
}
return goModel;
}
///
/// 设置角色动画
///
///
private void SetAnim(GameObject goRole)
{
Animator animator = goRole.GetComponentInChildren();
if (null == animator)
return;
string animName = CommonUtils.GetRandomItem(animationNames);
animator.Play(animName);
}
}
}