GameMgr.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. CheckIsReturnBtn();
  275. #if UNITY_EDITOR || OPENGM
  276. if (Input.GetMouseButtonDown(0))
  277. {
  278. Rect validRect = SafeRectCheck.Instance.safeArea;
  279. validRect.width = validRect.height * 0.025f;
  280. validRect.y = validRect.height + validRect.y - validRect.width;
  281. validRect.height = validRect.width;
  282. if (validRect.Contains(Input.mousePosition))
  283. {
  284. mouseDown = true;
  285. dragged = false;
  286. mouseDownTime = Time.time;
  287. mouseDownPos = Input.mousePosition;
  288. }
  289. else
  290. {
  291. clickCount = 0;
  292. }
  293. }
  294. if (mouseDown && !dragged && Vector3.Distance(Input.mousePosition, mouseDownPos) >= 5.0f)
  295. {
  296. dragged = true;
  297. }
  298. if (Input.GetMouseButtonUp(0))
  299. {
  300. if (mouseDown)
  301. {
  302. mouseDown = false;
  303. if (dragged)
  304. {
  305. clickCount = 0;
  306. }
  307. else
  308. {
  309. dragged = false;
  310. Rect validRect = SafeRectCheck.Instance.safeArea;
  311. validRect.width = validRect.height * 0.025f;
  312. validRect.y = validRect.height + validRect.y - validRect.width;
  313. validRect.height = validRect.width;
  314. if (Time.time - mouseDownTime < 2 && validRect.Contains(Input.mousePosition))
  315. {
  316. clickCount = clickCount + 1;
  317. if (clickCount >= 2)
  318. {
  319. clickCount = 0;
  320. var pLuaState = LuaMgr.GetMainState();
  321. if (null != pLuaState)
  322. {
  323. LuaMgr.GetMainState().DoString("local curUIId = ManagerContainer.LuaUIMgr:GetCurUIId()\nManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIGM, nil, curUIId)");
  324. }
  325. }
  326. }
  327. else
  328. {
  329. clickCount = 0;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. // private void OnGUI() {
  336. // Rect validRect = SafeRectCheck.Instance.safeArea;
  337. // validRect.width = validRect.height * 0.025f;
  338. // validRect.y = SafeRectCheck.Instance.screenHeight - validRect.height - validRect.y;
  339. // validRect.height = validRect.width;
  340. // GUI.DrawTexture(validRect, Texture2D.whiteTexture);
  341. // }
  342. private int clickCount = 0;
  343. private bool mouseDown = false;
  344. private bool dragged = false;
  345. private float mouseDownTime = 0f;
  346. private Vector2 mouseDownPos = Vector2.zero;
  347. #else
  348. }
  349. #endif
  350. protected override void Dispose()
  351. {
  352. base.Dispose();
  353. }
  354. private const float m_FrameUpdateInterval = 0.5f;
  355. private float m_FrameUpdateTime = 0f;
  356. private float m_MaxTimeAccumulator = 0;
  357. private int m_FrameCounter = 0;
  358. private int m_MaxFrameTime = 0;
  359. public int maxFrameTime
  360. {
  361. get { return m_MaxFrameTime; }
  362. }
  363. private void UpdateFPS()
  364. {
  365. float deltaTime = Time.deltaTime;
  366. if (deltaTime > 0)
  367. {
  368. m_FrameUpdateTime -= deltaTime;
  369. m_MaxTimeAccumulator += Time.timeScale / deltaTime;
  370. m_FrameCounter++;
  371. }
  372. if (m_FrameUpdateTime <= 0)
  373. {
  374. m_MaxFrameTime = Mathf.RoundToInt(m_MaxTimeAccumulator / m_FrameCounter);
  375. m_FrameUpdateTime = m_FrameUpdateInterval;
  376. m_MaxTimeAccumulator = 0;
  377. m_FrameCounter = 0;
  378. }
  379. }
  380. private void CheckIsReturnBtn()
  381. {
  382. if (Input.GetKeyDown(KeyCode.Escape))
  383. {
  384. mOnClickReturnBtn.Call(this);
  385. }
  386. }
  387. void EnterLuaLogin(bool relogin)
  388. {
  389. if (mCharacterInfo == null)
  390. {
  391. mCharacterInfo = new MainCharacter();
  392. }
  393. LuaMgr.Instance.EnterLogin(relogin);
  394. }
  395. void DisposeCharactorInfo()
  396. {
  397. mCharacterInfo.Dispose();
  398. mCharacterInfo = null;
  399. }
  400. void OnLogout(CoreEvent<int> ce)
  401. {
  402. }
  403. public void SetOnSceneLoadedLuaFunc(LuaFunction func)
  404. {
  405. OnSceneLoaded2Lua = func;
  406. }
  407. public void ReLogin()
  408. {
  409. ShutDownBattle(false);//清空战斗数据 逻辑 (等待loading界面完成 清空战斗实例)
  410. DisposeCharactorInfo();
  411. EnterLuaLogin(true);
  412. }
  413. public void ReLoginClearData()
  414. {
  415. ClearMainCharacter();
  416. ActorDataMgr.Instance.Clear();
  417. DisposeBattle();//销毁战斗实例
  418. BattleMgr.Instance.ClearSkillDirty();//销毁待更新技能
  419. SceneMgr.Instance.LoadMainScene("relogin");
  420. }
  421. void ClearMainCharacter()
  422. {
  423. if (mCharacterInfo != null)
  424. {
  425. mCharacterInfo.Clear();
  426. }
  427. }
  428. public void SetLuaPlayStoryFunc(LuaFunction func)
  429. {
  430. mPlayStoryLuaFun = func;
  431. }
  432. public void LoadCurrentBattle()
  433. {
  434. GameStateCtrl.Instance.GotoState(LoadingState);
  435. }
  436. public void CloseLoading()
  437. {
  438. if (BattleMgr.Instance.Battle == null) return;
  439. if (BattleMgr.Instance.Battle.IsBossBattle && BattleMgr.Instance.Battle.SubBattleMode == BattleSubMode.NewbieBoss)
  440. {
  441. BattleMgr.Instance.StartStoryScript();
  442. }
  443. }
  444. public void SetTeamData(LuaTable teamParam, LuaTable tbIsForce)
  445. {
  446. if (mCharacterInfo != null)
  447. {
  448. mCharacterInfo.SetTeamActors(teamParam);
  449. bool bIsForce = false;//是否强制同步
  450. if (tbIsForce[1] != null)
  451. {
  452. bool.TryParse(tbIsForce[1].ToString(), out bIsForce);
  453. }
  454. BattleMgr.Instance.SyncTeams(bIsForce);
  455. }
  456. }
  457. public void RefreshTeamData()
  458. {
  459. for (int idx = 0; idx < mCharacterInfo.TeamActors.Count; idx++)
  460. {
  461. ActorDataMgr.Instance.RefreshFellowActorData(mCharacterInfo.TeamActors[idx].ID, mCharacterInfo.TeamActors[idx].BaseId);
  462. }
  463. }
  464. public void UpdateTeamSkills(LuaTable skillsParam)
  465. {
  466. if (mCharacterInfo != null)
  467. {
  468. BattleMgr.Instance.UpdateTeamSkills(skillsParam);
  469. }
  470. }
  471. /// <summary>
  472. /// 完成角色创建
  473. /// </summary>
  474. /// <param name="modelGo"></param>
  475. public void CreateRoleViewComplete(GameObject modelGo)
  476. {
  477. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, false, false);
  478. }
  479. /// <summary>
  480. /// 角色形象更新完成
  481. /// </summary>
  482. /// <param name="modelGo"></param>
  483. public void RefreshRoleViewComplete(GameObject modelGo, bool isNew)
  484. {
  485. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, true, isNew);
  486. }
  487. /// <summary>
  488. /// 时装或者其它功能更新了角色形象,战斗内需要自行调用形象更新 参数 是否为转职后的新角色
  489. /// </summary>
  490. public void NotifyRefreshRoleView()
  491. {
  492. BattleMgr.Instance.NotifyRefreshRoleView();
  493. }
  494. public void SetMapLevelId(int mapId, int levelId)
  495. {
  496. if (mCharacterInfo == null) return;
  497. if (mCharacterInfo.CurMapId != mapId)
  498. {
  499. mCharacterInfo.CurMapId = mapId;
  500. mCharacterInfo.CurLevelId = levelId;
  501. }
  502. else
  503. {
  504. if (mCharacterInfo.CurLevelId != levelId)
  505. {
  506. mCharacterInfo.CurLevelId = levelId;
  507. BattleMgr.Instance.EnterNextBattle();
  508. }
  509. }
  510. //BattleMgr.Instance?.SetMapLevelId(mapId,levelId);
  511. Debug.Log($"=========SetMapLevelId============={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  512. }
  513. public void PreloadCreateRoleScene()
  514. {
  515. mEnterSceneType = SceneType.CreateRoleScene;
  516. CreateRoleMgr.Instance.LoadScene();
  517. }
  518. public void GotoLogin()
  519. {
  520. BattleMgr.Instance.ShutDownCurrentBattle();
  521. }
  522. //进入公会大厅
  523. public void PreloadGuildLobby()
  524. {
  525. mEnterSceneType = SceneType.GuildLobbyScene;
  526. BattleMgr.Instance.ShutDownCurrentBattle();
  527. GuildLobbyMgr.Instance.EnterGuildLobby();
  528. }
  529. public void PreloadAllFellows(ActorData[] actors)
  530. {
  531. if (actors == null || actors.Length == 0) return;
  532. for (int idx = 0; idx < actors.Length; idx++)
  533. {
  534. var actor = actors[idx];
  535. if (actor != null)
  536. {
  537. BattlePrepareManager.Instance.PrecacheModel(actor.AvatarData.prefab, actor.AType);
  538. BattlePrepareManager.Instance.PrecacheAnimatorCtrl(actor.ShowAnimCtrlName);
  539. }
  540. }
  541. }
  542. public void PreloadBattle(LuaTable teamParam)
  543. {
  544. mEnterSceneType = SceneType.NormalBattleScene;
  545. GuildLobbyMgr.Instance.Clear();
  546. mCharacterInfo.SetTeamActors(teamParam);
  547. LogicBattleStartInfo startInfo = new LogicBattleStartInfo(mCharacterInfo.CurMapId, mCharacterInfo.CurLevelId);
  548. Debug.Log($"============PreloadBattle=========={mCharacterInfo.CurMapId} --- {mCharacterInfo.CurLevelId}");
  549. BattleMgr.Instance.ShutDownCurrentBattle();
  550. BattleMgr.Instance.StartBattle(startInfo);
  551. }
  552. public List<ActorData> StartNewbieBossBattle(int sceneId, int roleSex)
  553. {
  554. mEnterSceneType = SceneType.BossBattleScene;
  555. BattleMgr.Instance.ShutDownCurrentBattle();
  556. return BattleMgr.Instance.StartNewbieBossBattle(sceneId, roleSex);
  557. }
  558. public void PreloadViewTeam(ActorData[] teamActors, string sceneName, LuaTable tbl)
  559. {
  560. mEnterSceneType = SceneType.TowerBattleScene;
  561. BattleMgr.Instance.ShutDownCurrentBattle();
  562. PreviewTeamMgr.Instance.LoadPreviewActorsAndScene(teamActors, sceneName, tbl);
  563. }
  564. public void PreloadVersusBattle(BattleSubMode mode,
  565. ActorData[] teamActors,
  566. ActorData[] enemyActors,
  567. string sceneName,
  568. float maxFightingTime,
  569. BattleEndCondition[] endConds,
  570. GvGMark[] OurMarks,
  571. GvGMark[] EnemyMarks,
  572. bool IsPresspoint,
  573. int nPresspoint)
  574. {
  575. mEnterSceneType = SceneType.TowerBattleScene;
  576. BattleMgr.Instance.ShutDownCurrentBattle();
  577. BattleMgr.Instance.StartVersusBattle(mode, teamActors, enemyActors, sceneName, maxFightingTime, endConds, OurMarks, EnemyMarks, IsPresspoint, nPresspoint);
  578. }
  579. public void PreloadBossBattle(ActorData[] actors,
  580. int bossId,
  581. int sceneId,
  582. BattleEndCondition[] endConds)
  583. {
  584. mEnterSceneType = SceneType.BossBattleScene;
  585. BattleMgr.Instance.ShutDownCurrentBattle();
  586. BattleMgr.Instance.StartBossBattle(actors, bossId, sceneId, endConds);
  587. }
  588. public bool PreloadTimeBattle(LuaTable luaTbl,
  589. BattleSubMode mode,
  590. float maxFightingTime,
  591. string sceneName,
  592. string bgmMusic,
  593. ActorData[] ourActors,
  594. ActorData[] enemyActors,
  595. ServerFighterParam[] fighterParams,
  596. BattleEndCondition[] endConds,
  597. ValType[] factors,
  598. int nRestoreSp,
  599. GvGMark[] OurMarks,
  600. GvGMark[] EnemyMarks
  601. )
  602. {
  603. mEnterSceneType = SceneType.TimeBattleScene;
  604. return BattleMgr.Instance.StartTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, ourActors, enemyActors, fighterParams, endConds, factors, nRestoreSp, OurMarks, EnemyMarks);
  605. }
  606. public bool ReplayTimeBattle(LuaTable luaTbl, BattleSubMode mode, float maxFightingTime, string sceneName, string bgmMusic, string battleRecordStr)
  607. {
  608. mEnterSceneType = SceneType.TimeBattleScene;
  609. return BattleMgr.Instance.ReplayTimeBattle(luaTbl, mode, maxFightingTime, sceneName, bgmMusic, battleRecordStr);
  610. }
  611. public void ForceStopBattle()
  612. {
  613. BattleMgr.Instance.ForceStopBattle();
  614. }
  615. //关闭清空战斗
  616. public void ShutDownBattle(bool bIsDispose = true)
  617. {
  618. BattleMgr.Instance.ShutDownCurrentBattle(bIsDispose);
  619. }
  620. //销毁战斗
  621. public void DisposeBattle()
  622. {
  623. BattleMgr.Instance.DisposeCurrentBattle();
  624. }
  625. public void SetGameSpeed(float speed)
  626. {
  627. BattleMgr.Instance.SetSpeedUp(speed);
  628. }
  629. public void SaveGameSpeed(float speed)
  630. {
  631. mGameSpeed = speed;
  632. BattleMgr.Instance.SetSpeedUp(speed);
  633. }
  634. public float GetGameSpeed()
  635. {
  636. return BattleMgr.speed_up_rate;
  637. }
  638. private void OnLoadComplete(CoreEvent<bool> ce)
  639. {
  640. #if USE_LUA
  641. if (OnSceneLoaded2Lua != null)
  642. {
  643. OnSceneLoaded2Lua.Call(this, mEnterSceneType, BattleMgr.Instance.Battle != null ? BattleMgr.Instance.Battle.SubBattleMode : BattleSubMode.None);
  644. }
  645. #else
  646. EnterBattleState();
  647. #endif
  648. }
  649. public int Random(int min, int max)
  650. {
  651. return max > min ? rand.Next(min, max) : min;
  652. }
  653. public uint CalcPassedTime(long timeStr)
  654. {
  655. ulong time = (ulong)timeStr;
  656. time = TimerManager.Instance.serverTime - time;
  657. if (time > uint.MaxValue)
  658. {
  659. return uint.MaxValue;
  660. }
  661. else if (time < 0)
  662. {
  663. return 0;
  664. }
  665. else
  666. {
  667. return (uint)time;
  668. }
  669. }
  670. public void QuitGame(bool isShowView = false)
  671. {
  672. //需要打开退出页面
  673. if (isShowView)
  674. {
  675. //如果sdk有就打开
  676. if (SDKMgr.Instance.CheckHasModul(SDKModulType.EXIT_VIEW))
  677. {
  678. SDKMgr.Instance.OpenModul(SDKModulType.EXIT_VIEW);
  679. }
  680. else
  681. {
  682. if (!SDKMgr.Instance.Exit())
  683. {
  684. KillApplication();
  685. }
  686. }
  687. }
  688. else
  689. {
  690. if (!SDKMgr.Instance.Exit())
  691. {
  692. KillApplication();
  693. }
  694. }
  695. }
  696. private void KillApplication()
  697. {
  698. // 游戏需要保存数据的在这里保存
  699. GameSettings.Instance.Save();
  700. if (SDKMgr.Instance.Quit()) return;
  701. #if UNITY_EDITOR
  702. UnityEditor.EditorApplication.isPlaying = false;
  703. #else
  704. Application.Quit();
  705. #endif
  706. }
  707. public void PlayDialog(int dialogueType, int dialogueId = 0)
  708. {
  709. if (mPlayStoryLuaFun != null)
  710. mPlayStoryLuaFun.Call(this, dialogueType, dialogueId);
  711. }
  712. public void RequestHttpServer(string url, LuaFunction luaFunc_)
  713. {
  714. StartCoroutine(DoRequestHttpServer(url, luaFunc_));
  715. }
  716. IEnumerator DoRequestHttpServer(string url, LuaFunction luaFunc_)
  717. {
  718. if (string.IsNullOrEmpty(url))
  719. {
  720. yield break;
  721. }
  722. WWW httpReq = new WWW(url);
  723. yield return httpReq;
  724. if (!string.IsNullOrEmpty(httpReq.error))
  725. {
  726. DebugHelper.LogError("DoRequestHttpServer failed:{0}[url = {1}]", httpReq.error,url);
  727. httpReq.Dispose();
  728. httpReq = null;
  729. yield break;
  730. }
  731. string content = httpReq.text;
  732. httpReq.Dispose();
  733. httpReq = null;
  734. if (luaFunc_ != null)
  735. {
  736. luaFunc_.Call(content);
  737. }
  738. }
  739. public void CleanUnusedAssets()
  740. {
  741. StartCoroutine(UnloadAssets_Coroutine());
  742. }
  743. public void OpenUrl(string url)
  744. {
  745. if (string.IsNullOrEmpty(url)) return;
  746. SDKMgr.Instance.OpenWebview(url);
  747. //Application.OpenURL(url);
  748. }
  749. public void EnableAntiAliasing()
  750. {
  751. if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.NORMAL_DEVICE)
  752. {
  753. QualitySettings.antiAliasing = 2;
  754. }
  755. else if (DeviceInfo.m_DeviceState == DeviceInfo.eDeviceState.GOOD_DEVICE)
  756. {
  757. QualitySettings.antiAliasing = 4;
  758. }
  759. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  760. }
  761. public void DisableAntiAliasing()
  762. {
  763. QualitySettings.antiAliasing = 0;
  764. //DebugHelper.LogError("antiAliasing" + QualitySettings.antiAliasing);
  765. }
  766. IEnumerator UnloadAssets_Coroutine()
  767. {
  768. yield return 0;
  769. yield return Resources.UnloadUnusedAssets();
  770. System.GC.Collect();
  771. }
  772. #region inner_methods
  773. private void RegisterEvents()
  774. {
  775. EventMgr.AddEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  776. EventMgr.AddEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  777. //EventMgr.AddEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  778. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  779. //EventMgr.AddEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  780. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  781. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  782. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  783. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  784. //EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  785. EventMgr.AddEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  786. SDKEventUtil.AddListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  787. SDKEventUtil.AddListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  788. SDKEventUtil.AddListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  789. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  790. SDKEventUtil.AddListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  791. SDKEventUtil.AddListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  792. SDKEventUtil.AddListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  793. SDKEventUtil.AddListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  794. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  795. SDKEventUtil.AddListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  796. SDKEventUtil.AddListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  797. }
  798. private void UnRegisterEvents()
  799. {
  800. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_Logout, OnLogout);
  801. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_LOAD_COMPLETE, OnLoadComplete);
  802. //EventMgr.RemoveEventListener<bool,string>(ECoreEventType.EID_SDK_INIT_RESULT, OnSdkInitRet);
  803. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_LOGIN_RESULT_NEW, OnSdkLoginRet);
  804. //EventMgr.RemoveEventListener<bool, UserInfo>(ECoreEventType.EID_SDK_SWITCH_ACCOUNT_NEW, OnSdkSwitchAccount);
  805. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_LOGOUT, OnSdkLogout);
  806. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_EXIT, OnSdkExit);
  807. //EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_SUCCESS, OnSdkPaySuccess);
  808. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_CANCEL, OnSdkPayCancel);
  809. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_PAY_FAILED, OnSdkPayFailed);
  810. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_SDK_QUESTION_REWARD_RESULT, OnSdkQuestionRewardResult);
  811. SDKEventUtil.RemoveListener(SDKCBEnum.EXIT_SUCCESS_CB, OnSdkExit);
  812. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_FAILED_CB, OnSdkInitFail);
  813. SDKEventUtil.RemoveListener(SDKCBEnum.INIT_SUCCESS_CB, OnSdkInitSuccess);
  814. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_FAILED_CB, OnSdkLoginFail);
  815. SDKEventUtil.RemoveListener(SDKCBEnum.LOGIN_SUCCESS_CB, OnSdkLoginSuccess);
  816. SDKEventUtil.RemoveListener(SDKCBEnum.LOGOUT_SUCCESS_CB, OnSdkLogoutSuccess);
  817. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_FAILED_CB, OnSdkPayFailed);
  818. SDKEventUtil.RemoveListener(SDKCBEnum.PAY_SUCCESS_CB, OnSdkPaySuccess);
  819. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_FAILED_CB, OnSdkSwitchAccountFailed);
  820. SDKEventUtil.RemoveListener(SDKCBEnum.SWITCH_SUCCESS_CB, OnSdkSwitchAccountSuccess);
  821. SDKEventUtil.RemoveListener(SDKCBEnum.CAN_ENTER_SERVER_JUDGE_CB, OnSdkCanEnterServerJudge);
  822. }
  823. private void StartLog()
  824. {
  825. DebugHelper debugHelper = this.gameObject.GetComponent<DebugHelper>();
  826. if (debugHelper == null)
  827. {
  828. this.gameObject.AddComponent<DebugHelper>();
  829. }
  830. DebugHelper.enableLog = true;
  831. DebugHelper.BeginLogs();
  832. }
  833. private IEnumerator ReadVersionFile()
  834. {
  835. string path = FileSystem.LocalPackagePath;
  836. #if !UNITY_STANDALONE_WIN
  837. #if UNITY_EDITOR //editor
  838. path = string.Format("{0}/", Application.dataPath);
  839. #elif UNITY_ANDROID && !UNITY_EDITOR//android release;
  840. path = FileSystem.LocalPackagePath;
  841. #elif UNITY_IPHONE && !UNITY_EDITOR //ios release;
  842. path = FileSystem.LocalPackagePath;
  843. #else //pc release;
  844. path = Application.dataPath;
  845. #endif
  846. #else
  847. path = FileSystem.LocalPackagePath;
  848. #endif
  849. //热更地址从新整合的配置中提取
  850. string UpdateFilePath = string.Format("{0}appbuildconfig.xml", path);
  851. WWW www = new WWW(UpdateFilePath);
  852. DebugHelper.Log("ReadUpdateFile : LocalPackagePath {0}", UpdateFilePath);
  853. yield return www;
  854. if (www.error == null)
  855. {
  856. DebugHelper.Log("ReadUpdateFile : {0}", www.text);
  857. www.Dispose();
  858. www = null;
  859. }
  860. else
  861. {
  862. DebugHelper.LogError("ReadUpdateFile {0} , {1}", www.error, UpdateFilePath);
  863. www.Dispose();
  864. www = null;
  865. }
  866. }
  867. #endregion
  868. #region ACCOUNT_SDK
  869. //private SDKMgr SdkManager = SDKMgr.Instance;
  870. private LuaFunction mSdkInitedLuaCB = null;
  871. private LuaFunction mSdkLoginedLuaCB = null;
  872. private LuaFunction mSdkLogoutLuaCB = null;
  873. private LuaFunction mSdkPayLuaCB = null;
  874. private LuaFunction msdkQuestionLuaCB = null;
  875. private LuaFunction mSdkCanEnterServerJudgeCB = null;
  876. private LuaFunction mOnClickReturnBtn = null;
  877. public void SdkInitFunc(LuaFunction func)
  878. {
  879. mSdkInitedLuaCB = func;
  880. //mSdkInitedLuaCB.Call(this, true, "");
  881. }
  882. public void SdkLoginFunc(LuaFunction func)
  883. {
  884. mSdkLoginedLuaCB = func;
  885. }
  886. public void SdkLogoutFunc(LuaFunction func)
  887. {
  888. mSdkLogoutLuaCB = func;
  889. }
  890. public void SdkPayFunc(LuaFunction func)
  891. {
  892. mSdkPayLuaCB = func;
  893. }
  894. public void SdkQuestionFunc(LuaFunction func)
  895. {
  896. msdkQuestionLuaCB = func;
  897. }
  898. public void SdkCanEnterServerJudgeFunc(LuaFunction func)
  899. {
  900. mSdkCanEnterServerJudgeCB = func;
  901. }
  902. public void SetmOnClickReturnBtnCb(LuaFunction cb)
  903. {
  904. mOnClickReturnBtn = cb;
  905. }
  906. public void SdkInit()
  907. {
  908. SDKMgr.Instance.Init();
  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. }