minepatrtor.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. start () {
  16. this.initValues();
  17. },
  18. // 初始化
  19. initValues:function() {
  20. // 球的状态信息
  21. this.ballconfig = require("ballconfig");
  22. // 球的运动区间
  23. this.flyLeft = 54;
  24. this.flyRight = 69;
  25. this.flipRight = 120;
  26. // 配置方程式的各项数据
  27. // 飞翔时候的数据
  28. this.flyX = 0;
  29. this.flyY = -860;
  30. this.flyDown = 14.2;
  31. // 弹起来时候的数据
  32. this.flipX = 0;
  33. this.flipY = -340;
  34. this.flipDown = 6.8;
  35. },
  36. // update (dt) {},
  37. // 发球时的方程式
  38. equation:function(x) {
  39. if (x >= this.flyLeft - 10 && x <= this.flyRight) {
  40. return x * this.flyDown + this.flyY;
  41. } else if (x > this.flyRight && x <= this.flipRight) {
  42. return x * this.flipDown + this.flipY;
  43. }
  44. return x * this.flipDown + this.flipY;
  45. },
  46. ballPat:function() {
  47. this.unschedule(this.patUpdate);
  48. this.x = this.flyLeft;
  49. this.schedule(this.patUpdate, 1.0 / 60);
  50. },
  51. ballStop:function() {
  52. this.unschedule(this.patUpdate);
  53. },
  54. patUpdate:function (dt) {
  55. this.g.clear();
  56. var speed = 0;
  57. var state;
  58. if (this.x >= this.flyLeft - 10 && this.x < this.flyRight) {
  59. speed = 1;
  60. state = this.ballconfig.BALL_STATE_MINE_RIGHT_TO_RIGHT_FLY;
  61. } else if (this.x >= this.flyRight && this.x < this.flipRight) {
  62. speed = 1;
  63. state = this.ballconfig.BALL_STATE_MINE_RIGHT_TO_RIGHT_FLIP;
  64. } else if (this.x >= this.flipRight) {
  65. speed = 1;
  66. state = this.ballconfig.BALL_STATE_DROP_IN_RIVAL;
  67. }
  68. if (this.stateCallback != null) {
  69. // 给控制器传递发球左侧的状态
  70. this.stateCallback(state);
  71. }
  72. // 判断是否需要磕碰
  73. var next = this.x + speed;
  74. if (this.x < this.flyRight && next >= this.flyRight) {
  75. this.bump.bump(cc.v2(this.x, this.equation(this.x)));
  76. }
  77. this.x = next;
  78. this.g.lineWidth = 3;
  79. this.g.moveTo(this.x, this.equation(this.x));
  80. this.g.bezierCurveTo(this.x, this.equation(this.x), this.x - 5, this.equation(this.x - 5), this.x - 10, this.equation(this.x - 10));
  81. this.g.stroke();
  82. },
  83. });