123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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<Node>(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<Node>(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++;
- }
- }
|