BattleScene.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using UnityEngine;
  2. using CommonAIClient.Client;
  3. using System;
  4. using CommonAI.ZoneClient;
  5. using CommonLang;
  6. using CommonLang.Concurrent;
  7. using CommonAI.Zone;
  8. using CommonAIClient.Unity.Utils;
  9. using CommonAI.Zone.ZoneEditor;
  10. namespace CommonAIClient.Unity.Battle
  11. {
  12. public partial class BattleScene : IDisposable
  13. {
  14. #region RefCount
  15. private static AtomicInteger s_alloc_count = new AtomicInteger(0);
  16. private static AtomicInteger s_active_count = new AtomicInteger(0);
  17. /// <summary>
  18. /// 分配实例数量
  19. /// </summary>
  20. public static int AllocCount { get { return s_alloc_count.Value; } }
  21. /// <summary>
  22. /// 未释放实例数量
  23. /// </summary>
  24. public static int ActiveCount { get { return s_active_count.Value; } }
  25. #endregion
  26. private bool mDisposed = false;
  27. private AbstractBattle mBattle;
  28. protected GameObject mMapRoot;
  29. /// <summary>
  30. /// 不绑定任何东西的特效会存在在这里
  31. /// </summary>
  32. private GameObject mEffectRoot;
  33. public bool IsDisposed { get { return mDisposed; } }
  34. protected AbstractBattle Battle { get { return mBattle; } }
  35. protected GameObject MapRoot { get { return mMapRoot; } }
  36. public GameObject EffectRoot { get { return mEffectRoot; } }
  37. public EditorTemplates DataRoot { get { return mBattle.DataRoot; } }
  38. public ZoneInfo Terrain { get { return mBattle.Layer.Terrain; } }
  39. public SceneData SceneData { get { return this.Battle.Layer.Data; } }
  40. public int SceneID { get { return this.Battle.Layer.SceneID; } }
  41. protected BattleScene(AbstractBattle client)
  42. {
  43. s_alloc_count++;
  44. s_active_count++;
  45. mBattle = client;
  46. mBattle.Layer.LayerInit += Layer_LayerInit;
  47. mBattle.Layer.ObjectEnter += Layer_ObjectEnter;
  48. mBattle.Layer.ObjectLeave += Layer_ObjectLeave;
  49. mBattle.Layer.MessageReceived += Layer_MessageReceived;
  50. mBattle.Layer.DecorationChanged += Layer_DecorationChanged;
  51. mBattle.Layer.OnChangeBGM += Layer_OnChangeBGM;
  52. mEffectRoot = new GameObject("EffectNode");
  53. RegistAllZoneEvent();
  54. }
  55. ~BattleScene()
  56. {
  57. s_alloc_count--;
  58. }
  59. protected virtual void OnLoadMapFinish()
  60. {
  61. }
  62. private void OnMapLoadFinish(bool success, GameObject o)
  63. {
  64. if (success)
  65. {
  66. mMapRoot = o;
  67. InitDecoration();
  68. }
  69. }
  70. public void Dispose()
  71. {
  72. if (!mDisposed)
  73. {
  74. OnDispose();
  75. OnDisposeMapRoot();
  76. mDisposed = true;
  77. s_active_count--;
  78. }
  79. }
  80. protected virtual void OnDisposeMapRoot()
  81. {
  82. GameObject.DestroyObject(mMapRoot);
  83. }
  84. protected virtual void OnDispose()
  85. {
  86. foreach (var elem in mBattleObjects)
  87. {
  88. elem.Value.Dispose();
  89. }
  90. mBattleObjects.Clear();
  91. mBattle.Layer.LayerInit -= Layer_LayerInit;
  92. mBattle.Layer.ObjectEnter -= Layer_ObjectEnter;
  93. mBattle.Layer.ObjectLeave -= Layer_ObjectLeave;
  94. mBattle.Layer.DecorationChanged -= Layer_DecorationChanged;
  95. AssetObjectExt[] aoes = mEffectRoot.GetComponentsInChildren<AssetObjectExt>();
  96. foreach (var elem in aoes)
  97. {
  98. EffectAutoDestroy[] scripts = elem.gameObject.GetComponents<EffectAutoDestroy>();
  99. if (scripts.Length > 0)
  100. {
  101. foreach (var script in scripts)
  102. {
  103. script.DoDestroy();
  104. }
  105. }
  106. else
  107. {
  108. BattleFactroy.Instance.GameObjectAdapter.Unload(elem);
  109. }
  110. }
  111. GameObject.DestroyObject(mEffectRoot);
  112. }
  113. public void Update(float deltaTime)
  114. {
  115. if (!mDisposed)
  116. {
  117. OnUpdate(deltaTime);
  118. }
  119. }
  120. protected virtual void OnUpdate(float deltaTime)
  121. {
  122. foreach (var elem in mBattleObjects)
  123. {
  124. elem.Value.Update(deltaTime);
  125. }
  126. }
  127. #region Objects
  128. private HashMap<uint, ComAICell> mBattleObjects = new HashMap<uint, ComAICell>();
  129. private IComAIActor mActor;
  130. public ComAICell GetBattleObject(uint id)
  131. {
  132. ComAICell outVal = null;
  133. mBattleObjects.TryGetValue(id, out outVal);
  134. return outVal;
  135. }
  136. public IComAIActor GetActor()
  137. {
  138. return mActor;
  139. }
  140. private void AddBattleObject(ComAICell obj)
  141. {
  142. if (obj != null)
  143. {
  144. mBattleObjects.Add(obj.ObjectID, obj);
  145. if (obj is ComAIActor)
  146. {
  147. mActor = obj as ComAIActor;
  148. }
  149. OnComAICellAdded(obj);
  150. }
  151. }
  152. private void RemoveBattleObject(uint id)
  153. {
  154. ComAICell outVal = mBattleObjects.RemoveByKey(id);
  155. if (outVal != null)
  156. {
  157. OnComAICellRemoved(outVal);
  158. outVal.Dispose();
  159. }
  160. }
  161. protected virtual void OnComAICellAdded(ComAICell o) { }
  162. protected virtual void OnComAICellRemoved(ComAICell o) { }
  163. protected virtual void Layer_LayerInit(ZoneLayer layer)
  164. {
  165. BattleFactroy.Instance.TerrainAdapter.Load(layer.Data.FileName, OnMapLoadFinish);
  166. BattleFactroy.Instance.SoundAdapter.PlayBGM(layer.Data.BGM);
  167. }
  168. private void Layer_ObjectEnter(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneObject obj)
  169. {
  170. AddBattleObject(BattleFactroy.Instance.CreateComAICell(this, obj));
  171. }
  172. private void Layer_ObjectLeave(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneObject obj)
  173. {
  174. RemoveBattleObject(obj.ObjectID);
  175. }
  176. #endregion
  177. private void Layer_MessageReceived(ZoneLayer layer, CommonLang.Protocol.IMessage msg)
  178. {
  179. if (msg is CommonAI.Zone.ZoneEvent)
  180. {
  181. Action<CommonAI.Zone.ZoneEvent> action = null;
  182. if (mZoneEvens.TryGetValue(msg.GetType(), out action))
  183. {
  184. action(msg as CommonAI.Zone.ZoneEvent);
  185. }
  186. }
  187. else if (msg is CommonAI.Zone.ObjectEvent)
  188. {
  189. var obj = GetBattleObject((msg as CommonAI.Zone.ObjectEvent).object_id);
  190. if (obj != null)
  191. {
  192. obj.DoObjectEvent(msg as CommonAI.Zone.ObjectEvent);
  193. }
  194. }
  195. }
  196. private void Layer_OnChangeBGM(ZoneLayer layer, string filename)
  197. {
  198. BattleFactroy.Instance.SoundAdapter.PlayBGM(filename);
  199. }
  200. public virtual void PlayEffectWithZoneCoord(LaunchEffect eff
  201. , float x, float y, float direct)
  202. {
  203. Vector3 pos = Extensions.ZonePos2NavPos(mBattle.Layer.Terrain.TotalHeight
  204. , x, y, 0);
  205. Quaternion rot = Extensions.ZoneRot2UnityRot(direct);
  206. if (eff != null)
  207. {
  208. if (!string.IsNullOrEmpty(eff.SoundName))
  209. {
  210. if (eff.IsLoop)
  211. {
  212. BattleFactroy.Instance.SoundAdapter.PlaySound(eff.SoundName, eff.EffectTimeMS, pos);
  213. }
  214. else
  215. {
  216. BattleFactroy.Instance.SoundAdapter.PlaySound(eff.SoundName, pos);
  217. }
  218. }
  219. //特效
  220. if (!string.IsNullOrEmpty(eff.Name))
  221. {
  222. BattleFactroy.Instance.GameObjectAdapter.Load(eff.Name
  223. , System.IO.Path.GetFileNameWithoutExtension(eff.Name)
  224. , (succ, aoe) =>
  225. {
  226. if (succ)
  227. {
  228. if (IsDisposed && eff.BindBody)
  229. {
  230. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  231. return;
  232. }
  233. OnLoadEffectSuccess(aoe, eff, pos, rot);
  234. }
  235. });
  236. }
  237. }
  238. }
  239. /// <summary>
  240. ///
  241. /// </summary>
  242. /// <param name="eff"></param>
  243. /// <param name="pos"> unity pos</param>
  244. /// <param name="rot"> unity rot</param>
  245. public virtual void PlayEffect(LaunchEffect eff, Vector3 pos, Quaternion rot)
  246. {
  247. if (eff != null)
  248. {
  249. //特效
  250. if (!string.IsNullOrEmpty(eff.Name))
  251. {
  252. BattleFactroy.Instance.GameObjectAdapter.Load(eff.Name
  253. , System.IO.Path.GetFileNameWithoutExtension(eff.Name)
  254. , (succ, aoe) =>
  255. {
  256. if (succ)
  257. {
  258. if (IsDisposed && eff.BindBody)
  259. {
  260. BattleFactroy.Instance.GameObjectAdapter.Unload(aoe);
  261. return;
  262. }
  263. OnLoadEffectSuccess(aoe, eff, pos, rot);
  264. }
  265. });
  266. }
  267. }
  268. }
  269. protected virtual void OnLoadEffectSuccess(AssetObjectExt aoe
  270. , LaunchEffect eff, Vector3 pos, Quaternion rot)
  271. {
  272. aoe.gameObject.Parent(mEffectRoot);
  273. aoe.gameObject.Position(pos);
  274. aoe.gameObject.Rotation(rot);
  275. var script = aoe.gameObject.AddComponent<EffectAutoDestroy>();
  276. script.aoeHandler = aoe;
  277. script.duration = eff.IsLoop ? eff.EffectTimeMS / 1000f : 0f;
  278. }
  279. }
  280. }