You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
7.3 KiB
213 lines
7.3 KiB
import { _decorator, Component, director, Node, v3, Vec3 } from 'cc';
|
|
import { gaoyaguo } from './gaoyaguo';
|
|
import { atgMonster } from './atgMonster';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
//创建怪物
|
|
|
|
|
|
//因为有个纵深的关系
|
|
@ccclass('creaMonster')
|
|
export class creaMonster extends Component {
|
|
@property(Node)
|
|
|
|
|
|
|
|
pointNodeArray:Node[] = []
|
|
|
|
createMonsterNum =300 //99*10个小怪物
|
|
createMonsterBoss = 1//一个boss
|
|
//创建怪物的时间间隔
|
|
createMonsterInterval = 0.7 //3秒创建一波
|
|
createTime = 0.7
|
|
bociSpeed = 15
|
|
canCreateMonster = false
|
|
//pointNodeArray 里面的位置是从pointNodeArray[0]位置移动到pointNodeArray[pointNodeArray.length-1]位置。
|
|
//scale的变化 因为是纵深设置3d一样的效果 0.3-0.5-0.8-1
|
|
//
|
|
isGameEnd = false
|
|
start() {
|
|
//预创建
|
|
this.canCreateMonster = true
|
|
this.scheduleOnce(()=>{
|
|
this.createMonsterPre()
|
|
},1.5)
|
|
|
|
}
|
|
|
|
createMonsterPre(){
|
|
//pointNodeArray[2]位置一直摆放到pointNodeArray[0]位置
|
|
let addY = this.pointNodeArray[0].position.y-this.pointNodeArray[2].position.y
|
|
addY = addY/8
|
|
let startY = 202
|
|
let startScale = 0.3
|
|
//依次缩小
|
|
let subScale = (0.6-0.3)/12
|
|
|
|
//创建12波小怪物不移动
|
|
for(let i = 0; i < 12; i++){
|
|
this.createMonsterNum--;
|
|
let monster = gaoyaguo.Instance.monsterCore.getGuaiwu()
|
|
startY-=addY
|
|
monster.setPosition(0, startY, 0)
|
|
gaoyaguo.Instance.monsterLayer.addChild(monster)
|
|
monster.scale = v3(startScale, startScale, startScale)
|
|
startScale+=subScale
|
|
|
|
}
|
|
//待机中
|
|
}
|
|
|
|
createMonster(){
|
|
if(this.createMonsterNum > 0){
|
|
this.createMonsterNum--;
|
|
let monster = gaoyaguo.Instance.monsterCore.getGuaiwu()
|
|
|
|
monster.children.forEach(child => {
|
|
child.getComponent(atgMonster).setMonstrData(3)
|
|
child.active = true
|
|
})
|
|
|
|
let startY = this.pointNodeArray[0].position.y
|
|
let startScale = this.pointNodeArray[0].scale
|
|
monster.setPosition(0, startY, 0)
|
|
gaoyaguo.Instance.monsterLayer.addChild(monster)
|
|
monster.scale = startScale
|
|
monster.setSiblingIndex(0)
|
|
}else{
|
|
this.createMonsterBoss--
|
|
let monster = gaoyaguo.Instance.monsterCore.getBoss()
|
|
|
|
let startY = this.pointNodeArray[0].position.y
|
|
let startScale = this.pointNodeArray[0].scale
|
|
monster.setPosition(0, startY, 0)
|
|
gaoyaguo.Instance.monsterLayer.addChild(monster)
|
|
monster.scale = startScale
|
|
if(this.createMonsterBoss<=0){
|
|
//boss死亡
|
|
//不创建怪物
|
|
this.canCreateMonster = false
|
|
}
|
|
monster.setSiblingIndex(0)
|
|
}
|
|
// gaoyaguo.Instance.monsterLayer.children.sort((a, b) => {
|
|
// return b.worldPosition.y - a.worldPosition.y
|
|
// })
|
|
|
|
}
|
|
update(deltaTime: number) {
|
|
if(gaoyaguo.Instance.isGameOver){
|
|
return
|
|
}
|
|
if(this.isGameEnd){
|
|
return
|
|
}
|
|
if((gaoyaguo.Instance.jiedianIndex == 3 || gaoyaguo.Instance.jiedianIndex == 4) && this.canCreateMonster){
|
|
//开启创建
|
|
//一秒钟创建一波怪物
|
|
this.createTime+=deltaTime
|
|
if(this.createTime>=this.createMonsterInterval){
|
|
this.createTime=0
|
|
this.createMonster()
|
|
|
|
}
|
|
|
|
//
|
|
|
|
}
|
|
if((gaoyaguo.Instance.jiedianIndex == 3 || gaoyaguo.Instance.jiedianIndex == 4)) {
|
|
|
|
//怪物移动 朝着pointNodeArray[pointNodeArray.length-1]位置移动 ,移动的过程中scale会变化
|
|
|
|
gaoyaguo.Instance.monsterLayer.children.forEach(child => {
|
|
let monArrNode = child
|
|
if(monArrNode.children.length>0){
|
|
//monArrNode 移动 朝着pointNodeArray[pointNodeArray.length-1]位置移动 ,移动的过程中scale会变化
|
|
const lastPoint = this.pointNodeArray[this.pointNodeArray.length - 1];
|
|
if (!lastPoint) {
|
|
//
|
|
this.isGameEnd = true
|
|
gaoyaguo.Instance.gameover()
|
|
return
|
|
};
|
|
|
|
const curPos = monArrNode.position.clone();
|
|
const targetPos = lastPoint.position.clone();
|
|
|
|
// 只按 Y 方向推进(保持当前 X),模拟纵深前进
|
|
const dirY = targetPos.y - curPos.y;
|
|
const stepY = Math.sign(dirY) * this.bociSpeed * deltaTime;
|
|
if (Math.abs(stepY) >= Math.abs(dirY)) {
|
|
curPos.y = targetPos.y;
|
|
} else {
|
|
curPos.y += stepY;
|
|
}
|
|
monArrNode.setPosition(curPos);
|
|
|
|
// 到达终点:怪物推进到最后一个点(或越过)则失败结算
|
|
if (!this.isGameEnd) {
|
|
const reached =
|
|
(dirY === 0) ||
|
|
(dirY < 0 && curPos.y <= targetPos.y) ||
|
|
(dirY > 0 && curPos.y >= targetPos.y);
|
|
if (reached) {
|
|
this.isGameEnd = true;
|
|
gaoyaguo.Instance.gameover();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// scale 按路径进度插值:point[0] -> point[last]
|
|
const startPoint = this.pointNodeArray[0];
|
|
if (startPoint) {
|
|
const total = Math.max(1e-6, startPoint.position.y - lastPoint.position.y);
|
|
const traveled = startPoint.position.y - curPos.y;
|
|
let t = traveled / total;
|
|
if (t < 0) t = 0;
|
|
if (t > 1) t = 1;
|
|
const startS = startPoint.scale.x;
|
|
const endS = lastPoint.scale.x;
|
|
const s = startS + (endS - startS) * t;
|
|
monArrNode.scale = v3(s, s, s);
|
|
}
|
|
}
|
|
//移动速度bociSpeed
|
|
})
|
|
|
|
|
|
//回收monArrNode.children.length == 0的节点
|
|
gaoyaguo.Instance.monsterLayer.children.forEach(child => {
|
|
let monArrNode = child
|
|
let isAllHide = true
|
|
monArrNode.children.forEach(child => {
|
|
if(child.active){
|
|
isAllHide = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if(isAllHide){
|
|
gaoyaguo.Instance.monsterCore.putGuaiwu(monArrNode)
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// if (director.getTotalFrames() % 10 === 0) {
|
|
// this.sortMonstersByYDis()
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
sortMonstersByYDis(){
|
|
gaoyaguo.Instance.monsterLayer.children.sort((a, b) => {
|
|
return b.worldPosition.y - a.worldPosition.y
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|