Commit 427b4d1b authored by Tt's avatar Tt

代码整理

parent 9b4a642f
......@@ -442,17 +442,6 @@ export class PlayComponent implements OnInit, OnDestroy {
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
/**
* 初始化游戏
* 依次初始化游戏中的各个元素,包括背景、标题、按钮和字母块
*/
initGame() {
// 初始化游戏背景
this.initBackground()
this.initListPage();
}
/**
* 清理游戏变量
* 重置所有游戏相关的变量和状态,为重新开始游戏做准备
......@@ -484,6 +473,19 @@ export class PlayComponent implements OnInit, OnDestroy {
}, 500)
}
/**
* 初始化游戏
* 依次初始化游戏中的各个元素,包括背景、标题、按钮和字母块
*/
initGame() {
// 初始化游戏背景
this.initBackground()
this.initListPage();
}
// --------------------------------列表部分----------------------------------------------
/**
* 初始化游戏背景
* 创建背景元素并设置为静态背景
......@@ -533,7 +535,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.render(jiaoshiElement.ref, zIndexMap.background + 1)
}
// 列表页面处理区
/**
* 初始化列表页面
* 创建一个可滑动的列表页面,包含多个子节点
......@@ -557,41 +559,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 渲染layout_page
this.render(layoutPage.ref, zIndexMap.card);
}
/**
* 切换项目的选中状态
* @param itemNode 项目节点
* @param dataIndex 数据索引
*/
toggleItemSelection(itemNode, dataIndex) {
if (!this.layoutPage.ref.visible) return;
this.g_cartoon.playAudio('click_pic')
const isSelected = itemNode.ref.isSelected || false;
const selectMark = itemNode.ref.selectMark;
if (!isSelected) {
// 选中状态:显示选择标记,添加到selectList
selectMark.visible = true;
itemNode.ref.isSelected = true;
if (!this.selectList.includes(dataIndex)) {
this.selectList.push(dataIndex);
}
} else {
// 取消选中:隐藏选择标记,从selectList移除
selectMark.visible = false;
itemNode.ref.isSelected = false;
const index = this.selectList.indexOf(dataIndex);
if (index > -1) {
this.selectList.splice(index, 1);
}
}
// 更新按钮状态
this.updateCheckBtnState();
console.log('当前选中项目:', this.selectList);
}
initPageTitle() {
// 创建标题背景图片元素
const titleBgElement = this.g_cartoon.createCartoonElement('page_title_bg', 'MySprite');
......@@ -624,72 +592,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.layoutPage.ref.addChild(titleBgElement.ref);
this.layoutPage.ref.addChild(titleTextElement.ref);
}
initCheckBtn() {
// 创建按钮元素
const checkBtnElement = this.g_cartoon.createCartoonElement('check_btn', 'MySprite');
// 根据selectList长度决定显示的按钮图片和文字
const updateButtonState = () => {
if (this.selectList.length >= 2) {
// 显示"开始游戏"按钮
checkBtnElement.ref.init(this.g_cartoon.images.get('kai_shi'));
} else {
// 显示"选择单词"按钮
checkBtnElement.ref.init(this.g_cartoon.images.get('xuan_ze'));
}
};
// 初始化按钮状态
updateButtonState();
// 设置按钮缩放
checkBtnElement.ref.scaleX = this.g_mapScale;
checkBtnElement.ref.scaleY = this.g_mapScale;
// 设置按钮位置 - 屏幕右下角
// kai_shi和xuan_ze的尺寸都是209x65
const buttonWidth = 209 * this.g_mapScale;
const buttonHeight = 65 * this.g_mapScale;
checkBtnElement.ref.x = this.g_canvasWidth - buttonWidth / 2 - 50 * this.g_mapScale; // 距离右边50像素
checkBtnElement.ref.y = this.g_canvasHeight - buttonHeight / 2 - 50 * this.g_mapScale; // 距离底部50像素
// 添加点击事件
this.m_mapDownQueue['check_btn'] = () => {
if (!this.layoutPage.ref.visible) return
// 播放点击音效
this.g_cartoon.playAudio('sm-click');
if (this.selectList.length >= 2) {
// 开始游戏逻辑
console.log('开始游戏,选中的项目:', this.selectList);
this.layoutPage.ref.visible = false;
this.checkBtnElement.ref.visible = false;
this.listItem.ref.visible = false;
this.initBattle();
} else {
// 提示需要选择更多单词
console.log('请至少选择2个单词');
// 这里可以添加提示逻辑
}
};
// 保存按钮引用,用于状态更新
this.checkBtnElement = checkBtnElement;
// 渲染按钮
this.render(checkBtnElement.ref, zIndexMap.card + 2);
}
updateCheckBtnState() {
if (this.checkBtnElement) {
if (this.selectList.length >= 2) {
// 显示"开始游戏"按钮
this.checkBtnElement.ref.init(this.g_cartoon.images.get('kai_shi'));
} else {
// 显示"选择单词"按钮
this.checkBtnElement.ref.init(this.g_cartoon.images.get('xuan_ze'));
}
}
}
initListItem() {
// 获取questions数据
const questions = this.g_formData.questions || [];
......@@ -796,7 +699,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.g_cartoon.playAudio('click');
// 切换选中状态
this.toggleItemSelection(itemNode, i);
this.onTouchItem(itemNode, i);
}
};
......@@ -868,304 +771,242 @@ export class PlayComponent implements OnInit, OnDestroy {
this.listItem = listItem;
}
initCheckBtn() {
// 创建按钮元素
const checkBtnElement = this.g_cartoon.createCartoonElement('check_btn', 'MySprite');
initBattle() {
this.g_enableMapDown = true;
if (this.g_formData.doubleModal) {
this.showVsAni('left');
} else {
this.startBattle();
}
}
/**
* 开始游戏战斗
* 初始化游戏元素并设置双人模式
*/
startBattle() {
// 如果是双人模式,记录玩家开始时间
if (this.g_formData.doubleModal) {
this.g_currentPlayer = 'left';
this.g_playerStartTime = Date.now();
}
// 初始化鱼游戏
this.initFishGame();
// 初始化发音按钮
this.initFaYinBtn();
}
// 根据selectList长度决定显示的按钮图片和文字
const updateButtonState = () => {
if (this.selectList.length >= 2) {
// 显示"开始游戏"按钮
checkBtnElement.ref.init(this.g_cartoon.images.get('kai_shi'));
} else {
// 显示"选择单词"按钮
checkBtnElement.ref.init(this.g_cartoon.images.get('xuan_ze'));
}
};
/**
* 初始化鱼游戏
*/
private cat_left: any;
private cat_right: any;
private cat_center: any;
initFishGame() {
console.log("this.selectList", this.selectList);
const targetList = this.selectList.concat();
const questions = this.g_formData.questions;
// 初始化按钮状态
updateButtonState();
// 1. 梳理出选中的数据数组
const selectedQuestions = targetList.map(index => questions[index]).filter(q => q); // 过滤掉可能的undefined值
// 设置按钮缩放
checkBtnElement.ref.scaleX = this.g_mapScale;
checkBtnElement.ref.scaleY = this.g_mapScale;
// 2. 根据梳理好的数组创建多个Stage
this.gameStages = []; // 清空之前的游戏阶段
selectedQuestions.forEach(currentQuestion => {
// 为每个选中的值创建一个Stage对象
// 正确值为当前值,列表值为整个梳理后的列表
const stage = new Stage(currentQuestion, selectedQuestions);
this.gameStages.push(stage);
});
// 设置按钮位置 - 屏幕右下角
// kai_shi和xuan_ze的尺寸都是209x65
const buttonWidth = 209 * this.g_mapScale;
const buttonHeight = 65 * this.g_mapScale;
checkBtnElement.ref.x = this.g_canvasWidth - buttonWidth / 2 - 50 * this.g_mapScale; // 距离右边50像素
checkBtnElement.ref.y = this.g_canvasHeight - buttonHeight / 2 - 50 * this.g_mapScale; // 距离底部50像素
// 打印创建的Stage对象,用于调试
console.log('Created Stages:', this.gameStages);
// 添加点击事件
this.m_mapDownQueue['check_btn'] = () => {
if (!this.layoutPage.ref.visible) return
// 播放点击音效
this.g_cartoon.playAudio('sm-click');
this.layoutPage.ref.visible = false;
this.checkBtnElement.ref.visible = false;
this.listItem.ref.visible = false;
// this.initVsView();
if (this.g_formData.doubleModal) {
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);
}
if (this.selectList.length >= 2) {
// 开始游戏逻辑
console.log('开始游戏,选中的项目:', this.selectList);
this.layoutPage.ref.visible = false;
this.checkBtnElement.ref.visible = false;
this.listItem.ref.visible = false;
this.initBattle();
} else {
// 提示需要选择更多单词
console.log('请至少选择2个单词');
// 这里可以添加提示逻辑
}
};
this.initStar();
// 保存按钮引用,用于状态更新
this.checkBtnElement = checkBtnElement;
this.initNextStage();
// 渲染按钮
this.render(checkBtnElement.ref, zIndexMap.card + 2);
}
/**
* 初始化星星标记区域
* 根据题目数量创建星星标记区域
* 背景使用star_bg
* 前景将star_0和star_1渲染在同一个节点,未通过关卡显示star_0,通过后显示star_1
* 切换项目的选中状态
* @param itemNode 项目节点
* @param dataIndex 数据索引
*/
initStar() {
// 获取题目数量
const stageCount = this.gameStages.length;
if (stageCount <= 0) return;
// 计算星星之间的间距和所需的总宽度
const starWidth = localImagesSize['star_0'].width * this.g_mapScale;
const starSpacing = 10 * this.g_mapScale; // 星星之间的间距
const totalStarsWidth = stageCount * starWidth + (stageCount - 1) * starSpacing; // 星星总宽度加上间距
// 计算背景所需的最小宽度,确保能容纳所有星星并留有边距
const minBgWidth = totalStarsWidth + 40 * this.g_mapScale; // 两侧各留20像素的边距
onTouchItem(itemNode, dataIndex) {
if (!this.layoutPage.ref.visible) return;
// 创建星星背景
const starBg = this.g_cartoon.createCartoonElement('star_bg_element', 'MySprite');
starBg.ref.init(this.g_cartoon.images.get('star_bg'));
this.g_cartoon.playAudio('click_pic')
// 计算背景需要的缩放比例,确保宽度足够
const originalBgWidth = localImagesSize['star_bg'].width;
const widthScaleFactor = minBgWidth / (originalBgWidth * this.g_mapScale);
const finalScaleX = this.g_mapScale * Math.max(1, widthScaleFactor); // 确保不会缩小背景
const isSelected = itemNode.ref.isSelected || false;
const selectMark = itemNode.ref.selectMark;
// 设置背景缩放
starBg.ref.scaleX = finalScaleX;
starBg.ref.scaleY = this.g_mapScale;
if (!isSelected) {
// 选中状态:显示选择标记,添加到selectList
selectMark.visible = true;
itemNode.ref.isSelected = true;
if (!this.selectList.includes(dataIndex)) {
this.selectList.push(dataIndex);
}
} else {
// 取消选中:隐藏选择标记,从selectList移除
selectMark.visible = false;
itemNode.ref.isSelected = false;
const index = this.selectList.indexOf(dataIndex);
if (index > -1) {
this.selectList.splice(index, 1);
}
}
// 设置星星背景位置在屏幕顶部中央
const bgWidth = originalBgWidth * finalScaleX;
const bgHeight = localImagesSize['star_bg'].height * this.g_mapScale;
starBg.ref.x = this.g_canvasWidth / 2;
starBg.ref.y = bgHeight / 2 + 10 * this.g_mapScale; // 距离顶部10像素
// 更新按钮状态
this.updateCheckBtn();
// 渲染星星背景
this.render(starBg.ref, zIndexMap.card + 10);
console.log('当前选中项目:', this.selectList);
}
updateCheckBtn() {
if (this.checkBtnElement) {
if (this.selectList.length >= 2) {
// 显示"开始游戏"按钮
this.checkBtnElement.ref.init(this.g_cartoon.images.get('kai_shi'));
} else {
// 显示"选择单词"按钮
this.checkBtnElement.ref.init(this.g_cartoon.images.get('xuan_ze'));
}
}
}
// 存储星星元素的数组,用于后续更新
this.starElements = [];
//--------------------------------------------------------------钓鱼部分---------------------------------------------------------
// 使用前面计算好的星星宽度和总宽度
// 计算第一个星星的X坐标,确保星星在背景上居中排列
const startX = this.g_canvasWidth / 2 - totalStarsWidth / 2 + starWidth / 2; // 第一个星星的X坐标
//--------------------------------------------------------------钓鱼-数据---------------------------------------------------------
private cat_left: any;
private cat_right: any;
private cat_center: any;
// 当前游戏阶段索引
private currentStageIndex = 0;
// 存储用户点击的鱼的数据
private clickedFishData: Question[] = [];
// 创建星星元素
for (let i = 0; i < stageCount; i++) {
// 创建未完成状态的星星
const star = this.g_cartoon.createCartoonElement(`star_${i}`, 'MySprite');
star.ref.init(this.g_cartoon.images.get('star_0')); // 初始显示未完成状态
star.ref.scaleX = this.g_mapScale;
star.ref.scaleY = this.g_mapScale;
star.ref.x = startX + i * (starWidth + starSpacing); // 星星X坐标,使用前面定义的间距
star.ref.y = starBg.ref.y; // 与背景Y坐标相同
private touchFish = null;
private currentCat
setCurrentCat(cat) {
this.currentCat = cat;
}
getCurrentCat() {
return this.currentCat;
}
// 渲染星星,层级比背景高
this.render(star.ref, zIndexMap.card + 11);
saveFishSuccess(fish) {
const currentStage = this.gameStages[this.currentStageIndex];
if (fish.questionData) {
this.clickedFishData.push(fish.questionData);
// 将星星元素添加到数组中,用于后续更新
this.starElements.push(star);
}
}
console.log('Clicked fish count:', this.clickedFishData.length);
/**
* 更新星星状态
* 根据当前关卡索引更新星星显示状态
* 已完成关卡显示star_1,未完成关卡显示star_0
*/
updateStar() {
if (!this.starElements || this.starElements.length === 0) return;
// 计算正确点击的次数
const correctClicks = this.clickedFishData.filter(data =>
data.audio === currentStage.correctQuestion.audio &&
data.image === currentStage.correctQuestion.image
).length;
// 遍历所有星星元素
for (let i = 0; i < this.starElements.length; i++) {
const star = this.starElements[i];
console.log('Correct clicks:', correctClicks);
// 如果当前关卡索引大于星星索引,说明该关卡已完成
if (this.currentStageIndex > i) {
// 更新为完成状态
star.ref.init(this.g_cartoon.images.get('star_1'));
} else {
// 更新为未完成状态
star.ref.init(this.g_cartoon.images.get('star_0'));
// 如果有两个数据为当前Stage的正确数据,则清空画面,等待1秒后进入下一轮游戏
if (correctClicks >= 2) {
console.log('Stage completed! Moving to next stage...');
// 清空画面(清除所有鱼)
this.clearFishes();
// 增加当前阶段索引
this.currentStageIndex++;
// 等待1秒后进入下一轮游戏
setTimeout(() => {
this.initNextStage();
}, 1000);
}
}
}
//--------------------------------------------------------------钓鱼-初始化---------------------------------------------------------
initBattle() {
this.g_enableMapDown = true;
if (this.g_formData.doubleModal) {
this.showVsAni('left');
} else {
this.startBattle();
}
}
/**
* 显示鱼掉落动画
* @param type 位置类型:'center', 'left', 'right'
* 开始游戏战斗
* 初始化游戏元素并设置双人模式
*/
showFishRain(type) {
// 根据type确定鱼掉落的位置
let fishX = this.g_canvasWidth / 2; // 默认中间位置
switch (type) {
case 'left':
fishX = this.g_canvasWidth / 2 - 400 * this.g_mapScale;
break;
case 'right':
fishX = this.g_canvasWidth / 2 + 400 * this.g_mapScale;
break;
case 'center':
default:
fishX = this.g_canvasWidth / 2;
break;
startBattle() {
// 如果是双人模式,记录玩家开始时间
if (this.g_formData.doubleModal) {
this.g_playerStartTime = Date.now();
this.isGameStart = true;
}
// 初始化鱼游戏
this.initFishGame();
// 初始化发音按钮
this.initFaYinBtn();
}
// 创建鱼元素
const fishRain = this.g_cartoon.createCartoonElement('fish_rain', 'MySprite');
fishRain.ref.init(this.g_cartoon.images.get('yu_00')); // 初始帧
fishRain.ref.scaleX = this.g_mapScale;
fishRain.ref.scaleY = this.g_mapScale;
fishRain.ref.x = fishX;
fishRain.ref.y = 245 * this.g_mapScale; // 从屏幕顶部开始
// 渲染鱼元素,使用较高的层级确保在其他元素上方
this.render(fishRain.ref, zIndexMap.cat + 10);
/**
* 初始化鱼游戏
*/
initFishGame() {
console.log("this.selectList", this.selectList);
const targetList = this.selectList.concat();
const questions = this.g_formData.questions;
// 播放音效
this.g_cartoon.playAudio('fish_down');
// 1. 梳理出选中的数据数组
const selectedQuestions = targetList.map(index => questions[index]).filter(q => q); // 过滤掉可能的undefined值
// 设置帧动画参数
let currentFrame = 0;
const maxFrame = 80;
const frameInterval = 50; // 每帧间隔时间,可以根据需要调整
// 2. 根据梳理好的数组创建多个Stage
this.gameStages = []; // 清空之前的游戏阶段
selectedQuestions.forEach(currentQuestion => {
// 为每个选中的值创建一个Stage对象
// 正确值为当前值,列表值为整个梳理后的列表
const stage = new Stage(currentQuestion, selectedQuestions);
this.gameStages.push(stage);
});
// 创建帧动画函数
const playNextFrame = () => {
if (currentFrame <= maxFrame) {
// 设置当前帧的图像
const frameNumber = currentFrame.toString().padStart(2, '0'); // 确保两位数格式:00, 01, ...
fishRain.ref.init(this.g_cartoon.images.get(`yu_${frameNumber}`));
// 打印创建的Stage对象,用于调试
console.log('Created Stages:', this.gameStages);
// 递增帧索引
currentFrame++;
this.layoutPage.ref.visible = false;
this.checkBtnElement.ref.visible = false;
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);
}
// 设置下一帧的定时器
const timeoutId = setTimeout(playNextFrame, frameInterval);
this.m_setTimeoutIDs.push(timeoutId);
} else {
// 动画播放完成后隐藏鱼元素
// 显示游戏结束提示
setTimeout(() => {
console.log("游戏结束!");
fishRain.ref.visible = false;
}, 200);
}
};
this.initStar();
// 开始播放帧动画
playNextFrame();
this.initNextStage();
}
/**
* 游戏结束处理
* 在双人模式下,第一个玩家结束后切换到第二个玩家
* 初始化下一个游戏阶段
* 1. 将当前Stage中的数组随机一次
* 2. 后续出鱼按照随机后的数组出现
* 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏
*/
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');
initNextStage() {
// 清空之前点击的鱼的数据
this.clickedFishData = [];
// 重置游戏状态,准备第二个玩家
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');
}
}
// 当前游戏阶段索引
private currentStageIndex = 0;
// 存储用户点击的鱼的数据
private clickedFishData: Question[] = [];
/**
* 初始化下一个游戏阶段
* 1. 将当前Stage中的数组随机一次
* 2. 后续出鱼按照随机后的数组出现
* 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏
*/
initNextStage() {
// 清空之前点击的鱼的数据
this.clickedFishData = [];
// 更新星星状态,显示已完成关卡的星星
this.updateStar();
// 更新星星状态,显示已完成关卡的星星
this.updateStar();
// 检查是否还有游戏阶段
if (this.currentStageIndex >= this.gameStages.length) {
......@@ -1202,294 +1043,117 @@ export class PlayComponent implements OnInit, OnDestroy {
}
/**
* 初始化VS视图,创建所有VS动画相关的元素
* 游戏结束处理
* 在双人模式下,第一个玩家结束后切换到第二个玩家
*/
initVsView() {
// 创建红色渐变面板 - 初始位置在屏幕左侧外
const hong_se_jian_bian = this.g_cartoon.createCartoonElement('hong_se_jian_bian', 'MySprite');
hong_se_jian_bian.ref.init(this.g_cartoon.images.get('hong_se_jian_bian'));
hong_se_jian_bian.ref.scaleX = this.g_mapScale;
hong_se_jian_bian.ref.scaleY = this.g_mapScale;
hong_se_jian_bian.ref.x = -400 * this.g_mapScale; // 初始位置在屏幕左侧外
hong_se_jian_bian.ref.y = this.g_canvasHeight / 2;
this.render(hong_se_jian_bian.ref, zIndexMap.card);
// 创建蓝色渐变面板 - 初始位置在屏幕右侧外
const lan_se_jian_bian = this.g_cartoon.createCartoonElement('lan_se_jian_bian', 'MySprite');
lan_se_jian_bian.ref.init(this.g_cartoon.images.get('lan_se_jian_bian'));
lan_se_jian_bian.ref.scaleX = this.g_mapScale;
lan_se_jian_bian.ref.scaleY = this.g_mapScale;
lan_se_jian_bian.ref.x = this.g_canvasWidth + 400 * this.g_mapScale; // 初始位置在屏幕右侧外
lan_se_jian_bian.ref.y = this.g_canvasHeight / 2;
this.render(lan_se_jian_bian.ref, zIndexMap.card);
const v_element = this.g_cartoon.createCartoonElement('v', 'MySprite');
v_element.ref.init(this.g_cartoon.images.get('v'));
v_element.ref.scaleX = this.g_mapScale;
v_element.ref.scaleY = this.g_mapScale;
v_element.ref.x = this.g_canvasWidth / 2 - 20 * this.g_mapScale;
v_element.ref.y = this.g_canvasHeight / 2;
this.render(v_element.ref, zIndexMap.card + 2);
gameOver() {
// 记录当前玩家的游戏时间
if (this.g_formData.doubleModal) {
const endTime = Date.now();
const playTime = endTime - this.g_playerStartTime;
const s_element = this.g_cartoon.createCartoonElement('s', 'MySprite');
s_element.ref.init(this.g_cartoon.images.get('s'));
s_element.ref.scaleX = this.g_mapScale;
s_element.ref.scaleY = this.g_mapScale;
s_element.ref.x = this.g_canvasWidth / 2 + 20 * this.g_mapScale;
s_element.ref.y = this.g_canvasHeight / 2;
this.render(s_element.ref, zIndexMap.card + 2);
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动画元素
// 左侧元素:橘猫头和V
const ju_mao_tou = this.g_cartoon.createCartoonElement('ju_mao_tou', 'MySprite');
ju_mao_tou.ref.init(this.g_cartoon.images.get('ju_mao_tou'));
ju_mao_tou.ref.scaleX = this.g_mapScale;
ju_mao_tou.ref.scaleY = this.g_mapScale;
ju_mao_tou.ref.x = this.g_canvasWidth / 4;
ju_mao_tou.ref.y = this.g_canvasHeight / 2;
this.render(ju_mao_tou.ref, zIndexMap.card + 1);
// 播放VS动画
this.showVsAni('right');
// 右侧元素:灰猫头和S
const hui_mao_tou = this.g_cartoon.createCartoonElement('hui_mao_tou', 'MySprite');
hui_mao_tou.ref.init(this.g_cartoon.images.get('hui_mao_tou'));
hui_mao_tou.ref.scaleX = this.g_mapScale;
hui_mao_tou.ref.scaleY = this.g_mapScale;
hui_mao_tou.ref.x = this.g_canvasWidth * 3 / 4;
hui_mao_tou.ref.y = this.g_canvasHeight / 2;
this.render(hui_mao_tou.ref, zIndexMap.card + 1);
// 重置游戏状态,准备第二个玩家
this.cleanGameVar();
// 播放VS音效
this.g_cartoon.playAudio('vs');
}
// 记录第二个玩家开始时间
this.g_playerStartTime = Date.now();
} else {
// 右侧玩家结束,记录时间
this.g_rightPlayerTime = playTime;
console.log('右侧玩家游戏时间:', this.g_rightPlayerTime, 'ms');
/**
* 显示VS动画
* @param type 动画类型,'left' 或 'right'
*/
showVsAni(type: string) {
// 动画配置参数
const config = {
// 面板动画配置
panel: {
// 红色面板配置
hong: {
offsetX: 350, // 红色面板X轴偏移量
initialOffsetX: -400, // 红色面板初始X轴偏移量(屏幕外)
moveTime: 0.3, // 移动动画时间
scaleRatio: 1.5, // 放大比例
moveDistance: 210 // 移动距离
},
// 蓝色面板配置
lan: {
offsetX: 340, // 蓝色面板X轴偏移量
initialOffsetX: 400, // 蓝色面板初始X轴偏移量(屏幕外)
moveTime: 0.3, // 移动动画时间
scaleRatio: 1.5, // 放大比例
moveDistance: 130 // 移动距离
}
},
// VS元素动画配置
vs: {
// V元素配置
v: {
offsetX: 20, // V元素X轴偏移量
initialOffsetX: -100, // V元素初始X轴偏移量(屏幕外)
moveTime: 0.2 // 移动动画时间
},
// S元素配置
s: {
offsetX: 20, // S元素X轴偏移量
initialOffsetX: 100, // S元素初始X轴偏移量(屏幕外)
moveTime: 0.2 // 移动动画时间
}
},
// 猫猫头动画配置
cat: {
// 橘猫头配置
ju: {
scaleRatio: 1.5, // 放大比例
shrinkRatio: 0.8, // 缩小比例
moveDistance: 80, // 移动距离
animTime: 0.3 // 动画时间
},
// 灰猫头配置
hui: {
scaleRatio: 1.5, // 放大比例
shrinkRatio: 0.8, // 缩小比例
moveDistance: 20, // 移动距离
animTime: 0.3 // 动画时间
// 双人游戏全部结束,显示鱼掉落动画
// 根据游戏时间决定在哪一边显示鱼掉落动画
let fishRainPosition;
if (this.g_rightPlayerTime < this.g_leftPlayerTime) {
// 右侧玩家时间更短(更快完成),在右侧显示
fishRainPosition = 'right';
} else {
fishRainPosition = 'left';
}
},
// 通用配置
common: {
vsAnimDelay: 200, // VS元素动画延迟时间
hideDelay: 3000 // 隐藏元素延迟时间
}
};
// 初始化VS视图
this.initVsView();
// 获取红色和蓝色渐变面板元素
const hong_se_jian_bian = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
const lan_se_jian_bian = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
// 设置红色渐变面板的目标位置
const hongTargetX = this.g_canvasWidth / 2 - config.panel.hong.offsetX * this.g_mapScale;
// 设置蓝色渐变面板的目标位置
const lanTargetX = this.g_canvasWidth / 2 + config.panel.lan.offsetX * this.g_mapScale;
// 使用moveItem函数实现红色渐变面板从左侧移动到目标位置的动画
moveItem(hong_se_jian_bian, hongTargetX, hong_se_jian_bian.y, config.panel.hong.moveTime);
// 使用moveItem函数实现蓝色渐变面板从右侧移动到目标位置的动画
moveItem(lan_se_jian_bian, lanTargetX, lan_se_jian_bian.y, config.panel.lan.moveTime);
// 获取VS元素
const v_element = this.g_cartoon.cartoonElementsBuffer['v'].ref;
const s_element = this.g_cartoon.cartoonElementsBuffer['s'].ref;
const ju_mao_tou = this.g_cartoon.cartoonElementsBuffer['ju_mao_tou'].ref;
const hui_mao_tou = this.g_cartoon.cartoonElementsBuffer['hui_mao_tou'].ref;
// 设置VS元素的初始位置(屏幕外)
v_element.x = config.vs.v.initialOffsetX * this.g_mapScale; // V元素初始位置在屏幕左侧外
s_element.x = this.g_canvasWidth + config.vs.s.initialOffsetX * this.g_mapScale; // S元素初始位置在屏幕右侧外
// VS元素动画延迟执行
setTimeout(() => {
// 使用moveItem函数实现V元素从屏幕外移动到目标位置的动画
moveItem(v_element, this.g_canvasWidth / 2 - config.vs.v.offsetX * this.g_mapScale, v_element.y, config.vs.v.moveTime);
// 显示鱼掉落动画
this.showFishRain(fishRainPosition);
// 使用moveItem函数实现S元素从屏幕外移动到目标位置的动画
moveItem(s_element, this.g_canvasWidth / 2 + config.vs.s.offsetX * this.g_mapScale, s_element.y, config.vs.s.moveTime, () => {
// VS元素移动完成后,调用showVsShadow函数创建阴影效果
this.showVsShadow();
this.g_cartoon.playAudio('vs');
});
setTimeout(() => {
// 隐藏VS元素
v_element.visible = false;
s_element.visible = false;
// 根据类型设置不同的动画效果
if (type === 'left') {
// 左侧胜利的动画效果 - 左侧猫猫头和红色面板放大,右侧猫猫头缩小并向右移动,蓝色底板向右移动
// 左侧猫猫头放大
ju_mao_tou.scaleX = ju_mao_tou.scaleY = this.g_mapScale;
tweenChange(ju_mao_tou, {
scaleX: this.g_mapScale * config.cat.ju.scaleRatio,
scaleY: this.g_mapScale * config.cat.ju.scaleRatio
}, config.cat.ju.animTime);
// 红色面板放大
const hong_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
tweenChange(hong_se_jian_bian_ref, {
scaleX: this.g_mapScale * config.panel.hong.scaleRatio,
scaleY: this.g_mapScale * config.panel.hong.scaleRatio
}, config.panel.hong.moveTime);
// 右侧猫猫头缩小并向右移动
hui_mao_tou.scaleX = hui_mao_tou.scaleY = this.g_mapScale;
tweenChange(hui_mao_tou, {
scaleX: this.g_mapScale * config.cat.hui.shrinkRatio,
scaleY: this.g_mapScale * config.cat.hui.shrinkRatio,
x: hui_mao_tou.x + config.cat.hui.moveDistance * this.g_mapScale,
y: hui_mao_tou.y + 95 * this.g_mapScale
}, config.cat.hui.animTime);
// 蓝色底板向右移动
const lan_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
moveItem(
lan_se_jian_bian_ref,
lan_se_jian_bian_ref.x + config.panel.lan.moveDistance * this.g_mapScale,
lan_se_jian_bian_ref.y + 95 * this.g_mapScale,
config.panel.lan.moveTime
);
} else if (type === 'right') {
// 右侧胜利的动画效果 - 右侧猫猫头和蓝色面板放大,左侧猫猫头缩小并向左移动,红色底板向左移动
// 重置双人模式状态
this.g_currentPlayer = 'left';
this.g_leftPlayerTime = 0;
this.g_rightPlayerTime = 0;
}
} else {
// 单人模式,直接显示中间的鱼掉落动画
this.showFishRain('center');
}
}
//-----------------------------------------------------------------钓鱼-创建---------------------------------------------------------
// 右侧猫猫头放大
hui_mao_tou.scaleX = hui_mao_tou.scaleY = this.g_mapScale;
tweenChange(hui_mao_tou, {
scaleX: this.g_mapScale * config.cat.hui.scaleRatio,
scaleY: this.g_mapScale * config.cat.hui.scaleRatio
}, config.cat.hui.animTime);
// 蓝色面板放大
const lan_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
tweenChange(lan_se_jian_bian_ref, {
scaleX: this.g_mapScale * config.panel.lan.scaleRatio,
scaleY: this.g_mapScale * config.panel.lan.scaleRatio
}, config.panel.lan.moveTime);
/**
* 处理鱼泡泡破裂
* 隐藏泡泡背景、小鱼和图片,显示泡泡破裂动画
* 存储用户点击的鱼的数据,检查是否有两个正确数据
* @param fish 被点击的鱼对象
*/
onTouchFish(fish) {
// 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex];
// 左侧猫猫头缩小并向左移动
ju_mao_tou.scaleX = ju_mao_tou.scaleY = this.g_mapScale;
tweenChange(ju_mao_tou, {
scaleX: this.g_mapScale * config.cat.ju.shrinkRatio,
scaleY: this.g_mapScale * config.cat.ju.shrinkRatio,
x: ju_mao_tou.x - config.cat.ju.moveDistance * this.g_mapScale,
y: ju_mao_tou.y + 95 * this.g_mapScale
}, config.cat.ju.animTime);
// 检查是否点击了正确的鱼
const isCorrect = fish.questionData &&
fish.questionData.audio === currentStage.correctQuestion.audio &&
fish.questionData.image === currentStage.correctQuestion.image;
// 红色底板向左移动
const hong_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
moveItem(
hong_se_jian_bian_ref,
hong_se_jian_bian_ref.x - config.panel.hong.moveDistance * this.g_mapScale,
hong_se_jian_bian_ref.y + 92 * this.g_mapScale,
config.panel.hong.moveTime
);
}
setTimeout(() => {
this.show321(type);
}, 1000);
}, config.vs.v.moveTime * 1000 + 500);
}, config.common.vsAnimDelay);
}
console.log('Clicked fish data:', fish.questionData);
console.log('Is correct:', isCorrect);
/**
* 显示VS阴影动画效果
* 在VS动画运动后的位置创建两个VS图标,并执行放大5倍、透明度为0的动画
* V字母向左移动50,S字母向右移动50,动画时间为0.1秒
*/
showVsShadow() {
// 创建V阴影元素
const v_shadow = this.g_cartoon.createCartoonElement('v_shadow', 'MySprite');
v_shadow.ref.init(this.g_cartoon.images.get('v'));
v_shadow.ref.scaleX = this.g_mapScale;
v_shadow.ref.scaleY = this.g_mapScale;
v_shadow.ref.x = this.g_canvasWidth / 2 - 20 * this.g_mapScale; // 与原始V元素位置相同
v_shadow.ref.y = this.g_canvasHeight / 2;
this.render(v_shadow.ref, zIndexMap.card + 1); // 放在原始VS元素下方
// 保存鱼的原始速度,用于后续恢复
const originalSpeed = fish.speed || 0.3;
// 点击后立即停止鱼的运动
fish.speed = 0;
// 创建S阴影元素
const s_shadow = this.g_cartoon.createCartoonElement('s_shadow', 'MySprite');
s_shadow.ref.init(this.g_cartoon.images.get('s'));
s_shadow.ref.scaleX = this.g_mapScale;
s_shadow.ref.scaleY = this.g_mapScale;
s_shadow.ref.x = this.g_canvasWidth / 2 + 20 * this.g_mapScale; // 与原始S元素位置相同
s_shadow.ref.y = this.g_canvasHeight / 2;
this.render(s_shadow.ref, zIndexMap.card + 1); // 放在原始VS元素下方
if (isCorrect) {
// 正确小鱼的处理逻辑
// 隐藏泡泡背景、小鱼和图片
fish['child_paopao_bg'].ref.visible = false;
fish['child_xiao_yu'].ref.visible = false;
fish['child_image'].ref.visible = false;
fish['child_paopao_ani'].ref.visible = true;
// 执行V阴影元素的动画:放大5倍,透明度为0,向左移动50
tweenChange(v_shadow.ref, {
scaleX: this.g_mapScale * 4,
scaleY: this.g_mapScale * 4,
alpha: 0.3,
x: v_shadow.ref.x - 50 * this.g_mapScale
}, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['v_shadow'].ref.visible = false;
});
const currentStage = this.gameStages[this.currentStageIndex];
if (currentStage && currentStage.correctQuestion && currentStage.correctQuestion.audio) {
// 播放当前正确问题的音频
this.g_cartoon.playAudio(currentStage.correctQuestion.audio);
}
// 注销该鱼的点击事件
this.unsubscribeFishEvent(fish.fishId);
// 创建飞向水桶的小鱼动画
this.createFlyingFish(fish);
this.brokePaopaoAni(fish);
} else {
// 错误小鱼的处理逻辑
// 播放错误音效
this.g_cartoon.playAudio('click_wrong');
this.aniOpacity(fish);
// 执行S阴影元素的动画:放大5倍,透明度为0,向右移动50
tweenChange(s_shadow.ref, {
scaleX: this.g_mapScale * 4,
scaleY: this.g_mapScale * 4,
alpha: 0.3,
x: s_shadow.ref.x + 50 * this.g_mapScale
}, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['s_shadow'].ref.visible = false;
});
// 1秒后恢复鱼的运动
setTimeout(() => {
fish.speed = originalSpeed;
}, 1000);
}
return fish;
}
createCat(type) {
// 创建cat根节点
const cat = this.g_cartoon.createCartoonElement('cat', 'MySprite');
......@@ -1746,62 +1410,298 @@ export class PlayComponent implements OnInit, OnDestroy {
})
return fish;
}
private touchFish = null;
//-----------------------------------------------------------------钓鱼-动画---------------------------------------------------------
/**
* 初始化VS视图,创建所有VS动画相关的元素
*/
initVsView() {
// 创建红色渐变面板 - 初始位置在屏幕左侧外
const hong_se_jian_bian = this.g_cartoon.createCartoonElement('hong_se_jian_bian', 'MySprite');
hong_se_jian_bian.ref.init(this.g_cartoon.images.get('hong_se_jian_bian'));
hong_se_jian_bian.ref.scaleX = this.g_mapScale;
hong_se_jian_bian.ref.scaleY = this.g_mapScale;
hong_se_jian_bian.ref.x = -400 * this.g_mapScale; // 初始位置在屏幕左侧外
hong_se_jian_bian.ref.y = this.g_canvasHeight / 2;
this.render(hong_se_jian_bian.ref, zIndexMap.card);
// 创建蓝色渐变面板 - 初始位置在屏幕右侧外
const lan_se_jian_bian = this.g_cartoon.createCartoonElement('lan_se_jian_bian', 'MySprite');
lan_se_jian_bian.ref.init(this.g_cartoon.images.get('lan_se_jian_bian'));
lan_se_jian_bian.ref.scaleX = this.g_mapScale;
lan_se_jian_bian.ref.scaleY = this.g_mapScale;
lan_se_jian_bian.ref.x = this.g_canvasWidth + 400 * this.g_mapScale; // 初始位置在屏幕右侧外
lan_se_jian_bian.ref.y = this.g_canvasHeight / 2;
this.render(lan_se_jian_bian.ref, zIndexMap.card);
const v_element = this.g_cartoon.createCartoonElement('v', 'MySprite');
v_element.ref.init(this.g_cartoon.images.get('v'));
v_element.ref.scaleX = this.g_mapScale;
v_element.ref.scaleY = this.g_mapScale;
v_element.ref.x = this.g_canvasWidth / 2 - 20 * this.g_mapScale;
v_element.ref.y = this.g_canvasHeight / 2;
this.render(v_element.ref, zIndexMap.card + 2);
const s_element = this.g_cartoon.createCartoonElement('s', 'MySprite');
s_element.ref.init(this.g_cartoon.images.get('s'));
s_element.ref.scaleX = this.g_mapScale;
s_element.ref.scaleY = this.g_mapScale;
s_element.ref.x = this.g_canvasWidth / 2 + 20 * this.g_mapScale;
s_element.ref.y = this.g_canvasHeight / 2;
this.render(s_element.ref, zIndexMap.card + 2);
// 创建其他VS动画元素
// 左侧元素:橘猫头和V
const ju_mao_tou = this.g_cartoon.createCartoonElement('ju_mao_tou', 'MySprite');
ju_mao_tou.ref.init(this.g_cartoon.images.get('ju_mao_tou'));
ju_mao_tou.ref.scaleX = this.g_mapScale;
ju_mao_tou.ref.scaleY = this.g_mapScale;
ju_mao_tou.ref.x = this.g_canvasWidth / 4;
ju_mao_tou.ref.y = this.g_canvasHeight / 2;
this.render(ju_mao_tou.ref, zIndexMap.card + 1);
// 右侧元素:灰猫头和S
const hui_mao_tou = this.g_cartoon.createCartoonElement('hui_mao_tou', 'MySprite');
hui_mao_tou.ref.init(this.g_cartoon.images.get('hui_mao_tou'));
hui_mao_tou.ref.scaleX = this.g_mapScale;
hui_mao_tou.ref.scaleY = this.g_mapScale;
hui_mao_tou.ref.x = this.g_canvasWidth * 3 / 4;
hui_mao_tou.ref.y = this.g_canvasHeight / 2;
this.render(hui_mao_tou.ref, zIndexMap.card + 1);
// 播放VS音效
this.g_cartoon.playAudio('vs');
}
/**
* 显示VS动画
* @param type 动画类型,'left' 或 'right'
*/
showVsAni(type: string) {
// 动画配置参数
const config = {
// 面板动画配置
panel: {
// 红色面板配置
hong: {
offsetX: 350, // 红色面板X轴偏移量
initialOffsetX: -400, // 红色面板初始X轴偏移量(屏幕外)
moveTime: 0.3, // 移动动画时间
scaleRatio: 1.5, // 放大比例
moveDistance: 210 // 移动距离
},
// 蓝色面板配置
lan: {
offsetX: 340, // 蓝色面板X轴偏移量
initialOffsetX: 400, // 蓝色面板初始X轴偏移量(屏幕外)
moveTime: 0.3, // 移动动画时间
scaleRatio: 1.5, // 放大比例
moveDistance: 130 // 移动距离
}
},
// VS元素动画配置
vs: {
// V元素配置
v: {
offsetX: 20, // V元素X轴偏移量
initialOffsetX: -100, // V元素初始X轴偏移量(屏幕外)
moveTime: 0.2 // 移动动画时间
},
// S元素配置
s: {
offsetX: 20, // S元素X轴偏移量
initialOffsetX: 100, // S元素初始X轴偏移量(屏幕外)
moveTime: 0.2 // 移动动画时间
}
},
// 猫猫头动画配置
cat: {
// 橘猫头配置
ju: {
scaleRatio: 1.5, // 放大比例
shrinkRatio: 0.8, // 缩小比例
moveDistance: 80, // 移动距离
animTime: 0.3 // 动画时间
},
// 灰猫头配置
hui: {
scaleRatio: 1.5, // 放大比例
shrinkRatio: 0.8, // 缩小比例
moveDistance: 20, // 移动距离
animTime: 0.3 // 动画时间
}
},
// 通用配置
common: {
vsAnimDelay: 200, // VS元素动画延迟时间
hideDelay: 3000 // 隐藏元素延迟时间
}
};
// 初始化VS视图
this.initVsView();
// 获取红色和蓝色渐变面板元素
const hong_se_jian_bian = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
const lan_se_jian_bian = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
// 设置红色渐变面板的目标位置
const hongTargetX = this.g_canvasWidth / 2 - config.panel.hong.offsetX * this.g_mapScale;
// 设置蓝色渐变面板的目标位置
const lanTargetX = this.g_canvasWidth / 2 + config.panel.lan.offsetX * this.g_mapScale;
// 使用moveItem函数实现红色渐变面板从左侧移动到目标位置的动画
moveItem(hong_se_jian_bian, hongTargetX, hong_se_jian_bian.y, config.panel.hong.moveTime);
// 使用moveItem函数实现蓝色渐变面板从右侧移动到目标位置的动画
moveItem(lan_se_jian_bian, lanTargetX, lan_se_jian_bian.y, config.panel.lan.moveTime);
// 获取VS元素
const v_element = this.g_cartoon.cartoonElementsBuffer['v'].ref;
const s_element = this.g_cartoon.cartoonElementsBuffer['s'].ref;
const ju_mao_tou = this.g_cartoon.cartoonElementsBuffer['ju_mao_tou'].ref;
const hui_mao_tou = this.g_cartoon.cartoonElementsBuffer['hui_mao_tou'].ref;
// 设置VS元素的初始位置(屏幕外)
v_element.x = config.vs.v.initialOffsetX * this.g_mapScale; // V元素初始位置在屏幕左侧外
s_element.x = this.g_canvasWidth + config.vs.s.initialOffsetX * this.g_mapScale; // S元素初始位置在屏幕右侧外
// VS元素动画延迟执行
setTimeout(() => {
// 使用moveItem函数实现V元素从屏幕外移动到目标位置的动画
moveItem(v_element, this.g_canvasWidth / 2 - config.vs.v.offsetX * this.g_mapScale, v_element.y, config.vs.v.moveTime);
// 使用moveItem函数实现S元素从屏幕外移动到目标位置的动画
moveItem(s_element, this.g_canvasWidth / 2 + config.vs.s.offsetX * this.g_mapScale, s_element.y, config.vs.s.moveTime, () => {
// VS元素移动完成后,调用showVsShadow函数创建阴影效果
this.showVsShadow();
this.g_cartoon.playAudio('vs');
});
setTimeout(() => {
// 隐藏VS元素
v_element.visible = false;
s_element.visible = false;
// 根据类型设置不同的动画效果
if (type === 'left') {
// 左侧胜利的动画效果 - 左侧猫猫头和红色面板放大,右侧猫猫头缩小并向右移动,蓝色底板向右移动
// 左侧猫猫头放大
ju_mao_tou.scaleX = ju_mao_tou.scaleY = this.g_mapScale;
tweenChange(ju_mao_tou, {
scaleX: this.g_mapScale * config.cat.ju.scaleRatio,
scaleY: this.g_mapScale * config.cat.ju.scaleRatio
}, config.cat.ju.animTime);
// 红色面板放大
const hong_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
tweenChange(hong_se_jian_bian_ref, {
scaleX: this.g_mapScale * config.panel.hong.scaleRatio,
scaleY: this.g_mapScale * config.panel.hong.scaleRatio
}, config.panel.hong.moveTime);
// 右侧猫猫头缩小并向右移动
hui_mao_tou.scaleX = hui_mao_tou.scaleY = this.g_mapScale;
tweenChange(hui_mao_tou, {
scaleX: this.g_mapScale * config.cat.hui.shrinkRatio,
scaleY: this.g_mapScale * config.cat.hui.shrinkRatio,
x: hui_mao_tou.x + config.cat.hui.moveDistance * this.g_mapScale,
y: hui_mao_tou.y + 95 * this.g_mapScale
}, config.cat.hui.animTime);
// 蓝色底板向右移动
const lan_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
moveItem(
lan_se_jian_bian_ref,
lan_se_jian_bian_ref.x + config.panel.lan.moveDistance * this.g_mapScale,
lan_se_jian_bian_ref.y + 95 * this.g_mapScale,
config.panel.lan.moveTime
);
} else if (type === 'right') {
// 右侧胜利的动画效果 - 右侧猫猫头和蓝色面板放大,左侧猫猫头缩小并向左移动,红色底板向左移动
// 右侧猫猫头放大
hui_mao_tou.scaleX = hui_mao_tou.scaleY = this.g_mapScale;
tweenChange(hui_mao_tou, {
scaleX: this.g_mapScale * config.cat.hui.scaleRatio,
scaleY: this.g_mapScale * config.cat.hui.scaleRatio
}, config.cat.hui.animTime);
// 蓝色面板放大
const lan_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['lan_se_jian_bian'].ref;
tweenChange(lan_se_jian_bian_ref, {
scaleX: this.g_mapScale * config.panel.lan.scaleRatio,
scaleY: this.g_mapScale * config.panel.lan.scaleRatio
}, config.panel.lan.moveTime);
// 左侧猫猫头缩小并向左移动
ju_mao_tou.scaleX = ju_mao_tou.scaleY = this.g_mapScale;
tweenChange(ju_mao_tou, {
scaleX: this.g_mapScale * config.cat.ju.shrinkRatio,
scaleY: this.g_mapScale * config.cat.ju.shrinkRatio,
x: ju_mao_tou.x - config.cat.ju.moveDistance * this.g_mapScale,
y: ju_mao_tou.y + 95 * this.g_mapScale
}, config.cat.ju.animTime);
// 红色底板向左移动
const hong_se_jian_bian_ref = this.g_cartoon.cartoonElementsBuffer['hong_se_jian_bian'].ref;
moveItem(
hong_se_jian_bian_ref,
hong_se_jian_bian_ref.x - config.panel.hong.moveDistance * this.g_mapScale,
hong_se_jian_bian_ref.y + 92 * this.g_mapScale,
config.panel.hong.moveTime
);
}
setTimeout(() => {
this.show321(type);
}, 1000);
}, config.vs.v.moveTime * 1000 + 500);
}, config.common.vsAnimDelay);
}
/**
* 处理鱼泡泡破裂
* 隐藏泡泡背景、小鱼和图片,显示泡泡破裂动画
* 存储用户点击的鱼的数据,检查是否有两个正确数据
* @param fish 被点击的鱼对象
* 显示VS阴影动画效果
* 在VS动画运动后的位置创建两个VS图标,并执行放大5倍、透明度为0的动画
* V字母向左移动50,S字母向右移动50,动画时间为0.1秒
*/
onTouchFish(fish) {
// 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex];
// 检查是否点击了正确的鱼
const isCorrect = fish.questionData &&
fish.questionData.audio === currentStage.correctQuestion.audio &&
fish.questionData.image === currentStage.correctQuestion.image;
showVsShadow() {
// 创建V阴影元素
const v_shadow = this.g_cartoon.createCartoonElement('v_shadow', 'MySprite');
v_shadow.ref.init(this.g_cartoon.images.get('v'));
v_shadow.ref.scaleX = this.g_mapScale;
v_shadow.ref.scaleY = this.g_mapScale;
v_shadow.ref.x = this.g_canvasWidth / 2 - 20 * this.g_mapScale; // 与原始V元素位置相同
v_shadow.ref.y = this.g_canvasHeight / 2;
this.render(v_shadow.ref, zIndexMap.card + 1); // 放在原始VS元素下方
console.log('Clicked fish data:', fish.questionData);
console.log('Is correct:', isCorrect);
// 创建S阴影元素
const s_shadow = this.g_cartoon.createCartoonElement('s_shadow', 'MySprite');
s_shadow.ref.init(this.g_cartoon.images.get('s'));
s_shadow.ref.scaleX = this.g_mapScale;
s_shadow.ref.scaleY = this.g_mapScale;
s_shadow.ref.x = this.g_canvasWidth / 2 + 20 * this.g_mapScale; // 与原始S元素位置相同
s_shadow.ref.y = this.g_canvasHeight / 2;
this.render(s_shadow.ref, zIndexMap.card + 1); // 放在原始VS元素下方
// 保存鱼的原始速度,用于后续恢复
const originalSpeed = fish.speed || 0.3;
// 点击后立即停止鱼的运动
fish.speed = 0;
// 执行V阴影元素的动画:放大5倍,透明度为0,向左移动50
tweenChange(v_shadow.ref, {
scaleX: this.g_mapScale * 4,
scaleY: this.g_mapScale * 4,
alpha: 0.3,
x: v_shadow.ref.x - 50 * this.g_mapScale
}, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['v_shadow'].ref.visible = false;
});
if (isCorrect) {
// 正确小鱼的处理逻辑
// 隐藏泡泡背景、小鱼和图片
fish['child_paopao_bg'].ref.visible = false;
fish['child_xiao_yu'].ref.visible = false;
fish['child_image'].ref.visible = false;
fish['child_paopao_ani'].ref.visible = true;
// 执行S阴影元素的动画:放大5倍,透明度为0,向右移动50
tweenChange(s_shadow.ref, {
scaleX: this.g_mapScale * 4,
scaleY: this.g_mapScale * 4,
alpha: 0.3,
x: s_shadow.ref.x + 50 * this.g_mapScale
}, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['s_shadow'].ref.visible = false;
});
}
const currentStage = this.gameStages[this.currentStageIndex];
if (currentStage && currentStage.correctQuestion && currentStage.correctQuestion.audio) {
// 播放当前正确问题的音频
this.g_cartoon.playAudio(currentStage.correctQuestion.audio);
}
// 注销该鱼的点击事件
this.unsubscribeFishEvent(fish.fishId);
// 创建飞向水桶的小鱼动画
this.createFlyingFish(fish);
this.brokePaopaoAni(fish);
} else {
// 错误小鱼的处理逻辑
// 播放错误音效
this.g_cartoon.playAudio('click_wrong');
this.aniOpacity(fish);
// 1秒后恢复鱼的运动
setTimeout(() => {
fish.speed = originalSpeed;
}, 1000);
}
return fish;
}
brokePaopaoAni(fish) {
// this.g_cartoon.playAudio('pop_bomb');
// 对于正确的鱼,显示泡泡破裂动画
......@@ -1859,41 +1759,151 @@ export class PlayComponent implements OnInit, OnDestroy {
playNextFrame();
}
saveFishSuccess(fish) {
const currentStage = this.gameStages[this.currentStageIndex];
if (fish.questionData) {
this.clickedFishData.push(fish.questionData);
console.log('Clicked fish count:', this.clickedFishData.length);
//----------------------------------------------------------------刷新部分----------------------------------------------------------------
// 计算正确点击的次数
const correctClicks = this.clickedFishData.filter(data =>
data.audio === currentStage.correctQuestion.audio &&
data.image === currentStage.correctQuestion.image
).length;
sleepRightCat() {
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 = false;
this.cat_right["child_mao_hui_pa"].ref.visible = true;
this.cat_right["child_gan"].ref.visible = false;
}
sleepLeftCat() {
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 = true;
this.cat_left["child_mao_hui_pa"].ref.visible = false;
}
console.log('Correct clicks:', correctClicks);
//----------------------------------------------------------------动画效果----------------------------------------------------------------
// 如果有两个数据为当前Stage的正确数据,则清空画面,等待1秒后进入下一轮游戏
if (correctClicks >= 2) {
console.log('Stage completed! Moving to next stage...');
// 清空画面(清除所有鱼)
this.clearFishes();
/**
* 创建临时小鱼并飞向水桶
* @param fish 原始鱼对象
*/
createFlyingFish(fish) {
// 1. 获取小鱼的全局坐标
const xiao_yu = fish['child_xiao_yu'];
const fishGlobalX = fish.ref.x + xiao_yu.ref.x;
const fishGlobalY = fish.ref.y + xiao_yu.ref.y;
// 增加当前阶段索引
this.currentStageIndex++;
// 2. 创建一个临时小鱼
const tempFishId = `temp_fish_${Math.floor(Math.random() * 1000)}_${Date.now()}`;
const tempFish = this.g_cartoon.createCartoonElement(tempFishId, 'MySprite');
// 等待1秒后进入下一轮游戏
setTimeout(() => {
this.initNextStage();
}, 1000);
}
}
// 使用与原小鱼相同的图像
tempFish.ref.init(xiao_yu.ref.img);
tempFish.ref.scaleX = xiao_yu.ref.scaleX;
tempFish.ref.scaleY = xiao_yu.ref.scaleY;
// 设置临时小鱼的全局位置
tempFish.ref.x = fishGlobalX;
tempFish.ref.y = fishGlobalY;
// 3. 获取水桶的全局位置
const tong = this.getCurrentCat()['child_tong_left'];
const tongGlobalX = this.getCurrentCat().ref.x + tong.ref.x;
const tongGlobalY = this.getCurrentCat().ref.y + tong.ref.y;
// 将临时小鱼添加到舞台
this.render(tempFish.ref, zIndexMap.card + 1);
// 4. 创建小鱼飞向水桶的动画
tweenChange(
tempFish.ref,
{
x: tongGlobalX,
y: tongGlobalY,
scaleX: tempFish.ref.scaleX * 0.5, // 飞行过程中缩小
scaleY: tempFish.ref.scaleY * 0.5
},
0.8, // 动画时间
() => {
// 动画完成后移除临时小鱼
this.m_renderArr = this.m_renderArr.filter(item => item !== tempFish.ref);
tempFish.ref.visible = false;
// 播放鱼箱子水花动画
if (this.getCurrentCat() && this.getCurrentCat()['child_tong_right_shui_hua']) {
// 播放水桶水花动画
this.playShuiHuaAnimation(this.getCurrentCat()['child_tong_left_shui_hua']);
}
this.saveFishSuccess(fish);
},
TWEEN.Easing.Quadratic.Out // 使用缓动函数使动画更平滑
);
}
/**
* 显示鱼掉落动画
* @param type 位置类型:'center', 'left', 'right'
*/
showFishRain(type) {
// 根据type确定鱼掉落的位置
let fishX = this.g_canvasWidth / 2; // 默认中间位置
switch (type) {
case 'left':
fishX = this.g_canvasWidth / 2 - 400 * this.g_mapScale;
break;
case 'right':
fishX = this.g_canvasWidth / 2 + 400 * this.g_mapScale;
break;
case 'center':
default:
fishX = this.g_canvasWidth / 2;
break;
}
// 创建鱼元素
const fishRain = this.g_cartoon.createCartoonElement('fish_rain', 'MySprite');
fishRain.ref.init(this.g_cartoon.images.get('yu_00')); // 初始帧
fishRain.ref.scaleX = this.g_mapScale;
fishRain.ref.scaleY = this.g_mapScale;
fishRain.ref.x = fishX;
fishRain.ref.y = 245 * this.g_mapScale; // 从屏幕顶部开始
// 渲染鱼元素,使用较高的层级确保在其他元素上方
this.render(fishRain.ref, zIndexMap.cat + 10);
// 播放音效
this.g_cartoon.playAudio('fish_down');
// 设置帧动画参数
let currentFrame = 0;
const maxFrame = 80;
const frameInterval = 50; // 每帧间隔时间,可以根据需要调整
// 创建帧动画函数
const playNextFrame = () => {
if (currentFrame <= maxFrame) {
// 设置当前帧的图像
const frameNumber = currentFrame.toString().padStart(2, '0'); // 确保两位数格式:00, 01, ...
fishRain.ref.init(this.g_cartoon.images.get(`yu_${frameNumber}`));
// 递增帧索引
currentFrame++;
// 设置下一帧的定时器
const timeoutId = setTimeout(playNextFrame, frameInterval);
this.m_setTimeoutIDs.push(timeoutId);
} else {
// 动画播放完成后隐藏鱼元素
// 显示游戏结束提示
setTimeout(() => {
console.log("游戏结束!");
fishRain.ref.visible = false;
}, 200);
}
};
// 开始播放帧动画
playNextFrame();
}
aniOpacity(fish) {
// 使用alphaItem创建平滑的透明度变化动画
const childNames = ['child_image', 'child_paopao_bg']
......@@ -1916,87 +1926,7 @@ export class PlayComponent implements OnInit, OnDestroy {
})
}
private currentCat
setCurrentCat(cat) {
this.currentCat = cat;
}
getCurrentCat() {
return this.currentCat;
}
sleepRightCat() {
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 = false;
this.cat_right["child_mao_hui_pa"].ref.visible = true;
this.cat_right["child_gan"].ref.visible = false;
}
sleepLeftCat() {
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 = true;
this.cat_left["child_mao_hui_pa"].ref.visible = false;
}
/**
* 创建临时小鱼并飞向水桶
* @param fish 原始鱼对象
*/
createFlyingFish(fish) {
// 1. 获取小鱼的全局坐标
const xiao_yu = fish['child_xiao_yu'];
const fishGlobalX = fish.ref.x + xiao_yu.ref.x;
const fishGlobalY = fish.ref.y + xiao_yu.ref.y;
// 2. 创建一个临时小鱼
const tempFishId = `temp_fish_${Math.floor(Math.random() * 1000)}_${Date.now()}`;
const tempFish = this.g_cartoon.createCartoonElement(tempFishId, 'MySprite');
// 使用与原小鱼相同的图像
tempFish.ref.init(xiao_yu.ref.img);
tempFish.ref.scaleX = xiao_yu.ref.scaleX;
tempFish.ref.scaleY = xiao_yu.ref.scaleY;
// 设置临时小鱼的全局位置
tempFish.ref.x = fishGlobalX;
tempFish.ref.y = fishGlobalY;
// 3. 获取水桶的全局位置
const tong = this.getCurrentCat()['child_tong_left'];
const tongGlobalX = this.getCurrentCat().ref.x + tong.ref.x;
const tongGlobalY = this.getCurrentCat().ref.y + tong.ref.y;
// 将临时小鱼添加到舞台
this.render(tempFish.ref, zIndexMap.card + 1);
// 4. 创建小鱼飞向水桶的动画
tweenChange(
tempFish.ref,
{
x: tongGlobalX,
y: tongGlobalY,
scaleX: tempFish.ref.scaleX * 0.5, // 飞行过程中缩小
scaleY: tempFish.ref.scaleY * 0.5
},
0.8, // 动画时间
() => {
// 动画完成后移除临时小鱼
this.m_renderArr = this.m_renderArr.filter(item => item !== tempFish.ref);
tempFish.ref.visible = false;
// 播放鱼箱子水花动画
if (this.getCurrentCat() && this.getCurrentCat()['child_tong_right_shui_hua']) {
// 播放水桶水花动画
this.playShuiHuaAnimation(this.getCurrentCat()['child_tong_left_shui_hua']);
}
this.saveFishSuccess(fish);
},
TWEEN.Easing.Quadratic.Out // 使用缓动函数使动画更平滑
);
}
//----------------------------------------------------------------UI部分----------------------------------------------------------------
/**
......@@ -2036,6 +1966,175 @@ export class PlayComponent implements OnInit, OnDestroy {
}
};
}
//----------------------------------------------------------------星星刷新部分----------------------------------------------------------------
/**
* 初始化星星标记区域
* 根据题目数量创建星星标记区域
* 背景使用star_bg
* 前景将star_0和star_1渲染在同一个节点,未通过关卡显示star_0,通过后显示star_1
*/
initStar() {
// 获取题目数量
const stageCount = this.gameStages.length;
if (stageCount <= 0) return;
// 计算星星之间的间距和所需的总宽度
const starWidth = localImagesSize['star_0'].width * this.g_mapScale;
const starSpacing = 10 * this.g_mapScale; // 星星之间的间距
const totalStarsWidth = stageCount * starWidth + (stageCount - 1) * starSpacing; // 星星总宽度加上间距
// 计算背景所需的最小宽度,确保能容纳所有星星并留有边距
const minBgWidth = totalStarsWidth + 40 * this.g_mapScale; // 两侧各留20像素的边距
// 创建星星背景
const starBg = this.g_cartoon.createCartoonElement('star_bg_element', 'MySprite');
starBg.ref.init(this.g_cartoon.images.get('star_bg'));
// 计算背景需要的缩放比例,确保宽度足够
const originalBgWidth = localImagesSize['star_bg'].width;
const widthScaleFactor = minBgWidth / (originalBgWidth * this.g_mapScale);
const finalScaleX = this.g_mapScale * Math.max(1, widthScaleFactor); // 确保不会缩小背景
// 设置背景缩放
starBg.ref.scaleX = finalScaleX;
starBg.ref.scaleY = this.g_mapScale;
// 设置星星背景位置在屏幕顶部中央
const bgWidth = originalBgWidth * finalScaleX;
const bgHeight = localImagesSize['star_bg'].height * this.g_mapScale;
starBg.ref.x = this.g_canvasWidth / 2;
starBg.ref.y = bgHeight / 2 + 10 * this.g_mapScale; // 距离顶部10像素
// 渲染星星背景
this.render(starBg.ref, zIndexMap.card + 10);
// 存储星星元素的数组,用于后续更新
this.starElements = [];
// 使用前面计算好的星星宽度和总宽度
// 计算第一个星星的X坐标,确保星星在背景上居中排列
const startX = this.g_canvasWidth / 2 - totalStarsWidth / 2 + starWidth / 2; // 第一个星星的X坐标
// 创建星星元素
for (let i = 0; i < stageCount; i++) {
// 创建未完成状态的星星
const star = this.g_cartoon.createCartoonElement(`star_${i}`, 'MySprite');
star.ref.init(this.g_cartoon.images.get('star_0')); // 初始显示未完成状态
star.ref.scaleX = this.g_mapScale;
star.ref.scaleY = this.g_mapScale;
star.ref.x = startX + i * (starWidth + starSpacing); // 星星X坐标,使用前面定义的间距
star.ref.y = starBg.ref.y; // 与背景Y坐标相同
// 渲染星星,层级比背景高
this.render(star.ref, zIndexMap.card + 11);
// 将星星元素添加到数组中,用于后续更新
this.starElements.push(star);
}
}
/**
* 更新星星状态
* 根据当前关卡索引更新星星显示状态
* 已完成关卡显示star_1,未完成关卡显示star_0
*/
updateStar() {
if (!this.starElements || this.starElements.length === 0) return;
// 遍历所有星星元素
for (let i = 0; i < this.starElements.length; i++) {
const star = this.starElements[i];
// 如果当前关卡索引大于星星索引,说明该关卡已完成
if (this.currentStageIndex > i) {
// 更新为完成状态
star.ref.init(this.g_cartoon.images.get('star_1'));
} else {
// 更新为未完成状态
star.ref.init(this.g_cartoon.images.get('star_0'));
}
}
}
/**
* 初始化退出按钮
......@@ -2489,11 +2588,14 @@ export class PlayComponent implements OnInit, OnDestroy {
* 更新鱼的运动
* 管理鱼从右侧游到左侧的动画效果
*/
private isGameStart = false;
updateFish() {
// 如果没有游戏阶段或当前阶段索引超出范围,不生成鱼
if (!this.gameStages || this.currentStageIndex >= this.gameStages.length) {
return;
}
if (!this.isGameStart) return;
// 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex];
......
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