Commit 51ee4291 authored by Tt's avatar Tt

refactor(play): 重构鱼群生成逻辑使用等待队列

parent 5639b3a2
......@@ -350,7 +350,6 @@ export class PlayComponent implements OnInit, OnDestroy {
this.m_fishArray = []; // 清空鱼群管理数组
this.m_lastFishTime = 0; // 重置上次生成鱼的时间
this.m_fishTimeAccumulator = 0; // 重置鱼生成的累积时间
this.currentFishIndex = 0; // 重置当前鱼索引
this.clickedFishData = []; // 清空用户点击的鱼的数据
this.touchFish = null; // 清空当前触摸的鱼
this.isGameStart = false;
......@@ -984,6 +983,8 @@ export class PlayComponent implements OnInit, OnDestroy {
const currentQuestion = this.gameStages[this.currentStageIndex];
// 将当前Stage中的数组随机一次
currentQuestion.randomList();
// 将当前阶段的随机问题列表塞入questionWaitList中
this.questionWaitList = [...currentQuestion.questionRandomList];
// 清空现有的鱼群
this.clearFishes();
......@@ -1018,7 +1019,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.g_cartoon.playAudio(currentQuestion.correctQuestion.audio, true);
}, 500);
} else {
this.g_cartoon.stopAllAudio();
this.g_cartoon.stopAllAudio();
this.g_cartoon.playAudio(currentQuestion.correctQuestion.audio, true);
}
}
......@@ -2939,9 +2940,12 @@ export class PlayComponent implements OnInit, OnDestroy {
*/
private isGameStart = false;
private currentFishIndex = 0;
private m_fishTimeAccumulator = 0; // 累积的时间,用于鱼的生成
// 用于等待生成的鱼
private questionWaitList = [];
private lastCount = 0;
updateFish(dt: number = 0) {
// 如果没有游戏阶段或当前阶段索引超出范围,不生成鱼
if (!this.gameStages || this.currentStageIndex >= this.gameStages.length) {
......@@ -2958,12 +2962,9 @@ export class PlayComponent implements OnInit, OnDestroy {
// 使用累积的时间来控制鱼的生成频率,从而间接控制屏幕上的鱼的数量
if (this.m_fishTimeAccumulator >= this.m_fishInterval) {
// 创建新的鱼
if (currentStage && currentStage.questionRandomList && currentStage.questionRandomList.length > 0) {
// 从当前阶段的随机问题列表中选择一个问题数据
if (!this.currentFishIndex) this.currentFishIndex = 0;
const randomIndex = this.currentFishIndex % currentStage.questionRandomList.length;
const fishData = currentStage.questionRandomList[randomIndex];
this.currentFishIndex++;
if (this.questionWaitList && this.questionWaitList.length > 0) {
// 从questionWaitList中shift一个数据出去用于创建
const fishData = this.questionWaitList.shift();
const type = this.g_formData.imgSize == 'small' ? 'small' : 'big';
......@@ -2975,7 +2976,9 @@ export class PlayComponent implements OnInit, OnDestroy {
// 设置鱼的初始位置在屏幕右侧外部
fish.ref.x = this.g_canvasWidth + fish.ref.width * fish.ref.scaleX / 2;
fish.ref.y = (this.currentFishIndex % 2 == 0 ? 400 : 300) * this.g_mapScale; // Math.random() * (this.g_canvasHeight * 0.35) + this.g_canvasHeight * 0.25; // 在屏幕中间区域随机高度
if (!this.lastCount) this.lastCount = 0;
fish.ref.y = (this.lastCount % 2 == 0 ? 400 : 300) * this.g_mapScale; // 在屏幕中间区域随机高度
this.lastCount++;
// 设置鱼的游动速度
fish.speed = 2;
......@@ -2992,6 +2995,8 @@ export class PlayComponent implements OnInit, OnDestroy {
// 重置累积时间(减去间隔时间,保留余数,避免时间损失)
this.m_fishTimeAccumulator -= this.m_fishInterval;
} else {
this.m_fishTimeAccumulator = this.m_fishInterval;
}
}
......@@ -3019,6 +3024,9 @@ export class PlayComponent implements OnInit, OnDestroy {
this.m_renderArr.push(item.element);
});
// 将鱼的数据重新添加到questionWaitList中
this.questionWaitList.push(fish.questionData);
// 从鱼群数组中移除
this.m_fishArray.splice(i, 1);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment