mainctrl.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Scene的控制器
  2. // 管理角色和球的信息
  3. var Role = require("role")
  4. var Ball = require("ball")
  5. var Numlabel = require("numlabel")
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. rival: {
  10. default: null,
  11. type: Role,
  12. },
  13. mine: {
  14. default: null,
  15. type: Role,
  16. },
  17. ball: {
  18. default: null,
  19. type: Ball,
  20. },
  21. // 分数标签
  22. mines: {
  23. default: null,
  24. type: Numlabel,
  25. },
  26. rivals: {
  27. default: null,
  28. type: Numlabel,
  29. },
  30. // 几个
  31. scorepanel: {
  32. default: null,
  33. type: cc.Node,
  34. },
  35. // 操作面板
  36. operatepanel: {
  37. default: null,
  38. type: cc.Node,
  39. },
  40. // 结束面板
  41. endpanel: {
  42. default: null,
  43. type: cc.Node,
  44. },
  45. // 音效
  46. // 击打
  47. crashclip: {
  48. default: null,
  49. type: cc.AudioClip,
  50. },
  51. winclip: {
  52. default: null,
  53. type: cc.AudioClip,
  54. },
  55. failclip: {
  56. default: null,
  57. type: cc.AudioClip,
  58. },
  59. // 摄像机
  60. camera: {
  61. default: null,
  62. type: cc.Camera,
  63. },
  64. },
  65. // LIFE-CYCLE CALLBACKS:
  66. // onLoad () {},
  67. start () {
  68. this.initValues();
  69. this.initListener();
  70. // 游戏开始
  71. this.gameStart();
  72. },
  73. // 初始化数据
  74. initValues:function() {
  75. // 球的状态信息
  76. this.ballconfig = require("ballconfig");
  77. // 加载全局变量
  78. this.global = require("global");
  79. // 当前球的状态,初始化为发球开始状态
  80. this.ballstate = this.ballconfig.BALL_STATE_SERVE_START;
  81. // 我方得分和敌方得分
  82. this.mineScore = 0;
  83. this.rivalScore = 0;
  84. var index = Math.random() * 3;
  85. if (index > 2) {
  86. this.camera.backgroundColor = new cc.Color().fromHEX("#FFB4DE");
  87. } else if (index > 1 && index < 2) {
  88. this.camera.backgroundColor = new cc.Color().fromHEX("#FFF8E0");
  89. } else {
  90. this.camera.backgroundColor = new cc.Color().fromHEX("#E0FFF0");
  91. }
  92. },
  93. // 初始化监听
  94. initListener:function() {
  95. var self = this;
  96. this.rival.serveStart = function() {
  97. cc.log ("发球了 ... ")
  98. // self.rivalserve.ballServe();
  99. self.ball.rivalServer();
  100. self.ball.ballAction();
  101. // 发出撞击声
  102. self.scheduleOnce (function() {
  103. cc.audioEngine.play(self.crashclip, false, 1);
  104. }, 1.2);
  105. }
  106. // 添加触摸开始事件
  107. this.node.on (cc.Node.EventType.TOUCH_START, function(event) {
  108. // 如果是处于结束状态,则无法响应
  109. if (self.isEnd) {
  110. return;
  111. }
  112. var touches = event.getTouches();
  113. var loc = self.node.convertToNodeSpaceAR(touches[0].getLocation());
  114. var value = Math.random() * 2;
  115. // 根据位置判断我方的方向
  116. if (loc.x < 0) {
  117. console.log ("在左侧 ... ")
  118. // 左侧可接,暂时我方左侧只有这个功能
  119. self.mine.pat.pat();
  120. self.mine.setDir(1);
  121. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_LEFT_PAT) {
  122. self.ball.ballStop();
  123. cc.audioEngine.play(self.crashclip, false, 1);
  124. // self.ball.minePatLToL_01();
  125. if (value > 1) {
  126. console.log ("往左侧打 ... ")
  127. // 往左边打
  128. self.ball.minePatLToL_01();
  129. } else {
  130. console.log ("往左侧打 ... ")
  131. // 往右边打
  132. self.ball.minePatLToR_01();
  133. }
  134. }
  135. } else {
  136. console.log ("来右边 ... ")
  137. self.mine.setDir(2);
  138. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PAT) {
  139. self.ball.ballStop();
  140. cc.audioEngine.play(self.crashclip, false, 1);
  141. // 右侧可接
  142. self.mine.pat.pat();
  143. // 这个里面只有往右的
  144. self.ball.minePatRToR_01();
  145. } else if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PEEL) {
  146. // 右侧可削
  147. self.mine.peel.peel();
  148. self.scheduleOnce (function() {
  149. // 需要保证不是在结束状态
  150. if (!this.isEnd) {
  151. self.ball.ballStop();
  152. cc.audioEngine.play(self.crashclip, false, 1);
  153. // 这个里面只有往左的
  154. self.ball.minePeelRToL_01();
  155. }
  156. }, 0.2);
  157. } else {
  158. self.mine.pat.pat();
  159. }
  160. }
  161. });
  162. // 配置球的状态判断回调
  163. this.ball.stateCallback = function(state) {
  164. // 此处为测试球运动专用功能
  165. // if (state == this.ballconfig.BALL_STATE_DROP_IN_RIVAL ||
  166. // state == this.ballconfig.BALL_STATE_DROP_IN_MINE) {
  167. // return
  168. // }
  169. self.ballstate = state;
  170. };
  171. },
  172. // 功能性方法
  173. // 帧判断,用来检测球状态,给对手的行为进行决策
  174. rivalUpdate:function (delay) {
  175. // 如果是无限模式,则永远不会失败
  176. if (this.global.infiMode == false) {
  177. // 否则需要进行校验
  178. var count = parseInt(Math.random() * 100);
  179. if (count > 15) {
  180. return;
  181. }
  182. } else {
  183. // 时间戳增加,用来调整难度
  184. this.timestamp++;
  185. // 保证速度不会小于50
  186. if (this.ball.speed > 50) {
  187. this.ball.speed -= 8.0 / 60;
  188. }
  189. }
  190. // 需要保证不打扰削球动作
  191. if (this.rival.ispeel == true) {
  192. return;
  193. }
  194. this.rival.ispeel = false;
  195. var value = Math.random() * 2;
  196. if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PAT) {
  197. this.ball.ballStop();
  198. cc.audioEngine.play(this.crashclip, false, 1);
  199. this.rival.pat.pat();
  200. this.rival.setDir(3);
  201. // 敌方左侧可接
  202. if (value > 1) {
  203. // 往左侧打
  204. this.ball.rivalPatLToL_01();
  205. } else {
  206. // 往右侧打
  207. this.ball.rivalPatLToR_01();
  208. }
  209. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PEEL) {
  210. this.rival.ispeel = true;
  211. this.rival.peel.peel();
  212. this.rival.setDir(3);
  213. var self = this;
  214. this.scheduleOnce (function() {
  215. self.rival.ispeel = false;
  216. if (!this.isEnd) {
  217. this.ball.ballStop();
  218. cc.audioEngine.play(self.crashclip, false, 1);
  219. // 敌方左侧可削
  220. if (value > 1) {
  221. // 往左侧削
  222. this.ball.rivalPeelLtoL_01();
  223. } else {
  224. // 往右侧削
  225. this.ball.rivalPeelLtoR_01();
  226. }
  227. }
  228. }, 0.2);
  229. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PAT) {
  230. this.ball.ballStop();
  231. cc.audioEngine.play(this.crashclip, false, 1);
  232. this.rival.pat.pat();
  233. this.rival.setDir(4);
  234. var index = Math.random() * 2;
  235. // 敌方右侧可接
  236. if (value > 1) {
  237. // 往左侧打
  238. if (index > 1) {
  239. this.ball.rivalPatRToL_01();
  240. } else {
  241. this.ball.rivalPatRToL_02();
  242. }
  243. } else {
  244. // 往右侧打
  245. if (index > 1) {
  246. this.ball.rivalPatRToR_01();
  247. } else {
  248. this.ball.rivalPatRToR_02();
  249. }
  250. }
  251. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PEEL) {
  252. // 没有
  253. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_MINE) {
  254. // 掉落到我方方位
  255. this.win = false;
  256. this.roundOver();
  257. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_RIVAL) {
  258. // 掉落到敌方方位
  259. this.win = true;
  260. this.roundOver();
  261. }
  262. },
  263. // 游戏开始
  264. gameStart:function() {
  265. this.ballstate = this.ballconfig.BALL_STATE_SERVE_START;
  266. this.ball.ballEnd();
  267. // 将双方置为等待状态
  268. this.mine.idle.idle();
  269. // 设置面板信息
  270. this.scorepanel.active = true;
  271. this.operatepanel.active = true;
  272. this.endpanel.active = false;
  273. // 其他数据
  274. this.isEnd = false;
  275. // 时间戳
  276. this.timestamp = 0;
  277. // 初始化速度
  278. this.ball.speed = 100;
  279. // 游戏开始,对方开始发球
  280. this.rival.setDir(4);
  281. this.rival.roleServe();
  282. this.schedule(this.rivalUpdate, 1.0 / 45);
  283. },
  284. // 回合结束
  285. roundOver:function() {
  286. if (this.global.infiMode) {
  287. this.global.score = this.timestamp / 45;
  288. console.log ("时间 -> " + this.global.score + ", 时间 = " + this.timestamp / 45)
  289. this.global.saveBestTime();
  290. }
  291. this.unschedule(this.rivalUpdate);
  292. this.isEnd = true;
  293. // 停止动作,主要是为了停止回调
  294. this.mine.stop();
  295. this.rival.stop();
  296. if (this.win) {
  297. cc.log ("游戏结束,胜利了 .. ")
  298. cc.audioEngine.play(this.winclip, false, 1);
  299. this.mine.win.win();
  300. this.rival.fail.fail();
  301. this.mines.value ++;
  302. this.mines.updateValue();
  303. if (this.mines.value == 5) {
  304. this.scheduleOnce(this.gameOver, 3);
  305. return;
  306. }
  307. } else {
  308. cc.log ("输了 ... ")
  309. cc.audioEngine.play(this.failclip, false, 1);
  310. this.rival.win.win();
  311. this.mine.fail.fail();
  312. this.rivals.value ++;
  313. this.rivals.updateValue();
  314. if (this.rivals.value == 5) {
  315. this.scheduleOnce(this.gameOver, 3);
  316. return;
  317. }
  318. }
  319. this.scheduleOnce(this.gameStart, 3);
  320. },
  321. // 游戏结束
  322. gameOver:function() {
  323. if (!this.global.infiMode) {
  324. if (this.mines.value == 5) {
  325. cc.log("我方胜利 ... ");
  326. // 得一分
  327. this.global.score ++;
  328. this.global.saveBestScore();
  329. } else {
  330. cc.log("敌方胜利 ... ");
  331. }
  332. }
  333. this.scorepanel.active = false;
  334. this.operatepanel.active = false;
  335. this.endpanel.active = true;
  336. this.mines.value = 0;
  337. this.mines.updateValue();
  338. this.rivals.value = 0;
  339. this.rivals.updateValue();
  340. },
  341. // 事件
  342. // 返回
  343. backClicked:function() {
  344. cc.director.loadScene("startscene")
  345. },
  346. // 重新开始
  347. restartClicked:function() {
  348. this.gameStart();
  349. },
  350. // 点击排行榜
  351. rankClicked:function() {
  352. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  353. wx.shareAppMessage({
  354. withShareTicket: true,
  355. title: '乒乓高手',
  356. query: "mode=" + "pingpong",
  357. imageUrl: 'https://pics6.baidu.com/feed/a8014c086e061d95efd7d3f118ee0dd463d9ca02.jpeg?token=5f7a71d9129d97b8411b4965f7e23a7c&s=A8E2AF0810E09ABEE6B56DDE010050A2',
  358. success: function(shareTickets,groupMsgInfos) {
  359. console.log('主动成功')
  360. }, fail: function(res) {
  361. console.log('主动失败');
  362. console.log(res);
  363. },
  364. });
  365. }
  366. }
  367. });