using CommonLang; using System.Collections.Generic; using UnityEngine; using CommonAIClient.Unity.Utils; namespace CommonAIClient.Unity.Battle { public partial class ComAIUnit { public class Avatar { private DisplayCell mDisplayCell; private object mTag; protected internal DisplayCell DisplayCell { get { return mDisplayCell; } internal set { mDisplayCell = value; } } public object Tag { get { return mTag; } set { mTag = value; } } } private List mAvatarStack = new List(); private IComparer mAvatarComparer; public IComparer AvatarComparer { set { mAvatarComparer = value; } } protected override void CorrectDummyNode() { if (mAvatarStack.Count == 0) { base.CorrectDummyNode(); return; } if (mAvatarStack[0].DisplayCell != null) { DisplayCell display = mAvatarStack[0].DisplayCell; foreach (var elem in this.Dummys) { GameObject trace = display.GetDummyNode(elem.Key); elem.Value.Init(elem.Key, trace); } } } /// /// 如果name不为空 绝对返回一个DummyNode /// /// /// public override DummyNode GetDummyNode(string name) { if (mAvatarStack.Count == 0 || mAvatarStack[0].DisplayCell == null) { return base.GetDummyNode(name); } if (string.IsNullOrEmpty(name)) { Debug.LogError("string.IsNullOrEmpty(name)"); return null; } DisplayCell display = mAvatarStack[0].DisplayCell; DummyNode dummyNode = null; if (!this.Dummys.TryGetValue(name, out dummyNode)) { GameObject node = display.GetDummyNode(name); if (node == null) { Debug.LogError("node not exist " + name); } GameObject tmp = new GameObject(name); tmp.ParentRoot(this.DummyRoot); dummyNode = tmp.AddComponent(); dummyNode.Init(name, node); this.Dummys.Add(name, dummyNode); } return dummyNode; } public override void PlayAnim(string name, bool crossFade, WrapMode wrapMode = WrapMode.Once, float speed = 1f) { if (mAvatarStack.Count == 0 || mAvatarStack[0].DisplayCell == null) { base.PlayAnim(name, crossFade, wrapMode, speed); } else { mAvatarStack[0].DisplayCell.PlayAnim(name, crossFade, wrapMode, speed); } } /// /// 增加一个avatar 返回在avatar列表中的顺序 /// /// /// /// public Avatar AddAvatar(string assetBundleName, string assetName) { if (!string.IsNullOrEmpty(assetBundleName) && !string.IsNullOrEmpty(assetName)) { Avatar info = new Avatar(); mAvatarStack.Add(info); if (mAvatarComparer != null) { mAvatarStack.Sort(mAvatarComparer); } BattleFactroy.Instance.GameObjectAdapter.Load(assetBundleName, assetName , (succ, aoe) => { if (succ) { if (IsDisposed || mAvatarStack.IndexOf(info) == -1) { BattleFactroy.Instance.GameObjectAdapter.Unload(aoe); return; } info.DisplayCell = BattleFactroy.Instance.CreateDisplayCell(mAvatarRoot); info.DisplayCell.Model = aoe; if (mAvatarStack[0] == info) { DisplayCell.ActiveSelf = false; //映射节点 CorrectDummyNode(); info.DisplayCell.PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed); } else { mAvatarStack[0].DisplayCell.ActiveSelf = false; } } }); return info; } return null; } public void RemoveAvatar(Avatar info) { int index = mAvatarStack.IndexOf(info); if (index == -1) return; if (info.DisplayCell != null) { info.DisplayCell.Dispose(); } mAvatarStack.Remove(info); if (index == 0) { if (mAvatarStack.Count > 0) { CorrectDummyNode(); mAvatarStack[0].DisplayCell.ActiveSelf = true; PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed); } else { this.DisplayCell.ActiveSelf = true; CorrectDummyNode(); } PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed); } } } }