minepatltor.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.flyLeft = -71;
  26. this.flyRight = 40;
  27. this.flipRight = 218;
  28. // 配置方程式的各项数据
  29. // 飞翔时候的数据
  30. this.flyX = 195;
  31. this.flyY = 230;
  32. this.flyDown = -250;
  33. // 弹起来时候的数据
  34. this.flipX = 183;
  35. this.flipY = 349;
  36. this.flipDown = -94;
  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.flyLeft - 10 && x <= this.flyRight) {
  43. return (x * x - this.flyX * 2 * x + this.flyX * this.flyX) / this.flyDown + this.flyY;
  44. } else if (x > this.flyRight && x <= this.flipRight) {
  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.flyLeft;
  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.flyLeft - 10 && this.x < this.flyRight) {
  62. speed = 10;
  63. state = this.ballconfig.BALL_STATE_MINE_LEFT_TO_RIGHT_FLY;
  64. } else if (this.x >= this.flyRight && this.x < this.flipRight) {
  65. speed = 10;
  66. state = this.ballconfig.BALL_STATE_MINE_LEFT_TO_RIGHT_FLIP;
  67. } else if (this.x >= this.flipRight) {
  68. speed = 10;
  69. state = this.ballconfig.BALL_STATE_DROP_IN_RIVAL;
  70. }
  71. if (this.stateCallback != null) {
  72. // 给控制器传递发球左侧的状态
  73. this.stateCallback(state);
  74. }
  75. // 判断是否需要磕碰
  76. var next = this.x + speed;
  77. if (this.x < this.flyRight && next > this.flyRight) {
  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. });