Commit 9b4a642f authored by Tt's avatar Tt

feat(双人模式): 添加双人游戏模式计时和切换功能

parent 39f5c21c
...@@ -123,6 +123,12 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -123,6 +123,12 @@ export class PlayComponent implements OnInit, OnDestroy {
g_formData: FormData; // 表单转换之后的数据 g_formData: FormData; // 表单转换之后的数据
g_teacherFlag = false; // 默认角色 g_teacherFlag = false; // 默认角色
g_currentUser; g_currentUser;
// 双人模式相关变量
g_currentPlayer = 'left'; // 当前玩家,left 或 right
g_leftPlayerTime = 0; // 左侧玩家游戏时间(毫秒)
g_rightPlayerTime = 0; // 右侧玩家游戏时间(毫秒)
g_playerStartTime = 0; // 当前玩家开始游戏的时间戳
// ------------------------------------ // ------------------------------------
// ------------ 私有数据 ------------ // ------------ 私有数据 ------------
...@@ -867,12 +873,20 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -867,12 +873,20 @@ export class PlayComponent implements OnInit, OnDestroy {
this.g_enableMapDown = true; this.g_enableMapDown = true;
if (this.g_formData.doubleModal) { if (this.g_formData.doubleModal) {
this.showVsAni('left'); this.showVsAni('left');
// this.showVsAni('right');
} else { } else {
this.startBattle(); this.startBattle();
} }
} }
/**
* 开始游戏战斗
* 初始化游戏元素并设置双人模式
*/
startBattle() { startBattle() {
// 如果是双人模式,记录玩家开始时间
if (this.g_formData.doubleModal) {
this.g_currentPlayer = 'left';
this.g_playerStartTime = Date.now();
}
// 初始化鱼游戏 // 初始化鱼游戏
this.initFishGame(); this.initFishGame();
// 初始化发音按钮 // 初始化发音按钮
...@@ -1071,7 +1085,6 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1071,7 +1085,6 @@ export class PlayComponent implements OnInit, OnDestroy {
setTimeout(() => { setTimeout(() => {
console.log("游戏结束!"); console.log("游戏结束!");
fishRain.ref.visible = false; fishRain.ref.visible = false;
this.gameOver();
}, 200); }, 200);
} }
}; };
...@@ -1080,10 +1093,59 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1080,10 +1093,59 @@ export class PlayComponent implements OnInit, OnDestroy {
playNextFrame(); playNextFrame();
} }
/**
* 游戏结束处理
* 在双人模式下,第一个玩家结束后切换到第二个玩家
*/
gameOver() { 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.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');
}
} }
...@@ -1109,7 +1171,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1109,7 +1171,7 @@ export class PlayComponent implements OnInit, OnDestroy {
if (this.currentStageIndex >= this.gameStages.length) { if (this.currentStageIndex >= this.gameStages.length) {
// 所有阶段都完成了,结束游戏 // 所有阶段都完成了,结束游戏
if (this.g_formData.doubleModal) { if (this.g_formData.doubleModal) {
this.showFishRain('left'); this.gameOver();
} else { } else {
this.showFishRain('center'); this.showFishRain('center');
this.endGame(); this.endGame();
......
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