Commit 427b4d1b authored by Tt's avatar Tt

代码整理

parent 9b4a642f
...@@ -442,17 +442,6 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -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 { ...@@ -484,6 +473,19 @@ export class PlayComponent implements OnInit, OnDestroy {
}, 500) }, 500)
} }
/**
* 初始化游戏
* 依次初始化游戏中的各个元素,包括背景、标题、按钮和字母块
*/
initGame() {
// 初始化游戏背景
this.initBackground()
this.initListPage();
}
// --------------------------------列表部分----------------------------------------------
/** /**
* 初始化游戏背景 * 初始化游戏背景
* 创建背景元素并设置为静态背景 * 创建背景元素并设置为静态背景
...@@ -533,7 +535,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -533,7 +535,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.render(jiaoshiElement.ref, zIndexMap.background + 1) this.render(jiaoshiElement.ref, zIndexMap.background + 1)
} }
// 列表页面处理区
/** /**
* 初始化列表页面 * 初始化列表页面
* 创建一个可滑动的列表页面,包含多个子节点 * 创建一个可滑动的列表页面,包含多个子节点
...@@ -557,41 +559,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -557,41 +559,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 渲染layout_page // 渲染layout_page
this.render(layoutPage.ref, zIndexMap.card); 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() { initPageTitle() {
// 创建标题背景图片元素 // 创建标题背景图片元素
const titleBgElement = this.g_cartoon.createCartoonElement('page_title_bg', 'MySprite'); const titleBgElement = this.g_cartoon.createCartoonElement('page_title_bg', 'MySprite');
...@@ -624,72 +592,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -624,72 +592,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.layoutPage.ref.addChild(titleBgElement.ref); this.layoutPage.ref.addChild(titleBgElement.ref);
this.layoutPage.ref.addChild(titleTextElement.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() { initListItem() {
// 获取questions数据 // 获取questions数据
const questions = this.g_formData.questions || []; const questions = this.g_formData.questions || [];
...@@ -796,7 +699,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -796,7 +699,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.g_cartoon.playAudio('click'); this.g_cartoon.playAudio('click');
// 切换选中状态 // 切换选中状态
this.toggleItemSelection(itemNode, i); this.onTouchItem(itemNode, i);
} }
}; };
...@@ -868,229 +771,275 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -868,229 +771,275 @@ export class PlayComponent implements OnInit, OnDestroy {
this.listItem = listItem; this.listItem = listItem;
} }
initCheckBtn() {
// 创建按钮元素
const checkBtnElement = this.g_cartoon.createCartoonElement('check_btn', 'MySprite');
initBattle() { // 根据selectList长度决定显示的按钮图片和文字
this.g_enableMapDown = true; const updateButtonState = () => {
if (this.g_formData.doubleModal) { if (this.selectList.length >= 2) {
this.showVsAni('left'); // 显示"开始游戏"按钮
checkBtnElement.ref.init(this.g_cartoon.images.get('kai_shi'));
} else { } else {
this.startBattle(); // 显示"选择单词"按钮
} checkBtnElement.ref.init(this.g_cartoon.images.get('xuan_ze'));
}
/**
* 开始游戏战斗
* 初始化游戏元素并设置双人模式
*/
startBattle() {
// 如果是双人模式,记录玩家开始时间
if (this.g_formData.doubleModal) {
this.g_currentPlayer = 'left';
this.g_playerStartTime = Date.now();
}
// 初始化鱼游戏
this.initFishGame();
// 初始化发音按钮
this.initFaYinBtn();
} }
};
/** // 初始化按钮状态
* 初始化鱼游戏 updateButtonState();
*/
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;
// 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 = []; // 清空之前的游戏阶段 // kai_shi和xuan_ze的尺寸都是209x65
selectedQuestions.forEach(currentQuestion => { const buttonWidth = 209 * this.g_mapScale;
// 为每个选中的值创建一个Stage对象 const buttonHeight = 65 * this.g_mapScale;
// 正确值为当前值,列表值为整个梳理后的列表 checkBtnElement.ref.x = this.g_canvasWidth - buttonWidth / 2 - 50 * this.g_mapScale; // 距离右边50像素
const stage = new Stage(currentQuestion, selectedQuestions); checkBtnElement.ref.y = this.g_canvasHeight - buttonHeight / 2 - 50 * this.g_mapScale; // 距离底部50像素
this.gameStages.push(stage);
});
// 打印创建的Stage对象,用于调试 // 添加点击事件
console.log('Created Stages:', this.gameStages); 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.layoutPage.ref.visible = false;
this.checkBtnElement.ref.visible = false; this.checkBtnElement.ref.visible = false;
this.listItem.ref.visible = false; this.listItem.ref.visible = false;
// this.initVsView(); this.initBattle();
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 { } else {
this.cat_center = this.createCat('center'); // 创建中间猫 // 提示需要选择更多单词
this.setCurrentCat(this.cat_center); console.log('请至少选择2个单词');
// 这里可以添加提示逻辑
} }
};
this.initStar(); // 保存按钮引用,用于状态更新
this.checkBtnElement = checkBtnElement;
this.initNextStage(); // 渲染按钮
this.render(checkBtnElement.ref, zIndexMap.card + 2);
} }
/** /**
* 初始化星星标记区域 * 切换项目的选中状态
* 根据题目数量创建星星标记区域 * @param itemNode 项目节点
* 背景使用star_bg * @param dataIndex 数据索引
* 前景将star_0和star_1渲染在同一个节点,未通过关卡显示star_0,通过后显示star_1
*/ */
initStar() { onTouchItem(itemNode, dataIndex) {
// 获取题目数量 if (!this.layoutPage.ref.visible) return;
const stageCount = this.gameStages.length;
if (stageCount <= 0) return;
// 计算星星之间的间距和所需的总宽度 this.g_cartoon.playAudio('click_pic')
const starWidth = localImagesSize['star_0'].width * this.g_mapScale;
const starSpacing = 10 * this.g_mapScale; // 星星之间的间距
const totalStarsWidth = stageCount * starWidth + (stageCount - 1) * starSpacing; // 星星总宽度加上间距
// 计算背景所需的最小宽度,确保能容纳所有星星并留有边距 const isSelected = itemNode.ref.isSelected || false;
const minBgWidth = totalStarsWidth + 40 * this.g_mapScale; // 两侧各留20像素的边距 const selectMark = itemNode.ref.selectMark;
// 创建星星背景 if (!isSelected) {
const starBg = this.g_cartoon.createCartoonElement('star_bg_element', 'MySprite'); // 选中状态:显示选择标记,添加到selectList
starBg.ref.init(this.g_cartoon.images.get('star_bg')); 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 originalBgWidth = localImagesSize['star_bg'].width; this.updateCheckBtn();
const widthScaleFactor = minBgWidth / (originalBgWidth * this.g_mapScale);
const finalScaleX = this.g_mapScale * Math.max(1, widthScaleFactor); // 确保不会缩小背景
// 设置背景缩放 console.log('当前选中项目:', this.selectList);
starBg.ref.scaleX = finalScaleX; }
starBg.ref.scaleY = this.g_mapScale; 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'));
}
}
}
// 设置星星背景位置在屏幕顶部中央 //--------------------------------------------------------------钓鱼部分---------------------------------------------------------
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); private cat_left: any;
private cat_right: any;
private cat_center: any;
// 当前游戏阶段索引
private currentStageIndex = 0;
// 存储用户点击的鱼的数据
private clickedFishData: Question[] = [];
// 存储星星元素的数组,用于后续更新 private touchFish = null;
this.starElements = []; private currentCat
setCurrentCat(cat) {
this.currentCat = cat;
}
getCurrentCat() {
return this.currentCat;
}
// 使用前面计算好的星星宽度和总宽度 saveFishSuccess(fish) {
// 计算第一个星星的X坐标,确保星星在背景上居中排列 const currentStage = this.gameStages[this.currentStageIndex];
const startX = this.g_canvasWidth / 2 - totalStarsWidth / 2 + starWidth / 2; // 第一个星星的X坐标 if (fish.questionData) {
this.clickedFishData.push(fish.questionData);
// 创建星星元素 console.log('Clicked fish count:', this.clickedFishData.length);
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); const correctClicks = this.clickedFishData.filter(data =>
data.audio === currentStage.correctQuestion.audio &&
data.image === currentStage.correctQuestion.image
).length;
// 将星星元素添加到数组中,用于后续更新 console.log('Correct clicks:', correctClicks);
this.starElements.push(star);
// 如果有两个数据为当前Stage的正确数据,则清空画面,等待1秒后进入下一轮游戏
if (correctClicks >= 2) {
console.log('Stage completed! Moving to next stage...');
// 清空画面(清除所有鱼)
this.clearFishes();
// 增加当前阶段索引
this.currentStageIndex++;
// 等待1秒后进入下一轮游戏
setTimeout(() => {
this.initNextStage();
}, 1000);
}
} }
} }
/** //--------------------------------------------------------------钓鱼-初始化---------------------------------------------------------
* 更新星星状态
* 根据当前关卡索引更新星星显示状态
* 已完成关卡显示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];
// 如果当前关卡索引大于星星索引,说明该关卡已完成 initBattle() {
if (this.currentStageIndex > i) { this.g_enableMapDown = true;
// 更新为完成状态 if (this.g_formData.doubleModal) {
star.ref.init(this.g_cartoon.images.get('star_1')); this.showVsAni('left');
} else { } else {
// 更新为未完成状态 this.startBattle();
star.ref.init(this.g_cartoon.images.get('star_0')); }
} }
/**
* 开始游戏战斗
* 初始化游戏元素并设置双人模式
*/
startBattle() {
// 如果是双人模式,记录玩家开始时间
if (this.g_formData.doubleModal) {
this.g_playerStartTime = Date.now();
this.isGameStart = true;
} }
// 初始化鱼游戏
this.initFishGame();
// 初始化发音按钮
this.initFaYinBtn();
} }
/** /**
* 显示鱼掉落动画 * 初始化鱼游戏
* @param type 位置类型:'center', 'left', 'right'
*/ */
showFishRain(type) { initFishGame() {
// 根据type确定鱼掉落的位置 console.log("this.selectList", this.selectList);
let fishX = this.g_canvasWidth / 2; // 默认中间位置 const targetList = this.selectList.concat();
const questions = this.g_formData.questions;
switch (type) { // 1. 梳理出选中的数据数组
case 'left': const selectedQuestions = targetList.map(index => questions[index]).filter(q => q); // 过滤掉可能的undefined值
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;
}
// 创建鱼元素 // 2. 根据梳理好的数组创建多个Stage
const fishRain = this.g_cartoon.createCartoonElement('fish_rain', 'MySprite'); this.gameStages = []; // 清空之前的游戏阶段
fishRain.ref.init(this.g_cartoon.images.get('yu_00')); // 初始帧 selectedQuestions.forEach(currentQuestion => {
fishRain.ref.scaleX = this.g_mapScale; // 为每个选中的值创建一个Stage对象
fishRain.ref.scaleY = this.g_mapScale; // 正确值为当前值,列表值为整个梳理后的列表
fishRain.ref.x = fishX; const stage = new Stage(currentQuestion, selectedQuestions);
fishRain.ref.y = 245 * this.g_mapScale; // 从屏幕顶部开始 this.gameStages.push(stage);
});
// 渲染鱼元素,使用较高的层级确保在其他元素上方 // 打印创建的Stage对象,用于调试
this.render(fishRain.ref, zIndexMap.cat + 10); console.log('Created Stages:', this.gameStages);
// 播放音效 this.layoutPage.ref.visible = false;
this.g_cartoon.playAudio('fish_down'); 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);
}
// 设置帧动画参数 this.initStar();
let currentFrame = 0;
const maxFrame = 80;
const frameInterval = 50; // 每帧间隔时间,可以根据需要调整
// 创建帧动画函数 this.initNextStage();
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++; * 初始化下一个游戏阶段
* 1. 将当前Stage中的数组随机一次
* 2. 后续出鱼按照随机后的数组出现
* 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏
*/
initNextStage() {
// 清空之前点击的鱼的数据
this.clickedFishData = [];
// 设置下一帧的定时器 // 更新星星状态,显示已完成关卡的星星
const timeoutId = setTimeout(playNextFrame, frameInterval); this.updateStar();
this.m_setTimeoutIDs.push(timeoutId);
// 检查是否还有游戏阶段
if (this.currentStageIndex >= this.gameStages.length) {
// 所有阶段都完成了,结束游戏
if (this.g_formData.doubleModal) {
this.gameOver();
} else { } else {
// 动画播放完成后隐藏鱼元素 this.showFishRain('center');
// 显示游戏结束提示 this.endGame();
setTimeout(() => { }
console.log("游戏结束!"); return;
fishRain.ref.visible = false;
}, 200);
} }
};
// 开始播放帧动画 // 获取当前游戏阶段
playNextFrame(); const currentStage = this.gameStages[this.currentStageIndex];
// 将当前Stage中的数组随机一次
currentStage.randomList();
console.log('Current Stage:', currentStage);
console.log('Random List:', currentStage.questionRandomList);
// 清空现有的鱼群
this.clearFishes();
// 修改鱼的生成逻辑,使用当前阶段的随机问题列表
this.m_fishInterval = 3000; // 设置鱼的生成间隔为3秒
this.m_lastFishTime = 0; // 重置上次生成鱼的时间,使下一帧立即生成鱼
// 每一个stage开始的时候播放一次正确问题的音频
if (currentStage.correctQuestion && currentStage.correctQuestion.audio) {
this.g_cartoon.playAudio(currentStage.correctQuestion.audio);
}
} }
/** /**
...@@ -1108,6 +1057,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1108,6 +1057,7 @@ export class PlayComponent implements OnInit, OnDestroy {
this.g_leftPlayerTime = playTime; this.g_leftPlayerTime = playTime;
console.log('左侧玩家游戏时间:', this.g_leftPlayerTime, 'ms'); console.log('左侧玩家游戏时间:', this.g_leftPlayerTime, 'ms');
this.currentStageIndex = 0; this.currentStageIndex = 0;
this.isGameStart = false;
// 切换到右侧玩家 // 切换到右侧玩家
this.g_currentPlayer = 'right'; this.g_currentPlayer = 'right';
...@@ -1147,349 +1097,63 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1147,349 +1097,63 @@ export class PlayComponent implements OnInit, OnDestroy {
this.showFishRain('center'); this.showFishRain('center');
} }
} }
//-----------------------------------------------------------------钓鱼-创建---------------------------------------------------------
// 当前游戏阶段索引
private currentStageIndex = 0;
// 存储用户点击的鱼的数据
private clickedFishData: Question[] = [];
/** /**
* 初始化下一个游戏阶段 * 处理鱼泡泡破裂
* 1. 将当前Stage中的数组随机一次 * 隐藏泡泡背景、小鱼和图片,显示泡泡破裂动画
* 2. 后续出鱼按照随机后的数组出现 * 存储用户点击的鱼的数据,检查是否有两个正确数据
* 3. 用户点击鱼后存储对应数据,如果数据中有两个数据为当前Stage的正确数据则清空画面,等待1秒后进行下一轮游戏 * @param fish 被点击的鱼对象
*/ */
initNextStage() { onTouchFish(fish) {
// 清空之前点击的鱼的数据
this.clickedFishData = [];
// 更新星星状态,显示已完成关卡的星星
this.updateStar();
// 检查是否还有游戏阶段
if (this.currentStageIndex >= this.gameStages.length) {
// 所有阶段都完成了,结束游戏
if (this.g_formData.doubleModal) {
this.gameOver();
} else {
this.showFishRain('center');
this.endGame();
}
return;
}
// 获取当前游戏阶段 // 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex]; const currentStage = this.gameStages[this.currentStageIndex];
// 将当前Stage中的数组随机一次 // 检查是否点击了正确的鱼
currentStage.randomList(); const isCorrect = fish.questionData &&
fish.questionData.audio === currentStage.correctQuestion.audio &&
console.log('Current Stage:', currentStage); fish.questionData.image === currentStage.correctQuestion.image;
console.log('Random List:', currentStage.questionRandomList);
// 清空现有的鱼群
this.clearFishes();
// 修改鱼的生成逻辑,使用当前阶段的随机问题列表
this.m_fishInterval = 3000; // 设置鱼的生成间隔为3秒
this.m_lastFishTime = 0; // 重置上次生成鱼的时间,使下一帧立即生成鱼
// 每一个stage开始的时候播放一次正确问题的音频
if (currentStage.correctQuestion && currentStage.correctQuestion.audio) {
this.g_cartoon.playAudio(currentStage.correctQuestion.audio);
}
}
/**
* 初始化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);
}
/** console.log('Clicked fish data:', fish.questionData);
* 显示VS阴影动画效果 console.log('Is correct:', isCorrect);
* 在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元素下方
// 创建S阴影元素 // 保存鱼的原始速度,用于后续恢复
const s_shadow = this.g_cartoon.createCartoonElement('s_shadow', 'MySprite'); const originalSpeed = fish.speed || 0.3;
s_shadow.ref.init(this.g_cartoon.images.get('s')); // 点击后立即停止鱼的运动
s_shadow.ref.scaleX = this.g_mapScale; fish.speed = 0;
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元素下方
// 执行V阴影元素的动画:放大5倍,透明度为0,向左移动50 if (isCorrect) {
tweenChange(v_shadow.ref, { // 正确小鱼的处理逻辑
scaleX: this.g_mapScale * 4, // 隐藏泡泡背景、小鱼和图片
scaleY: this.g_mapScale * 4, fish['child_paopao_bg'].ref.visible = false;
alpha: 0.3, fish['child_xiao_yu'].ref.visible = false;
x: v_shadow.ref.x - 50 * this.g_mapScale fish['child_image'].ref.visible = false;
}, 0.3, () => { fish['child_paopao_ani'].ref.visible = true;
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['v_shadow'].ref.visible = false;
});
// 执行S阴影元素的动画:放大5倍,透明度为0,向右移动50 const currentStage = this.gameStages[this.currentStageIndex];
tweenChange(s_shadow.ref, { if (currentStage && currentStage.correctQuestion && currentStage.correctQuestion.audio) {
scaleX: this.g_mapScale * 4, // 播放当前正确问题的音频
scaleY: this.g_mapScale * 4, this.g_cartoon.playAudio(currentStage.correctQuestion.audio);
alpha: 0.3,
x: s_shadow.ref.x + 50 * this.g_mapScale
}, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['s_shadow'].ref.visible = false;
});
} }
// 注销该鱼的点击事件
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;
}
createCat(type) { createCat(type) {
// 创建cat根节点 // 创建cat根节点
const cat = this.g_cartoon.createCartoonElement('cat', 'MySprite'); const cat = this.g_cartoon.createCartoonElement('cat', 'MySprite');
...@@ -1736,72 +1400,308 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1736,72 +1400,308 @@ export class PlayComponent implements OnInit, OnDestroy {
if (!fish['child_paopao_bg'].ref.visible) { if (!fish['child_paopao_bg'].ref.visible) {
return; return;
} }
console.log('fish click', fish); console.log('fish click', fish);
// 点击气泡时执行brokePaopao方法 // 点击气泡时执行brokePaopao方法
this.touchFish = fish; this.touchFish = fish;
this.onTouchFish(fish); this.onTouchFish(fish);
setTimeout(() => {
this.touchFish = null;
}, 100);
})
return fish;
}
//-----------------------------------------------------------------钓鱼-动画---------------------------------------------------------
/**
* 初始化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(() => { setTimeout(() => {
this.touchFish = null; this.show321(type);
}, 100); }, 1000);
}) }, config.vs.v.moveTime * 1000 + 500);
return fish; }, config.common.vsAnimDelay);
} }
private touchFish = null;
/** /**
* 处理鱼泡泡破裂 * 显示VS阴影动画效果
* 隐藏泡泡背景、小鱼和图片,显示泡泡破裂动画 * 在VS动画运动后的位置创建两个VS图标,并执行放大5倍、透明度为0的动画
* 存储用户点击的鱼的数据,检查是否有两个正确数据 * V字母向左移动50,S字母向右移动50,动画时间为0.1秒
* @param fish 被点击的鱼对象
*/ */
onTouchFish(fish) { showVsShadow() {
// 获取当前游戏阶段 // 创建V阴影元素
const currentStage = this.gameStages[this.currentStageIndex]; 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;
const isCorrect = fish.questionData && v_shadow.ref.scaleY = this.g_mapScale;
fish.questionData.audio === currentStage.correctQuestion.audio && v_shadow.ref.x = this.g_canvasWidth / 2 - 20 * this.g_mapScale; // 与原始V元素位置相同
fish.questionData.image === currentStage.correctQuestion.image; 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 originalSpeed = fish.speed || 0.3; const s_shadow = this.g_cartoon.createCartoonElement('s_shadow', 'MySprite');
// 点击后立即停止鱼的运动 s_shadow.ref.init(this.g_cartoon.images.get('s'));
fish.speed = 0; 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) { // 执行V阴影元素的动画:放大5倍,透明度为0,向左移动50
// 正确小鱼的处理逻辑 tweenChange(v_shadow.ref, {
// 隐藏泡泡背景、小鱼和图片 scaleX: this.g_mapScale * 4,
fish['child_paopao_bg'].ref.visible = false; scaleY: this.g_mapScale * 4,
fish['child_xiao_yu'].ref.visible = false; alpha: 0.3,
fish['child_image'].ref.visible = false; x: v_shadow.ref.x - 50 * this.g_mapScale
fish['child_paopao_ani'].ref.visible = true; }, 0.3, () => {
// 动画完成后移除元素
this.g_cartoon.cartoonElementsBuffer['v_shadow'].ref.visible = false;
});
const currentStage = this.gameStages[this.currentStageIndex]; // 执行S阴影元素的动画:放大5倍,透明度为0,向右移动50
if (currentStage && currentStage.correctQuestion && currentStage.correctQuestion.audio) { tweenChange(s_shadow.ref, {
// 播放当前正确问题的音频 scaleX: this.g_mapScale * 4,
this.g_cartoon.playAudio(currentStage.correctQuestion.audio); 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;
});
} }
// 注销该鱼的点击事件
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) { brokePaopaoAni(fish) {
// this.g_cartoon.playAudio('pop_bomb'); // this.g_cartoon.playAudio('pop_bomb');
// 对于正确的鱼,显示泡泡破裂动画 // 对于正确的鱼,显示泡泡破裂动画
...@@ -1859,39 +1759,149 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1859,39 +1759,149 @@ export class PlayComponent implements OnInit, OnDestroy {
playNextFrame(); 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); //----------------------------------------------------------------刷新部分----------------------------------------------------------------
// 计算正确点击的次数 sleepRightCat() {
const correctClicks = this.clickedFishData.filter(data => this.cat_right["child_mao_ju_zuo"].ref.visible = false;
data.audio === currentStage.correctQuestion.audio && this.cat_right["child_mao_ju_pa"].ref.visible = false;
data.image === currentStage.correctQuestion.image this.cat_right["child_mao_hui_zuo"].ref.visible = false;
).length; 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;
// 增加当前阶段索引 // 2. 创建一个临时小鱼
this.currentStageIndex++; const tempFishId = `temp_fish_${Math.floor(Math.random() * 1000)}_${Date.now()}`;
const tempFish = this.g_cartoon.createCartoonElement(tempFishId, 'MySprite');
// 等待1秒后进入下一轮游戏 // 使用与原小鱼相同的图像
setTimeout(() => { tempFish.ref.init(xiao_yu.ref.img);
this.initNextStage(); tempFish.ref.scaleX = xiao_yu.ref.scaleX;
}, 1000); 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) { aniOpacity(fish) {
...@@ -1916,87 +1926,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1916,87 +1926,7 @@ export class PlayComponent implements OnInit, OnDestroy {
}) })
} }
//----------------------------------------------------------------UI部分----------------------------------------------------------------
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 // 使用缓动函数使动画更平滑
);
}
/** /**
...@@ -2036,6 +1966,175 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -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 { ...@@ -2489,11 +2588,14 @@ export class PlayComponent implements OnInit, OnDestroy {
* 更新鱼的运动 * 更新鱼的运动
* 管理鱼从右侧游到左侧的动画效果 * 管理鱼从右侧游到左侧的动画效果
*/ */
private isGameStart = false;
updateFish() { updateFish() {
// 如果没有游戏阶段或当前阶段索引超出范围,不生成鱼 // 如果没有游戏阶段或当前阶段索引超出范围,不生成鱼
if (!this.gameStages || this.currentStageIndex >= this.gameStages.length) { if (!this.gameStages || this.currentStageIndex >= this.gameStages.length) {
return; return;
} }
if (!this.isGameStart) return;
// 获取当前游戏阶段 // 获取当前游戏阶段
const currentStage = this.gameStages[this.currentStageIndex]; 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