GameRePlay.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties: {
  4. m_UINode:cc.Node,
  5. m_BtLast:cc.Button,
  6. m_BtNext:cc.Button,
  7. m_BtLastGame:cc.Button,
  8. m_BtNextGame:cc.Button,
  9. m_BtPlay:cc.Button,
  10. m_BtStop:cc.Button,
  11. },
  12. onLoad:function () {
  13. this.m_gameNode = this.node.getChildByName("GameNode");
  14. this.m_BGSprite = this.m_gameNode.getComponent(cc.Sprite);
  15. this.m_UINode.active = false;
  16. this.schedule(this.CheckRePlay,1.5);
  17. },
  18. //游戏自动播放
  19. CheckRePlay:function(){
  20. if(this.m_ReplayEngine == null) return
  21. if(!this.m_ReplayEngine.IsReady()) return
  22. if(this.m_FirstOpen) return this.m_FirstOpen = false;
  23. if(this.m_BtPlay.node.active == false) this.m_ReplayEngine.PlayNext();
  24. },
  25. OnHideView:function(){
  26. if( this.m_GameEngine ) this.m_GameEngine.OnDestroy();
  27. var kernel = gClientKernel.get();
  28. if(kernel != null) gClientKernel.destory();
  29. //播放背景音乐
  30. var BGMIndex = cc.sys.localStorage.getItem(window.clientName+window.Key_TableBGM);
  31. if(BGMIndex == null) BGMIndex=0
  32. cc.gSoundRes.PlayMusic("BGM"+BGMIndex, false);
  33. this.node.active = false;
  34. },
  35. //按钮状态
  36. ChangeUIState:function(GameIndex,wPorgress,EndIndex,EndProgress){
  37. this.m_UINode.active = true;
  38. this.m_BtLast.interactable = wPorgress > 0;
  39. this.m_BtNext.interactable = wPorgress < EndProgress;
  40. //this.m_BtLastGame.interactable = GameIndex > 0;
  41. //this.m_BtNextGame.interactable = GameIndex < EndIndex;
  42. if(wPorgress >= EndProgress){
  43. this.m_BtPlay.interactable = false;
  44. this.m_BtPlay.active = true;
  45. this.m_BtStop.active = false;
  46. if(this.m_BtPlay.node.active == false){
  47. this.m_BtPlay.node.active = true;
  48. this.m_BtStop.node.active = false;
  49. }
  50. }else{
  51. this.m_BtPlay.interactable = true;
  52. }
  53. },
  54. OnCheckReady:function(){
  55. if(!this.m_ReplayEngine || !this.m_ReplayEngine.m_bInitScene) return false
  56. return true;
  57. },
  58. //上一步
  59. OnBtLast:function(){
  60. if(!this.OnCheckReady()) return
  61. this.OnBtStop();
  62. this.m_ReplayEngine.PlayLast();
  63. },
  64. //下一步
  65. OnBtNext:function(){
  66. if(!this.OnCheckReady()) return
  67. this.OnBtStop();
  68. this.m_ReplayEngine.PlayNext();
  69. },
  70. //播放
  71. OnBtPlay:function(){
  72. if(!this.OnCheckReady()) return
  73. this.m_BtPlay.node.active = false;
  74. this.m_BtStop.node.active = true;
  75. },
  76. //暂停
  77. OnBtStop:function(){
  78. if(!this.OnCheckReady()) return
  79. this.m_BtPlay.node.active = true;
  80. this.m_BtStop.node.active = false;
  81. },
  82. //上一局
  83. OnBtLastGame:function(){
  84. if(!this.OnCheckReady()) return
  85. this.m_wProgress = parseInt(this.m_wProgress) - 1;
  86. this.UpdateReplayData();
  87. },
  88. //下一局
  89. OnBtNextGame:function(){
  90. if(!this.OnCheckReady()) return
  91. this.m_wProgress = parseInt(this.m_wProgress) + 1;
  92. this.UpdateReplayData();
  93. },
  94. //加载回放&游戏组件
  95. LoadGameRes:function(wKindID){
  96. this.m_BGSprite.spriteFrame = null;
  97. this.m_GameEngine = null;
  98. this.m_ReplayEngine = null;
  99. this.m_FirstOpen = true;
  100. this.m_PlayIndex = 0;
  101. this.OnBtStop();
  102. try {
  103. this.m_ReplayEngine = this.node.getComponent('ReplayEngine_'+wKindID);
  104. if(this.m_ReplayEngine == null) this.m_ReplayEngine = this.node.addComponent('ReplayEngine_'+wKindID);
  105. } catch (error) {
  106. if(window.LOG_NET_DATA)console.log(error)
  107. return false;
  108. }
  109. var kernel = gClientKernel.get();
  110. if(kernel == null||kernel == 0) kernel = gClientKernel.create();
  111. this.m_ReplayEngine.Init(this);
  112. this.unschedule(this.OnTimer_LoadGCE);
  113. this.scheduleOnce(this.OnTimer_LoadGCE, 0.1);
  114. return true;
  115. },
  116. OnTimer_LoadGCE: function() {
  117. this.ShowGamePrefab('GameClientEngine',GameDef.KIND_ID,this.m_gameNode,function(Js){
  118. this.m_GameEngine = Js;
  119. this.m_ReplayEngine.SetGameEngine(Js);
  120. }.bind(this));
  121. },
  122. //设置回放数据
  123. LoadRePlayData:function(LookUser, RecordID, wProgress){
  124. var self = this;
  125. this.m_PlayID = RecordID;
  126. this.m_wProgress = wProgress;
  127. this.m_LookUser = LookUser;
  128. this.UpdateReplayData();
  129. },
  130. UpdateReplayData:function(){
  131. var self = this;
  132. g_Lobby.ShowLoading();
  133. var webUrl = window.PHP_HOME+'/GameRecord.php?&GetMark=1&ID='+this.m_PlayID+'&GameIndex='+this.m_wProgress;
  134. WebCenter.GetData(webUrl, 999999, function (data) {
  135. g_Lobby.StopLoading();
  136. if(data.length < 50){
  137. self.ShowAlert('重播檔案遺失!',Alert_Yes, function() {
  138. self.HideView();
  139. });
  140. return
  141. }
  142. var RecordInfo = JSON.parse(data);
  143. if(RecordInfo[0] == 0) {
  144. self.ShowAlert('重播檔案讀取失敗!',Alert_Yes, function() {
  145. self.HideView();
  146. });
  147. return
  148. }
  149. self.m_BtLastGame.interactable = RecordInfo[1] > 0;
  150. self.m_BtNextGame.interactable = parseInt(RecordInfo[1]) + 1 < RecordInfo[2];
  151. if(self.m_ReplayEngine) self.m_ReplayEngine.SetData(RecordInfo[3], self.m_LookUser);
  152. });
  153. },
  154. update (dt) {
  155. if(window.gGameBG == 'loading') {
  156. window.gGameBG = cc.gPreLoader.LoadRes(`Image_BG_BG${GameDef.BGIndex}`, '' + GameDef.KIND_ID, function(res) {
  157. this.m_BGSprite.spriteFrame = res;
  158. }.bind(this));
  159. }
  160. },
  161. });