common.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import Tools from "./Tools";
  2. export class AttributeUnit {
  3. /** 名字-单元资源包 */
  4. bundleName: string;
  5. /** 名字-单元配置文件 */
  6. configName: string;
  7. /** bundle */
  8. bundle: cc.AssetManager.Bundle;
  9. /** config */
  10. config: AttributeGame[];
  11. }
  12. export class AttributeGame {
  13. /** 名字-视频 */
  14. videoName?: string;
  15. /** 名字-预制体 */
  16. prefabName?: string;
  17. /** 名字-脚本 */
  18. scriptName: string;
  19. }
  20. class common {
  21. private static _instance: common;
  22. public static getInstance(): common {
  23. if (this._instance) {
  24. return this._instance;
  25. }
  26. this._instance = new common();
  27. this._instance.editSpineRun();
  28. return this._instance;
  29. };
  30. isDebug: boolean = false;
  31. project: string = "luojigou_yinliu";
  32. unitCur: number = 3;// 第几集,从1开始
  33. pageCur: number = 6;// 第几个游戏,从1开始
  34. isCanPlayVideo = false;
  35. attributeMap: any = {};// 内部形式为 { "0" : AttributeUtil }
  36. getUnitNum() {
  37. return this.unitCur > 0 ? this.unitCur : 1;
  38. };
  39. setUnitNum(unitNum) {
  40. this.unitCur = unitNum;
  41. };
  42. getPageNum() {
  43. return this.pageCur > 0 ? this.pageCur : 1;
  44. };
  45. setPageNum(pageNum) {
  46. this.pageCur = pageNum;
  47. };
  48. //隐藏页面loading
  49. hideLoading() {
  50. var splash = document.getElementById('splash');
  51. splash.style.visibility = 'hidden';
  52. console.log("--hideLoading--: 隐藏加载界面");
  53. }
  54. /** 获取当前游戏的本地数据 */
  55. getGameFormLocal(): number {
  56. let objLocal: any = Tools.getLocalStorage(this.project, null);
  57. objLocal = JSON.parse(objLocal);
  58. this.log("获取本地数据:", JSON.stringify(objLocal, null, 4));
  59. if (!objLocal) return -1;
  60. if (!objLocal.objUnit) return -1;
  61. let unitOne = objLocal.objUnit[this.getUnitNum()];
  62. if (!unitOne) return -1;
  63. let gameOne = unitOne[this.getPageNum()];
  64. return Number(gameOne);
  65. };
  66. /** 设置当前游戏标志 */
  67. setGameToLocal(): void {
  68. let unit = this.getUnitNum();
  69. let page = this.getPageNum();
  70. let objLocal: any = Tools.getLocalStorage(this.project, null);
  71. objLocal = JSON.parse(objLocal);
  72. if (objLocal) {
  73. if (!objLocal.objUnit) {
  74. objLocal.objUnit = {};
  75. }
  76. if (!objLocal.objUnit[unit]) {
  77. objLocal.objUnit[unit] = {};
  78. }
  79. if (objLocal.objUnit[unit][page]) {
  80. return;
  81. }
  82. else {
  83. objLocal.objUnit[unit][page] = page;
  84. }
  85. }
  86. else {
  87. objLocal = {};
  88. objLocal.project = this.project;
  89. objLocal.objUnit = {};
  90. objLocal.objUnit[unit] = {};
  91. objLocal.objUnit[unit][page] = page;
  92. }
  93. this.log("设置本地数据:", JSON.stringify(objLocal, null, 4));
  94. Tools.setLocalStorage(this.project, JSON.stringify(objLocal));
  95. };
  96. /** cocos日志打印 */
  97. log(...params: any) {
  98. // if (!CC_DEBUG) return;
  99. var logContent = [];
  100. for (var i in params) {
  101. logContent.push(typeof (params[i]) == "object" ? JSON.stringify(params[i]) : params[i]);
  102. }
  103. console.log(" #cocos# " + logContent.join(""));
  104. };
  105. /** 设置编译器spine播放 */
  106. editSpineRun() {
  107. if (!CC_EDITOR) {
  108. return;
  109. }
  110. // 重写update方法 达到在编辑模式下 自动播放动画的功能
  111. sp.Skeleton.prototype["update"] = function (dt) {
  112. cc["engine"]._animatingInEditMode = 1;
  113. cc["engine"].animatingInEditMode = 1;
  114. if (this.paused) return;
  115. dt *= this.timeScale * sp["timeScale"];
  116. if (this.isAnimationCached()) {
  117. // Cache mode and has animation queue.
  118. if (this._isAniComplete) {
  119. if (this._animationQueue.length === 0 && !this._headAniInfo) {
  120. let frameCache = this._frameCache;
  121. if (frameCache && frameCache.isInvalid()) {
  122. frameCache.updateToFrame();
  123. let frames = frameCache.frames;
  124. this._curFrame = frames[frames.length - 1];
  125. }
  126. return;
  127. }
  128. if (!this._headAniInfo) {
  129. this._headAniInfo = this._animationQueue.shift();
  130. }
  131. this._accTime += dt;
  132. if (this._accTime > this._headAniInfo.delay) {
  133. let aniInfo = this._headAniInfo;
  134. this._headAniInfo = null;
  135. this.setAnimation(0, aniInfo.animationName, aniInfo.loop);
  136. }
  137. return;
  138. }
  139. this._updateCache(dt);
  140. } else {
  141. this._updateRealtime(dt);
  142. }
  143. }
  144. }
  145. };
  146. export default common.getInstance();