rivalpatrtol.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. this.flyRight = 123;
  26. this.flyLeft = -21;
  27. this.flipLeft = -140;
  28. // 配置方程式的各项数据
  29. // 飞翔时候的数据
  30. this.flyX = 0;
  31. this.flyY = 70;
  32. this.flyDown = 2.35;
  33. // 弹起来时候的数据
  34. this.flipX = -5;
  35. this.flipY = 25;
  36. this.flipDown = -80;
  37. },
  38. // update (dt) {},
  39. // 发球时的方程式
  40. equation:function(x) {
  41. // return (x * x - this.flipX * 2 * x + this.flipX * this.flipX) / this.flipDown + this.flipY;
  42. if (x <= this.flyRight + 10 && x > this.flyLeft) {
  43. return x * this.flyDown + this.flyY;
  44. } else if (x <= this.flyLeft && x > this.flipLeft) {
  45. return (x * x - this.flipX * 2 * x + this.flipX * this.flipX) / this.flipDown + this.flipY;
  46. }
  47. return (x * x - this.flipX * 2 * x + this.flipX * this.flipX) / this.flipDown + this.flipY;
  48. },
  49. ballPat:function() {
  50. this.unschedule(this.patUpdate);
  51. this.x = this.flyRight;
  52. this.schedule(this.patUpdate, 1.0 / 60);
  53. },
  54. ballStop:function() {
  55. this.unschedule(this.patUpdate);
  56. },
  57. patUpdate:function (dt) {
  58. this.g.clear();
  59. var speed = 0;
  60. var state;
  61. if (this.x <= this.flyRight + 10 && this.x > this.flyLeft) {
  62. speed = 7;
  63. state = this.ballconfig.BALL_STATE_RIVAL_RIGHT_TO_LEFT_FLY;
  64. } else if (this.x <= this.flyLeft && this.x > this.flipLeft) {
  65. speed = 5;
  66. state = this.ballconfig.BALL_STATE_RIVAL_RIGHT_TO_LEFT_FLIP;
  67. } else if (this.x <= this.flipLeft) {
  68. speed = 5;
  69. state = this.ballconfig.BALL_STATE_DROP_IN_MINE;
  70. }
  71. if (this.stateCallback != null) {
  72. // 给控制器传递发球左侧的状态
  73. this.stateCallback(state);
  74. }
  75. // 判断是否需要磕碰
  76. var next = this.x - speed;
  77. if (this.x > this.flyLeft && next <= this.flyLeft) {
  78. this.bump.bump(cc.v2(this.x, this.equation(this.x)));
  79. }
  80. this.x = next;
  81. this.g.lineWidth = 3;
  82. this.g.moveTo(this.x, this.equation(this.x));
  83. 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));
  84. this.g.stroke();
  85. },
  86. });