GameMgr.cs 35 KB

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