123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- import { _decorator, Color, Component, instantiate, PhysicsSystem2D, v2, Label, Node, Prefab, random, randomRangeInt, Vec3, SpriteFrame, Sprite, color } from 'cc';
- const { ccclass, property } = _decorator;
- import { GameManager } from './Manager/GameManager';
- import { AudioManage } from './Manager/AudioManage';
- import { Buttet } from './Buttet';
- import { Item } from './Item';
- @ccclass('Game')
- export class Game extends Component {
- @property([SpriteFrame])
- frames: SpriteFrame[] = [];
- @property(Prefab)
- item: Prefab = null;
- @property(Prefab)
- bullet: Prefab = null;
- items: Node;
- bullets: Node;
- firePos: Vec3;
- fireIndex: number;
- isOver: boolean;
- callback: Function;
- typeArr: number[];
- onLoad() {
- PhysicsSystem2D.instance.gravity = v2();
- this.items = this.node.getChildByName("Items")
- this.bullets = this.node.getChildByName("Bullets")
- this.node.on(Node.EventType.TOUCH_END, this._onTouchStart, this);
- this.firePos = this.node.getChildByPath("Fire/Node").position
- this.callback = function () {
- this.createItem()
- }
- // this.gameStart()
- }
- _onTouchStart(touchEvent) {
- if (this.isOver || !GameManager.getInstance().isStartGame) return
- console.log("Fire" + this.fireIndex)
- let obj = instantiate(this.bullet)
- obj.getComponent(Buttet).targetIndex = this.fireIndex
- obj.setParent(this.bullets)
- obj.worldPosition = this.firePos
- obj.setPosition(this.firePos);
- }
- gameStart() {
- console.log("gameStart")
- let arr = [2, 2, 2, 3, 3, 3, 4, 4, 4, 4]
- let arr2 = []
- for (let i = 0; i < arr[GameManager.getInstance().curLevel - 1]; i++) {
- while (true) {
- let value = randomRangeInt(0, 6)
- if (arr2.find((data) => data === value)) {
- } else {
- arr2.push(value)
- break
- }
- }
- }
- this.typeArr = arr2
- console.log(this.typeArr)
- this.isOver = false
- this.randomFire()
- this.createFruit()
- }
- randomFire() {
- this.fireIndex = this.typeArr[randomRangeInt(0, this.typeArr.length)]
- this.node.getChildByPath("Fire/Sprite").getComponent(Sprite).spriteFrame = this.frames[this.fireIndex]
- }
- createFruit() {
- // this.unschedule(this.createItem)
- // 以秒为单位的时间间隔
- let interval = 2;
- // 重复次数
- let repeat = 1000;
- // 开始延时
- let delay = 0;
- this.schedule(
- // 这里的 this 指向 component
- this.callback, interval, repeat, delay);
- }
- createItem() {
- let obj = instantiate(this.item)
- obj.getComponent(Item).arr = this.typeArr
- obj.setParent(this.items)
- }
- reset() {
- this.items.removeAllChildren()
- this.bullets.removeAllChildren()
- this.gameStart();
- }
- gameOver() {
- this.unschedule(this.callback)
- this.isOver = true
- }
- getItem(isRight) {
- if (isRight) {
- GameManager.getInstance().addScore(20)
- if (GameManager.getInstance().curLevelScore >= GameManager.getInstance().curLevelData.targetScore) {
- this.gameOver()
- }
- // if(this.num<=0){
- // if(isNext){
- // // this.audioSource.playOneShot(this.otherAudioArr[1],1)
- // this.gameOver()
- // AudioManage.instance.playSound("/audio/晋级下一关音效",1)
- // }
- } else {
- GameManager.getInstance().minusScore(20)
- // if (isOver) {
- // this.gameOver()
- // }
- }
- }
- playGet() {
- AudioManage.instance.playSound("/audio/水果被击中音效", 1)
- }
- update(deltaTime: number) {
- }
- }
|