Commit 8f39edd0 authored by Tt's avatar Tt

refactor(play): 重构双人模式游戏逻辑和状态管理

parent 025ec7a3
...@@ -124,11 +124,6 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -124,11 +124,6 @@ export class PlayComponent implements OnInit, OnDestroy {
g_teacherFlag = false; // 默认角色 g_teacherFlag = false; // 默认角色
g_currentUser; g_currentUser;
// 双人模式相关变量
g_currentPlayer = 'left'; // 当前玩家,left 或 right
g_leftPlayerTime = 0; // 左侧玩家游戏时间(毫秒)
g_rightPlayerTime = 0; // 右侧玩家游戏时间(毫秒)
g_playerStartTime = 0; // 当前玩家开始游戏的时间戳
// ------------------------------------ // ------------------------------------
// ------------ 私有数据 ------------ // ------------ 私有数据 ------------
...@@ -744,6 +739,12 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -744,6 +739,12 @@ export class PlayComponent implements OnInit, OnDestroy {
//--------------------------------------------------------------钓鱼部分--------------------------------------------------------- //--------------------------------------------------------------钓鱼部分---------------------------------------------------------
//--------------------------------------------------------------钓鱼-数据--------------------------------------------------------- //--------------------------------------------------------------钓鱼-数据---------------------------------------------------------
// 双人模式相关变量
private currentPlayer = 'left'; // 当前玩家,left 或 right
private leftPlayerTime = 0; // 左侧玩家游戏时间(毫秒)
private rightPlayerTime = 0; // 右侧玩家游戏时间(毫秒)
private playerStartTime = 0; // 当前玩家开始游戏的时间戳
private cat_left: any; private cat_left: any;
private cat_right: any; private cat_right: any;
private cat_center: any; private cat_center: any;
...@@ -753,12 +754,28 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -753,12 +754,28 @@ export class PlayComponent implements OnInit, OnDestroy {
private clickedFishData: Question[] = []; private clickedFishData: Question[] = [];
private touchFish = null; private touchFish = null;
private currentCat
setCurrentCat(cat) {
this.currentCat = cat;
}
getCurrentCat() { getCurrentCat() {
return this.currentCat; if (this.g_formData.doubleModal) {
return this.currentPlayer == 'left' ? this.cat_left : this.cat_right;
} else {
return this.cat_center;
}
}
initStageData() {
const targetList = this.selectList.concat();
const questions = this.g_formData.questions;
// 1. 梳理出选中的数据数组
const selectedQuestions = targetList.map(index => questions[index]).filter(q => q); // 过滤掉可能的undefined值
// 2. 根据梳理好的数组创建多个Stage
this.gameStages = []; // 清空之前的游戏阶段
selectedQuestions.forEach(currentQuestion => {
// 为每个选中的值创建一个Stage对象
// 正确值为当前值,列表值为整个梳理后的列表
const stage = new Stage(currentQuestion, selectedQuestions);
this.gameStages.push(stage);
});
} }
saveFishSuccess(fish) { saveFishSuccess(fish) {
...@@ -788,7 +805,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -788,7 +805,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 等待1秒后进入下一轮游戏 // 等待1秒后进入下一轮游戏
setTimeout(() => { setTimeout(() => {
this.initNextStage(); this.startNextQuestion();
}, 1000); }, 1000);
} }
} }
...@@ -796,134 +813,93 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -796,134 +813,93 @@ export class PlayComponent implements OnInit, OnDestroy {
//--------------------------------------------------------------钓鱼-初始化--------------------------------------------------------- //--------------------------------------------------------------钓鱼-初始化---------------------------------------------------------
// 第一次进入 // 第一次进入
initBattle() { initBattle() {
this.g_enableMapDown = true; this.g_enableMapDown = true;
this.isGameStart = false;
if (this.g_formData.doubleModal) { if (this.g_formData.doubleModal) {
this.showVsAni('left').then(() => { this.showVsAni('left').then(() => {
this.show321('left').then(() => { this.show321('left').then(() => {
this.startBattle(); this.firstBattle();
}) })
}) })
} else { } else {
this.startBattle(); this.firstBattle();
} }
} }
// 后续完成进入
gameOver() {
// 记录当前玩家的游戏时间
if (this.g_formData.doubleModal) {
const endTime = Date.now();
const playTime = endTime - this.g_playerStartTime;
if (this.g_currentPlayer === 'left') {
// 左侧玩家结束,记录时间并切换到右侧玩家
this.g_leftPlayerTime = playTime;
console.log('左侧玩家游戏时间:', this.g_leftPlayerTime, 'ms');
this.currentStageIndex = 0;
this.isGameStart = false;
// 切换到右侧玩家
this.g_currentPlayer = 'right';
// 播放VS动画
this.showVsAni('right');
// 重置游戏状态,准备第二个玩家
this.cleanGameVar();
// 记录第二个玩家开始时间
this.g_playerStartTime = Date.now();
} else {
// 右侧玩家结束,记录时间
this.g_rightPlayerTime = playTime;
console.log('右侧玩家游戏时间:', this.g_rightPlayerTime, 'ms');
// 双人游戏全部结束,显示鱼掉落动画
// 根据游戏时间决定在哪一边显示鱼掉落动画
let fishRainPosition;
if (this.g_rightPlayerTime < this.g_leftPlayerTime) {
// 右侧玩家时间更短(更快完成),在右侧显示
fishRainPosition = 'right';
} else {
fishRainPosition = 'left';
}
// 显示鱼掉落动画
this.showFishRain(fishRainPosition);
// 重置双人模式状态
this.g_currentPlayer = 'left';
this.g_leftPlayerTime = 0;
this.g_rightPlayerTime = 0;
}
} else {
// 单人模式,直接显示中间的鱼掉落动画
this.showFishRain('center');
}
}
/** /**
* 开始游戏战斗 * 开始游戏战斗
* 初始化游戏元素并设置双人模式 * 初始化游戏元素并设置双人模式
*/ */
startBattle() { firstBattle() {
// 如果是双人模式,记录玩家开始时间 this.initStageData();
if (this.g_formData.doubleModal) {
this.g_playerStartTime = Date.now(); this.playerStartTime = Date.now();
this.isGameStart = true; this.isGameStart = true;
} this.initCat();
// 初始化鱼游戏 this.initStar();
this.initFishGame();
// 初始化发音按钮
this.initFaYinBtn(); this.initFaYinBtn();
}
/** this.updateCat();
* 初始化鱼游戏 this.startNextQuestion();
*/ }
initFishGame() {
console.log("this.selectList", this.selectList); nextBattle(playTime) {
const targetList = this.selectList.concat(); this.cleanGameVar();
const questions = this.g_formData.questions; // 左侧玩家结束,记录时间并切换到右侧玩家
this.leftPlayerTime = playTime;
this.currentStageIndex = 0;
this.isGameStart = false;
this.currentPlayer = 'right';
this.playerStartTime = Date.now();
//临时隐藏猫猫
this.cat_left.ref.visible = false;
this.cat_right.ref.visible = false;
// 播放VS动画
this.showVsAni('right').then(() => {
this.show321('right').then(() => {
this.playerStartTime = Date.now();
this.isGameStart = true;
this.cat_left.ref.visible = true;
this.cat_right.ref.visible = true;
this.updateCat();
this.startNextQuestion();
})
})
}
// 1. 梳理出选中的数据数组 // 第一次进入
const selectedQuestions = targetList.map(index => questions[index]).filter(q => q); // 过滤掉可能的undefined值 initBattle2() {
// 2. 根据梳理好的数组创建多个Stage // 播放VS动画
this.gameStages = []; // 清空之前的游戏阶段 this.showVsAni('right').then(() => {
selectedQuestions.forEach(currentQuestion => { this.show321('right').then(() => {
// 为每个选中的值创建一个Stage对象 this.firstBattle();
// 正确值为当前值,列表值为整个梳理后的列表 })
const stage = new Stage(currentQuestion, selectedQuestions); })
this.gameStages.push(stage); }
});
// 打印创建的Stage对象,用于调试 // 后续完成进入
console.log('Created Stages:', this.gameStages); gameOver() {
// 记录当前玩家的游戏时间
const endTime = Date.now();
const playTime = endTime - this.playerStartTime;
this.layoutPage.ref.visible = false; if (this.currentPlayer === 'left') {
this.checkBtnElement.ref.visible = false; return this.nextBattle(playTime);
this.listItem.ref.visible = false;
// this.initVsView();
if (this.g_formData.doubleModal) {
this.isGameStart = false;
this.cat_left = this.createCat('left'); // 创建左侧猫
this.cat_right = this.createCat('right'); // 创建右侧猫
this.setCurrentCat(this.cat_left);
this.sleepRightCat();
} else {
this.cat_center = this.createCat('center'); // 创建中间猫
this.setCurrentCat(this.cat_center);
} }
// 右侧玩家结束,记录时间
this.rightPlayerTime = playTime;
let fishRainPosition = this.rightPlayerTime < this.leftPlayerTime ? 'right' : 'left';
// 显示鱼掉落动画
this.showFishRain(fishRainPosition);
}
this.initStar();
this.initNextStage();
}
/** /**
* 初始化下一个游戏阶段 * 初始化下一个游戏阶段
...@@ -931,7 +907,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -931,7 +907,7 @@ export class PlayComponent implements OnInit, OnDestroy {
* 2. 后续出鱼按照随机后的数组出现 * 2. 后续出鱼按照随机后的数组出现
* 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏 * 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏
*/ */
initNextStage() { startNextQuestion() {
// 清空之前点击的鱼的数据 // 清空之前点击的鱼的数据
this.clickedFishData = []; this.clickedFishData = [];
...@@ -949,16 +925,10 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -949,16 +925,10 @@ export class PlayComponent implements OnInit, OnDestroy {
} }
return; return;
} }
// 获取当前游戏阶段 // 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex]; const currentQuestion = this.gameStages[this.currentStageIndex];
// 将当前Stage中的数组随机一次 // 将当前Stage中的数组随机一次
currentStage.randomList(); currentQuestion.randomList();
console.log('Current Stage:', currentStage);
console.log('Random List:', currentStage.questionRandomList);
// 清空现有的鱼群 // 清空现有的鱼群
this.clearFishes(); this.clearFishes();
...@@ -967,8 +937,8 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -967,8 +937,8 @@ export class PlayComponent implements OnInit, OnDestroy {
this.m_lastFishTime = 0; // 重置上次生成鱼的时间,使下一帧立即生成鱼 this.m_lastFishTime = 0; // 重置上次生成鱼的时间,使下一帧立即生成鱼
// 每一个stage开始的时候播放一次正确问题的音频 // 每一个stage开始的时候播放一次正确问题的音频
if (currentStage.correctQuestion && currentStage.correctQuestion.audio) { if (currentQuestion.correctQuestion && currentQuestion.correctQuestion.audio) {
this.g_cartoon.playAudio(currentStage.correctQuestion.audio); this.g_cartoon.playAudio(currentQuestion.correctQuestion.audio);
} }
} }
...@@ -1558,18 +1528,42 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1558,18 +1528,42 @@ export class PlayComponent implements OnInit, OnDestroy {
//----------------------------------------------------------------刷新部分---------------------------------------------------------------- //----------------------------------------------------------------刷新部分----------------------------------------------------------------
sleepRightCat() {
this.cat_right["child_mao_ju_zuo"].ref.visible = false; initCat() {
this.cat_right["child_mao_ju_pa"].ref.visible = false; if (this.g_formData.doubleModal) {
this.cat_right["child_mao_hui_zuo"].ref.visible = false; this.cat_left = this.createCat('left'); // 创建左侧猫
this.cat_right["child_mao_hui_pa"].ref.visible = true; this.cat_right = this.createCat('right'); // 创建右侧猫
this.cat_right["child_gan"].ref.visible = false; } else {
this.cat_center = this.createCat('center'); // 创建中间猫
}
} }
sleepLeftCat() {
this.cat_left["child_mao_ju_zuo"].ref.visible = false; updateCat() {
this.cat_left["child_mao_ju_pa"].ref.visible = true; if (this.currentPlayer == 'left') {
this.cat_left["child_mao_hui_zuo"].ref.visible = true; this.cat_right["child_mao_ju_zuo"].ref.visible = false;
this.cat_left["child_mao_hui_pa"].ref.visible = false; this.cat_right["child_mao_ju_pa"].ref.visible = false;
this.cat_right["child_mao_hui_zuo"].ref.visible = false;
this.cat_right["child_mao_hui_pa"].ref.visible = true;
this.cat_right["child_gan"].ref.visible = false;
this.cat_left["child_mao_ju_zuo"].ref.visible = true;
this.cat_left["child_mao_ju_pa"].ref.visible = false;
this.cat_left["child_mao_hui_zuo"].ref.visible = false;
this.cat_left["child_mao_hui_pa"].ref.visible = false;
this.cat_left["child_gan"].ref.visible = true;
} else {
this.cat_right["child_mao_ju_zuo"].ref.visible = false;
this.cat_right["child_mao_ju_pa"].ref.visible = false;
this.cat_right["child_mao_hui_zuo"].ref.visible = true;
this.cat_right["child_mao_hui_pa"].ref.visible = false;
this.cat_right["child_gan"].ref.visible = true;
this.cat_left["child_mao_ju_zuo"].ref.visible = false;
this.cat_left["child_mao_ju_pa"].ref.visible = true;
this.cat_left["child_mao_hui_zuo"].ref.visible = false;
this.cat_left["child_mao_hui_pa"].ref.visible = false;
this.cat_left["child_gan"].ref.visible = false;
}
} }
//----------------------------------------------------------------动画效果---------------------------------------------------------------- //----------------------------------------------------------------动画效果----------------------------------------------------------------
...@@ -1599,7 +1593,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1599,7 +1593,7 @@ export class PlayComponent implements OnInit, OnDestroy {
tempFish.ref.y = fishGlobalY; tempFish.ref.y = fishGlobalY;
// 3. 获取水桶的全局位置 // 3. 获取水桶的全局位置
const tong = this.getCurrentCat()['child_tong_left']; const tong = this.getCurrentCat()[this.currentPlayer == 'left' ? 'child_tong_left' : 'child_tong_right'];
const tongGlobalX = this.getCurrentCat().ref.x + tong.ref.x; const tongGlobalX = this.getCurrentCat().ref.x + tong.ref.x;
const tongGlobalY = this.getCurrentCat().ref.y + tong.ref.y; const tongGlobalY = this.getCurrentCat().ref.y + tong.ref.y;
...@@ -1620,12 +1614,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1620,12 +1614,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 动画完成后移除临时小鱼 // 动画完成后移除临时小鱼
this.m_renderArr = this.m_renderArr.filter(item => item !== tempFish.ref); this.m_renderArr = this.m_renderArr.filter(item => item !== tempFish.ref);
tempFish.ref.visible = false; tempFish.ref.visible = false;
this.playShuiHuaAnimation(this.getCurrentCat()[this.currentPlayer == 'left' ? 'child_tong_left_shui_hua' : 'child_tong_right_shui_hua']);
// 播放鱼箱子水花动画
if (this.getCurrentCat() && this.getCurrentCat()['child_tong_right_shui_hua']) {
// 播放水桶水花动画
this.playShuiHuaAnimation(this.getCurrentCat()['child_tong_left_shui_hua']);
}
this.saveFishSuccess(fish); this.saveFishSuccess(fish);
}, },
TWEEN.Easing.Quadratic.Out // 使用缓动函数使动画更平滑 TWEEN.Easing.Quadratic.Out // 使用缓动函数使动画更平滑
......
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