GameMgr.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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)
  474. {
  475. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, false, false);
  476. }
  477. /// <summary>
  478. /// 角色形象更新完成
  479. /// </summary>
  480. /// <param name="modelGo"></param>
  481. public void RefreshRoleViewComplete(GameObject modelGo, bool isNew)
  482. {
  483. BattleMgr.Instance.OnLoadRoleCompleted(modelGo, true, isNew);
  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. }
  903. public void SdkLogin()
  904. {
  905. SDKMgr.Instance.Login();
  906. }
  907. public void SdkPay(int goodsId, string goodsName, string goodsDesc,
  908. int count, float amount,
  909. string cpOrderId, string extrasParams)
  910. {
  911. int decimalVal = 1000;
  912. int tempVal = Mathf.FloorToInt((amount + 0.0005f) * decimalVal);
  913. amount = (float)tempVal / decimalVal;
  914. if (string.IsNullOrEmpty(goodsName))
  915. {
  916. goodsName = "初心者";
  917. }
  918. SDKMgr.Instance.Pay(goodsId, goodsName, goodsDesc,
  919. count, amount,
  920. cpOrderId, extrasParams);
  921. //Debug.Log($"goodsId = {goodsId} ==== goodsName = {goodsName} ===== goodsDesc = {goodsDesc} ===== count = {count} ===== amount = {amount} ====== cpOrderId = {cpOrderId} ==== extrasParams = {extrasParams}");
  922. }
  923. public void SdkLogout()
  924. {
  925. SDKMgr.Instance.Logout();
  926. }
  927. public void SdkExit()
  928. {
  929. }
  930. public void SdkShowToolbar()
  931. {
  932. //int ret = quicksdk.QuickSDK.getInstance().showToolBar(quicksdk.ToolbarPlace.QUICK_SDK_TOOLBAR_MID_LEFT);
  933. //DebugHelper.LogError("SdkShowToolbar:" + ret);
  934. }
  935. public void SdkHideToolBar()
  936. {
  937. //SdkManager.SdkHideToolBar();
  938. }
  939. private void OnSdkInitFail(object obj)
  940. {
  941. if (mSdkInitedLuaCB != null)
  942. {
  943. mSdkInitedLuaCB.Call(this, false, SDKMgr.Instance.GetSDKName());
  944. }
  945. }
  946. private void OnSdkInitSuccess(object obj)
  947. {
  948. if (mSdkInitedLuaCB != null)
  949. {
  950. mSdkInitedLuaCB.Call(this, true, SDKMgr.Instance.GetSDKName());
  951. }
  952. }
  953. private void OnSdkLoginFail(object obj)
  954. {
  955. if (mSdkLoginedLuaCB != null)
  956. {
  957. mSdkLoginedLuaCB.Call(this, false, "", "", false);
  958. SDKMgr.Instance.ReportRoleEnterFail();
  959. }
  960. }
  961. private void OnSdkLoginSuccess(object obj)
  962. {
  963. if (mSdkLoginedLuaCB != null)
  964. {
  965. if (obj != null)
  966. {
  967. UserInfo userInfo = (UserInfo)obj;
  968. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, false);
  969. SDKMgr.Instance.ReportIdentification(SDKMgr.Instance.GetInt64TimeStamp());
  970. }
  971. else
  972. {
  973. mSdkLoginedLuaCB.Call(this, true, "", "", false);
  974. }
  975. }
  976. }
  977. private void OnSdkSwitchAccountFailed(object obj)
  978. {
  979. if (mSdkLoginedLuaCB != null)
  980. {
  981. UserInfo userInfo = (UserInfo)obj;
  982. mSdkLoginedLuaCB.Call(this, false, userInfo.uid, userInfo.token, true);
  983. }
  984. }
  985. private void OnSdkSwitchAccountSuccess(object obj)
  986. {
  987. if (mSdkLoginedLuaCB != null)
  988. {
  989. UserInfo userInfo = (UserInfo)obj;
  990. mSdkLoginedLuaCB.Call(this, true, userInfo.uid, userInfo.token, true);
  991. }
  992. }
  993. private void OnSdkLogoutSuccess(object obj)
  994. {
  995. if (mSdkLogoutLuaCB != null)
  996. {
  997. mSdkLogoutLuaCB.Call(this);
  998. }
  999. }
  1000. private void OnSdkExit(object obj)
  1001. {
  1002. KillApplication();
  1003. }
  1004. private void OnSdkPaySuccess(object obj)
  1005. {
  1006. if (mSdkPayLuaCB != null)
  1007. {
  1008. mSdkPayLuaCB.Call(this, true);
  1009. }
  1010. }
  1011. private void OnSdkPayCancel(CoreEvent<bool> ce)
  1012. {
  1013. if (mSdkPayLuaCB != null)
  1014. {
  1015. mSdkPayLuaCB.Call(this, false);
  1016. }
  1017. }
  1018. private void OnSdkPayFailed(object obj)
  1019. {
  1020. if (mSdkPayLuaCB != null)
  1021. {
  1022. mSdkPayLuaCB.Call(this, false);
  1023. }
  1024. }
  1025. private void OnSdkQuestionRewardResult(CoreEvent<bool> ce)
  1026. {
  1027. if (msdkQuestionLuaCB != null)
  1028. {
  1029. msdkQuestionLuaCB.Call(this, true);
  1030. }
  1031. }
  1032. private void OnSdkCanEnterServerJudge(object obj)
  1033. {
  1034. if (mSdkCanEnterServerJudgeCB != null)
  1035. {
  1036. mSdkCanEnterServerJudgeCB.Call(this, "" + obj);
  1037. }
  1038. }
  1039. #endregion
  1040. #region 新加
  1041. private Dictionary<int, int> mDifAttrs = new Dictionary<int, int>();
  1042. public void ClearDifAttr()
  1043. {
  1044. mDifAttrs.Clear();
  1045. }
  1046. public void AddDifAttr(int type,int attr)
  1047. {
  1048. mDifAttrs.Add(type, attr);
  1049. }
  1050. public void AddDifAttrs(Dictionary<object, object> difAttrs)
  1051. {
  1052. foreach (var item in difAttrs)
  1053. {
  1054. mDifAttrs.Add((int)item.Key,(int)item.Value);
  1055. }
  1056. }
  1057. public Dictionary<int, int> GetDifAttrs()
  1058. {
  1059. return mDifAttrs;
  1060. }
  1061. #endregion
  1062. }