NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Level/LevelFightEnd/FightWinComponent.cs

352 lines
12 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2024-03-29 19:30:27 +08:00
using System.Linq;
using Cysharp.Threading.Tasks;
using Gameplay;
using PhxhSDK;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
using UnityEngine.Timeline;
2024-03-26 19:33:53 +08:00
using Framework;
using Gameplay.Common;
2024-05-21 19:47:03 +08:00
using Gameplay.Emoji;
using Gameplay.Level;
2024-03-26 19:33:53 +08:00
using Constants = Framework.Constants;
2024-03-29 19:30:27 +08:00
using YooAsset;
public class FightWinComponent: MonoBehaviour
{
private Scene _scene;
private List<uint> _characterIDs;
private uint _vehicleID;
private GameObject _objRoot;
private List<Transform> _posTransList;
private Camera _camera;
2024-03-27 14:52:35 +08:00
private PlayableDirector _timelinePlayable;
2024-03-29 19:30:27 +08:00
private Dictionary<string, bool> _needToRelease;
2024-05-21 19:47:03 +08:00
private List<UnitEmojiManager> _emojiMgrList = new List<UnitEmojiManager>();
2024-03-29 19:30:27 +08:00
private TimelineClip _cacheTimelineClip;
//test
private List<GameObject> _testObjs = new List<GameObject>();
private readonly int Vehicle_Model_Index = 5;
#region Unity EventFunction
private void Awake()
{
_SetData();
_camera = GameObject.Find("CameraRoot").GetComponent<CameraRoot>().Camera;
_objRoot = GameObject.Find("Content/ObjRoot");
var posRootTrans = GameObject.Find("Content/PosRoot").transform;
_posTransList = new List<Transform>();
for (int i = 0; i < posRootTrans.childCount; i++)
{
var child = posRootTrans.GetChild(i);
_posTransList.Add(child);
}
EventManager.Instance.Register(EventManager.EventName.FightCmpTimelinePause, _OnPauseEvent);
2024-03-29 19:30:27 +08:00
_needToRelease = new Dictionary<string, bool>();
_Load();
}
private void OnDestroy()
{
EventManager.Instance.Unregister(EventManager.EventName.FightCmpTimelinePause, _OnPauseEvent);
2024-03-29 19:30:27 +08:00
2024-05-21 19:47:03 +08:00
foreach (var emojiMgr in _emojiMgrList)
{
emojiMgr.Dispose();
}
_emojiMgrList.Clear();
2024-03-29 19:30:27 +08:00
foreach (var path in _needToRelease)
{
//handle.Release();
}
2024-07-16 14:41:38 +08:00
//_timelinePlayable.stopped -= OnPlayableStopped;
}
#endregion
private void _SetData()
{
SetData(LevelManager.Instance.ShowCharIDs, LevelManager.Instance.ShowVehicleID);
}
private async void _Load()
{
await _LoadModels();
await _LoadTimeline();
2024-03-29 19:30:27 +08:00
await UniTask.DelayFrame(1);
2024-07-16 14:41:38 +08:00
_timelinePlayable.extrapolationMode = DirectorWrapMode.Hold;
2024-03-29 19:30:27 +08:00
_timelinePlayable.Play();
2024-07-16 14:41:38 +08:00
//_timelinePlayable.stopped += OnPlayableStopped;
}
private void _OnPauseEvent()
{
if (_timelinePlayable)
{
_timelinePlayable.Pause();
}
}
private async UniTask _LoadModels()
{
if (_characterIDs != null)
{
for (int i = 0; i < _characterIDs.Count; i++)
{
await _AddCharacterModel(_characterIDs[i], i);
}
}
if (_vehicleID != Constants.INVALID_UINT_ID)
{
await _AddVehicleModel(_vehicleID);
}
}
private async UniTask _AddCharacterModel(uint uid, int idx)
{
var modelPath = CommonUtils.GetCharacterShowModelPath(uid);
if (modelPath != null)
{
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(modelPath);
var model = Instantiate(prefab, _objRoot.transform);
var posTrans = _posTransList[idx];
model.transform.SetPositionAndRotation(posTrans.position, posTrans.rotation);
model.transform.localScale = posTrans.localScale;
model.SetLayerEx(LayerDefine.TeamEdit);
2024-05-21 19:47:03 +08:00
var charDataCfg = TableManager.Instance.Tables.CharacterAttri;
if (charDataCfg.DataMap.TryGetValue((int)uid, out var charSingleData))
{
var emojiMgr = new UnitEmojiManager(model, charSingleData.EmojiEyesColor,
2024-06-18 16:52:21 +08:00
charSingleData.EmojiEyeBowsType, charSingleData.EmojiEyeBowsColor);
2024-05-21 19:47:03 +08:00
emojiMgr.SwitchEmoji(charSingleData.DefaultDisplayEmojiId);
_emojiMgrList.Add(emojiMgr);
}
}
}
private async UniTask _AddVehicleModel(uint uid)
{
var modelPath = CommonUtils.GetVehicleShowModelPath(uid);
if (modelPath != null)
{
var prefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(modelPath);
var model = Instantiate(prefab, _objRoot.transform);
var posTrans = _posTransList[Vehicle_Model_Index];
model.transform.SetPositionAndRotation(posTrans.position, posTrans.rotation);
model.transform.localScale = posTrans.localScale;
model.SetLayerEx(LayerDefine.TeamEdit);
}
}
private async UniTask _LoadTimeline()
{
var timeLineObj = new GameObject("GameTimeLine");
timeLineObj.transform.SetParent(transform);
timeLineObj.SetLayerEx(LayerDefine.TeamEdit);
var timeline = timeLineObj.AddComponent<PlayableDirector>();
2024-03-29 19:30:27 +08:00
var timeLineAsset = ScriptableObject.CreateInstance<TimelineAsset>();//playable as TimelineAsset;
if (!timeLineAsset)
{
DebugUtil.LogError("加载的TimeLine资源为空");
return;
}
timeline.playableAsset = timeLineAsset;
timeline.timeUpdateMode = DirectorUpdateMode.GameTime;
timeline.playOnAwake = false;
2024-03-27 14:52:35 +08:00
_timelinePlayable = timeline;
2024-03-29 19:30:27 +08:00
//新建轨道
for (int i = 0; i < 5; i++)
{
2024-03-29 19:40:22 +08:00
if (i >= _characterIDs.Count)
{
2024-03-29 19:58:08 +08:00
break;
2024-03-29 19:40:22 +08:00
}
2024-03-29 19:30:27 +08:00
var child = _objRoot.transform.GetChild(i);
if (!child)
{
continue;
}
var track = timeLineAsset.CreateTrack<AnimationTrack>();
2024-03-29 19:30:27 +08:00
var animObj = child.GetChild(0).gameObject;
var anim = animObj.GetComponent<Animator>();
if (!anim)
{
DebugUtil.LogError("TimeLine使用的模型未绑定Animator组件");
}
timeline.SetGenericBinding(track, anim);
anim.cullingMode = AnimatorCullingMode.AlwaysAnimate;
}
var trackCamera = timeLineAsset.CreateTrack<AnimationTrack>();
var animCamera = _camera.GetComponent<Animator>();
if (animCamera)
{
timeline.SetGenericBinding(trackCamera, animCamera);
animCamera.cullingMode = AnimatorCullingMode.AlwaysAnimate;
}
var trackIEnumerable = timeLineAsset.GetOutputTracks();
int index = 0;
2024-03-29 19:30:27 +08:00
var cameraAnimPath = "Assets/Art/Animations/Level/VictoryCameraAnimation.anim";
foreach (var trackAsset in trackIEnumerable)
{
2024-03-29 19:30:27 +08:00
var animTrackAsset = trackAsset as AnimationTrack;
if (index < _characterIDs.Count)
{
2024-03-29 19:30:27 +08:00
var animPaths = GetAnimPaths(_characterIDs[index], index == 0);
List<AnimationClip> animClips = new List<AnimationClip>();
var animClip1 = await AssetManager.Instance.LoadAssetAsync<AnimationClip>(animPaths[0]);
var animClip2 = await AssetManager.Instance.LoadAssetAsync<AnimationClip>(animPaths[1]);
_needToRelease.TryAdd(animPaths[0], true);
_needToRelease.TryAdd(animPaths[1], true);
animClips.Add(animClip1);
animClips.Add(animClip2);
if (animTrackAsset)
{
2024-03-29 19:30:27 +08:00
if (index == 0)
{
var clip0 = animTrackAsset.CreateClip(animClips[0]);
clip0.start = 2.516667;//capClipInfo[0].Item1;
clip0.duration = 5.683333;//capClipInfo[0].Item2;
clip0.blendOutDuration = 0.683333;
var clip1 = animTrackAsset.CreateClip(animClips[1]);
clip1.blendInDuration = 0.683333;
clip1.start = 7.516667;//capClipInfo[1].Item1;
clip1.duration = 4.6; //capClipInfo[1].Item2;
}
else
{
var clip0 = animTrackAsset.CreateClip(animClips[0]);
clip0.start = 0;//teamClipInfo[0].Item1;
clip0.duration = 5.683333;//teamClipInfo[0].Item2;
clip0.blendOutDuration = 0.683333;
var clip1 = animTrackAsset.CreateClip(animClips[1]);
clip1.blendInDuration = 0.683333;
clip1.start = 5;//teamClipInfo[1].Item1;
clip1.duration = 4.6; //teamClipInfo[1].Item2;
}
}
}
2024-03-29 19:58:59 +08:00
else if(index == _characterIDs.Count)
{
2024-03-29 19:30:27 +08:00
var cameraClip = await AssetManager.Instance.LoadAssetAsync<AnimationClip>(cameraAnimPath);
if (animTrackAsset)
{
2024-03-29 19:30:27 +08:00
animTrackAsset.trackOffset = TrackOffset.ApplyTransformOffsets;
var clip = animTrackAsset.CreateClip(cameraClip);
var animAsset = clip.asset as AnimationPlayableAsset;
if (animAsset)
{
animAsset.removeStartOffset = false;
animAsset.useTrackMatchFields = false;
animAsset.matchTargetFields =
MatchTargetFields.PositionX | MatchTargetFields.PositionY | MatchTargetFields.PositionZ
| MatchTargetFields.RotationX | MatchTargetFields.RotationY | MatchTargetFields.RotationZ;
animAsset.ResetOffsets();
}
clip.asset = animAsset;
clip.start = 0;
_cacheTimelineClip = clip;
}
2024-03-29 19:30:27 +08:00
break;
}
index++;
2024-03-29 19:30:27 +08:00
_timelinePlayable.RebindPlayableGraphOutputs();
2024-03-29 19:30:27 +08:00
await UniTask.DelayFrame(1);
}
}
2024-03-29 19:30:27 +08:00
private List<string> GetAnimPaths(uint charID, bool isCaptain = false)
{
List<string> retAnimClips = new List<string>();
var cfg = CharacterDataInfoManager.Instance.DataDic[charID].Cfg;
var weaponID = cfg.WeaponId;
var weaponCfg = TableManager.Instance.Tables.WeaponConfig.GetOrDefault(weaponID);
if (weaponCfg == null)
{
weaponCfg = TableManager.Instance.Tables.WeaponConfig.GetOrDefault(1);
}
string fbxPath;
if (isCaptain)
{
fbxPath = weaponCfg.SettleCapAnimPath;
}
else
{
fbxPath = weaponCfg.SettleTeamAnimPath;
}
retAnimClips.Add(fbxPath + ".anim");
retAnimClips.Add(fbxPath + "_idle.anim");
return retAnimClips;
}
2024-07-16 14:41:38 +08:00
private void OnPlayableStopped(PlayableDirector playable)
{
for (int i = 0; i < _objRoot.transform.childCount; i++)
{
var trans = _objRoot.transform.GetChild(i);
if (trans.gameObject.name.Contains("P_S"))
{
var anim = trans.GetChild(0).GetComponent<Animator>();
if (anim)
{
anim.Play("standready001");
}
}
}
}
2024-03-27 14:52:35 +08:00
#region API
public void SetData(List<uint> charIDList)
{
_characterIDs = charIDList;
}
public void SetData(uint vehicleID)
{
_vehicleID = vehicleID;
}
public void SetData(List<uint> charIDList, uint vehicleID)
{
_characterIDs = charIDList;
_vehicleID = vehicleID;
}
2024-03-27 14:52:35 +08:00
public void StartPlay()
{
_timelinePlayable.Play();
}
#endregion
}