ComAIUnit.Avatar.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using CommonLang;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using CommonAIClient.Unity.Utils;
  5. namespace CommonAIClient.Unity.Battle
  6. {
  7. public partial class ComAIUnit
  8. {
  9. public class Avatar
  10. {
  11. private DisplayCell mDisplayCell;
  12. private object mTag;
  13. protected internal DisplayCell DisplayCell
  14. {
  15. get { return mDisplayCell; }
  16. internal set { mDisplayCell = value; }
  17. }
  18. public object Tag
  19. {
  20. get { return mTag; }
  21. set { mTag = value; }
  22. }
  23. }
  24. private List<Avatar> mAvatarStack = new List<Avatar>();
  25. private IComparer<Avatar> mAvatarComparer;
  26. public IComparer<Avatar> AvatarComparer { set { mAvatarComparer = value; } }
  27. protected override void CorrectDummyNode()
  28. {
  29. if (mAvatarStack.Count == 0)
  30. {
  31. base.CorrectDummyNode();
  32. return;
  33. }
  34. if (mAvatarStack[0].DisplayCell != null)
  35. {
  36. DisplayCell display = mAvatarStack[0].DisplayCell;
  37. foreach (var elem in this.Dummys)
  38. {
  39. GameObject trace = display.GetDummyNode(elem.Key);
  40. elem.Value.Init(elem.Key, trace);
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 如果name不为空 绝对返回一个DummyNode
  46. /// </summary>
  47. /// <param name="name"></param>
  48. /// <returns></returns>
  49. public override DummyNode GetDummyNode(string name)
  50. {
  51. if (mAvatarStack.Count == 0 || mAvatarStack[0].DisplayCell == null)
  52. {
  53. return base.GetDummyNode(name);
  54. }
  55. if (string.IsNullOrEmpty(name))
  56. {
  57. Debug.LogError("string.IsNullOrEmpty(name)");
  58. return null;
  59. }
  60. DisplayCell display = mAvatarStack[0].DisplayCell;
  61. DummyNode dummyNode = null;
  62. if (!this.Dummys.TryGetValue(name, out dummyNode))
  63. {
  64. GameObject node = display.GetDummyNode(name);
  65. if (node == null)
  66. {
  67. Debug.LogError("node not exist " + name);
  68. }
  69. GameObject tmp = new GameObject(name);
  70. tmp.ParentRoot(this.DummyRoot);
  71. dummyNode = tmp.AddComponent<DummyNode>();
  72. dummyNode.Init(name, node);
  73. this.Dummys.Add(name, dummyNode);
  74. }
  75. return dummyNode;
  76. }
  77. public override void PlayAnim(string name, bool crossFade,
  78. WrapMode wrapMode = WrapMode.Once, float speed = 1f)
  79. {
  80. if (mAvatarStack.Count == 0 || mAvatarStack[0].DisplayCell == null)
  81. {
  82. base.PlayAnim(name, crossFade, wrapMode, speed);
  83. }
  84. else
  85. {
  86. mAvatarStack[0].DisplayCell.PlayAnim(name, crossFade, wrapMode, speed);
  87. }
  88. }
  89. /// <summary>
  90. /// 增加一个avatar 返回在avatar列表中的顺序
  91. /// </summary>
  92. /// <param name="assetBundleName"></param>
  93. /// <param name="assetName"></param>
  94. /// <returns></returns>
  95. public Avatar AddAvatar(string assetBundleName, string assetName)
  96. {
  97. if (!string.IsNullOrEmpty(assetBundleName)
  98. && !string.IsNullOrEmpty(assetName))
  99. {
  100. Avatar info = new Avatar();
  101. mAvatarStack.Add(info);
  102. if (mAvatarComparer != null)
  103. {
  104. mAvatarStack.Sort(mAvatarComparer);
  105. }
  106. BattleFactroy.Instance.GameObjectAdapter.Load(assetBundleName, assetName
  107. , (succ, aoe) =>
  108. {
  109. if (succ)
  110. {
  111. if (IsDisposed || mAvatarStack.IndexOf(info) == -1)
  112. {
  113. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  114. return;
  115. }
  116. info.DisplayCell = BattleFactroy.Instance.CreateDisplayCell(mAvatarRoot);
  117. info.DisplayCell.Model = aoe;
  118. if (mAvatarStack[0] == info)
  119. {
  120. DisplayCell.ActiveSelf = false;
  121. //映射节点
  122. CorrectDummyNode();
  123. info.DisplayCell.PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed);
  124. }
  125. else
  126. {
  127. mAvatarStack[0].DisplayCell.ActiveSelf = false;
  128. }
  129. }
  130. });
  131. return info;
  132. }
  133. return null;
  134. }
  135. public void RemoveAvatar(Avatar info)
  136. {
  137. int index = mAvatarStack.IndexOf(info);
  138. if (index == -1)
  139. return;
  140. if (info.DisplayCell != null)
  141. {
  142. info.DisplayCell.Dispose();
  143. }
  144. mAvatarStack.Remove(info);
  145. if (index == 0)
  146. {
  147. if (mAvatarStack.Count > 0)
  148. {
  149. CorrectDummyNode();
  150. mAvatarStack[0].DisplayCell.ActiveSelf = true;
  151. PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed);
  152. }
  153. else
  154. {
  155. this.DisplayCell.ActiveSelf = true;
  156. CorrectDummyNode();
  157. }
  158. PlayAnim(LastAnimName, false, LastWrapMode, LastSpeed);
  159. }
  160. }
  161. }
  162. }