LuaBattleBridge.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections;
  4. using LuaInterface;
  5. using UnityEngine.UI;
  6. using System.Collections.Generic;
  7. public static class LuaBattleBridge
  8. {
  9. public static void ClickChallegeBoss(ValType[] factors)
  10. {
  11. EventMgr.DispatchEvent<ValType[]>(new CoreEvent<ValType[]>(ECoreEventType.EID_Click_Challenge, factors));
  12. }
  13. public static void ClickReplayBattle(string recordStr)
  14. {
  15. BattleMgr.Instance.ReplayBattle(recordStr);
  16. }
  17. public static void SetReplayHeroActorName(string strName)
  18. {
  19. if (null != BattleMgr.Instance.Battle.Recorder)
  20. BattleMgr.Instance.Battle.Recorder.SetLeftHeroName(strName);
  21. }
  22. public static void SkipReplay()
  23. {
  24. BattleMgr.Instance.Skip();
  25. }
  26. public static void SetLuaBattleMgr(BattleMode battleMode, LuaTable luaBattleMgr)
  27. {
  28. if (battleMode == BattleMode.Normal)
  29. BattleMgr.Instance.LuaBattleMgr = luaBattleMgr;
  30. else if (battleMode == BattleMode.Versus)
  31. BattleMgr.Instance.LuaTowerMgr = luaBattleMgr;
  32. else if (battleMode == BattleMode.Boss)
  33. BattleMgr.Instance.LuaBossBattleMgr = luaBattleMgr;
  34. }
  35. public static void SetLuaGuildLobbyMgr(LuaTable luaGuildLobbyMgr)
  36. {
  37. GuildLobbyMgr.Instance.LuaGuildLobbyMgr = luaGuildLobbyMgr;
  38. }
  39. public static void SetGoColor(GameObject go, Color clr)
  40. {
  41. if (go == null) return;
  42. Renderer r = go.GetComponent<Renderer>();
  43. if (r == null) return;
  44. Material mat = r.material;
  45. if (mat == null) return;
  46. mat.color = clr;
  47. }
  48. public static Vector3 GetCanMovePoint(Vector3 source, Vector3 moveDir, float distance)
  49. {
  50. NavMeshHit hit;
  51. Vector3 dir = moveDir * distance;
  52. Vector3 target = dir + source;
  53. if (!NavMesh.Raycast(source, target, out hit, NavMesh.AllAreas))
  54. {
  55. return target;
  56. }
  57. Vector3 hitNormal = hit.normal;
  58. // 完全相反,则不能移动
  59. if (Mathf.Approximately(Vector3.Angle(moveDir, hitNormal), 180))
  60. {
  61. return source;
  62. }
  63. Vector3 newMoveDir = Vector3.Cross(Vector3.up, hitNormal).normalized;
  64. float dot = Vector3.Dot(moveDir, newMoveDir);
  65. if (dot < 0)
  66. {
  67. newMoveDir = -newMoveDir;
  68. }
  69. float newDistance = Vector3.Project(dir, newMoveDir).magnitude;
  70. Vector3 newPos = source + newMoveDir * newDistance;
  71. if (!NavMesh.Raycast(source, newPos, out hit, NavMesh.AllAreas))
  72. {
  73. return newPos;
  74. }
  75. return source;
  76. }
  77. public static bool GetCanMoveToPoint(Vector3 center, Quaternion quat, float minRange, float maxRange, out Vector3 result)
  78. {
  79. Matrix4x4 rotateMatrix = Matrix4x4.Rotate(quat);
  80. Vector3 defaultDir = rotateMatrix.MultiplyVector(Vector3.forward);
  81. defaultDir.Normalize();
  82. Vector3 target = center + defaultDir * maxRange;
  83. NavMeshPath navMeshPath = new NavMeshPath();
  84. NavMesh.CalculatePath(center, target, NavMesh.AllAreas, navMeshPath);
  85. if (navMeshPath.status == NavMeshPathStatus.PathComplete)
  86. {
  87. result = target;
  88. return true;
  89. }
  90. float minSqr = minRange * minRange;
  91. float maxSqr = maxRange * maxRange;
  92. Vector3[] corners = new Vector3[2];
  93. if (navMeshPath.status == NavMeshPathStatus.PathPartial)
  94. {
  95. navMeshPath.GetCornersNonAlloc(corners);
  96. if ((corners[1] - center).sqrMagnitude >= minSqr)
  97. {
  98. result = corners[1];
  99. return true;
  100. }
  101. }
  102. List<Vector3> canPoints = new List<Vector3>();
  103. float angle = (float)360 / 30;
  104. for (int i = 1; i < 29; i++)
  105. {
  106. Vector3 dir = Matrix4x4.Rotate(Quaternion.Euler(0, i * angle, 0)).MultiplyVector(defaultDir);
  107. Vector3 randomPoint = center + dir.normalized * maxRange;
  108. NavMesh.CalculatePath(center, randomPoint, NavMesh.AllAreas, navMeshPath);
  109. if (navMeshPath.status == NavMeshPathStatus.PathComplete)
  110. {
  111. canPoints.Add(randomPoint);
  112. }
  113. else if (navMeshPath.status == NavMeshPathStatus.PathPartial)
  114. {
  115. navMeshPath.GetCornersNonAlloc(corners);
  116. if ((corners[1] - center).sqrMagnitude >= minSqr)
  117. {
  118. canPoints.Add(corners[1]);
  119. }
  120. }
  121. }
  122. if (canPoints.Count > 0)
  123. {
  124. result = canPoints[Random.Range(0, canPoints.Count)];
  125. return true;
  126. }
  127. else
  128. {
  129. result = Vector3.zero;
  130. return false;
  131. }
  132. }
  133. public static void AddNavMeshCapsule(GameObject go, Vector3 center, float radius, float height)
  134. {
  135. NavMeshObstacle navMeshObstacle = go.GetComponent<NavMeshObstacle>();
  136. if (navMeshObstacle == null)
  137. {
  138. navMeshObstacle = go.AddComponent<NavMeshObstacle>();
  139. }
  140. navMeshObstacle.carving = true;
  141. navMeshObstacle.center = center;
  142. navMeshObstacle.height = height;
  143. navMeshObstacle.radius = radius;
  144. navMeshObstacle.shape = NavMeshObstacleShape.Capsule;
  145. }
  146. public static Camera InitMainCamera()
  147. {
  148. GameObject cameraGo = GameObject.FindWithTag("MainCamera");
  149. cameraGo.AddComponent<Cinemachine.CinemachineBrain>();
  150. return cameraGo.GetComponent<Camera>();
  151. }
  152. public static void SetIconGray(Image img, bool gray)
  153. {
  154. if (img == null) return;
  155. Material mat = img.material;
  156. if (mat == null) return;
  157. if (mat.HasProperty("_NeedGray"))
  158. mat.SetFloat("_NeedGray", gray ? 1 : 0);
  159. }
  160. public static int GetCurLevelExp()
  161. {
  162. return BattleMgr.Instance.CurExp;
  163. }
  164. public static int GetCurLevelParnterExp()
  165. {
  166. return BattleMgr.Instance.CurParnterExp;
  167. }
  168. public static int GetCurLevelGold()
  169. {
  170. return BattleMgr.Instance.CurGold;
  171. }
  172. public static int GetCurLevelCruise()
  173. {
  174. return BattleMgr.Instance.CurCruise;
  175. }
  176. public static int GetCurLevelZeny()
  177. {
  178. return BattleMgr.Instance.CurZeny;
  179. }
  180. public static int GetCurLevelEvil()
  181. {
  182. return BattleMgr.Instance.CurEvil;
  183. }
  184. public static string GetCurLevelName()
  185. {
  186. if (null == BattleMgr.Instance.Battle)
  187. return string.Empty;
  188. if (null == BattleMgr.Instance.Battle.CurLevelItem)
  189. return string.Empty;
  190. return BattleMgr.Instance.Battle.CurLevelItem.Name;
  191. }
  192. public static int GetCurLevelChallengeLv()
  193. {
  194. return BattleMgr.Instance.ChallengeLevel;
  195. }
  196. public static void SetRewardDropPos(Vector3 pos)
  197. {
  198. BattleMgr.s_reward_pos = pos;
  199. BattleMgr.s_reward_pos.z = 0;
  200. }
  201. public static List<Vector3> GetFightersPos()
  202. {
  203. return BattleMgr.Instance.GetFightersPos();
  204. }
  205. public static void GenerateDropItems(Vector3 startPos, bool createGold, Vector3 toGoldPos, bool createExp, Vector3 toExpPos, bool createPartnerExp, Vector3 toPartnerExpPos, bool createJobExp, Vector3 toJobExpPos, float goldRange, float expRange, params string[] itemIcons)
  206. {
  207. BattleDropMgr.Instance.GenerateDropItems(startPos, createGold, toGoldPos, createExp, toExpPos, createPartnerExp, toPartnerExpPos, createJobExp, toJobExpPos, itemIcons, goldRange, expRange);
  208. }
  209. public static string GetLevelName(int levelId)
  210. {
  211. Dictionary<string, string> cfg = ConfigMgr.Instance.getLine(levelId, Config.LevelCfgName);
  212. if (cfg != null)
  213. return cfg["Name"];
  214. return "";
  215. }
  216. public static void BeginVersus()
  217. {
  218. BattleMgr.Instance.BeginVersus();
  219. }
  220. public static void SetVersusActors(ActorData[] teamActors, ActorData[] enemyActors, float maxBattlingTime)
  221. {
  222. BattleMgr.Instance.SetVersusActors(teamActors, enemyActors, maxBattlingTime);
  223. }
  224. public static void AddActorToBattle(ActorData actor)
  225. {
  226. BattleMgr.Instance.AddActorToBossBattle(actor);
  227. }
  228. public static void SetActorDead(long actorId)
  229. {
  230. BattleMgr.Instance.SetActorDead(actorId);
  231. }
  232. public static void RemoveActorFromBattle(long actorId)
  233. {
  234. BattleMgr.Instance.RemoveActorFromBossBattle(actorId);
  235. }
  236. public static void SyncWorldBossLife(int life)
  237. {
  238. BattleMgr.Instance.SyncBossLife(life);
  239. }
  240. public static void ShowPreviewTeam()
  241. {
  242. BattleMgr.Instance.ShutDownCurrentBattle();
  243. PreviewTeamMgr.Instance.Show();
  244. }
  245. public static void HidePreviewTeam()
  246. {
  247. PreviewTeamMgr.Instance.Hide();
  248. }
  249. public static void ClearPreviewTeam()
  250. {
  251. if (PreviewTeamMgr.Instance.InPreview)
  252. {
  253. PreviewTeamMgr.Instance.Clean();
  254. }
  255. }
  256. public static void PlayEffect(int uid)
  257. {
  258. if (PreviewTeamMgr.Instance.InPreview)
  259. {
  260. PreviewTeamMgr.Instance.PlayEffect(uid);
  261. }
  262. }
  263. public static void StopEffect(int uid)
  264. {
  265. if (PreviewTeamMgr.Instance.InPreview)
  266. {
  267. PreviewTeamMgr.Instance.StopEffect(uid);
  268. }
  269. }
  270. public static void PlayAnimInDojo(int uid,string animName)
  271. {
  272. if (PreviewTeamMgr.Instance.InPreview)
  273. {
  274. PreviewTeamMgr.Instance.PlayAnim(uid,animName);
  275. }
  276. }
  277. public static void SetBattleFlag(int uid, bool inBattle)
  278. {
  279. if (PreviewTeamMgr.Instance.InPreview)
  280. {
  281. PreviewTeamMgr.Instance.SetBattleFlag(uid, inBattle);
  282. }
  283. }
  284. public static bool InSeason(string seasonStartTime,string seasonEndTime)
  285. {
  286. return DateTimeUtil.ContainTime(seasonStartTime, seasonEndTime, (long)(TimerManager.Instance.serverTime * 0.001f));
  287. }
  288. public static int CalcLeftTime(string endTimeStr)
  289. {
  290. return DateTimeUtil.CaclLeftTime((long)(TimerManager.Instance.serverTime * 0.001f), endTimeStr);
  291. }
  292. public static int CaclLeftTimeWitTimeStamp(string endTimeStr)
  293. {
  294. return DateTimeUtil.CaclLeftTimeWitTimeStamp(TimerManager.Instance.serverTime, endTimeStr);
  295. }
  296. public static void TweemFillAmount(GameObject go,float to, float time)
  297. {
  298. TweenFillAmount.Begin(go, time ,to);
  299. }
  300. public static void SetTweenFillAmount(GameObject go,bool enabled)
  301. {
  302. TweenFillAmount tween = go.GetComponent<TweenFillAmount>();
  303. if(tween != null)
  304. {
  305. tween.enabled = false;
  306. }
  307. }
  308. public static void BeginTweenPosition(GameObject go, Vector3 to)
  309. {
  310. TweenPosition.Begin(go, 0, to);
  311. }
  312. public static void BeginTweenPosition(GameObject go,float duration,Vector3 to,bool worldSpace)
  313. {
  314. TweenPosition.Begin(go, duration,to,worldSpace);
  315. }
  316. public static void BeginTweenRecTransformPos(GameObject go, float duration, Vector3 to)
  317. {
  318. TweenRectTransformPosition.Begin(go, duration, to);
  319. }
  320. public static void AddCollider(GameObject go,Vector3 size,Vector3 center)
  321. {
  322. if (go == null) return;
  323. BoxCollider collider = go.AddComponent<BoxCollider>();
  324. collider.size = size;
  325. collider.center = center;
  326. }
  327. public static CapsuleCollider AddCapsuleCollider(GameObject go, float size, Vector3 center)
  328. {
  329. if (go == null) return null;
  330. CapsuleCollider collider = go.AddComponent<CapsuleCollider>();
  331. collider.height = 2.5f;
  332. collider.center = center;
  333. return collider;
  334. }
  335. public static void SetCreateRoleLuaTable(LuaTable luaTbl)
  336. {
  337. CreateRoleMgr.Instance.SetLuaTable(luaTbl);
  338. }
  339. public static void ClearCreateRole()
  340. {
  341. CreateRoleMgr.Instance.Clear();
  342. }
  343. public static void PlayCamAnim(string animName,bool forward)
  344. {
  345. CreateRoleMgr.Instance.PlayAnim(animName, forward);
  346. }
  347. public static bool IsPlayCamAnimEnd(string animName)
  348. {
  349. return CreateRoleMgr.Instance.IsPlayAnimEnd(animName);
  350. }
  351. public static void ShowCreateRoleCam()
  352. {
  353. CreateRoleMgr.Instance.EnableCam();
  354. }
  355. public static void OpenDof()
  356. {
  357. CreateRoleMgr.Instance.OpenDof();
  358. }
  359. public static void CloseDof()
  360. {
  361. CreateRoleMgr.Instance.CloseDof();
  362. }
  363. public static void PlayCreateRoleTransferEffect()
  364. {
  365. CreateRoleMgr.Instance.PlayTransferEffect();
  366. }
  367. public static void CurStoryOver(int storyId)
  368. {
  369. EventMgr.DispatchEvent<int>(new CoreEvent<int>(ECoreEventType.EID_Dialogue_Finished, storyId));
  370. }
  371. public static void SetGameObjectLayerFrom(GameObject go,string fromLayer,string toLayer)
  372. {
  373. CommonUtil.SetGameObjectLayerFrom(go, fromLayer, toLayer);
  374. }
  375. public static void EnableBattleCam()
  376. {
  377. BattleCamera.Instance.EnableBattleCam();
  378. }
  379. public static void RefreshMinimap(Vector3 center, int mapSizeX,int mapSizeZ,int miniMapSizeX,int miniMapSizeZ, GameObject hero,params RectTransform[] goes)
  380. {
  381. if(goes!=null)
  382. {
  383. BattleMgr.Instance.RefreshMinimap(center, mapSizeX, mapSizeZ, miniMapSizeX, miniMapSizeZ, hero, goes);
  384. }
  385. }
  386. public static float GetOriFighterRotate(int cfgId, int teamId)
  387. {
  388. float oriAngle = 0;
  389. if (BattleMgr.Instance.Battle == null) return oriAngle;
  390. Fighter f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByBaseId(cfgId, (eTeamType)teamId);
  391. if (f != null)
  392. {
  393. oriAngle = f.EulerAngle.y;
  394. }
  395. return oriAngle;
  396. }
  397. public static void DoFighterRotate(int cfgId,int teamId,float angle)
  398. {
  399. if (BattleMgr.Instance.Battle == null) return;
  400. Fighter f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByBaseId(cfgId, (eTeamType)teamId);
  401. if(f!=null)
  402. {
  403. f.SetRotation(Quaternion.Euler(0, angle, 0));
  404. }
  405. }
  406. public static void DoFighterPlayAnimation(int cfgId,int teamId,string animName)
  407. {
  408. if (BattleMgr.Instance.Battle == null || string.IsNullOrEmpty(animName)) return;
  409. Fighter f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByBaseId(cfgId, (eTeamType)teamId);
  410. if(f!=null)
  411. {
  412. f.Ctrl.Animator.Play(animName);
  413. }
  414. }
  415. public static void DoFighterPlayEffect(int cfgId,int teamId,int effectId)
  416. {
  417. if (BattleMgr.Instance.Battle == null || effectId == 0) return;
  418. Fighter f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByBaseId(cfgId, (eTeamType)teamId);
  419. if (f != null)
  420. {
  421. EffectManager.Instance.PlayEffect(effectId, f, f);
  422. }
  423. }
  424. public static void PlayTweenAlpha(GameObject target, float delay = 0, float duration = 1)
  425. {
  426. if (null == target)
  427. return;
  428. TweenAlpha alpha = target.GetComponent<TweenAlpha>();
  429. if (null != alpha)
  430. {
  431. alpha.enabled = true;
  432. alpha.delay = delay;
  433. alpha.duration = duration;
  434. alpha.ResetToBeginning();
  435. }
  436. else
  437. Debug.LogWarningFormat("{0}上没有挂载TweenAlpha脚本", target.name);
  438. }
  439. public static void SkipNewbieBattle()
  440. {
  441. BattleMgr.Instance.OnBattleEnd(2);
  442. }
  443. public static void CloseBgm()
  444. {
  445. MusicMgr.Instance.CleanBGMusic();
  446. }
  447. public static float GetLeftFightingTime()
  448. {
  449. return BattleMgr.Instance.GetLeftFightingTime();
  450. }
  451. public static List<string> GetBattleLog()
  452. {
  453. if (BattleMgr.Instance.Battle == null) return null;
  454. return BattleMgr.Instance.Battle.BattleOut.OutputList;
  455. }
  456. public static string GetBattleRecord(long timeStamp)
  457. {
  458. return BattleMgr.Instance.PopBattleRecord(timeStamp);
  459. }
  460. public static BattleStatistics GetBattleStatistics(BattleMode mode, BattleSubMode subMode)
  461. {
  462. return BattleMgr.Instance.GetBattleStatistics(mode,subMode);
  463. }
  464. public static bool HasStatistics(BattleMode mode, BattleSubMode subMode)
  465. {
  466. return BattleMgr.Instance.HasBattleStatistics(mode,subMode);
  467. }
  468. public static BattleMode CurrentBattleMode()
  469. {
  470. return BattleMgr.Instance.Battle!=null? BattleMgr.Instance.Battle.Mode:BattleMode.None;
  471. }
  472. public static BattleSubMode CurrentBattleSubMode()
  473. {
  474. return BattleMgr.Instance.Battle != null ? BattleMgr.Instance.Battle.SubBattleMode : BattleSubMode.None;
  475. }
  476. public static bool CurrentBattleIsPlayRecord()
  477. {
  478. return BattleMgr.Instance.Battle != null ? BattleMgr.Instance.Battle.IsPlayRecord : false;
  479. }
  480. public static void LoadPreviewActors(ActorData[] actors,LuaTable lbl)
  481. {
  482. AvatarRTMgr.Instance.LoadPreviewActors(actors,lbl);
  483. }
  484. public static string GetTestBattleRecorder()
  485. {
  486. #if UNITY_EDITOR
  487. TextAsset ta = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Content/Test/records.txt");
  488. if (ta == null) return "";
  489. return ta.text;
  490. #else
  491. return "";
  492. #endif
  493. }
  494. public static List<ActorData> GetBattleTeamActors()
  495. {
  496. if (BattleMgr.Instance.Battle == null) return null;
  497. return BattleMgr.Instance.Battle.TeamActors;
  498. }
  499. public static List<ActorData> GetBattleEnemyActors()
  500. {
  501. if (BattleMgr.Instance.Battle == null) return null;
  502. return BattleMgr.Instance.Battle.EnemyActors;
  503. }
  504. public static List<ActorData> GetBattleTeamPlayerActors()
  505. {
  506. if (BattleMgr.Instance.Battle == null) return null;
  507. return BattleMgr.Instance.Battle.TeamPlayerActors;
  508. }
  509. public static List<ActorData> GetBattleEnemyPlayerActors()
  510. {
  511. if (BattleMgr.Instance.Battle == null) return null;
  512. return BattleMgr.Instance.Battle.EnemyPlayerActors;
  513. }
  514. public static ActorData GetFighterActorDataByUid(long uid, int teamSide)
  515. {
  516. if (BattleMgr.Instance.Battle == null) return null;
  517. var f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByID(uid, (eTeamType)teamSide);
  518. if(f != null)
  519. {
  520. return f.Actor;
  521. }
  522. return null;
  523. }
  524. public static float GetFightingTime()
  525. {
  526. return BattleMgr.Instance.FightingTime;
  527. }
  528. public static bool ActorInFighting(long uid)
  529. {
  530. if (BattleMgr.Instance.Battle == null) return false;
  531. var f = BattleMgr.Instance.Battle.FighterMgr.GetFighterByID(uid, eTeamType.Friend);
  532. if (f != null)
  533. {
  534. return true;
  535. }
  536. return false;
  537. }
  538. }