Files
plane-game/assets/src/gameManager.ts
2023-04-28 14:54:01 +08:00

164 lines
4.1 KiB
TypeScript

import { _decorator, Component, Node, Prefab, instantiate, math, Animation } from 'cc';
import { dpComponent } from './dpComponent';
import { dpImpl } from './dpImpl';
import { zd } from './zd';
const { ccclass, property } = _decorator;
@ccclass('gameManager')
export class gameManager extends Component {
@property(Node)
public plant:Node = null;
@property(Prefab)
public dp1:Prefab = null;
@property(Prefab)
public dp2:Prefab = null;
@property(Node)
public zdOut:Node = null;
@property(Prefab)
public zd1:Prefab = null;
@property(Prefab)
public zd2:Prefab = null;
@property(Prefab)
public zd3:Prefab = null;
/**子弹移动速度*/
@property
public zd_speed:number = 0.2;
/**发射子弹的间隔*/
@property
public new_zd_time = 0.1;
/**当前发射子弹的间隔*/
public _new_zd_time = 0;
/**敌人生成的间隔时间 */
@property
public createDpTime = 3;
/**当前敌人生成的时间 */
public _createDpTime = 0;
/**组合间隔状态 */
public _createDpArr = 0;
@property
public dp1_speed:number = 0.05;
@property
public dp2_speed:number = 0.03;
private _isTouch:boolean = false;
private lasttype:number = 0;
start() {
this._new_zd_time = this.new_zd_time;
this.changeDpMode();
}
update(deltaTime: number) {
this._new_zd_time += deltaTime;
if(this._isTouch && this._new_zd_time > this.new_zd_time){
this.createZd();
this._new_zd_time = 0;
}
this._createDpTime += deltaTime;
if(this._createDpArr <= 2){
if(this._createDpTime > this.createDpTime){
this.cerateDp(0);
this._createDpTime = 0;
}
}else{
if(this._createDpTime > this.createDpTime * 0.8){
let rm = math.randomRangeInt(1,3);
if(rm == 2 && this.lasttype == 0){
this.cerateDpArr(this._createDpArr > 2 && this._createDpArr <= 6 ? 1:2);
this.lasttype=1;
}
else{
this.cerateDp(this._createDpArr > 2 && this._createDpArr <= 6 ? 1:2);
this.lasttype=0;
}
this._createDpTime = 0;
}
}
}
public cerateDp(n:number) {
let randm = math.randomRangeInt(1,3);
let p:Prefab = null;
let speed = 0;
let t = 1;
if(randm === dpImpl.dpType.TYPE1){
p = this.dp1;
speed = this.dp1_speed+(math.randomRange(0.01,0.02)*(math.randomRangeInt(-1,2)));
t=1;
}else{
p = this.dp2;
speed = this.dp2_speed+(math.randomRange(0.01,0.02)*(math.randomRangeInt(-1,2)));
t=2;
}
const dp = instantiate(p);
dp.setParent(this.node);
const dpc = dp.getComponent(dpComponent);
dpc._init(speed,n,0);
let loc = math.randomRange(-3.8,3.8);
dp.setPosition(loc, 1, -dpc.dp_max);
}
public cerateDpArr(n:number) {
let parr = new Array<Node>(5);
for (let i = 0; i < parr.length; i++) {
let randm = math.randomRangeInt(1,3);
parr[i] = instantiate(randm==1?this.dp1:this.dp2);
parr[i].setParent(this.node);
const dpc = parr[i].getComponent(dpComponent);
dpc._init(randm==1?this.dp1_speed+(math.randomRange(0.01,0.02)*(math.randomRangeInt(-1,2))):this.dp2_speed+(math.randomRange(0.01,0.02)*(math.randomRangeInt(-1,2))),n,1);
parr[i].setPosition(i*1.8-3.6, 1, -dpc.dp_max);
}
}
public createZd(){
const zd01 = instantiate(this.zd1);
zd01.setParent(this.zdOut);
const ppos = this.plant.position;
zd01.setPosition(ppos.x, ppos.y, ppos.z - 1);
const zdcop = zd01.getComponent(zd);
zdcop.z_speed = this.zd_speed;
}
isTouch(val:boolean){
this._isTouch = val;
}
changeDpMode(){
this.schedule(()=>{
this._createDpArr++;
},10,3);
}
}