mainctrl.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // LIFE-CYCLE CALLBACKS:
  47. // onLoad () {},
  48. start () {
  49. this.initValues();
  50. this.initListener();
  51. // 游戏开始
  52. this.gameStart();
  53. },
  54. // 初始化数据
  55. initValues:function() {
  56. // 球的状态信息
  57. this.ballconfig = require("ballconfig");
  58. // 当前球的状态,初始化为发球开始状态
  59. this.ballstate = this.ballconfig.BALL_STATE_SERVE_START;
  60. this.mineScore = 0;
  61. this.rivalScore = 0;
  62. },
  63. // 初始化监听
  64. initListener:function() {
  65. var self = this;
  66. this.rival.serveStart = function() {
  67. cc.log ("发球了 ... ")
  68. // self.rivalserve.ballServe();
  69. self.ball.rivalServer();
  70. self.ball.ballAction();
  71. }
  72. // 添加触摸开始事件
  73. this.node.on (cc.Node.EventType.TOUCH_START, function(event) {
  74. // 如果是处于结束状态,则无法响应
  75. if (self.isEnd) {
  76. return;
  77. }
  78. var touches = event.getTouches();
  79. var loc = self.node.convertToNodeSpaceAR(touches[0].getLocation());
  80. var value = Math.random() * 2;
  81. // 根据位置判断我方的方向
  82. if (loc.x < 0) {
  83. cc.log ("在左侧 ... ")
  84. // 左侧可接,暂时我方左侧只有这个功能
  85. self.mine.pat.pat();
  86. self.mine.setDir(1);
  87. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_LEFT_PAT) {
  88. self.ball.ballStop();
  89. if (value > 1) {
  90. // 往左边打
  91. self.ball.minePatLToL_01();
  92. } else {
  93. // 往右边打
  94. self.ball.minePatLToR_01();
  95. }
  96. }
  97. } else {
  98. cc.log ("来右边 ... ")
  99. self.mine.setDir(2);
  100. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PAT) {
  101. self.ball.ballStop();
  102. // 右侧可接
  103. self.mine.pat.pat();
  104. // 这个里面只有往右的
  105. self.ball.minePatRToR_01();
  106. } else if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PEEL) {
  107. // 右侧可削
  108. self.mine.peel.peel();
  109. self.scheduleOnce (function() {
  110. // 需要保证不是在结束状态
  111. if (!this.isEnd) {
  112. self.ball.ballStop();
  113. // 这个里面只有往左的
  114. self.ball.minePeelRToL_01();
  115. }
  116. }, 0.2);
  117. } else {
  118. self.mine.pat.pat();
  119. }
  120. }
  121. });
  122. // 配置球的状态判断回调
  123. this.ball.stateCallback = function(state) {
  124. // 此处为测试球运动专用功能
  125. // if (state == this.ballconfig.BALL_STATE_DROP_IN_RIVAL ||
  126. // state == this.ballconfig.BALL_STATE_DROP_IN_MINE) {
  127. // return
  128. // }
  129. self.ballstate = state;
  130. };
  131. },
  132. // 功能性方法
  133. // 帧判断,用来检测球状态,给对手的行为进行决策
  134. rivalUpdate:function (delay) {
  135. // 需要保证不打扰削球动作
  136. if (this.rival.ispeel == true) {
  137. return;
  138. }
  139. this.rival.ispeel = false;
  140. var value = Math.random() * 2;
  141. if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PAT) {
  142. this.ball.ballStop();
  143. this.rival.pat.pat();
  144. this.rival.setDir(3);
  145. // 敌方左侧可接
  146. if (value > 1) {
  147. // 往左侧打
  148. this.ball.rivalPatLToL_01();
  149. } else {
  150. // 往右侧打
  151. this.ball.rivalPatLToR_01();
  152. }
  153. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PEEL) {
  154. this.rival.ispeel = true;
  155. this.rival.peel.peel();
  156. this.rival.setDir(3);
  157. var self = this;
  158. this.scheduleOnce (function() {
  159. self.rival.ispeel = false;
  160. if (!this.isEnd) {
  161. this.ball.ballStop();
  162. // 敌方左侧可削
  163. if (value > 1) {
  164. // 往左侧削
  165. this.ball.rivalPeelLtoL_01();
  166. } else {
  167. // 往右侧削
  168. this.ball.rivalPeelLtoR_01();
  169. }
  170. }
  171. }, 0.2);
  172. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PAT) {
  173. this.ball.ballStop();
  174. this.rival.pat.pat();
  175. this.rival.setDir(4);
  176. var index = Math.random() * 2;
  177. // 敌方右侧可接
  178. if (value > 1) {
  179. // 往左侧打
  180. if (index > 1) {
  181. this.ball.rivalPatRToL_01();
  182. } else {
  183. this.ball.rivalPatRToL_02();
  184. }
  185. } else {
  186. // 往右侧打
  187. if (index > 1) {
  188. this.ball.rivalPatRToR_01();
  189. } else {
  190. this.ball.rivalPatRToR_02();
  191. }
  192. }
  193. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PEEL) {
  194. // 没有
  195. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_MINE) {
  196. // 掉落到我方方位
  197. this.win = false;
  198. this.roundOver();
  199. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_RIVAL) {
  200. // 掉落到敌方方位
  201. this.win = true;
  202. this.roundOver();
  203. }
  204. },
  205. // 游戏开始
  206. gameStart:function() {
  207. this.ballstate = this.ballconfig.BALL_STATE_SERVE_START;
  208. this.ball.ballEnd();
  209. // 将双方置为等待状态
  210. this.mine.idle.idle();
  211. // 设置面板信息
  212. this.scorepanel.active = true;
  213. this.operatepanel.active = true;
  214. this.endpanel.active = false;
  215. // 其他数据
  216. this.isEnd = false;
  217. // 游戏开始,对方开始发球
  218. this.rival.setDir(4);
  219. this.rival.roleServe();
  220. cc.log ("发起了 .. ")
  221. this.schedule(this.rivalUpdate, 1.0 / 60);
  222. },
  223. // 回合结束
  224. roundOver:function() {
  225. this.unschedule(this.rivalUpdate);
  226. this.isEnd = true;
  227. // 停止动作,主要是为了停止回调
  228. this.mine.stop();
  229. this.rival.stop();
  230. if (this.win) {
  231. cc.log ("游戏结束,胜利了 .. ")
  232. this.mine.win.win();
  233. this.rival.fail.fail();
  234. this.mines.value ++;
  235. this.mines.updateValue();
  236. if (this.mines.value == 5) {
  237. this.scheduleOnce(this.gameOver, 3);
  238. return;
  239. }
  240. } else {
  241. cc.log ("输了 ... ")
  242. this.rival.win.win();
  243. this.mine.fail.fail();
  244. this.rivals.value ++;
  245. this.rivals.updateValue();
  246. if (this.rivals.value == 5) {
  247. this.scheduleOnce(this.gameOver, 3);
  248. return;
  249. }
  250. }
  251. this.scheduleOnce(this.gameStart, 3);
  252. },
  253. // 游戏结束
  254. gameOver:function() {
  255. if (this.mines.value == 5) {
  256. cc.log("我方胜利 ... ");
  257. } else {
  258. cc.log("敌方胜利 ... ");
  259. }
  260. this.scorepanel.active = false;
  261. this.operatepanel.active = false;
  262. this.endpanel.active = true;
  263. this.mines.value = 0;
  264. this.mines.updateValue();
  265. this.rivals.value = 0;
  266. this.rivals.updateValue();
  267. },
  268. // 事件
  269. // 重新开始
  270. restartClicked:function() {
  271. this.gameStart();
  272. },
  273. // 点击排行榜
  274. rankClicked:function() {
  275. if (cc.sys.platform == cc.sys.WECHAT_GAME) {
  276. wx.shareAppMessage({
  277. withShareTicket: true,
  278. title: '我要主动拉起分享啦',
  279. success: function(shareTickets,groupMsgInfos){console.log('主动成功')},
  280. fail: function(res){console.log('主动失败');console.log(res);},
  281. });
  282. }
  283. }
  284. });