rivalserve.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // 对手发球的运动
  2. var Bump = require("bump")
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. g: {
  7. default: null,
  8. type: cc.Graphics,
  9. },
  10. bump: {
  11. default: null,
  12. type: Bump,
  13. },
  14. },
  15. // LIFE-CYCLE CALLBACKS:
  16. // onLoad () {},
  17. start () {
  18. this.initValues();
  19. },
  20. // 初始化数据
  21. initValues:function() {
  22. // 球的状态信息
  23. this.ballconfig = require("ballconfig");
  24. // 球的动作
  25. // 发球开始的区间
  26. this.serveStartRight = 167;
  27. this.serveStartLeft = 105;
  28. // 发球右的区间
  29. this.serveRightLeft = 28;
  30. // 发球中的区间
  31. this.serveMidLeft = -10;
  32. // 发球左的区间
  33. this.serveLeftLeft = -100;
  34. // 配置方程式的各项数据
  35. // 发球时数据
  36. this.serveStartX = 135;
  37. this.serveStartY = 350;
  38. this.serveStartDown = -30;
  39. // 发球时右边位置数据
  40. this.serveRightX = 118;
  41. this.serveRightY = 285;
  42. this.serveRightDown = -45;
  43. // 发球时中间位置数据
  44. this.serveMidX = 118;
  45. this.serveMidY = 190;
  46. this.serveMidDown = -95;
  47. // 发球时左侧的位置数据
  48. this.serveLeftX = 0;
  49. this.serveLeftY = 18;
  50. this.serveLeftDown = -92;
  51. this.g.lineWidth = 4;
  52. this.g.fillColor.fromHEX('#ffffff');
  53. this.x = this.serveStartRight;
  54. },
  55. ballServe:function() {
  56. this.unschedule(this.ballUpdate);
  57. this.x = this.serveStartRight;
  58. this.schedule(this.ballUpdate, 1.0 / 60);
  59. },
  60. ballStop:function() {
  61. this.unschedule(this.ballUpdate);
  62. },
  63. ballUpdate:function (dt) {
  64. this.g.clear();
  65. var speed = 0;
  66. var state;
  67. if (this.x <= this.serveStartRight + 10 && this.x >= this.serveStartLeft) {
  68. speed = 1.309;
  69. state = this.ballconfig.BALL_STATE_SERVE_START;
  70. } else if (this.x < this.serveStartLeft && this.x >= this.serveRightLeft) {
  71. speed = 12;
  72. state = this.ballconfig.BALL_STATE_SERVE_RIGHT;
  73. } else if (this.x < this.serveRightLeft && this.x >= this.serveMidLeft) {
  74. speed = 9;
  75. state = this.ballconfig.BALL_STATE_SERVE_MID;
  76. } else if (this.x < this.serveMidLeft && this.x >= this.serveLeftLeft) {
  77. speed = 8;
  78. state = this.ballconfig.BALL_STATE_SERVE_LEFT;
  79. } else {
  80. speed = 8;
  81. state = this.ballconfig.BALL_STATE_DROP_IN_MINE;
  82. }
  83. if (this.stateCallback != null) {
  84. // 给控制器传递发球左侧的状态
  85. this.stateCallback(state);
  86. }
  87. // 判断是否需要磕碰
  88. var next = this.x - speed;
  89. if (this.x > this.serveMidLeft && next < this.serveMidLeft) {
  90. this.bump.bump(cc.v2(this.x, this.serveEquation(this.x)));
  91. }
  92. this.x = next;
  93. this.g.lineWidth = 3;
  94. this.g.moveTo(this.x, this.serveEquation(this.x));
  95. this.g.bezierCurveTo(this.x, this.serveEquation(this.x), this.x + 5, this.serveEquation(this.x + 5), this.x + 10, this.serveEquation(this.x + 10));
  96. this.g.stroke();
  97. },
  98. // 发球时的方程式
  99. serveEquation:function(x) {
  100. // 判断各个不同的区间
  101. if (x <= this.serveStartRight + 10 && x >= this.serveStartLeft) {
  102. return (x * x - this.serveStartX * 2 * x + this.serveStartX * this.serveStartX) / this.serveStartDown + this.serveStartY;
  103. } else if (x < this.serveStartLeft && x >= this.serveRightLeft) {
  104. return (x * x - this.serveRightX * 2 * x + this.serveRightX * this.serveRightX) / this.serveRightDown + this.serveRightY;
  105. } else if (x < this.serveRightLeft && x >= this.serveMidLeft) {
  106. return (x * x - this.serveMidX * 2 * x + this.serveMidX * this.serveMidX) / this.serveMidDown + this.serveMidY;
  107. }
  108. return (x * x - this.serveLeftX * 2 * x + this.serveLeftX * this.serveLeftX) / this.serveLeftDown + this.serveLeftY;
  109. // return 0;
  110. },
  111. // 绘制坐标线
  112. drawAxle:function() {
  113. this.g.lineWidth = 2;
  114. this.g.moveTo(-1000, 0);
  115. this.g.lineTo(1000, 0);
  116. this.g.stroke();
  117. this.g.moveTo(0, -1000);
  118. this.g.lineTo(0, 1000);
  119. this.g.stroke();
  120. },
  121. });