BattleMgr.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using CommonAI.Zone;
  2. using CommonAI.ZoneClient;
  3. using CommonLang;
  4. using CommonLang.Geometry;
  5. using ET.Client;
  6. using ET.EventType;
  7. using System.IO;
  8. using XmdsCommon.Message;
  9. namespace ET
  10. {
  11. [FriendOf(typeof(UnitMgr))]
  12. public partial class BattleMgr : Singleton<BattleMgr>, ISingletonAwake, ISingletonUpdate, ILayerClient
  13. {
  14. public ZoneLayer Layer;
  15. private bool isInited = false;
  16. private readonly MemoryStream writeBuffer = new MemoryStream(2048);
  17. private EventDispatcher<ZoneEvent> zoneEventHandler;
  18. private EventDispatcher<ObjectEvent> objectEventHandler;
  19. private static CommonLang.Geometry.Vector3 vecTemp = new();
  20. public void Awake()
  21. {
  22. zoneEventHandler = new();
  23. objectEventHandler = new();
  24. registerEventHandler();
  25. Layer = TemplateManager.Factory.CreateClientZoneLayer(BattleResourceMgr.Instance.GameEditorTemplates, this);
  26. Layer.ActorSyncMode = SyncMode.MoveByClient_PreSkillByClient;
  27. Layer.LayerInit += LayerEvent_Init;
  28. Layer.ObjectEnter += LayerEvent_ObjectEnter;
  29. Layer.ObjectLeave += LayerEvent_ObjectLeave;
  30. Layer.MessageReceived += LayerEvent_MessageReceived;
  31. Layer.DecorationChanged += LayerEvent_DecorationChanged;
  32. isInited = true;
  33. }
  34. public void Update()
  35. {
  36. if (!isInited) return;
  37. //TODO: Send ping
  38. Layer.Update();
  39. this.doUpdate();
  40. }
  41. //ILayerClient接口实现,供layer层调用
  42. //layer层在收到LockActorEvent消息后调用
  43. void ILayerClient.BattleReady(bool bok)
  44. {
  45. Log.Debug($"battle ready......{bok}");
  46. }
  47. //ILayerClient接口实现,供layer层调用
  48. //layer层中需要发送消息到战斗服
  49. public void SendAction(CommonAI.Zone.Action action)
  50. {
  51. if(PlayerComponent.Instance == null)
  52. {
  53. Log.Error("playerComponent is null");
  54. return;
  55. }
  56. Log.Debug($">>>Send BattleMsg: {action.GetType()}");
  57. writeBuffer.Position = 0;
  58. if (BattleResourceMgr.Instance.BattleMsgDecoder.doEncode(writeBuffer, action))
  59. {
  60. PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>().Session.Send(new BattleEventPushToServer() { data = writeBuffer.ToArray() });
  61. }
  62. }
  63. //客户端自己模拟创建消息,发送到zonelayer层处理
  64. public void PostMsg2Layer(CommonLang.Protocol.IMessage msg)
  65. {
  66. Layer.ProcessMessage(msg);
  67. }
  68. //layer层在处理好战斗服推送的ClientEnterScene消息后回调
  69. protected void LayerEvent_Init(CommonAI.ZoneClient.ZoneLayer layer)
  70. {
  71. Log.Debug($"OnLayerInit- scene template ID:{layer.Data.ID}");
  72. }
  73. //单位进入战斗
  74. protected void LayerEvent_ObjectEnter(ZoneLayer layer, ZoneObject obj)
  75. {
  76. Log.Debug($"OnObjectEnter: {obj}@{obj.ObjectID}");
  77. var unit = BattleUnitFactory.Instance.Create(obj);
  78. if(unit != null )
  79. {
  80. UnitMgr.Instance.PutUnit(obj.ObjectID, unit);
  81. if (unit is BattleActor)
  82. {
  83. UnitMgr.Instance.Actor = unit as BattleActor;
  84. }
  85. unit.OnAwake(obj);
  86. }
  87. else
  88. {
  89. Log.Error($"unknow object enter:{obj.Name} ID:{obj.ObjectID}");
  90. }
  91. }
  92. //单位离开战斗
  93. protected void LayerEvent_ObjectLeave(ZoneLayer layer, ZoneObject obj)
  94. {
  95. Log.Debug($"OnObjectLeave: {obj.ObjectID}");
  96. var unit = UnitMgr.Instance.GetUnit(obj.ObjectID);
  97. if(unit == null)
  98. {
  99. Log.Error($"LayerEvent_ObjectLeave not exist:{obj.Name}:{obj.ObjectID}");
  100. return;
  101. }
  102. unit.OnSleep();
  103. UnitMgr.Instance.RemoveUnit(obj.ObjectID);
  104. }
  105. protected void LayerEvent_DecorationChanged(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneEditorDecoration ed)
  106. {
  107. Log.Error("Layer_DecorationChanged");
  108. /*DataMgr.Instance.RegionManager.OnDecorationChanged(ed);
  109. if (mDecoMgr != null)
  110. {
  111. mDecoMgr.OnStateChange(ed);
  112. }*/
  113. }
  114. protected void LayerEvent_MessageReceived(CommonAI.ZoneClient.ZoneLayer layer, CommonLang.Protocol.IMessage msg)
  115. {
  116. if (msg is ZoneEvent)
  117. {
  118. zoneEventHandler.Notify(msg as ZoneEvent);
  119. }
  120. else if(msg is ObjectEvent)
  121. {
  122. objectEventHandler.Notify(msg as ObjectEvent);
  123. }
  124. }
  125. private void registerEventHandler()
  126. {
  127. //actor===================
  128. objectEventHandler.AddListener<SetAutoBattleB2C>((ev) => {
  129. var isAutoFight = (ev as SetAutoBattleB2C).isAutoBattle != 0;
  130. Log.Debug($"AutoFight: {isAutoFight}");
  131. (UnitMgr.Instance.Actor.ZUnit as ZoneActor).IsGuard = isAutoFight;
  132. });
  133. objectEventHandler.AddListener<ShowTipsEventB2C>((ev) =>
  134. {
  135. });
  136. objectEventHandler.AddListener<UnitDeadEvent>((ev) =>
  137. {
  138. });
  139. objectEventHandler.AddListener<UnitRebirthEvent>((ev) =>
  140. {
  141. });
  142. objectEventHandler.AddListener<UnitHitEvent>((ev) =>
  143. {
  144. });
  145. objectEventHandler.AddListener<PlayerPKModeChangeEventB2C>((ev) =>
  146. {
  147. });
  148. objectEventHandler.AddListener<PlayerBattlePropChangeEventB2C>((ev) =>
  149. {
  150. });
  151. objectEventHandler.AddListener<PlayerPKValueChangeEventB2C>((ev) =>
  152. {
  153. });
  154. objectEventHandler.AddListener<BuffActiveSkillEventB2C>((ev) =>
  155. {
  156. });
  157. objectEventHandler.AddListener<UnitEffectEvent>((ev) =>
  158. {
  159. var e = ev as UnitEffectEvent;
  160. EventSystem.Instance.Publish<PlayEffectEvent>(PlayEffectEvent.Static.Clone(e.effect, e.ObjectID, CommonLang.Geometry.Vector3.Zero));
  161. });
  162. //NPCCureEventB2C
  163. //MonsterSufferDamageInfoB2C
  164. //BattleHintNumberB2C
  165. //BattleFloatTipsEventB2C
  166. //BubbleTipsEventB2C
  167. zoneEventHandler.AddListener<AddEffectEvent>((ev) => {
  168. var e = ev as AddEffectEvent;
  169. if(e.hostId == 0 || UnitMgr.Instance.HasUnit(e.hostId))
  170. {
  171. vecTemp.Set(e.x, e.y, 0);
  172. EventSystem.Instance.Publish<PlayEffectEvent>(PlayEffectEvent.Static.Clone(e.effect, e.hostId, vecTemp));
  173. }
  174. });
  175. /*BubbleTalkEvent
  176. AddEffectEvent
  177. SyncEnvironmentVarEvent
  178. ScriptCommandEvent
  179. ScriptAddUnitEventsB2C
  180. ScriptRemoveUnitEventsB2C
  181. PlaySoundEventB2CForAll
  182. SyncFlagsEvent
  183. ChangeBGMEvent
  184. PlayDestoryEffect*/
  185. /*var e = msg as CommonAI.Zone.Event;
  186. if (e is ChatEvent)
  187. {
  188. var evt = (e as ChatEvent);
  189. var message = evt.Message;
  190. var evt_param = message.Split('|');
  191. if (evt_param.Length >= 2 && evt_param[0] == "Notice.Msg")
  192. {
  193. var dic = GameUtil.GetDBData("NoticeMsg", int.Parse(evt_param[1]));
  194. if (dic != null)
  195. {
  196. string result = "";
  197. if (evt_param.Length >= 3 && evt_param[2] != null)
  198. {
  199. //根据参数个数 依次填值
  200. string[] par = evt_param[2].Split('.');
  201. if (par != null && par.Length > 0)
  202. {
  203. switch (par.Length)
  204. {
  205. case 1:
  206. {
  207. result = Average(dic["MsgContent"] as string, par[0]);
  208. break;
  209. }
  210. case 2:
  211. {
  212. result = Average(dic["MsgContent"] as string, par[0], par[1]);
  213. break;
  214. }
  215. case 3:
  216. {
  217. result = Average(dic["MsgContent"] as string, par[0], par[1], par[2]);
  218. break;
  219. }
  220. case 4:
  221. {
  222. result = Average(dic["MsgContent"] as string, par[0], par[1], par[2], par[3]);
  223. break;
  224. }
  225. case 5:
  226. {
  227. result = Average(dic["MsgContent"] as string, par[0], par[1], par[2], par[3], par[4]);
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. else
  234. {
  235. //没有参数
  236. result = (dic["MsgContent"] as string);
  237. }
  238. //
  239. EventManager.Fire("Event.OnEventHandler.star", new Dictionary<string, string>() {
  240. {"content", result}, {"keepTime", "" + (evt.KeepTimeMS/1000)}, {"content_is_text", "true"}
  241. });
  242. }
  243. else
  244. {
  245. YXJDebug.logError("config not exist >" + msg);
  246. }
  247. }
  248. else if (evt_param.Length == 2)
  249. {
  250. string funcName = "GlobalHooks." + evt_param[0];
  251. object[] param = evt_param[1].Split(',');
  252. //Client.GetMainState().GetFunction(funcName).LazyCall(param);
  253. if (param.Length == 0)
  254. Client.GetMainState().Call(funcName, Client.LogMiss);
  255. else if (param.Length == 1)
  256. Client.GetMainState().Call(funcName, param[0], Client.LogMiss);
  257. else if (param.Length == 2)
  258. Client.GetMainState().Call(funcName, param[0], param[1], Client.LogMiss);
  259. else if (param.Length == 3)
  260. Client.GetMainState().Call(funcName, param[0], param[1], param[2], Client.LogMiss);
  261. else if (param.Length == 4)
  262. Client.GetMainState().Call(funcName, param[0], param[1], param[2], param[3], Client.LogMiss);
  263. else if (param.Length == 5)
  264. Client.GetMainState().Call(funcName, param[0], param[1], param[2], param[3], param[4], Client.LogMiss);
  265. else
  266. {
  267. YXJDebug.logError("BattleClientBase.OnEventHandler Error!funcName is {0}", funcName);
  268. }
  269. }
  270. else
  271. {
  272. //位面进入退出效果
  273. if (msg == "EnterAOI")
  274. {
  275. var wwe = Camera.main.GetComponent<WaterWaveEffect>();
  276. if (wwe != null)
  277. {
  278. wwe.SetParam(40, -30, 3, 2);
  279. wwe.play();
  280. }
  281. //GameAlertManager.Instance.setAutoAnimiVisible(false);
  282. InAOI = true;
  283. }
  284. else if (msg == "LeaveAOI")
  285. {
  286. var wwe = Camera.main.GetComponent<WaterWaveEffect>();
  287. if (wwe != null)
  288. {
  289. wwe.SetParam(40, -30, 3, 2);
  290. wwe.play();
  291. }
  292. InAOI = false;
  293. }
  294. else
  295. {
  296. //普通chatevent
  297. EventManager.Fire("Event.OnEventHandler.star", new Dictionary<string, string>() {
  298. {"content", msg}, {"isImportant", "1"}, {"keepTime", "" + (evt.KeepTimeMS/1000)}
  299. });
  300. }
  301. }
  302. }
  303. else if (e is CommonAI.Zone.ZoneEvent)
  304. {
  305. if (e is BubbleTalkEvent)
  306. {
  307. BubbleTalkEvent evt = e as BubbleTalkEvent;
  308. if (evt.TalkInfos != null)
  309. {
  310. foreach (var info in evt.TalkInfos)
  311. {
  312. System.Action callback = delegate ()
  313. {
  314. if (info.TalkUnit == 0)
  315. {
  316. YXJDebug.logError("旁白气泡还没有实现>{0}", info.TalkContent);
  317. return;
  318. }
  319. ComAIUnit unit = GetUnitById(info.TalkUnit) as ComAIUnit;
  320. if (unit == null)
  321. {
  322. if (info.TalkContent != "bubblechat")
  323. {
  324. YXJDebug.logDebug(">BubbleTalkEvent unit not exist: {0}>>{1}", info.TalkUnit, info.TalkContent);
  325. }
  326. return;
  327. }
  328. if (info.TalkContent == "bubblechat")
  329. {
  330. UIBridgeManager.Instance.QuestUIBridge.BubbleTalkEventSend(unit);
  331. }
  332. else
  333. {
  334. var txt = ConfigMgr.Instance.TxtCfg.GetTextByKey("GameEditor_" + info.TalkContent);
  335. unit.AddBubbleChat(txt, info.TalkKeepTimeMS / 1000);
  336. }
  337. if (!string.IsNullOrEmpty(info.TalkActionType))
  338. {
  339. unit.PlayAnimationWhileIdle(info.TalkActionType);
  340. }
  341. };
  342. if (info.TalkDelayTimeMS > 0)
  343. {
  344. GameGlobal.Instance.StartCoroutine(WaitForSeconds(info.TalkDelayTimeMS / 1000, callback));
  345. }
  346. else
  347. {
  348. callback.Invoke();
  349. }
  350. }
  351. }
  352. }
  353. else if (e is AddEffectEvent && battleManager != null)
  354. {
  355. AddEffectEvent evt = e as AddEffectEvent;
  356. if (evt.hostId == 0 || GetUnitById(evt.hostId) != null)
  357. {
  358. Vector3 pos = battleManager.GetU3DPosByUnitCell(evt.x, evt.y);
  359. pos = ComAICell.AdjustHeight(pos);
  360. Quaternion rotation = Quaternion.Euler(0, evt.direction * Mathf.Rad2Deg + 90, 0);
  361. EffectPlayer.SetRotationOnetime(rotation); //--->这个功能加个参数不就好了 →_→
  362. EffectPlayer.Play(evt.effect, pos, false);
  363. }
  364. }
  365. else if (e is AddEffectEvent2 && battleManager != null)
  366. {
  367. AddEffectEvent2 evt = e as AddEffectEvent2;
  368. if (evt.hostId == 0 || GetUnitById(evt.hostId) != null)
  369. {
  370. Vector3 pos = battleManager.GetU3DPosByUnitCell(evt.x, evt.y);
  371. pos = ComAICell.AdjustHeight(pos);
  372. Vector3 targetPos = battleManager.GetU3DPosByUnitCell(evt.TargetX, evt.TargetY);
  373. EffectPlayer.PlayLocal(evt.effect, pos, false, 0, 0, 0, 0, evt.ET, targetPos, evt.FollowActor);
  374. }
  375. }
  376. else if (e is SyncEnvironmentVarEvent)
  377. {
  378. SyncEnvironmentVarEvent evt = e as SyncEnvironmentVarEvent;
  379. UIBridgeManager.Instance.BattleUIBridge.EnvironmentVarChange(evt.Key, evt.Value);
  380. }
  381. else if (e is ScriptCommandEvent)
  382. {
  383. ScriptCommandEvent evt = e as ScriptCommandEvent;
  384. string[] evt_param = evt.message.Split('|');
  385. if (evt_param.Length == 2)
  386. {
  387. object[] param = evt_param[1].Split(',');
  388. string funcName = "GlobalHooks." + evt_param[0];
  389. if (param.Length == 0)
  390. Client.GetMainState().Call(funcName, Client.LogMiss);
  391. else if (param.Length == 1)
  392. Client.GetMainState().Call(funcName, param[0], Client.LogMiss);
  393. else if (param.Length == 2)
  394. Client.GetMainState().Call(funcName, param[0], param[1], Client.LogMiss);
  395. else if (param.Length == 3)
  396. Client.GetMainState().Call(funcName, param[0], param[1], param[2], Client.LogMiss);
  397. else if (param.Length == 4)
  398. Client.GetMainState().Call(funcName, param[0], param[1], param[2], param[3], Client.LogMiss);
  399. else if (param.Length == 5)
  400. Client.GetMainState().Call(funcName, param[0], param[1], param[2], param[3], param[4], Client.LogMiss);
  401. else
  402. {
  403. YXJDebug.logError("BattleClientBase.OnEventHandler Error!funcName is {0}", funcName);
  404. }
  405. }
  406. }
  407. else if (e is LockActorEvent)
  408. {
  409. LockActorEvent evt = e as LockActorEvent;
  410. OnLockActorEvent(evt);
  411. }
  412. else if (e is ScriptAddUnitEventsB2C)
  413. {
  414. ScriptAddUnitEventsB2C evt = e as ScriptAddUnitEventsB2C;
  415. OnScriptAddUnitEvent(evt);
  416. }
  417. else if (e is ScriptRemoveUnitEventsB2C)
  418. {
  419. ScriptRemoveUnitEventsB2C evt = e as ScriptRemoveUnitEventsB2C;
  420. OnScriptRemoveUnitEvents(evt);
  421. }
  422. else if (e is PlaySoundEventB2CForAll)
  423. {
  424. PlaySoundEventB2CForAll evt = e as PlaySoundEventB2CForAll;
  425. XmdsSoundManager.GetXmdsInstance().PlaySound(evt.SoundName);
  426. }
  427. else if (e is SyncFlagsEvent && mDecoMgr != null)
  428. {
  429. mDecoMgr.LoadDecos();
  430. }
  431. else if (e is ChangeBGMEvent)
  432. {
  433. XmdsSoundManager.GetXmdsInstance().ChangeBGM((e as ChangeBGMEvent).FileName);
  434. }
  435. else if (e is PlayDestoryEffect)
  436. {
  437. if (EffectPlayer == null) return;
  438. PlayDestoryEffect destoryEffect = e as PlayDestoryEffect;
  439. Vector3 vec = new Vector3(destoryEffect.X, destoryEffect.Y, destoryEffect.Z);
  440. Quaternion rotation = Quaternion.Euler(0, destoryEffect.Direction * Mathf.Rad2Deg + 90, 0);
  441. LaunchEffect effect = new LaunchEffect();
  442. effect.Name = destoryEffect.EffectName;
  443. EffectPlayer.SetRotationOnetime(rotation);
  444. EffectPlayer.Play(effect, vec, true);
  445. }
  446. else
  447. {
  448. //YXJDebug.logInfo("OnEventHandler not implemented! {0}", e.ToString());
  449. }
  450. }*/
  451. }
  452. }
  453. }