GameMain.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Component, Node, NodeEventType, _decorator, instantiate, math, random } from 'cc';
  2. import { GameManager } from '../../Manager/GameManager';
  3. import { AudioManage } from '../../Manager/AudioManage';
  4. import { Global } from '../../Common/Global';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('GameMain')
  7. export class GameMain extends Component {
  8. @property(Node)
  9. mainNode: Node = null;
  10. @property(Node)
  11. player: Node = null;
  12. @property(Node)
  13. guaishou: Node = null;
  14. @property(Node)
  15. zidan: Node = null;
  16. @property(Node)
  17. zidanNode: Node = null;
  18. @property(Node)
  19. guaishouNode: Node = null;
  20. isUpdate: boolean = true;
  21. isBull = false;
  22. bullInterval = 0.5;
  23. touchTime = 0;
  24. onLoad() {
  25. }
  26. start() {
  27. }
  28. startGame(): void {
  29. this.gameOver()
  30. this.unscheduleAllCallbacks();
  31. //初始化
  32. this.isUpdate = true;
  33. this.node.on(NodeEventType.TOUCH_START, this.onThouchStart, this);
  34. this.node.on(NodeEventType.TOUCH_END, this.onThouchEnd, this);
  35. this.node.on(NodeEventType.TOUCH_CANCEL, this.onThouchEnd, this);
  36. this.updateGuaishou();
  37. }
  38. updateGuaishou() {
  39. if (!this.isUpdate) return
  40. let guaishou = instantiate(this.guaishou);
  41. guaishou.active = true;
  42. this.guaishouNode.addChild(guaishou);
  43. const ranY = math.randomRangeInt(0, 3);
  44. const posX = guaishou.position.x;
  45. guaishou.setPosition(posX, -380 + ranY * 380);
  46. let time = GameManager.getInstance().getRand(GameManager.getInstance().intervalTime[0], GameManager.getInstance().intervalTime[1]);
  47. if (this.isUpdate) {
  48. this.scheduleOnce(
  49. this.updateGuaishou.bind(this), time / 1000
  50. )
  51. // setTimeout(() => {
  52. // this.updateGuaishou()
  53. // }, time);
  54. } else {
  55. this.unscheduleAllCallbacks();
  56. }
  57. }
  58. onThouchStart(e) {
  59. this.isBull = true;
  60. console.log('onThouchStart');
  61. }
  62. onThouchEnd(e) {
  63. this.isBull = false;
  64. console.log('onThouchEnd');
  65. }
  66. gameOver() {
  67. //游戏结束
  68. this.zidanNode.removeAllChildren();
  69. this.guaishouNode.removeAllChildren();
  70. this.isUpdate = false;
  71. // AudioManage.instance.playSound(Global.Audio_gameOver);
  72. }
  73. update(deltaTime: number) {
  74. if (!GameManager.getInstance().isStartGame)
  75. return;
  76. if (this.isBull) {
  77. this.touchTime += deltaTime;
  78. if (this.touchTime >= this.bullInterval) {
  79. let zidan = instantiate(this.zidan);
  80. zidan.active = true;
  81. this.zidanNode.addChild(zidan);
  82. zidan.setPosition(-212, this.player.position.y - 15);
  83. AudioManage.instance.playSound(Global.Audio_biu);
  84. this.touchTime = 0;
  85. }
  86. }
  87. }
  88. }