Game.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { _decorator, Color, Component, instantiate, PhysicsSystem2D, v2, Label, Node, Prefab, random, randomRangeInt, Vec3, SpriteFrame, Sprite, color } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. import { GameManager } from './Manager/GameManager';
  4. import { AudioManage } from './Manager/AudioManage';
  5. import { Buttet } from './Buttet';
  6. import { Item } from './Item';
  7. @ccclass('Game')
  8. export class Game extends Component {
  9. @property([SpriteFrame])
  10. frames: SpriteFrame[] = [];
  11. @property(Prefab)
  12. item: Prefab = null;
  13. @property(Prefab)
  14. bullet: Prefab = null;
  15. items: Node;
  16. bullets: Node;
  17. firePos: Vec3;
  18. fireIndex: number;
  19. isOver: boolean;
  20. callback: Function;
  21. typeArr: number[];
  22. onLoad() {
  23. PhysicsSystem2D.instance.gravity = v2();
  24. this.items = this.node.getChildByName("Items")
  25. this.bullets = this.node.getChildByName("Bullets")
  26. this.node.on(Node.EventType.TOUCH_END, this._onTouchStart, this);
  27. this.firePos = this.node.getChildByPath("Fire/Node").position
  28. this.callback = function () {
  29. this.createItem()
  30. }
  31. // this.gameStart()
  32. }
  33. _onTouchStart(touchEvent) {
  34. if (this.isOver || !GameManager.getInstance().isStartGame) return
  35. console.log("Fire" + this.fireIndex)
  36. let obj = instantiate(this.bullet)
  37. obj.getComponent(Buttet).targetIndex = this.fireIndex
  38. obj.setParent(this.bullets)
  39. obj.worldPosition = this.firePos
  40. obj.setPosition(this.firePos);
  41. }
  42. gameStart() {
  43. console.log("gameStart")
  44. let arr = [2, 2, 2, 3, 3, 3, 4, 4, 4, 4]
  45. let arr2 = []
  46. for (let i = 0; i < arr[GameManager.getInstance().curLevel - 1]; i++) {
  47. while (true) {
  48. let value = randomRangeInt(0, 6)
  49. if (arr2.find((data) => data === value)) {
  50. } else {
  51. arr2.push(value)
  52. break
  53. }
  54. }
  55. }
  56. this.typeArr = arr2
  57. console.log(this.typeArr)
  58. this.isOver = false
  59. this.randomFire()
  60. this.createFruit()
  61. }
  62. randomFire() {
  63. this.fireIndex = this.typeArr[randomRangeInt(0, this.typeArr.length)]
  64. this.node.getChildByPath("Fire/Sprite").getComponent(Sprite).spriteFrame = this.frames[this.fireIndex]
  65. }
  66. createFruit() {
  67. // this.unschedule(this.createItem)
  68. // 以秒为单位的时间间隔
  69. let interval = 2;
  70. // 重复次数
  71. let repeat = 1000;
  72. // 开始延时
  73. let delay = 0;
  74. this.schedule(
  75. // 这里的 this 指向 component
  76. this.callback, interval, repeat, delay);
  77. }
  78. createItem() {
  79. let obj = instantiate(this.item)
  80. obj.getComponent(Item).arr = this.typeArr
  81. obj.setParent(this.items)
  82. }
  83. reset() {
  84. this.items.removeAllChildren()
  85. this.bullets.removeAllChildren()
  86. this.gameStart();
  87. }
  88. gameOver() {
  89. this.unschedule(this.callback)
  90. this.isOver = true
  91. }
  92. getItem(isRight) {
  93. if (isRight) {
  94. GameManager.getInstance().addScore(20)
  95. if (GameManager.getInstance().curLevelScore >= GameManager.getInstance().curLevelData.targetScore) {
  96. this.gameOver()
  97. }
  98. // if(this.num<=0){
  99. // if(isNext){
  100. // // this.audioSource.playOneShot(this.otherAudioArr[1],1)
  101. // this.gameOver()
  102. // AudioManage.instance.playSound("/audio/晋级下一关音效",1)
  103. // }
  104. } else {
  105. GameManager.getInstance().minusScore(20)
  106. // if (isOver) {
  107. // this.gameOver()
  108. // }
  109. }
  110. }
  111. playGet() {
  112. AudioManage.instance.playSound("/audio/水果被击中音效", 1)
  113. }
  114. update(deltaTime: number) {
  115. }
  116. }