GameMgr.cs 36 KB

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