Block.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import GamePage from "../GamePage";
  2. import GameConfig from "../../GameConfig";
  3. const {ccclass, property} = cc._decorator;
  4. /**方块基类 */
  5. @ccclass
  6. export default class Block extends cc.Component {
  7. @property({type: cc.Sprite})
  8. private face: cc.Sprite = null;
  9. @property({type: cc.SpriteFrame})
  10. private face_sp: cc.SpriteFrame[] = [];
  11. private type: Type;
  12. private config: BlockConfig;
  13. beat: number;//节奏时间点
  14. index: number;
  15. startTime: number = 0;
  16. existTime: number = 0;
  17. hasLogReachTime: boolean = false;
  18. static seed: number = 0;
  19. init(beat: number) {
  20. this.beat = beat;
  21. let random = Math.random();
  22. this.index = random < 0.3 ? 0 : (random < 0.8 ? 1 : 2);
  23. this.startTime = beat - GameConfig.block_come_time;
  24. this.existTime = 0;
  25. this.hasLogReachTime = false;
  26. //如果长节拍存在,之后的方块节拍会在别的道路产出,不会占用同一道路
  27. if (GamePage.instance.block_long.visiable) {
  28. let indexes = [];
  29. for (let i = 0; i < 3; i++) {
  30. if (i != GamePage.instance.block_long.index) {
  31. indexes.push(i);
  32. }
  33. }
  34. this.index = indexes[Math.random() * indexes.length << 0];
  35. }
  36. this.config = GameConfig.block_configs[this.index];
  37. this.type = Block.seed % 2 == 0 ? Type.blue : Type.orange;
  38. Block.seed++;
  39. if (this.index == 1) {
  40. if (this.type == Type.blue) {
  41. this.face.spriteFrame = this.face_sp[0];
  42. this.face.node.setPosition(21.216, -104.576); //纠正新美术资源偏差
  43. } else if (this.type == Type.orange) {
  44. this.face.spriteFrame = this.face_sp[2];
  45. this.face.node.setPosition(0, -97.419); //纠正新美术资源偏差
  46. }
  47. } else {
  48. if (this.type == Type.blue) {
  49. this.face.spriteFrame = this.face_sp[1];
  50. this.face.node.setPosition(-8, -104.69); //纠正新美术资源偏差
  51. } else if (this.type == Type.orange) {
  52. this.face.spriteFrame = this.face_sp[3];
  53. this.face.node.setPosition(0, -97.69); //纠正新美术资源偏差
  54. }
  55. }
  56. this.node.setPosition(this.config.first_pos);
  57. this.node.setScale(this.config.first_scale);
  58. if (GameConfig.block_motion_type < 0) {
  59. this.node.runAction(cc.sequence(
  60. cc.spawn(
  61. cc.moveTo(
  62. this.config.move_time,
  63. this.config.final_pos
  64. ).easing(cc.easeIn(this.config.ease_rate)),
  65. cc.scaleTo(
  66. this.config.move_time,
  67. this.config.final_scale.x,
  68. this.config.final_scale.y
  69. ).easing(cc.easeIn(this.config.ease_rate))
  70. ),
  71. cc.callFunc(this.removeSelf, this)
  72. ));
  73. }
  74. }
  75. update(dt: number) {
  76. this.existTime += dt;
  77. this.node.zIndex = cc.winSize.height / 2 - this.node.y;
  78. if (GameConfig.block_motion_type >= 0) {
  79. let audioCurrentTime = cc.audioEngine.getCurrentTime(GamePage.instance.music_id);
  80. let t = 0;
  81. if (audioCurrentTime == 0) {
  82. t = Date.now() - GamePage.instance.startTime - this.startTime;
  83. } else {
  84. t = audioCurrentTime - this.startTime;
  85. }
  86. let distance = GameConfig.seekDistance(this.config.acc, t);
  87. let vector = this.config.final_pos.sub(this.config.first_pos);
  88. let mul = distance / vector.mag();
  89. if (mul < 1) {
  90. this.node.setPosition(this.config.first_pos.add(vector.mul(mul)));
  91. this.node.setScale(this.config.final_scale.sub(this.config.first_scale).mul(mul));
  92. if (this.existTime > 5) {
  93. this.removeSelf();
  94. }
  95. } else {
  96. this.removeSelf();
  97. }
  98. }
  99. //输出方块到达击打线的所需时间
  100. if (CC_DEBUG && !this.hasLogReachTime && this.node.y <= GameConfig.end_line_y + 3) {
  101. this.hasLogReachTime = true;
  102. // console.log(this.existTime);
  103. }
  104. }
  105. removeSelf() {
  106. GamePage.instance.putBlock(this.node);
  107. GamePage.instance.miss();
  108. GamePage.instance.countOverBeat(1);
  109. }
  110. /**判断是否在击打区域 */
  111. canHit(index: number) {
  112. let faultTolerance = 120;//容错距离
  113. if (
  114. this.index == index &&
  115. this.node.y <= GameConfig.end_line_y + faultTolerance * 1.5 &&
  116. this.node.y > GameConfig.fianl_line_y - faultTolerance * 0.5
  117. ) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**添加打击碰撞特效 */
  124. addCollision() {
  125. this.node.stopAllActions();
  126. GamePage.instance.putBlock(this.node);
  127. // GamePage.instance.hit();
  128. // GamePage.instance.countHitBeat(1);
  129. GamePage.instance.countOverBeat(1);
  130. let c: dragonBones.ArmatureDisplay = null;
  131. if (this.type == Type.blue) {
  132. c = GamePage.instance.collision1s[this.index];
  133. } else if (this.type == Type.orange) {
  134. c = GamePage.instance.collision2s[this.index];
  135. }
  136. c.node.active = true;
  137. let config = GameConfig.collision_configs[this.index];
  138. // let rate = (GameConfig.end_line_y - this.node.y) / (GameConfig.end_line_y + cc.winSize.height / 2);
  139. // let scale = rate * 0.2 + 1;
  140. // c.node.setPosition(this.node.getPosition());
  141. // c.node.setScale(config.scaleX * scale, config.scaleY * scale);
  142. // c.node.setScale(config.scaleX, config.scaleY);
  143. // c.node.setPosition(this.node.x, GameConfig.end_line_y);
  144. c.node.setScale(config.scaleX * this.node.scaleY, config.scaleY * this.node.scaleY);
  145. c.node.setPosition(this.node.x, this.node.y + 50 * this.node.scaleY);
  146. c.playAnimation("01", 1);
  147. }
  148. }
  149. enum Type {
  150. blue,
  151. orange
  152. }