mainctrl.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. // self.rivalserve.ballServe();
  68. self.ball.rivalServer();
  69. self.ball.ballAction();
  70. }
  71. // 添加触摸开始事件
  72. this.node.on (cc.Node.EventType.TOUCH_START, function(event) {
  73. // 如果是处于结束状态,则无法响应
  74. if (self.isEnd) {
  75. return;
  76. }
  77. var touches = event.getTouches();
  78. var loc = self.node.convertToNodeSpaceAR(touches[0].getLocation());
  79. var value = Math.random() * 2;
  80. // 根据位置判断我方的方向
  81. if (loc.x < 0) {
  82. cc.log ("在左侧 ... ")
  83. // 左侧可接,暂时我方左侧只有这个功能
  84. self.mine.pat.pat();
  85. self.mine.setDir(1);
  86. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_LEFT_PAT) {
  87. self.ball.ballStop();
  88. if (value > 1) {
  89. // 往左边打
  90. self.ball.minePatLToL_01();
  91. } else {
  92. // 往右边打
  93. self.ball.minePatLToR_01();
  94. }
  95. }
  96. } else {
  97. cc.log ("来右边 ... ")
  98. self.mine.setDir(2);
  99. if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PAT) {
  100. self.ball.ballStop();
  101. // 右侧可接
  102. self.mine.pat.pat();
  103. // 这个里面只有往右的
  104. self.ball.minePatRToR_01();
  105. } else if (self.ballstate == self.ballconfig.BALL_STATE_MINE_RIGHT_PEEL) {
  106. self.ball.ballStop();
  107. // 右侧可削
  108. self.mine.peel.peel();
  109. // 这个里面只有往左的
  110. self.ball.minePeelRToL_01();
  111. } else {
  112. self.mine.pat.pat();
  113. }
  114. }
  115. });
  116. // 配置球的状态判断回调
  117. this.ball.stateCallback = function(state) {
  118. self.ballstate = state;
  119. };
  120. },
  121. // 功能性方法
  122. // 帧判断,用来检测球状态,给对手的行为进行决策
  123. rivalUpdate:function (delay) {
  124. var value = Math.random() * 2;
  125. if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PAT) {
  126. this.ball.ballStop();
  127. this.rival.pat.pat();
  128. this.rival.setDir(3);
  129. // 敌方左侧可接
  130. if (value > 1) {
  131. // 往左侧打
  132. this.ball.rivalPatLToL_01();
  133. } else {
  134. // 往右侧打
  135. this.ball.rivalPatLToR_01();
  136. }
  137. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_LEFT_PEEL) {
  138. this.ball.ballStop();
  139. this.rival.peel.peel();
  140. this.rival.setDir(3);
  141. // 敌方左侧可削
  142. if (value > 1) {
  143. // 往左侧削
  144. this.ball.rivalPeelLtoL_01();
  145. } else {
  146. // 往右侧削
  147. this.ball.rivalPeelLtoR_01();
  148. }
  149. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PAT) {
  150. this.ball.ballStop();
  151. this.rival.pat.pat();
  152. this.rival.setDir(4);
  153. var index = Math.random() * 2;
  154. // 敌方右侧可接
  155. if (value > 1) {
  156. // 往左侧打
  157. if (index > 1) {
  158. this.ball.rivalPatRToL_01();
  159. } else {
  160. this.ball.rivalPatRToL_02();
  161. }
  162. } else {
  163. // 往右侧打
  164. if (index > 1) {
  165. this.ball.rivalPatRToR_01();
  166. } else {
  167. this.ball.rivalPatRToR_02();
  168. }
  169. }
  170. } else if (this.ballstate == this.ballconfig.BALL_STATE_RIVAL_RIGHT_PEEL) {
  171. // 没有
  172. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_MINE) {
  173. // 掉落到我方方位
  174. this.win = false;
  175. this.roundOver();
  176. } else if (this.ballstate == this.ballconfig.BALL_STATE_DROP_IN_RIVAL) {
  177. // 掉落到敌方方位
  178. this.win = true;
  179. this.roundOver();
  180. }
  181. },
  182. // 游戏开始
  183. gameStart:function() {
  184. this.ball.ballEnd();
  185. // 将双方置为等待状态
  186. this.mine.idle.idle();
  187. // 设置面板信息
  188. this.scorepanel.active = true;
  189. this.operatepanel.active = true;
  190. this.endpanel.active = false;
  191. // 其他数据
  192. this.isEnd = false;
  193. // 游戏开始,对方开始发球
  194. this.rival.setDir(4);
  195. this.rival.roleServe();
  196. this.schedule(this.rivalUpdate, 1);
  197. },
  198. // 回合结束
  199. roundOver:function() {
  200. this.unschedule(this.rivalUpdate);
  201. this.isEnd = true;
  202. if (this.win) {
  203. cc.log ("游戏结束,胜利了 .. ")
  204. this.mine.win.win();
  205. this.rival.fail.fail();
  206. this.mines.value ++;
  207. this.mines.updateValue();
  208. if (this.mines.value == 5) {
  209. this.scheduleOnce(this.gameOver, 3);
  210. return;
  211. }
  212. } else {
  213. cc.log ("输了 ... ")
  214. this.rival.win.win();
  215. this.mine.fail.fail();
  216. this.rivals.value ++;
  217. this.rivals.updateValue();
  218. if (this.rivals.value == 5) {
  219. this.scheduleOnce(this.gameOver, 3);
  220. return;
  221. }
  222. }
  223. this.scheduleOnce(this.gameStart, 3);
  224. },
  225. // 游戏结束
  226. gameOver:function() {
  227. if (this.mines.value == 5) {
  228. cc.log("我方胜利 ... ");
  229. } else {
  230. cc.log("敌方胜利 ... ");
  231. }
  232. this.scorepanel.active = false;
  233. this.operatepanel.active = false;
  234. this.endpanel.active = true;
  235. this.mines.value = 0;
  236. this.mines.updateValue();
  237. this.rivals.value = 0;
  238. this.rivals.updateValue();
  239. },
  240. // 事件
  241. // 重新开始
  242. restartClicked:function() {
  243. this.gameStart();
  244. },
  245. // 点击排行榜
  246. rankClicked:function() {
  247. this.rank.showRank();
  248. }
  249. });