GameMgr.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. using System.Collections.Generic;
  5. using System;
  6. public class GameMgr : SingletonMono<GameMgr>
  7. {
  8. public const string VersionUpdateState = "VersionUpdateState";
  9. public const string LoginState = "LoginState";
  10. public const string LoadingState = "LoadingState";
  11. public const string BattleState = "BattleState";
  12. public static int RandSeed = 100000;
  13. public float mGameSpeed = 1.0f;
  14. private bool mIsSingle = false;
  15. private string mGameVersion;
  16. public string GameVersion
  17. {
  18. get { return mGameVersion; }
  19. }
  20. private string mResVersion;
  21. public string ResVersion
  22. {
  23. get { return mResVersion; }
  24. }
  25. public bool IsSingle
  26. {
  27. get { return mIsSingle; }
  28. }
  29. public float GameSpeed
  30. {
  31. get { return mGameSpeed; }
  32. }
  33. private LuaFunction OnSceneLoaded2Lua;
  34. private LuaFunction mPlayStoryLuaFun;
  35. private static GameMgr instance;
  36. public static new GameMgr Instance
  37. {
  38. get { return instance; }
  39. }
  40. private bool mIsStartDungeon = false;
  41. public bool InStartDungeon
  42. {
  43. get { return mIsStartDungeon; }
  44. }
  45. private bool mIsEditorMode = false;
  46. public bool IsEditorMode
  47. {
  48. get { return mIsEditorMode; }
  49. }
  50. public GameObject CreateMaleRoot
  51. {
  52. get { return CreateRoleMgr.Instance.MaleRoot; }
  53. }
  54. public GameObject CreateFemaleRoot
  55. {
  56. get { return CreateRoleMgr.Instance.FemaleRoot; }
  57. }
  58. public GameObject CreateShowRoot
  59. {
  60. get { return CreateRoleMgr.Instance.ShowRoot; }
  61. }
  62. public eNetType NetStatus
  63. {
  64. get { return DeviceInfo.GetNetType(); }
  65. }
  66. private MainCharacter mCharacterInfo;
  67. public MainCharacter CharacterInfo
  68. {
  69. get { return mCharacterInfo; }
  70. }
  71. public string DeviceId
  72. {
  73. get { return DeviceInfo.m_deviceId; }
  74. }
  75. public string CurLangKey
  76. {
  77. get { return ConfigMgr.CurLangKey; }
  78. set { ConfigMgr.CurLangKey = value; }
  79. }
  80. public bool IsMobileDevice
  81. {
  82. get
  83. {
  84. #if UNITY_EDITOR
  85. return false;
  86. #elif UNITY_ANDROID || UNITY_IPHONE
  87. return true;
  88. #else
  89. return false;
  90. #endif
  91. }
  92. }
  93. public bool IsBadDevice
  94. {
  95. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.BAD_DEVICE; }
  96. }
  97. public bool IsNormalDevice
  98. {
  99. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE; }
  100. }
  101. public bool IsGoodDevice
  102. {
  103. get { return DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE; }
  104. }
  105. NPack.MersenneTwister rand = null;
  106. private SceneType mEnterSceneType = 0;
  107. private BattleSubMode mbossMode = BattleSubMode.None;
  108. private bool bInited = false;
  109. private void Awake()
  110. {
  111. #if !UNITY_EDITOR
  112. DebugHelper.LogLevel = LogLevel.Error;
  113. #endif
  114. instance = this;
  115. DontDestroyOnLoad(this.gameObject);
  116. rand = new NPack.MersenneTwister(int.MaxValue);
  117. }
  118. bool bPaused = false;
  119. float mPausedTime = 0;
  120. private void OnApplicationFocus(bool focus)
  121. {
  122. if (bPaused && focus)
  123. {
  124. float pausePassedTime = Time.realtimeSinceStartup - mPausedTime;
  125. // if (pausePassedTime >= 300) //切后台5分钟网络重新连接
  126. // {
  127. // NetworkMgr.Instance.Resume();
  128. // }
  129. }
  130. }
  131. private void OnApplicationPause(bool pause)
  132. {
  133. bPaused = pause;
  134. if (bPaused)
  135. {
  136. mPausedTime = Time.realtimeSinceStartup;
  137. GameSettings.Instance.Save();
  138. }
  139. }
  140. protected override void OnApplicationQuit()
  141. {
  142. GameSettings.Instance.Save();
  143. base.OnApplicationQuit();
  144. }
  145. private void InitSDK_E()
  146. {
  147. StartCoroutine(initSdk_E());
  148. }
  149. private IEnumerator initSdk_E()
  150. {
  151. SDKMgr.Instance.Init();
  152. yield return null;
  153. }
  154. private void Start()
  155. {
  156. if (bInited) return;
  157. SDKMgr.Instance.ReportActivation(SDKMgr.Instance.GetInt64TimeStamp());
  158. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  159. QualitySettings.vSyncCount = 0;
  160. RegisterEvents();
  161. #if UNITY_IOS && !UNITY_EDITOR
  162. InitSDK_E();
  163. #endif
  164. //InitSDK_E();
  165. StartDetector();
  166. DeviceInfo.GetDeviceState();
  167. if (DeviceInfo.m_DeviceState <= DeviceInfo.eDeviceState.NORMAL_DEVICE)
  168. {
  169. Application.targetFrameRate = 60;
  170. BattleMgr.c_updateFPS = 15;
  171. }
  172. else
  173. {
  174. Application.targetFrameRate = 60;
  175. BattleMgr.c_updateFPS = 30;
  176. }
  177. Input.multiTouchEnabled = false;
  178. //启用日志
  179. StartLog();
  180. //读取Apk设置文件
  181. //yield return StartCoroutine(ReadVersionFile());
  182. //InitData();
  183. //InitBuildConfig();
  184. //InitData();
  185. LaunchLoadMgr lanuchLoadMgr = new LaunchLoadMgr();
  186. lanuchLoadMgr.StartLaunch(OnLanuchLoadCompleted, OnAssetMapInitComplete);
  187. //检测非法修改
  188. AntiCheatMgr.Instance.GetOrCreateAnti(EnAntiCheatType.enSystemTime).Init(AntiCheatCfg.c_fTimeSysteCheckTime);
  189. AntiCheatMgr.Instance.GetOrCreateAnti(EnAntiCheatType.enScaleTime).Init(AntiCheatCfg.c_fTimeScaleCheckTime);
  190. bInited = true;
  191. }
  192. public void DoTaskByCorutine(Func<IEnumerator> func)
  193. {
  194. StartCoroutine(func.Invoke());
  195. }
  196. private void OnLanuchLoadCompleted(bool success)
  197. {
  198. EnterLuaLogin(false);
  199. }
  200. private void OnAssetMapInitComplete()
  201. {
  202. CheckVersion();
  203. InitBugly();
  204. }
  205. private void CheckVersion()
  206. {
  207. mGameVersion = Application.version;
  208. int versionCodeInt = 0;// Wenting.Lebian.LeBianSDK.instance.GetResVerCode();
  209. VersionCode versionCode1;
  210. if (versionCodeInt <= 0)
  211. {
  212. versionCode1 = mGameVersion;
  213. }
  214. else
  215. {
  216. versionCode1 = (VersionCode)(uint)versionCodeInt;
  217. }
  218. VersionCode versionCode2 = AssetsMgr.Instance.resVersionCode;
  219. if (versionCode1 < versionCode2)
  220. {
  221. mResVersion = versionCode2.ToString();
  222. }
  223. else
  224. {
  225. mResVersion = versionCode1.ToString();
  226. }
  227. }
  228. void InitBugly()
  229. {
  230. #if BUGLY
  231. #if UNITY_IPHONE || UNITY_IOS
  232. string channel = ""; //Wenting.Lebian.LeBianSDK.instance.GetClientChId();
  233. BuglyAgent.ConfigDefault(channel, mResVersion, string.Empty, 0);
  234. BuglyAgent.InitWithAppId("3e7f97a53b");
  235. BuglyAgent.EnableExceptionHandler();
  236. #elif UNITY_ANDROID
  237. string channel = "";//Wenting.Lebian.LeBianSDK.instance.GetClientChId();
  238. BuglyAgent.ConfigDefault(channel, mResVersion, string.Empty, 0);
  239. #if LEBIAN_YUN_CLIENT
  240. BuglyAgent.InitWithAppId("96a729256a");
  241. #else
  242. BuglyAgent.InitWithAppId("c7177b1ae7");
  243. #endif
  244. BuglyAgent.EnableExceptionHandler();
  245. #endif
  246. #endif
  247. }
  248. public void StartDetector()
  249. {
  250. DataCheatingDetector.StartDetection(OnDataCheatingDetected);
  251. SpeedHackDetector.StartDetection(OnSpeedHackDetected);
  252. }
  253. bool speedHackDetected = false;
  254. bool dataCheatingDetected = false;
  255. void OnDataCheatingDetected()
  256. {
  257. dataCheatingDetected = true;
  258. //LocalPlayerInfo.SendCheat(ECheatType.CHEAT_DATA);
  259. DebugHelper.LogError("Data Cheating Detected!");
  260. }
  261. void OnSpeedHackDetected()
  262. {
  263. speedHackDetected = true;
  264. //LocalPlayerInfo.SendCheat(ECheatType.CHEAT_SPEED);
  265. DebugHelper.LogError("Speed hack Detected!");
  266. }
  267. private void Update()
  268. {
  269. AntiCheatMgr.Instance.Update();
  270. ResourceMgr.Instance.Update();
  271. TimerManager.Instance.Update();
  272. NetworkMgr.Instance.Update();
  273. UpdateFPS();
  274. #if UNITY_EDITOR || OPENGM
  275. if (Input.GetMouseButtonDown(0))
  276. {
  277. Rect validRect = SafeRectCheck.Instance.safeArea;
  278. validRect.width = validRect.height * 0.025f;
  279. validRect.y = validRect.height + validRect.y - validRect.width;
  280. validRect.height = validRect.width;
  281. if (validRect.Contains(Input.mousePosition))
  282. {
  283. mouseDown = true;
  284. dragged = false;
  285. mouseDownTime = Time.time;
  286. mouseDownPos = Input.mousePosition;
  287. }
  288. else
  289. {
  290. clickCount = 0;
  291. }
  292. }
  293. if (mouseDown && !dragged && Vector3.Distance(Input.mousePosition, mouseDownPos) >= 5.0f)
  294. {
  295. dragged = true;
  296. }
  297. if (Input.GetMouseButtonUp(0))
  298. {
  299. if (mouseDown)
  300. {
  301. mouseDown = false;
  302. if (dragged)
  303. {
  304. clickCount = 0;
  305. }
  306. else
  307. {
  308. dragged = false;
  309. Rect validRect = SafeRectCheck.Instance.safeArea;
  310. validRect.width = validRect.height * 0.025f;
  311. validRect.y = validRect.height + validRect.y - validRect.width;
  312. validRect.height = validRect.width;
  313. if (Time.time - mouseDownTime < 2 && validRect.Contains(Input.mousePosition))
  314. {
  315. clickCount = clickCount + 1;
  316. if (clickCount >= 2)
  317. {
  318. clickCount = 0;
  319. var pLuaState = LuaMgr.GetMainState();
  320. if (null != pLuaState)
  321. {
  322. LuaMgr.GetMainState().DoString("local curUIId = ManagerContainer.LuaUIMgr:GetCurUIId()\nManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIGM, nil, curUIId)");
  323. }
  324. }
  325. }
  326. else
  327. {
  328. clickCount = 0;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. // private void OnGUI() {
  335. // Rect validRect = SafeRectCheck.Instance.safeArea;
  336. // validRect.width = validRect.height * 0.025f;
  337. // validRect.y = SafeRectCheck.Instance.screenHeight - validRect.height - validRect.y;
  338. // validRect.height = validRect.width;
  339. // GUI.DrawTexture(validRect, Texture2D.whiteTexture);
  340. // }
  341. private int clickCount = 0;
  342. private bool mouseDown = false;
  343. private bool dragged = false;
  344. private float mouseDownTime = 0f;
  345. private Vector2 mouseDownPos = Vector2.zero;
  346. #else
  347. }
  348. #endif
  349. protected override void Dispose()
  350. {
  351. base.Dispose();
  352. }
  353. private const float m_FrameUpdateInterval = 0.5f;
  354. private float m_FrameUpdateTime = 0f;
  355. private float m_MaxTimeAccumulator = 0;
  356. private int m_FrameCounter = 0;
  357. private int m_MaxFrameTime = 0;
  358. public int maxFrameTime
  359. {
  360. get { return m_MaxFrameTime; }
  361. }
  362. private void UpdateFPS()
  363. {
  364. float deltaTime = Time.deltaTime;
  365. if (deltaTime > 0)
  366. {
  367. m_FrameUpdateTime -= deltaTime;
  368. m_MaxTimeAccumulator += Time.timeScale / deltaTime;
  369. m_FrameCounter++;
  370. }
  371. if (m_FrameUpdateTime <= 0)
  372. {
  373. m_MaxFrameTime = Mathf.RoundToInt(m_MaxTimeAccumulator / m_FrameCounter);
  374. m_FrameUpdateTime = m_FrameUpdateInterval;
  375. m_MaxTimeAccumulator = 0;
  376. m_FrameCounter = 0;
  377. }
  378. }
  379. void EnterLuaLogin(bool relogin)
  380. {
  381. if (mCharacterInfo == null)
  382. {
  383. mCharacterInfo = new MainCharacter();
  384. }
  385. LuaMgr.Instance.EnterLogin(relogin);
  386. }
  387. void DisposeCharactorInfo()
  388. {
  389. mCharacterInfo.Dispose();
  390. mCharacterInfo = null;
  391. }
  392. void OnLogout(CoreEvent<int> ce)
  393. {
  394. }
  395. public void SetOnSceneLoadedLuaFunc(LuaFunction func)
  396. {
  397. OnSceneLoaded2Lua = func;
  398. }
  399. public void ReLogin()
  400. {
  401. ShutDownBattle(false);//清空战斗数据 逻辑 (等待loading界面完成 清空战斗实例)
  402. DisposeCharactorInfo();
  403. EnterLuaLogin(true);
  404. }
  405. public void ReLoginClearData()
  406. {
  407. ClearMainCharacter();
  408. ActorDataMgr.Instance.Clear();
  409. DisposeBattle();//销毁战斗实例
  410. BattleMgr.Instance.ClearSkillDirty();//销毁待更新技能
  411. SceneMgr.Instance.LoadMainScene("relogin");
  412. }
  413. void ClearMainCharacter()
  414. {
  415. if (mCharacterInfo != null)
  416. {
  417. mCharacterInfo.Clear();
  418. }
  419. }
  420. public void SetLuaPlayStoryFunc(LuaFunction func)
  421. {
  422. mPlayStoryLuaFun = func;
  423. }
  424. public void LoadCurrentBattle()
  425. {
  426. GameStateCtrl.Instance.GotoState(LoadingState);
  427. }
  428. public void CloseLoading()
  429. {
  430. if (BattleMgr.Instance.Battle == null) return;
  431. if (BattleMgr.Instance.Battle.IsBossBattle && BattleMgr.Instance.Battle.SubBattleMode == BattleSubMode.NewbieBoss)
  432. {
  433. BattleMgr.Instance.StartStoryScript();
  434. }
  435. }
  436. public void SetTeamData(LuaTable teamParam, LuaTable tbIsForce)
  437. {
  438. if (mCharacterInfo != null)
  439. {
  440. mCharacterInfo.SetTeamActors(teamParam);
  441. bool bIsForce = false;//是否强制同步
  442. if (tbIsForce[1] != null)
  443. {
  444. bool.TryParse(tbIsForce[1].ToString(), out bIsForce);
  445. }
  446. BattleMgr.Instance.SyncTeams(bIsForce);
  447. }
  448. }
  449. public void RefreshTeamData()
  450. {
  451. for (int idx = 0; idx < mCharacterInfo.TeamActors.Count; idx++)
  452. {
  453. ActorDataMgr.Instance.RefreshFellowActorData(mCharacterInfo.TeamActors[idx].ID, mCharacterInfo.TeamActors[idx].BaseId);
  454. }
  455. }
  456. public void UpdateTeamSkills(LuaTable skillsParam)
  457. {
  458. if (mCharacterInfo != null)
  459. {
  460. BattleMgr.Instance.UpdateTeamSkills(skillsParam);
  461. }
  462. }
  463. /// <summary>
  464. /// 完成角色创建
  465. /// </summary>
  466. /// <param name="modelGo"></param>
  467. public void CreateRoleViewComplete(GameObject modelGo)
  468. {
  469. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, false, false);
  470. }
  471. /// <summary>
  472. /// 角色形象更新完成
  473. /// </summary>
  474. /// <param name="modelGo"></param>
  475. public void RefreshRoleViewComplete(GameObject modelGo, bool isNew)
  476. {
  477. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, true, isNew);
  478. }
  479. /// <summary>
  480. /// 时装或者其它功能更新了角色形象,战斗内需要自行调用形象更新 参数 是否为转职后的新角色
  481. /// </summary>
  482. public void NotifyRefreshRoleView()
  483. {
  484. BattleMgr.Instance.NotifyRefreshRoleView();
  485. }
  486. public void SetMapLevelId(int mapId, int levelId)
  487. {
  488. if (mCharacterInfo == null) return;
  489. if (mCharacterInfo.CurMapId != mapId)
  490. {
  491. mCharacterInfo.CurMapId = mapId;
  492. mCharacterInfo.CurLevelId = levelId;
  493. }
  494. else
  495. {
  496. if (mCharacterInfo.CurLevelId != levelId)
  497. {
  498. mCharacterInfo.CurLevelId = levelId;
  499. BattleMgr.Instance.EnterNextBattle();
  500. }
  501. }
  502. //BattleMgr.Instance?.SetMapLevelId(mapId,levelId);
  503. Debug.Log($"=========SetMapLevelId============={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  504. }
  505. public void PreloadCreateRoleScene()
  506. {
  507. mEnterSceneType = SceneType.CreateRoleScene;
  508. CreateRoleMgr.Instance.LoadScene();
  509. }
  510. public void GotoLogin()
  511. {
  512. BattleMgr.Instance.ShutDownCurrentBattle();
  513. }
  514. //进入公会大厅
  515. public void PreloadGuildLobby()
  516. {
  517. mEnterSceneType = SceneType.GuildLobbyScene;
  518. BattleMgr.Instance.ShutDownCurrentBattle();
  519. GuildLobbyMgr.Instance.EnterGuildLobby();
  520. }
  521. public void PreloadAllFellows(ActorData[] actors)
  522. {
  523. if (actors == null || actors.Length == 0) return;
  524. for (int idx = 0; idx < actors.Length; idx++)
  525. {
  526. var actor = actors[idx];
  527. if (actor != null)
  528. {
  529. BattlePrepareManager.Instance.PrecacheModel(actor.AvatarData.prefab, actor.AType);
  530. BattlePrepareManager.Instance.PrecacheAnimatorCtrl(actor.ShowAnimCtrlName);
  531. }
  532. }
  533. }
  534. public void PreloadBattle(LuaTable teamParam)
  535. {
  536. mEnterSceneType = SceneType.NormalBattleScene;
  537. GuildLobbyMgr.Instance.Clear();
  538. mCharacterInfo.SetTeamActors(teamParam);
  539. LogicBattleStartInfo startInfo = new LogicBattleStartInfo(mCharacterInfo.CurMapId, mCharacterInfo.CurLevelId);
  540. Debug.Log($"============PreloadBattle=========={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  541. BattleMgr.Instance.ShutDownCurrentBattle();
  542. BattleMgr.Instance.StartBattle(startInfo);
  543. }
  544. public List<ActorData> StartNewbieBossBattle(int sceneId, int roleSex)
  545. {
  546. mEnterSceneType = SceneType.BossBattleScene;
  547. BattleMgr.Instance.ShutDownCurrentBattle();
  548. return BattleMgr.Instance.StartNewbieBossBattle(sceneId, roleSex);
  549. }
  550. public void PreloadViewTeam(ActorData[] teamActors, string sceneName, LuaTable tbl)
  551. {
  552. mEnterSceneType = SceneType.TowerBattleScene;
  553. BattleMgr.Instance.ShutDownCurrentBattle();
  554. PreviewTeamMgr.Instance.LoadPreviewActorsAndScene(teamActors, sceneName, tbl);
  555. }
  556. public void PreloadVersusBattle(BattleSubMode mode,
  557. ActorData[] teamActors,
  558. ActorData[] enemyActors,
  559. string sceneName,
  560. float maxFightingTime,
  561. BattleEndCondition[] endConds,
  562. GvGMark[] OurMarks,
  563. GvGMark[] EnemyMarks,
  564. bool IsPresspoint,
  565. int nPresspoint)
  566. {
  567. mEnterSceneType = SceneType.TowerBattleScene;
  568. BattleMgr.Instance.ShutDownCurrentBattle();
  569. BattleMgr.Instance.StartVersusBattle(mode, teamActors, enemyActors, sceneName, maxFightingTime, endConds, OurMarks, EnemyMarks, IsPresspoint, nPresspoint);
  570. }
  571. public void PreloadBossBattle(ActorData[] actors,
  572. int bossId,
  573. int sceneId,
  574. BattleEndCondition[] endConds)
  575. {
  576. mEnterSceneType = SceneType.BossBattleScene;
  577. BattleMgr.Instance.ShutDownCurrentBattle();
  578. BattleMgr.Instance.StartBossBattle(actors, bossId, sceneId, endConds);
  579. }
  580. public bool PreloadTimeBattle(LuaTable luaTbl,
  581. BattleSubMode mode,
  582. float maxFightingTime,
  583. string sceneName,
  584. string bgmMusic,
  585. ActorData[] ourActors,
  586. ActorData[] enemyActors,
  587. ServerFighterParam[] fighterParams,
  588. BattleEndCondition[] endConds,
  589. ValType[] factors,
  590. int nRestoreSp,
  591. GvGMark[] OurMarks,
  592. GvGMark[] EnemyMarks
  593. )
  594. {
  595. mEnterSceneType = SceneType.TimeBattleScene;
  596. return BattleMgr.Instance.StartTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, ourActors, enemyActors, fighterParams, endConds, factors, nRestoreSp, OurMarks, EnemyMarks);
  597. }
  598. public bool ReplayTimeBattle(LuaTable luaTbl, BattleSubMode mode, float maxFightingTime, string sceneName, string bgmMusic, string battleRecordStr)
  599. {
  600. mEnterSceneType = SceneType.TimeBattleScene;
  601. return BattleMgr.Instance.ReplayTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, battleRecordStr);
  602. }
  603. public void ForceStopBattle()
  604. {
  605. BattleMgr.Instance.ForceStopBattle();
  606. }
  607. //关闭清空战斗
  608. public void ShutDownBattle(bool bIsDispose = true)
  609. {
  610. BattleMgr.Instance.ShutDownCurrentBattle(bIsDispose);
  611. }
  612. //销毁战斗
  613. public void DisposeBattle()
  614. {
  615. BattleMgr.Instance.DisposeCurrentBattle();
  616. }
  617. public void SetGameSpeed(float speed)
  618. {
  619. BattleMgr.Instance.SetSpeedUp(speed);
  620. }
  621. public void SaveGameSpeed(float speed)
  622. {
  623. mGameSpeed = speed;
  624. BattleMgr.Instance.SetSpeedUp(speed);
  625. }
  626. public float GetGameSpeed()
  627. {
  628. return BattleMgr.speed_up_rate;
  629. }
  630. private void OnLoadComplete(CoreEvent<bool> ce)
  631. {
  632. #if USE_LUA
  633. if (OnSceneLoaded2Lua != null)
  634. {
  635. OnSceneLoaded2Lua.Call(this, mEnterSceneType, BattleMgr.Instance.Battle != null ? BattleMgr.Instance.Battle.SubBattleMode : BattleSubMode.None);
  636. }
  637. #else
  638. EnterBattleState();
  639. #endif
  640. }
  641. public int Random(int min, int max)
  642. {
  643. return max > min ? rand.Next(min, max) : min;
  644. }
  645. public uint CalcPassedTime(long timeStr)
  646. {
  647. ulong time = (ulong)timeStr;
  648. time = TimerManager.Instance.serverTime - time;
  649. if (time > uint.MaxValue)
  650. {
  651. return uint.MaxValue;
  652. }
  653. else if (time < 0)
  654. {
  655. return 0;
  656. }
  657. else
  658. {
  659. return (uint)time;
  660. }
  661. }
  662. public void QuitGame(bool isShowView = false)
  663. {
  664. //需要打开退出页面
  665. if (isShowView)
  666. {
  667. //如果sdk有就打开
  668. if (SDKMgr.Instance.CheckHasModul(SDKModulType.EXIT_VIEW))
  669. {
  670. SDKMgr.Instance.OpenModul(SDKModulType.EXIT_VIEW);
  671. }
  672. else
  673. {
  674. if (!SDKMgr.Instance.Exit())
  675. {
  676. KillApplication();
  677. }
  678. }
  679. }
  680. else
  681. {
  682. if (!SDKMgr.Instance.Exit())
  683. {
  684. KillApplication();
  685. }
  686. }
  687. }
  688. private void KillApplication()
  689. {
  690. // 游戏需要保存数据的在这里保存
  691. GameSettings.Instance.Save();
  692. if (SDKMgr.Instance.Quit()) return;
  693. #if UNITY_EDITOR
  694. UnityEditor.EditorApplication.isPlaying = false;
  695. #else
  696. Application.Quit();
  697. #endif
  698. }
  699. public void PlayDialog(int dialogueType, int dialogueId = 0)
  700. {
  701. if (mPlayStoryLuaFun != null)
  702. mPlayStoryLuaFun.Call(this, dialogueType, dialogueId);
  703. }
  704. public void RequestHttpServer(string url, LuaFunction luaFunc_)
  705. {
  706. StartCoroutine(DoRequestHttpServer(url, luaFunc_));
  707. }
  708. IEnumerator DoRequestHttpServer(string url, LuaFunction luaFunc_)
  709. {
  710. if (string.IsNullOrEmpty(url))
  711. {
  712. yield break;
  713. }
  714. WWW httpReq = new WWW(url);
  715. yield return httpReq;
  716. if (!string.IsNullOrEmpty(httpReq.error))
  717. {
  718. DebugHelper.LogError("DoRequestHttpServer failed:{0}[url = {1}]", httpReq.error,url);
  719. httpReq.Dispose();
  720. httpReq = null;
  721. yield break;
  722. }
  723. string content = httpReq.text;
  724. httpReq.Dispose();
  725. httpReq = null;
  726. if (luaFunc_ != null)
  727. {
  728. luaFunc_.Call(content);
  729. }
  730. }
  731. public void CleanUnusedAssets()
  732. {
  733. StartCoroutine(UnloadAssets_Coroutine());
  734. }
  735. public void OpenUrl(string url)
  736. {
  737. if (string.IsNullOrEmpty(url)) return;
  738. SDKMgr.Instance.OpenWebview(url);
  739. //Application.OpenURL(url);
  740. }
  741. public void EnableAntiAliasing()
  742. {
  743. if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  744. {
  745. QualitySettings.antiAliasing = 2;
  746. }
  747. else if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE)
  748. {
  749. QualitySettings.antiAliasing = 4;
  750. }
  751. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  752. }
  753. public void DisableAntiAliasing()
  754. {
  755. QualitySettings.antiAliasing = 0;
  756. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  757. }
  758. IEnumerator UnloadAssets_Coroutine()
  759. {
  760. yield return 0;
  761. yield return Resources.UnloadUnusedAssets();
  762. System.GC.Collect();
  763. }
  764. #region inner_methods
  765. private void RegisterEvents()
  766. {
  767. EventMgr.AddEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  768. EventMgr.AddEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  769. //EventMgr.AddEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  770. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  771. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  772. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  773. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  774. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  775. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  776. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  777. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  778. SDKEventUtil.AddListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  779. SDKEventUtil.AddListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  780. SDKEventUtil.AddListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  781. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  782. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  783. SDKEventUtil.AddListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  784. SDKEventUtil.AddListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  785. SDKEventUtil.AddListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  786. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  787. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  788. SDKEventUtil.AddListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  789. }
  790. private void UnRegisterEvents()
  791. {
  792. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  793. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  794. //EventMgr.RemoveEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  795. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  796. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  797. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  798. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  799. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  800. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  801. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  802. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  803. SDKEventUtil.RemoveListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  804. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  805. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  806. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  807. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  808. SDKEventUtil.RemoveListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  809. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  810. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  811. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  812. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  813. SDKEventUtil.RemoveListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  814. }
  815. private void StartLog()
  816. {
  817. DebugHelper debugHelper = this.gameObject.GetComponent<DebugHelper>();
  818. if (debugHelper == null)
  819. {
  820. this.gameObject.AddComponent<DebugHelper>();
  821. }
  822. DebugHelper.enableLog = true;
  823. DebugHelper.BeginLogs();
  824. }
  825. private IEnumerator ReadVersionFile()
  826. {
  827. string path = FileSystem.LocalPackagePath;
  828. #if !UNITY_STANDALONE_WIN
  829. #if UNITY_EDITOR //editor
  830. path = string.Format("{0}/", Application.dataPath);
  831. #elif UNITY_ANDROID && !UNITY_EDITOR//android release;
  832. path = FileSystem.LocalPackagePath;
  833. #elif UNITY_IPHONE && !UNITY_EDITOR //ios release;
  834. path = FileSystem.LocalPackagePath;
  835. #else //pc release;
  836. path = Application.dataPath;
  837. #endif
  838. #else
  839. path = FileSystem.LocalPackagePath;
  840. #endif
  841. //热更地址从新整合的配置中提取
  842. string UpdateFilePath = string.Format("{0}appbuildconfig.xml", path);
  843. WWW www = new WWW(UpdateFilePath);
  844. DebugHelper.Log("ReadUpdateFile : LocalPackagePath {0}", UpdateFilePath);
  845. yield return www;
  846. if (www.error == null)
  847. {
  848. DebugHelper.Log("ReadUpdateFile : {0}", www.text);
  849. www.Dispose();
  850. www = null;
  851. }
  852. else
  853. {
  854. DebugHelper.LogError("ReadUpdateFile {0} , {1}", www.error, UpdateFilePath);
  855. www.Dispose();
  856. www = null;
  857. }
  858. }
  859. #endregion
  860. #region ACCOUNT_SDK
  861. //private SDKMgr SdkManager = SDKMgr.Instance;
  862. private LuaFunction mSdkInitedLuaCB = null;
  863. private LuaFunction mSdkLoginedLuaCB = null;
  864. private LuaFunction mSdkLogoutLuaCB = null;
  865. private LuaFunction mSdkPayLuaCB = null;
  866. private LuaFunction msdkQuestionLuaCB = null;
  867. private LuaFunction mSdkCanEnterServerJudgeCB = null;
  868. public void SdkInitFunc(LuaFunction func)
  869. {
  870. mSdkInitedLuaCB = func;
  871. //mSdkInitedLuaCB.Call(this, true, "");
  872. }
  873. public void SdkLoginFunc(LuaFunction func)
  874. {
  875. mSdkLoginedLuaCB = func;
  876. }
  877. public void SdkLogoutFunc(LuaFunction func)
  878. {
  879. mSdkLogoutLuaCB = func;
  880. }
  881. public void SdkPayFunc(LuaFunction func)
  882. {
  883. mSdkPayLuaCB = func;
  884. }
  885. public void SdkQuestionFunc(LuaFunction func)
  886. {
  887. msdkQuestionLuaCB = func;
  888. }
  889. public void SdkCanEnterServerJudgeFunc(LuaFunction func)
  890. {
  891. mSdkCanEnterServerJudgeCB = func;
  892. }
  893. public void SdkInit()
  894. {
  895. SDKMgr.Instance.Init();
  896. }
  897. public void SdkLogin()
  898. {
  899. SDKMgr.Instance.Login();
  900. }
  901. public void SdkPay(int goodsId, string goodsName, string goodsDesc,
  902. int count, float amount,
  903. string cpOrderId, string extrasParams)
  904. {
  905. int decimalVal = 1000;
  906. int tempVal = Mathf.FloorToInt((amount + 0.0005f) * decimalVal);
  907. amount = (float)tempVal / decimalVal;
  908. if (string.IsNullOrEmpty(goodsName))
  909. {
  910. goodsName = "初心者";
  911. }
  912. SDKMgr.Instance.Pay(goodsId, goodsName, goodsDesc,
  913. count, amount,
  914. cpOrderId, extrasParams);
  915. //Debug.Log($"goodsId = {goodsId} ==== goodsName = {goodsName} ===== goodsDesc = {goodsDesc} ===== count = {count} ===== amount = {amount} ====== cpOrderId = {cpOrderId} ==== extrasParams = {extrasParams}");
  916. }
  917. public void SdkLogout()
  918. {
  919. SDKMgr.Instance.Logout();
  920. }
  921. public void SdkExit()
  922. {
  923. }
  924. public void SdkShowToolbar()
  925. {
  926. //int ret = quicksdk.QuickSDK.getInstance().showToolBar(quicksdk.ToolbarPlace.QUICK_SDK_TOOLBAR_MID_LEFT);
  927. //DebugHelper.LogError("SdkShowToolbar:" + ret);
  928. }
  929. public void SdkHideToolBar()
  930. {
  931. //SdkManager.SdkHideToolBar();
  932. }
  933. private void OnSdkInitFail(object obj)
  934. {
  935. if (mSdkInitedLuaCB != null)
  936. {
  937. mSdkInitedLuaCB.Call(this, false, SDKMgr.Instance.GetSDKName());
  938. }
  939. }
  940. private void OnSdkInitSuccess(object obj)
  941. {
  942. if (mSdkInitedLuaCB != null)
  943. {
  944. mSdkInitedLuaCB.Call(this, true, SDKMgr.Instance.GetSDKName());
  945. }
  946. }
  947. private void OnSdkLoginFail(object obj)
  948. {
  949. if (mSdkLoginedLuaCB != null)
  950. {
  951. mSdkLoginedLuaCB.Call(this, false, "", "", false);
  952. SDKMgr.Instance.ReportRoleEnterFail();
  953. }
  954. }
  955. private void OnSdkLoginSuccess(object obj)
  956. {
  957. if (mSdkLoginedLuaCB != null)
  958. {
  959. if (obj != null)
  960. {
  961. UserInfo userInfo = (UserInfo)obj;
  962. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, false);
  963. SDKMgr.Instance.ReportIdentification(SDKMgr.Instance.GetInt64TimeStamp());
  964. }
  965. else
  966. {
  967. mSdkLoginedLuaCB.Call(this, true, "", "", false);
  968. }
  969. }
  970. }
  971. private void OnSdkSwitchAccountFailed(object obj)
  972. {
  973. if (mSdkLoginedLuaCB != null)
  974. {
  975. UserInfo userInfo = (UserInfo)obj;
  976. mSdkLoginedLuaCB.Call(this, false, userInfo.uid, userInfo.token, true);
  977. }
  978. }
  979. private void OnSdkSwitchAccountSuccess(object obj)
  980. {
  981. if (mSdkLoginedLuaCB != null)
  982. {
  983. UserInfo userInfo = (UserInfo)obj;
  984. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, true);
  985. }
  986. }
  987. private void OnSdkLogoutSuccess(object obj)
  988. {
  989. if (mSdkLogoutLuaCB != null)
  990. {
  991. mSdkLogoutLuaCB.Call(this);
  992. }
  993. }
  994. private void OnSdkExit(object obj)
  995. {
  996. KillApplication();
  997. }
  998. private void OnSdkPaySuccess(object obj)
  999. {
  1000. if (mSdkPayLuaCB != null)
  1001. {
  1002. mSdkPayLuaCB.Call(this, true);
  1003. }
  1004. }
  1005. private void OnSdkPayCancel(CoreEvent<bool> ce)
  1006. {
  1007. if (mSdkPayLuaCB != null)
  1008. {
  1009. mSdkPayLuaCB.Call(this, false);
  1010. }
  1011. }
  1012. private void OnSdkPayFailed(object obj)
  1013. {
  1014. if (mSdkPayLuaCB != null)
  1015. {
  1016. mSdkPayLuaCB.Call(this, false);
  1017. }
  1018. }
  1019. private void OnSdkQuestionRewardResult(CoreEvent<bool> ce)
  1020. {
  1021. if (msdkQuestionLuaCB != null)
  1022. {
  1023. msdkQuestionLuaCB.Call(this, true);
  1024. }
  1025. }
  1026. private void OnSdkCanEnterServerJudge(object obj)
  1027. {
  1028. if (mSdkCanEnterServerJudgeCB != null)
  1029. {
  1030. mSdkCanEnterServerJudgeCB.Call(this, "" + obj);
  1031. }
  1032. }
  1033. #endregion
  1034. #region 新加
  1035. private Dictionary<int, int> mDifAttrs = new Dictionary<int, int>();
  1036. public void ClearDifAttr()
  1037. {
  1038. mDifAttrs.Clear();
  1039. }
  1040. public void AddDifAttr(int type,int attr)
  1041. {
  1042. mDifAttrs.Add(type, attr);
  1043. }
  1044. public void AddDifAttrs(Dictionary<object, object> difAttrs)
  1045. {
  1046. foreach (var item in difAttrs)
  1047. {
  1048. mDifAttrs.Add((int)item.Key,(int)item.Value);
  1049. }
  1050. }
  1051. public Dictionary<int, int> GetDifAttrs()
  1052. {
  1053. return mDifAttrs;
  1054. }
  1055. #endregion
  1056. }