minepeelrtor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // 我方削球,从右侧到右侧
  2. // 角色的方向
  3. var Peel_State = cc.Enum ({
  4. Peel_Fly: 1,
  5. Peel_Flip: 2
  6. });
  7. var Bump = require("bump")
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. g: {
  12. default: null,
  13. type: cc.Graphics,
  14. },
  15. bump: {
  16. default: null,
  17. type: Bump,
  18. },
  19. },
  20. // LIFE-CYCLE CALLBACKS:
  21. // onLoad () {},
  22. start () {
  23. this.initValues();
  24. },
  25. // 初始化
  26. initValues:function() {
  27. // 球的状态信息
  28. this.ballconfig = require("ballconfig");
  29. this.state = Peel_State.Peel_Fly;
  30. // 球的运动区间
  31. this.flyRight = 159;
  32. this.flyLeft = 78;
  33. this.flipRight = 109;
  34. // 配置方程式的各项数据
  35. // 飞翔时候的数据
  36. this.flyX = 75;
  37. this.flyY = 105;
  38. this.flyDown = -25;
  39. // 弹起来时候的数据
  40. this.flipX = 0;
  41. this.flipY = -800;
  42. this.flipDown = 11.8;
  43. },
  44. // 发球时的方程式
  45. equation:function(x) {
  46. if (this.state == Peel_State.Peel_Fly) {
  47. return (x * x - this.flyX * 2 * x + this.flyX * this.flyX) / this.flyDown + this.flyY;
  48. } else {
  49. return x * this.flipDown + this.flipY;
  50. }
  51. return 0;
  52. },
  53. ballPeel:function() {
  54. this.unschedule(this.peelUpdate);
  55. this.x = this.flyRight;
  56. this.schedule(this.peelUpdate, 1.0 / 60);
  57. },
  58. ballStop:function() {
  59. this.unschedule(this.peelUpdate);
  60. },
  61. peelUpdate:function (dt) {
  62. this.g.clear();
  63. var speed = 0;
  64. var state;
  65. if (this.state == Peel_State.Peel_Fly) {
  66. if (this.x >= this.flyLeft) {
  67. speed = -2;
  68. state = this.ballconfig.BALL_STATE_MINE_RIGHT_TO_RIGHT_FLY;
  69. } else {
  70. this.state = Peel_State.Peel_Flip;
  71. }
  72. } else {
  73. if (this.x <= this.flipRight) {
  74. speed = 1;
  75. state = this.ballconfig.BALL_STATE_MINE_RIGHT_TO_RIGHT_FLIP;
  76. } else {
  77. speed = 1;
  78. state = this.ballconfig.BALL_STATE_DROP_IN_RIVAL;
  79. }
  80. }
  81. if (this.stateCallback != null) {
  82. // 给控制器传递发球左侧的状态
  83. this.stateCallback(state);
  84. }
  85. // 判断是否需要磕碰
  86. var next = this.x + speed;
  87. if (this.x < this.flyRight && next >= this.flyRight) {
  88. this.bump.bump(cc.v2(this.x, this.equation(this.x)));
  89. }
  90. this.x = next;
  91. this.g.lineWidth = 3;
  92. this.g.moveTo(this.x, this.equation(this.x));
  93. if (this.state == Peel_State.Peel_Fly) {
  94. 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));
  95. } else {
  96. this.g.bezierCurveTo(this.x, this.equation(this.x), this.x + 1, this.equation(this.x + 1), this.x + 2, this.equation(this.x + 2));
  97. }
  98. this.g.stroke();
  99. },
  100. // update (dt) {},
  101. });