GamePage.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import GameConfig from "../GameConfig";
  2. import GameMgr, { Sound } from "../GameMgr";
  3. import Block from "./game_page/Block";
  4. import BlockLong from "./game_page/BlockLong";
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class GamePage extends cc.Component {
  8. @property({type: cc.Prefab})
  9. block_prefab: cc.Prefab = null;
  10. @property({type: cc.Node})
  11. blocks_node: cc.Node = null;
  12. @property({type: dragonBones.ArmatureDisplay})
  13. collision1s: dragonBones.ArmatureDisplay[] = [];
  14. @property({type: dragonBones.ArmatureDisplay})
  15. collision2s: dragonBones.ArmatureDisplay[] = [];
  16. @property({type: cc.Node})
  17. road_lights: cc.Node[] = [];
  18. @property({type: cc.Sprite})
  19. keys: cc.Sprite[] = [];
  20. @property({type: cc.Node})
  21. key_checks: cc.Node[] = [];
  22. @property({type: cc.SpriteFrame})
  23. keys_on: cc.SpriteFrame[] = [];
  24. @property({type: cc.SpriteFrame})
  25. keys_out: cc.SpriteFrame[] = [];
  26. @property({type: BlockLong})
  27. block_long: BlockLong = null;
  28. @property({type: dragonBones.ArmatureDisplay})
  29. font_glown: dragonBones.ArmatureDisplay = null;
  30. @property({type: cc.Label})
  31. double_hit: cc.Label = null;
  32. @property({type: cc.Font})
  33. double_hit_fonts: cc.Font[] = [];
  34. //连击数
  35. hitCount: number = 0;
  36. //成功击中的节拍数
  37. hitBeat: number = 0;
  38. //已经消失的节拍数
  39. overBeat: number = 0;
  40. //开始时间
  41. startTime: number;
  42. music: cc.AudioClip;
  43. music_id: number;
  44. beats_index: number = 0;
  45. beats: number[];
  46. beats_long: number[];
  47. static instance: GamePage;
  48. onLoad() {
  49. GamePage.instance = this;
  50. window.game_page = this;
  51. window.game_all_hit = () => {
  52. this.hitBeat = this.beats.length;
  53. };
  54. window.game_acc_finish = () => {
  55. this.overBeat = this.beats.length;
  56. this.gameOver();
  57. };
  58. this.music = GameMgr.instance.song;
  59. this.beats = GameMgr.instance.beats;
  60. this.initBlockPool(5);
  61. for (let collision of this.collision1s) {
  62. collision.addEventListener(dragonBones.EventObject.COMPLETE, () => {
  63. collision.node.active = false;
  64. });
  65. }
  66. for (let i = 0; i < this.key_checks.length; i++) {
  67. this.key_checks[i].on(cc.Node.EventType.TOUCH_START, () => {
  68. this.onRoad(i);
  69. });
  70. this.key_checks[i].on(cc.Node.EventType.TOUCH_END, () => {
  71. this.outRoad(i);
  72. });
  73. this.key_checks[i].on(cc.Node.EventType.TOUCH_CANCEL, () => {
  74. this.outRoad(i);
  75. });
  76. }
  77. //初始化发光动画监听
  78. this.font_glown.addEventListener(dragonBones.EventObject.START, () => {
  79. this.font_glown.node.opacity = 255;
  80. });
  81. this.font_glown.addEventListener(dragonBones.EventObject.COMPLETE, () => {
  82. this.font_glown.node.opacity = 0;
  83. });
  84. }
  85. onDestroy() {
  86. window.game_page = null;
  87. }
  88. start() {
  89. // //剔除紧密的节拍,控制节拍间最小间隔
  90. // let beat_min_interval = GameConfig.song_star_limit_beat_min_interval[GameConfig.song_infos[GameMgr.instance.song_id].stars];
  91. // for (let i = 1; i < this.beats.length; i++) {
  92. // if (this.beats[i] == undefined) {
  93. // break;
  94. // }
  95. // while (this.beats[i] != undefined && this.beats[i] - this.beats[i - 1] < beat_min_interval) {
  96. // this.beats.splice(i, 1);
  97. // }
  98. // }
  99. //动态设立长节拍触发点
  100. this.beats_long = JSON.parse(JSON.stringify(this.beats));
  101. // let var_time = 0;
  102. // let record_indexes = [];
  103. // for (let i = 1; i < this.beats_long.length; i++) {
  104. // let last_time = this.beats_long[i - 1];
  105. // if (var_time > GameConfig.beat_long_interval) {
  106. // // this.beats_long[i - 1] = -1;
  107. // var_time = 0;
  108. // record_indexes.push(i - 1);
  109. // }
  110. // var_time += this.beats_long[i] - last_time;
  111. // }
  112. // //新增需求,长条出现1~2次就好,那就取两次
  113. // let beat_long_indexes = [];
  114. // if (record_indexes.length >= 3) {
  115. // let record_pos1 = Math.floor(record_indexes.length * 1 / 3);
  116. // let record_pos2 = Math.floor(record_indexes.length * 2 / 3);
  117. // beat_long_indexes.push(record_indexes[record_pos1]);
  118. // beat_long_indexes.push(record_indexes[record_pos2]);
  119. // } else {
  120. // beat_long_indexes = record_indexes;
  121. // }
  122. // for (let beat_long_index of beat_long_indexes) {
  123. // this.beats_long[beat_long_index] = -1;
  124. // }
  125. this.startTime = Date.now();
  126. let first_time = this.beats[0] - GameConfig.block_come_time;
  127. if (first_time < 0) {
  128. for (let i = 0; i < this.beats.length; i++) {
  129. let beat = this.beats[i];
  130. if (beat - GameConfig.block_come_time < 0) {
  131. this.scheduleOnce(() => {
  132. this.addBlock(beat);
  133. this.beats_index++;
  134. }, (beat - GameConfig.block_come_time) - first_time);
  135. } else {
  136. break;
  137. }
  138. }
  139. this.scheduleOnce(() => {
  140. this.music_id = cc.audioEngine.playMusic(this.music, false);
  141. }, -first_time);
  142. } else {
  143. this.music_id = cc.audioEngine.playMusic(this.music, false);
  144. }
  145. }
  146. update() {
  147. if (this.music_id > -1) {
  148. let music_progress_time = cc.audioEngine.getCurrentTime(this.music_id);
  149. let beat_long = this.beats_long[this.beats_index];
  150. if (beat_long < 0) {//当前节拍触发长条
  151. if (this.beats[this.beats_index] - music_progress_time <= GameConfig.long_come_time) {
  152. let count = 0;
  153. let first_beat = this.beats[this.beats_index];
  154. for (let i = this.beats_index; i < this.beats.length; i++) {
  155. let d_value = this.beats[i] - first_beat
  156. // 退出时间x2,能够让长条退场过程中不会出现别的方块
  157. if (d_value <= GameConfig.long_idle_time + GameConfig.long_out_time * 2) {
  158. this.beats_index++;
  159. count++;
  160. } else {
  161. break;
  162. }
  163. }
  164. if (count > 0) {
  165. this.block_long.initCount = count;
  166. this.block_long.count = count;
  167. this.block_long.release();
  168. }
  169. }
  170. } else {
  171. while (this.beats_index < this.beats.length && this.beats[this.beats_index] - music_progress_time <= GameConfig.block_come_time) {
  172. this.addBlock(this.beats[this.beats_index]);
  173. this.beats_index++;
  174. }
  175. }
  176. }
  177. }
  178. block_pool: cc.NodePool = new cc.NodePool();
  179. initBlockPool(count: number) {
  180. for (let i = 0; i < count; ++i) {
  181. let block = cc.instantiate(this.block_prefab);
  182. this.block_pool.put(block);
  183. }
  184. }
  185. getBlock(): cc.Node {
  186. let block = null;
  187. if (this.block_pool.size() > 0) {
  188. block = this.block_pool.get();
  189. } else {
  190. block = cc.instantiate(this.block_prefab);
  191. }
  192. return block;
  193. }
  194. putBlock(block: cc.Node) {
  195. this.block_pool.put(block);
  196. }
  197. addBlock(beat: number) {
  198. let node = this.getBlock();
  199. this.blocks_node.addChild(node);
  200. node.getComponent(Block).init(beat);
  201. }
  202. /**
  203. * 拳击袋攻击次数统计,用于计算卡路里
  204. */
  205. checkHit_count: number = 0;
  206. /**
  207. * 拳击袋专用接口-不区分打击方向
  208. */
  209. checkHit() {
  210. this.checkHit_count++;
  211. let index = -1;
  212. if (this.block_long.canHit) {
  213. index = this.block_long.index;
  214. } else {
  215. let block = this.findBestBlock();
  216. if (block) {
  217. index = block.index;
  218. }
  219. }
  220. if (index > -1) {
  221. this.key_checks[index].emit(cc.Node.EventType.TOUCH_START);
  222. this.scheduleOnce(() => {
  223. this.key_checks[index].emit(cc.Node.EventType.TOUCH_END);
  224. }, 0.05);
  225. }
  226. }
  227. /**拳袋专用接口2-区分打击方向 */
  228. checkHit2(index: number) {
  229. this.key_checks[index].emit(cc.Node.EventType.TOUCH_START);
  230. this.scheduleOnce(() => {
  231. this.key_checks[index].emit(cc.Node.EventType.TOUCH_END);
  232. }, 0.05);
  233. }
  234. /**寻找最佳的击打方块,目前是以离检测区域中点最近的为佳 */
  235. findBestBlock(index?: number): Block {
  236. let bestBlock = null;
  237. let deltaY = 1 << 12;
  238. let end_center_y = (GameConfig.end_line_y + GameConfig.fianl_line_y) / 2;
  239. for (let node of this.blocks_node.children) {
  240. if(node.y > GameConfig.end_line_y+150) continue;
  241. if(node.y < GameConfig.end_line_y-150) continue;
  242. let block = node.getComponent(Block);
  243. if ((index == undefined || block.index == index) && block.existTime > 0.5) {
  244. let dy = Math.abs(node.y - end_center_y);
  245. if (dy < deltaY) {
  246. bestBlock = block;
  247. deltaY = dy;
  248. }
  249. }
  250. }
  251. return bestBlock;
  252. }
  253. checkHitRoad(index: number) {
  254. let blockExplode = this.findBestBlock(index);
  255. if (blockExplode instanceof Block) {
  256. if (blockExplode.canHit(index)) {
  257. this.hit();
  258. this.countHitBeat(1);
  259. }
  260. blockExplode.addCollision();
  261. return;
  262. }
  263. //长条击打检测
  264. if (this.block_long.canHit && index == this.block_long.index) {
  265. this.hit();
  266. if (this.block_long.count > 0) {
  267. this.countHitBeat(1);
  268. }
  269. this.block_long.count--;
  270. let c: dragonBones.ArmatureDisplay = this.collision2s[this.block_long.index];
  271. c.node.active = true;
  272. let config = GameConfig.collision_configs[this.block_long.index];
  273. c.node.setPosition(config.posX_long, config.posY_long);
  274. c.node.setScale(config.scaleX, config.scaleY);
  275. c.playAnimation("02", 1);
  276. }
  277. }
  278. onRoad(index: number) {
  279. this.keys[index].spriteFrame = this.keys_on[index];
  280. this.road_lights[index].active = true;
  281. this.checkHitRoad(index);
  282. }
  283. outRoad(index: number) {
  284. this.keys[index].spriteFrame = this.keys_out[index];
  285. this.road_lights[index].active = false;
  286. }
  287. miss() {
  288. //新增,需求说连击不要断
  289. // this.hitCount = 0;
  290. }
  291. hit() {
  292. this.hitCount++;
  293. let targetScale = [12, 7.5];
  294. let targetScaleStartRate = 0.2;
  295. let targetScaleEndRate = 0.25;
  296. this.double_hit.node.opacity = 255;
  297. this.double_hit.node.setScale(targetScale[0] * targetScaleStartRate, targetScale[1] * targetScaleStartRate);
  298. this.double_hit.string = this.hitCount.toString();
  299. this.double_hit.font = this.double_hit_fonts[1];
  300. this.double_hit.node.stopAllActions();
  301. this.double_hit.node.runAction(cc.sequence(
  302. cc.scaleTo(0.1, targetScale[0], targetScale[1]),
  303. cc.scaleTo(0.1, targetScale[0] * targetScaleEndRate, targetScale[1] * targetScaleEndRate),
  304. cc.callFunc(() => {
  305. this.double_hit.font = this.double_hit_fonts[0];
  306. }),
  307. cc.delayTime(0.23),
  308. cc.fadeOut(0.23)
  309. ));
  310. this.font_glown.timeScale = 2;
  311. this.font_glown.playAnimation("01", 1);
  312. // GameMgr.instance.playEffect(Sound.hit_effect);
  313. }
  314. countHitBeat(plus_value: number) {
  315. this.hitBeat += plus_value;
  316. }
  317. countOverBeat(plus_value: number) {
  318. this.overBeat += plus_value;
  319. if (this.overBeat == this.beats.length) {
  320. this.gameOver();
  321. }
  322. }
  323. gameOver() {
  324. cc.audioEngine.stopMusic();
  325. this.scheduleOnce(() => {
  326. GameMgr.instance.addPage(GameMgr.instance.settle_page);
  327. }, 0.5);
  328. }
  329. back() {
  330. if (GameMgr.instance.debug) {
  331. alert("如需重复测试,请关闭网页重新打开!")
  332. return;
  333. }
  334. this.node.destroy();
  335. GameMgr.instance.playEffect(Sound.button_effect);
  336. GameMgr.instance.addPage(GameMgr.instance.main_page);
  337. }
  338. }