GameController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import GameModel from "../Model/GameModel";
  2. import Toast from "../Utils/Toast";
  3. import SdkUtil from "../Utils/SdkUtil"
  4. import MusicImgChange from "../Utils/MusicImgChange"
  5. import AudioUtils from "../Utils/AudioUtils";
  6. import Utils from "../Utils/Utils";
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. grid:{
  11. default:null,
  12. type:cc.Node
  13. },
  14. audioButton:{
  15. default: null,
  16. type: cc.Node
  17. },
  18. audioSource:{
  19. type:cc.AudioSource
  20. },
  21. skillTip:{
  22. default:null,
  23. type:cc.Node
  24. },
  25. //点击时的音效
  26. click:{
  27. default:null,
  28. type: cc.AudioClip
  29. },
  30. isVideoAd:false,
  31. //静音按钮是否按下:false表示未按下
  32. musicBtnState: false,
  33. },
  34. isCrushOne: false, //消除一个格子
  35. isStepPause: false, //计步器
  36. isCrushRow: false, //消除整行
  37. isCrushCol: false, //消除整列
  38. isCrushOneType: false, //消除一种类型
  39. // LIFE-CYCLE CALLBACKS:
  40. onLoad () {
  41. //只消除一个的道具数量
  42. this.eliminateOneNum = 10;
  43. //暂停步数的道具数量
  44. this.stepPauseNum = 10;
  45. //剩余刷新次数
  46. this.refreshNum = 10;
  47. //消除整行的道具数量
  48. this.crushRowAllNum = 10;
  49. //消除整列的道具数量
  50. this.crushColAllNum = 10;
  51. //消除一种类型的道具数据
  52. this.crushOneTypeNum = 10;
  53. // let audioButton = this.node.parent.getChildByName("AudioButton")
  54. // audioButton.on("click",this.callback,this)
  55. SdkUtil.Instance.motivational_Video_Load()
  56. this.gameModel = new GameModel();
  57. // this.gameModel.init(this.initTypeNum);
  58. this.gameModel.init(4);
  59. this.gridScript = this.grid.getComponent("GridView");
  60. this.gridScript.setController(this);
  61. this.gridScript.initWithCellModels(this.gameModel.getCells());
  62. this.audioSource = cc.find("Canvas/GameScene")._components[1].audio;
  63. // console.log("num:",this.initTypeNum)
  64. },
  65. // callback(){
  66. // let state = this.audioSource._state;
  67. // state === 1 ? this.audioSource.pause() : this.audioSource.play();
  68. // Toast(state === 1 ? "关闭背景音乐🎵" : "打开背景音乐🎵");
  69. // },
  70. //音乐开关按钮
  71. musicBtn(){
  72. let sprite = cc.find("Canvas/MusicBtn")
  73. this.musicBtnState = !this.musicBtnState;
  74. if (this.musicBtnState){
  75. // console.log("音乐关")
  76. this.audioSource.pause()
  77. sprite.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("MusicImgChange").imgOff
  78. }else {
  79. // console.log("音乐开")
  80. this.audioSource.play()
  81. sprite.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("MusicImgChange").imgOn
  82. }
  83. },
  84. selectCell(pos){
  85. // console.log("ppos:",pos)
  86. return this.gameModel.selectCell(pos);
  87. },
  88. cleanCmd(){
  89. this.gameModel.cleanCmd();
  90. },
  91. // start () {},
  92. update (dt) {
  93. //当前的关卡数
  94. // let curLevel = cc.find("Canvas/TopResult/LevelNum").getComponent(cc.Label)
  95. // let curLevelNum = this.levelNum + 1;
  96. // curLevel.string = "第" + curLevelNum + "关";
  97. // let sprite = cc.find("Canvas/TopResult/TargetArea")
  98. //
  99. // //关卡目标
  100. // if (this.levelNum >= 8){
  101. // let targetOne = cc.find("Canvas/TopResult/TargetArea/Layout/TargetOne");
  102. // let targetTwo = cc.find("Canvas/TopResult/TargetArea/Layout/TargetTwo");
  103. // let targetThree = cc.find("Canvas/TopResult/TargetArea/Layout/TargetThree");
  104. // targetOne.active = true;
  105. // targetTwo.active = true;
  106. // targetThree.active = true;
  107. //
  108. // targetOne.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").bear;
  109. // targetTwo.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").cat;
  110. // targetThree.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").fox;
  111. //
  112. // }else if (this.levelNum >= 5){
  113. // let targetOne = cc.find("Canvas/TopResult/TargetArea/Layout/TargetOne");
  114. // let targetTwo = cc.find("Canvas/TopResult/TargetArea/Layout/TargetTwo");
  115. // targetOne.active = true;
  116. // targetTwo.active = true;
  117. // targetOne.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").bear;
  118. // targetTwo.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").cat;
  119. // }else {
  120. // let targetOne = cc.find("Canvas/TopResult/TargetArea/Layout/TargetOne");
  121. // targetOne.active = true;
  122. // targetOne.getComponent(cc.Sprite).spriteFrame = sprite.getComponent("TargetUtils").bear;
  123. // }
  124. //获取只消除一个的道具数量
  125. let eliminateNum = cc.find("Canvas/BottomUtils/Utils/eliminateOne/eliminateOneNum").getComponent(cc.Label);
  126. eliminateNum.string = this.eliminateOneNum;
  127. //获取暂停步数的道具数量
  128. let stepPauseNum = cc.find("Canvas/BottomUtils/Utils/stepPause/stepPauseNum").getComponent(cc.Label);
  129. stepPauseNum.string = this.stepPauseNum;
  130. //获取刷新次数
  131. let refreshNum = cc.find("Canvas/BottomUtils/Utils/refresh/refreshNum").getComponent(cc.Label);
  132. refreshNum.string = this.refreshNum;
  133. //获取消除整行的道具数量
  134. let crushOneRow = cc.find("Canvas/BottomUtils/Utils/crushRow/crushRowNum").getComponent(cc.Label);
  135. crushOneRow.string = this.crushRowAllNum;
  136. //获取消除整列的道具数量
  137. let crushOneCol = cc.find("Canvas/BottomUtils/Utils/crushCol/crushColNum").getComponent(cc.Label);
  138. crushOneCol.string = this.crushColAllNum;
  139. //获取消除一种类型的道具数量
  140. let crushOneType = cc.find("Canvas/BottomUtils/Utils/bombOne/crushOneTypeNum").getComponent(cc.Label);
  141. crushOneType.string = this.crushOneTypeNum;
  142. },
  143. //道具消除一个块
  144. processCrushOne(pos){
  145. return this.gameModel.processCrushOne(pos);
  146. },
  147. //道具消除一行
  148. processCrushRow(pos){
  149. return this.gameModel.processCrushRow(pos);
  150. },
  151. //道具消除一列
  152. processCrushCol(pos){
  153. return this.gameModel.processCrushCol(pos);
  154. },
  155. //道具消除一种类型
  156. processCrushOneType(pos){
  157. return this.gameModel.processCrushOneType(pos);
  158. },
  159. onSkill(e,type){
  160. if(this.isVideoAd){
  161. SdkUtil.Instance.motivational_Video_Show(()=>{
  162. this[type]()
  163. }, ()=>{
  164. console.log('激励广告加载失败')
  165. })
  166. }else{
  167. this[type]()
  168. }
  169. },
  170. //道具消除一个
  171. onEliminateOne(){
  172. cc.audioEngine.play(this.click, false, 1);
  173. if (this.gridScript.isInPlayAni) {
  174. return
  175. }
  176. if (this.eliminateOneNum > 0){
  177. // this.eliminateOneNum--;
  178. this.isCrushOne = true;
  179. const msg = this.skillTip.getChildByName("msg");
  180. msg.getComponent(cc.Label).string = "选择一个需要消除的块"
  181. this.showSkillTip(true);
  182. }else {
  183. Toast("道具已用完")
  184. }
  185. },
  186. //步数暂停
  187. onStepPause(){
  188. cc.audioEngine.play(this.click, false, 1);
  189. if (this.gridScript.isInPlayAni) {
  190. return
  191. };
  192. if (this.stepPauseNum > 0){
  193. this.stepPauseNum--;
  194. this.isStepPause = true;
  195. const msg = this.skillTip.getChildByName("msg");
  196. msg.getComponent(cc.Label).string = "步数暂停中";
  197. this.showSkillTip(true);
  198. }else {
  199. Toast("道具已用完")
  200. }
  201. },
  202. //刷新
  203. onRefresh(){
  204. cc.audioEngine.play(this.click, false, 1);
  205. if (this.gridScript.isInPlayAni) {
  206. return
  207. };
  208. if (this.refreshNum > 0){
  209. this.refreshNum--;
  210. this.grid.children.forEach(item => {
  211. if (item.name != "bg"){
  212. item.destroy();
  213. }
  214. })
  215. this.gameModel = new GameModel();
  216. this.gameModel.init(4);
  217. this.gridScript = this.grid.getComponent("GridView");
  218. this.gridScript.setController(this);
  219. // console.log("this:",this)
  220. this.gridScript.initWithCellModels(this.gameModel.getCells());
  221. // console.log(this.refreshNum)
  222. }else {
  223. Toast("刷新次数已用完")
  224. }
  225. },
  226. showSkillTip(isShow){
  227. // console.log("isShow:",isShow)
  228. this.skillTip.active = isShow;
  229. },
  230. //道具消除整行
  231. onSkillRowAll(){
  232. // console.log("点击了消除整行按钮")
  233. cc.audioEngine.play(this.click, false, 1);
  234. if (this.gridScript.isInPlayAni) {
  235. return
  236. }
  237. if (this.crushRowAllNum > 0){
  238. // this.crushRowAllNum--;
  239. this.isCrushRow = true;
  240. const msg = this.skillTip.getChildByName("msg");
  241. msg.getComponent(cc.Label).string = "选择需要删除的行";
  242. this.showSkillTip(true);
  243. }else {
  244. Toast("道具已用完")
  245. }
  246. },
  247. //道具消除整列
  248. onSkillColAll(){
  249. // console.log("点击了消除整列")
  250. cc.audioEngine.play(this.click, false, 1); //增加点击音效
  251. if (this.gridScript.isInPlayAni) {
  252. return
  253. }
  254. if (this.crushColAllNum > 0){
  255. // this.crushColAllNum--;
  256. this.isCrushCol = true;
  257. const msg = this.skillTip.getChildByName("msg");
  258. msg.getComponent(cc.Label).string = "选择需要删除的列";
  259. this.showSkillTip(true);
  260. }else {
  261. Toast("道具已用完")
  262. }
  263. },
  264. //道具消除一种类型
  265. onSkillOneType(){
  266. cc.audioEngine.play(this.click, false, 1);
  267. if (this.gridScript.isInPlayAni) {
  268. return
  269. }
  270. if (this.crushOneTypeNum > 0){
  271. // this.crushOneTypeNum--;
  272. this.isCrushOneType = true;
  273. const msg = this.skillTip.getChildByName("msg");
  274. msg.getComponent(cc.Label).string = "选择需要删除的类型";
  275. this.showSkillTip(true);
  276. }else {
  277. Toast("道具已用完")
  278. }
  279. }
  280. });
  281. /**
  282. * 组件脚本:
  283. * AudioUtils.js、CellView.js、EffectLayer.js、GameController.js、GridView.js、LoginController.js
  284. *
  285. */