GameController.js 11 KB

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