import { _decorator, Component, instantiate, math, Node, Prefab } from 'cc'; import { bullet } from '../bullet/bullet'; import { constant } from './constant'; import { enemyPlane } from '../plane/enemyPlane' const { ccclass, property } = _decorator; @ccclass('gameMangager') export class gameMangager extends Component { @property(Node) public playerPlane: Node = null; // bullet @property(Prefab) public bullet01: Prefab = null; @property(Prefab) public bullet02: Prefab = null; @property(Prefab) public bullet03: Prefab = null; @property(Prefab) public bullet04: Prefab = null; @property(Prefab) public bullet05: Prefab = null; @property public shootTime = 0.1; // 子弹周期 @property public bulletSpeed = 1; // 子弹速度 @property(Node) public bulletRoot: Node = null; // 敌机 @property(Prefab) public enemyPlane01: Prefab = null; @property(Prefab) public enemyPlane02: Prefab = null; @property public createEnemyTime = 1; @property public enemySpeed1 = 0.5; @property public enemySpeed2 = 0.7; private _currShootTime = 0; private _isShooting = false; private _currCreatEnemyTime = 0; private _comBinationInterval = constant.Combination.PLAN1; start() { this._init() } update(deltaTime: number) { this._currShootTime += deltaTime if (this._isShooting && this._currShootTime > this.shootTime) { this.createPlayerBullet() this._currShootTime = 0 } // 敌机类型 this._currCreatEnemyTime += deltaTime if (this._comBinationInterval === constant.Combination.PLAN1) { if (this._currCreatEnemyTime > this.createEnemyTime) { this.createEnemyPlane(); this._currCreatEnemyTime = 0; } } else if (this._comBinationInterval === constant.Combination.PLAN2) { if (this._currCreatEnemyTime > this.createEnemyTime * 0.9) { const randomCombiation = math.randomRangeInt(1, 3); if (randomCombiation === constant.Combination.PLAN2) { this.createCombination1() } else { this.createEnemyPlane(); } this._currCreatEnemyTime = 0; } } else { if (this._currCreatEnemyTime > this.createEnemyTime * 0.8) { const randomCombiation = math.randomRangeInt(1, 4); if (randomCombiation === constant.Combination.PLAN2) { this.createCombination1() } else if (randomCombiation === constant.Combination.PLAN3) { this.createCombination2() } else { this.createEnemyPlane(); } this._currCreatEnemyTime = 0; } } } // 创建子弹 public createPlayerBullet() { const bullet01 = instantiate(this.bullet01); bullet01.setParent(this.bulletRoot); const pos = this.playerPlane.position; bullet01.setPosition(pos.x + 0, pos.y, pos.z) const bulletComp = bullet01.getComponent(bullet) bulletComp.bulletSpeed = this.bulletSpeed } // 创建敌机 public createEnemyPlane() { const whichEnemy = math.randomRangeInt(1, 3); let prefab: Prefab = null; let speed = 0; if (whichEnemy == constant.EnemyPlaneType.TYPE1) { prefab = this.enemyPlane01; speed = this.enemySpeed1 } else { prefab = this.enemyPlane02; speed = this.enemySpeed2 } const enemy = instantiate(prefab) enemy.setParent(this.node) const enemyComp = enemy.getComponent(enemyPlane) enemyComp.show(speed) const randomPos = math.randomRangeInt(-11, 11) enemy.setPosition(30, 0, randomPos) } public createCombination1() { const enemyArry = new Array(3); for (let i = 0; i < enemyArry.length; i++) { enemyArry[i] = instantiate(this.enemyPlane01); const element = enemyArry[i] element.parent = this.node; element.setPosition(30, 0, -6 + i * 6) const enemyComp = element.getComponent(enemyPlane) enemyComp.show(this.enemySpeed1) } } public createCombination2() { const enemyArry = new Array(5); const combinationPost = [ -8, 0, 40, -4, 0, 35, 0, 0, 30, 4, 0, 35, 8, 0, 40, ] for (let i = 0; i < enemyArry.length; i++) { enemyArry[i] = instantiate(this.enemyPlane02); const element = enemyArry[i] element.parent = this.node; const startIndex = i * 3 element.setPosition(combinationPost[startIndex + 2], combinationPost[startIndex + 1], combinationPost[startIndex]) const enemyComp = element.getComponent(enemyPlane) enemyComp.show(this.enemySpeed2) } } public isShooting(val: boolean) { this._isShooting = val } private _init() { this._currShootTime = this.shootTime this.changePlaneMode() } private changePlaneMode() { this.schedule(this._modeChanged, 10, 3) } private _modeChanged() { this._comBinationInterval++; } }