No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 5 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2014,www.easygame.org
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the easygame.org nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY EASYEGRET.COM AND CONTRIBUTORS "AS IS" AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL EGRET-LABS.ORG AND CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. var __reflect = (this && this.__reflect) || function (p, c, t) {
  28. p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
  29. };
  30. var __extends = this && this.__extends || function __extends(t, e) {
  31. function r() {
  32. this.constructor = t;
  33. }
  34. for (var i in e) e.hasOwnProperty(i) && (t[i] = e[i]);
  35. r.prototype = e.prototype, t.prototype = new r();
  36. };
  37. var Main = (function (_super) {
  38. __extends(Main, _super);
  39. function Main() {
  40. var _this = _super.call(this) || this;
  41. //RES资源全部下载完成
  42. _this.loadResAllComplete = false;
  43. //easy game的配置文件时候下载完成
  44. _this._isEasyGameOk = false; //游戏配置下载完毕
  45. _this._isSoundOk = false; //声音准备完毕
  46. _this._isProjectUIOk = false; //公用UI下载完毕
  47. _this._isCreateScene = false; //是否已创建场景
  48. _this._isLoadingViewOk = false; //view的loading是否准备好了
  49. _this._progressCalculate = null;
  50. _this._isViewEnter = false; //第一个加载的view是否已经准备完成
  51. _this.loadResAllComplete = false;
  52. _this.addEventListener(egret.Event.ADDED_TO_STAGE, _this.onAddToStage, _this);
  53. return _this;
  54. }
  55. Main.prototype.onAddToStage = function (event) {
  56. egret.lifecycle.addLifecycleListener(function (context) {
  57. // custom lifecycle plugin
  58. context.onUpdate = function () {
  59. };
  60. });
  61. egret.lifecycle.onPause = function () {
  62. //egret.ticker.pause();
  63. };
  64. egret.lifecycle.onResume = function () {
  65. //egret.ticker.resume();
  66. };
  67. //初始化全局数据,以便Heatbeat可以使用
  68. easy.GlobalSetting.initData();
  69. //初始化Resource资源加载库
  70. easy.ResManager.loadConfig(easy.GlobalSetting.CDN_RES + "loading_main.json", ["loading_main"], this.onResLoadingMianComplete, this);
  71. //下载loading view的内容
  72. easy.ResManager.loadConfig(easy.GlobalSetting.CDN_RES + "loading_view.json", ["loading_view"], this.onResLoadingViewComplete, this);
  73. easy.EventManager.addEventListener(easy.EventType.VIEW_ENTER, this.onHiddenloadingMain, this);
  74. };
  75. /**
  76. * loading view配置文件的加载完成
  77. * @param event
  78. */
  79. Main.prototype.onResLoadingViewComplete = function (groupName) {
  80. this._isLoadingViewOk = true;
  81. };
  82. /**
  83. * loading main配置文件的加载完成
  84. * @param event
  85. */
  86. Main.prototype.onResLoadingMianComplete = function (groupName) {
  87. //console.log("@Main onLoadingMianResourceLoadComplete=" + event.groupName);
  88. if (groupName == "loading_main") {
  89. //设置加载进度界面
  90. this.loadingMain = new modules.loadingProgressMainBar(); //loading main
  91. this.stage.addChild(this.loadingMain);
  92. this.loadingMain.width = easy.GlobalSetting.DISPLAY_WIDTH;
  93. this.loadingMain.height = easy.GlobalSetting.DISPLAY_HEIGHT;
  94. this.loadingMain.enter();
  95. //启动进度条计数
  96. this._progressCalculate = new easy.ProgressCalculate(10);
  97. easy.HeartBeat.addListener(this, this.onHbProgress, 2);
  98. //初始化Resource资源加载库
  99. easy.ResManager.loadConfig(easy.GlobalSetting.CDN_RES + "resource.json", ["group_easygame_config"], this.onResLoadGameConfigComplete, this);
  100. //提前加载Json数据,创建场景的时候,可能就需要用到数据了
  101. //modules.DataManager.loadJsonFile();
  102. //加载公用资源,如果设置有公用资源请打开此项
  103. //easy.ResManager.loadResFile("modules");
  104. //easy.EventManager.addEventListener(easy.EventType.PROJECT_RES_DOWNLOADED, this.onProjectResDownloaded, this);
  105. this._isProjectUIOk = true;
  106. }
  107. };
  108. /**
  109. * 控制进度条
  110. * @type {number}
  111. */
  112. Main.prototype.onHbProgress = function () {
  113. var progress = this._progressCalculate.progress();
  114. //console.log("progress=" + progress);
  115. this._progressCalculate._progressSpeedUp = (this.loadResAllComplete && this._isProjectUIOk && this._isViewEnter);
  116. this.loadingMain.setProgress(progress, 100);
  117. if (this.loadResAllComplete && !this._isCreateScene && this._isLoadingViewOk)
  118. this.createScene();
  119. if (progress == 100) {
  120. easy.HeartBeat.removeListener(this, this.onHbProgress);
  121. this.removeLoadingUI();
  122. }
  123. };
  124. /**
  125. * 将loading页面移除,开始创建场景
  126. */
  127. Main.prototype.removeLoadingUI = function () {
  128. if (this.loadingMain) {
  129. this.loadingMain.outer();
  130. }
  131. };
  132. /**
  133. * 公用资源加载完成的通知
  134. * @param myEvent
  135. */
  136. Main.prototype.onProjectResDownloaded = function (myEvent) {
  137. easy.EventManager.removeEventListener(easy.EventType.PROJECT_RES_DOWNLOADED, this.onProjectResDownloaded, this);
  138. this._isProjectUIOk = true;
  139. this.checkAllResourceLoaded();
  140. };
  141. /**
  142. * preload资源组加载完成
  143. */
  144. Main.prototype.onResLoadGameConfigComplete = function (groupName) {
  145. //console.log("@Main onResourceLoadComplete=" + event.groupName);
  146. if (groupName == "group_easygame_config") {
  147. this._isEasyGameOk = true;
  148. //下载全部完成
  149. //设置帧频
  150. easy.GlobalSetting.FRAME_RATE = 60;
  151. //EasyGame初始化
  152. easy.EasyGame.init();
  153. //设置界面切换的loading,view的loading和Win的loading可以设置成不一样的类
  154. easy.ViewManager.defaultLoadingClass = easy.DefaultLoadingView; //loading view
  155. easy.PopupManager.defaultLoadingClass = easy.DefaultLoadingView; //loading view
  156. }
  157. this.checkAllResourceLoaded();
  158. };
  159. /**
  160. * 声音加载完成
  161. */
  162. Main.prototype.onResLoadSoundComplete = function (groupName) {
  163. //console.log("@Main onResourceLoadComplete=" + event.groupName);
  164. if (groupName == "sound") {
  165. this._isSoundOk = true;
  166. //声音加载完毕,播放背景音文件
  167. easy.Sound.play("sound_bg", 0, 0);
  168. }
  169. };
  170. /**
  171. * 检测是否所有需要的资源全部加载完成
  172. * 下载全部完成的情况下设置loadAllComplete标志,等待loading的全部反应
  173. */
  174. Main.prototype.checkAllResourceLoaded = function () {
  175. if (this._isEasyGameOk && this._isProjectUIOk) {
  176. //创建场景
  177. if (easy.GlobalSetting.APP_STORAGE == easy.GlobalSetting.STORAGE_NET || easy.GlobalSetting.APP_STORAGE == easy.GlobalSetting.STORAGE_LOCAL_NET) {
  178. this.loadDataFromNet();
  179. }
  180. else {
  181. this.loadResAllComplete = true;
  182. }
  183. }
  184. };
  185. /**
  186. * 隐藏加载进度条
  187. */
  188. Main.prototype.onHiddenloadingMain = function (myEvent) {
  189. this._isViewEnter = true;
  190. easy.EventManager.removeEventListener(easy.EventType.VIEW_ENTER, this.onHiddenloadingMain, this);
  191. //声音加载
  192. easy.ResManager.addGroupCompleteListener("sound", this.onResLoadSoundComplete, this);
  193. };
  194. /**
  195. * 需要从网络初始化信息
  196. */
  197. Main.prototype.loadDataFromNet = function () {
  198. //TODO 这里填写需要网络加载的信息,完成后,请调用loadDataFromNetComplete()方法,加载主场景
  199. //this.loadDataFromNetComplete();
  200. };
  201. /**
  202. * 从网络加载初始化信息完成
  203. */
  204. Main.prototype.loadDataFromNetComplete = function () {
  205. //TODO 网络信息的初始化,请在这里填写
  206. this.loadResAllComplete = true;
  207. };
  208. /**
  209. * 创建场景
  210. */
  211. Main.prototype.createScene = function () {
  212. if (this._isCreateScene)
  213. return; //防止多次创建
  214. this._isCreateScene = true;
  215. //console.log("main.createScene")
  216. //TODO 切换到第一个主场景
  217. easy.ViewManager.show(modules.index1View, null, false);
  218. //TODO 提前预设空闲下载,加快后续的模块载入速度
  219. easy.ResManager.addIdleDownload(modules.index1View);
  220. easy.ResManager.addIdleDownload(modules.index2View);
  221. easy.ResManager.addIdleDownload(modules.index3View);
  222. easy.ResManager.addIdleDownload(modules.index4View);
  223. easy.ResManager.addIdleDownload(modules.index5View);
  224. easy.ResManager.addIdleDownload(modules.index6View);
  225. easy.ResManager.addIdleDownload(modules.index7View);
  226. easy.ResManager.addIdleDownload(modules.index8View);
  227. easy.ResManager.addIdleDownload(modules.index9View);
  228. //TODO 动画的预加载方式
  229. //easy.AnimateManager.getAnimateData("need_to_download_name");
  230. };
  231. return Main;
  232. }(egret.DisplayObjectContainer));
  233. __reflect(Main.prototype, "Main");