GameCtr.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Constant, { GameState, PageName, PanelName } from "./Constant";
  2. import { cocosz } from "./CocosZ";
  3. import { gameMgr } from "../Game/gameMgr";
  4. /**
  5. * 游戏控制类
  6. * 实现游戏的基础逻辑
  7. */
  8. export default class GameCtr {
  9. public curUseSkinId: number = -1;// 使用皮肤id
  10. public loadProgress: number = 0;// 总加载进度
  11. private _loadMapPro: number = 0;// 地图加载进度
  12. private _totalCount: number = 0;// 资源总数
  13. private _compCount: number = 0;// 资源完成数量
  14. private _pathBack: string = "";
  15. private _prefabBack: cc.Prefab = null;
  16. //游戏初始化
  17. public init() {
  18. this.loadProgress = 0;
  19. this._loadMapPro = 0;
  20. this._totalCount = 0;
  21. this._compCount = 0;
  22. this.loadRes();
  23. }
  24. updateLoadRes() {
  25. this.loadProgress = (this._loadMapPro + this._compCount / this._totalCount) / 2;
  26. // 开启地图加载
  27. if (this._compCount >= this._totalCount && this._loadMapPro == 0) {
  28. this._loadMapPro = 0.01;
  29. this.loadMap();
  30. }
  31. cc.game.emit(Constant.E_GAME_LOGIC, { type: Constant.E_UPDATE_PROGRESS, data: this.loadProgress })
  32. }
  33. loadRes() {
  34. // 游戏音效
  35. let mess1: any = [];
  36. // 地下城音效
  37. let mess2: any = [];
  38. // 地下城技能预制体
  39. let mess3: any = [];
  40. // 游戏音效
  41. cocosz.getDirWithPath("audio_game", cc.AudioClip, mess1);
  42. cocosz.resMgr.loadAndCacheResArray(mess1, cc.AudioClip, null, () => {
  43. this._compCount++;
  44. this.updateLoadRes();
  45. });
  46. // 地下城
  47. if (6 == cocosz.gameMode) {
  48. // 音效
  49. cocosz.getDirWithPath("audio_zombie", cc.AudioClip, mess2);
  50. cocosz.resMgr.loadAndCacheResArray(mess2, cc.AudioClip, null, () => {
  51. this._compCount++;
  52. this.updateLoadRes();
  53. });
  54. // 预制体
  55. cocosz.getDirWithPath("prefab_zombie_skill", cc.Prefab, mess3);
  56. cocosz.resMgr.loadAndCacheResArray(mess3, cc.Prefab, null, () => {
  57. this._compCount++;
  58. this.updateLoadRes();
  59. })
  60. }
  61. // 资源总数
  62. this._totalCount = mess1.length + mess2.length + mess3.length;
  63. }
  64. loadMap() {
  65. let path = "maps/mapzombie";
  66. // 释放地图资源
  67. if (this._pathBack != path && this._prefabBack && this._prefabBack.isValid) {
  68. cc.assetManager.releaseAsset(this._prefabBack)
  69. }
  70. // 加载地图
  71. cocosz.resMgr.loadRes(path, cc.Prefab, ((cur, total) => {
  72. if (cur / total > this._loadMapPro) {
  73. this._loadMapPro = cur / total;
  74. }
  75. if (this._loadMapPro > 0.99) this._loadMapPro = 0.99;
  76. this.updateLoadRes();
  77. }), ((err, res: any) => {
  78. if (err) {
  79. console.log(err);
  80. cocosz.sceneMgr.loadScene("Home", (() => {
  81. cocosz.uiMgr.openPage(PageName.UIHomePage);
  82. }));
  83. }
  84. else {
  85. this._pathBack = path;
  86. this._prefabBack = res;
  87. gameMgr.map = cc.instantiate(res);
  88. gameMgr.initPos();
  89. gameMgr.mapSize = gameMgr.map.getContentSize();
  90. gameMgr.node.addChild(gameMgr.map, -5);
  91. this._loadMapPro = 1;
  92. this.updateLoadRes();
  93. }
  94. }))
  95. }
  96. }