// 监视果实
monitor() : void {
let num = 0;
for (let i = 0; i < this.num ; ++i) {
if(this.alive[i]) num++; // 计数存活果实的数量
if(num < 15) {
// 产生一个果实
this.reset()
return ;
}
}
}
//重置果实的状态
reset() {
for (let i = 0; i < this.num; ++i) {
if(!this.alive[i]) {
this.born(i); // 假如存活为 false , 让它重新出生。
return ; // 每次只重置一个果实
}
}
}
// 初始化果实
born(i){
let aneId = Math.floor( Math.random() * anemones.num ) // 随机拿到一个果实的 ID
this.x[i] = anemones.x[aneId]; // 设置果实的 x 坐标为海葵的 x 坐标
this.y[i] = cvs_height - anemones.height[aneId]; // 设置果实的 y 坐标,为海葵高度的顶点坐标
this.speed[i] = Math.random() * 0.02 + 0.005; // 设置速度在区间 0.005 - 0.025 里
this.alive[i] = true; // 先设置它的存活为 true
this.diameter[i] = 0; // 未生长出来的果实半径为0
}
constructor(){
for (let i = 0; i < this.num; ++i) {
this.born(i);
}
this.orange.src = 'assets/img/fruit.png';
this.blue.src = 'assets/img/blue.png';
}
function gameLoop() {
const now = Date.now()
deltaTime = now - lastTime;
lastTime = now;
console.log(deltaTime);
drawBackbround()
anemones.draw()
fruits.draw()
fruits.monitor()
requestAnimationFrame(gameLoop);
}