Commit e181fd51 authored by Tt's avatar Tt

feat(play): 适配

parent 0504c46b
......@@ -346,6 +346,7 @@ export class PlayComponent implements OnInit, OnDestroy {
* 初始化列表页面
* 创建一个可滑动的列表页面,包含多个子节点
*/
private layoutPage: any;
initListPage() {
// 创建layout_page空节点作为页面容器
const layoutPage = this.g_cartoon.createCartoonElement('layout_page', 'ShapeRectNew');
......@@ -353,6 +354,7 @@ export class PlayComponent implements OnInit, OnDestroy {
layoutPage.ref.x = this.g_canvasWidth / 2;
layoutPage.ref.y = this.g_canvasHeight / 2;
layoutPage.ref.fillColor = 'rgba(0, 0, 0, 0)';
this.layoutPage = layoutPage;
this.initPageTitle();
......@@ -406,8 +408,8 @@ export class PlayComponent implements OnInit, OnDestroy {
titleBgElement.ref.scaleY = this.g_mapScale;
// 设置背景图片位置 - 水平居中,垂直在顶部
titleBgElement.ref.x = this.g_canvasWidth / 2;
titleBgElement.ref.y = 40 * this.g_mapScale; // 距离顶部100像素
titleBgElement.ref.x = 0;
titleBgElement.ref.y = -this.g_canvasHeight / 2 + 40 * this.g_mapScale;
// 创建标题文字元素
const titleTextElement = this.g_cartoon.createCartoonElement('page_title_text', 'Label');
......@@ -418,14 +420,14 @@ export class PlayComponent implements OnInit, OnDestroy {
// 设置字体颜色为深色
titleTextElement.ref.fontColor = '#FFFFFF';
// 设置文字位置与背景图片相同
titleTextElement.ref.x = titleBgElement.ref.x + 15;
titleTextElement.ref.y = titleBgElement.ref.y + 10;
titleTextElement.ref.x = titleBgElement.ref.x + 15 * this.g_mapScale;
titleTextElement.ref.y = titleBgElement.ref.y + 10 * this.g_mapScale;
// 设置文本对齐方式为居中
titleTextElement.ref.textAlign = 'center';
// 渲染背景图片和文字元素
this.render(titleBgElement.ref, zIndexMap.background + 1);
this.render(titleTextElement.ref, zIndexMap.background + 2);
this.layoutPage.ref.addChild(titleBgElement.ref);
this.layoutPage.ref.addChild(titleTextElement.ref);
}
initCheckBtn() {
// 创建按钮元素
......@@ -464,6 +466,8 @@ export class PlayComponent implements OnInit, OnDestroy {
if (this.selectList.length >= 2) {
// 开始游戏逻辑
console.log('开始游戏,选中的项目:', this.selectList);
this.layoutPage.ref.visible = false;
this.initFishGame();
// 这里可以添加开始游戏的具体逻辑
} else {
// 提示需要选择更多单词
......@@ -503,24 +507,24 @@ export class PlayComponent implements OnInit, OnDestroy {
// 创建list_item节点作为滑动列表容器
const listItem = this.g_cartoon.createCartoonElement('list_item', 'ShapeRectNew');
// 设置固定高度为600
const listHeight = 600;
// 设置固定高度为600,应用屏幕适配参数
const listHeight = 600 * this.g_mapScale;
// 每列最多2个子节点
const maxItemsPerColumn = 2;
// 计算需要的列数
const columnsNeeded = Math.ceil(totalItems / maxItemsPerColumn);
// 每个子节点的宽度和高度
const itemWidth = 280;
const itemHeight = 185;
// 子节点之间的间距
const itemSpacing = 50;
// 每个子节点的宽度和高度,应用屏幕适配参数
const itemWidth = 280 * this.g_mapScale;
const itemHeight = 185 * this.g_mapScale;
// 子节点之间的间距,应用屏幕适配参数
const itemSpacing = 50 * this.g_mapScale;
// 计算list_item的总宽度
const listWidth = columnsNeeded * (itemWidth + itemSpacing) - itemSpacing;
// 初始化list_item - 将锚点设置为左侧 (0, 0.5)
listItem.ref.init(null, 0, 0.5);
listItem.ref.x = 0;
listItem.ref.y = this.g_canvasHeight / 2 + 100;
listItem.ref.y = this.g_canvasHeight / 2 + 100 * this.g_mapScale;
listItem.ref.fillColor = 'rgba(231, 193, 193, 0.5)';
// 设置list_item的宽高
listItem.ref.setSize(listWidth, listHeight);
......@@ -550,7 +554,7 @@ export class PlayComponent implements OnInit, OnDestroy {
itemNode.ref.isSelected = false;
itemNode.ref.data = questionData; // 存储问题数据
// 计算子节点位置 - 基于左锚点的坐标计算
// 计算子节点位置 - 基于左锚点的坐标计算,应用屏幕适配参数
// X坐标:从左到右排列,考虑间距,基于listItem的左边界
const itemX = listItem.ref.x + (column * (itemWidth + itemSpacing)) + (itemWidth / 2);
// Y坐标:从上到下排列,考虑间距,基于listItem的中心
......@@ -564,17 +568,16 @@ export class PlayComponent implements OnInit, OnDestroy {
// 创建图片元素显示question的image
if (questionData.image) {
const imageElement = this.g_cartoon.createCartoonElement(`list_item_image_${i}`, 'MySprite');
const img = this.g_cartoon.images.get(questionData.image)
this.g_cartoon.preloadImage(questionData.image).then((img: any) => {
// 图片加载完成后初始化sprite
imageElement.ref.init(img);
// 设置图片位置居中
imageElement.ref.x = 0;
imageElement.ref.y = 0;
// 根据背景卡片尺寸调整图片大小,保持适当的边距
// 根据背景卡片尺寸调整图片大小,保持适当的边距,应用屏幕适配参数
const padding = 20; // 边距
const maxWidth = itemWidth - padding * 2;
const maxHeight = itemHeight - padding * 2;
const maxWidth = itemWidth / this.g_mapScale - padding * 2;
const maxHeight = itemHeight / this.g_mapScale - padding * 2;
const scaleX = maxWidth / img.width;
const scaleY = maxHeight / img.height;
const scale = Math.min(scaleX, scaleY); // 保持宽高比
......@@ -588,9 +591,8 @@ export class PlayComponent implements OnInit, OnDestroy {
// 添加选择标记(dui_hao图片)
const selectMarkElement = this.g_cartoon.createCartoonElement(`list_item_select_${i}`, 'MySprite');
selectMarkElement.ref.init(this.g_cartoon.images.get('dui_hao'));
// 放置在右下角位置(根据bg_danci尺寸280x185和dui_hao尺寸89x64计算)
selectMarkElement.ref.x = itemWidth * 0.35; // 相对于itemNode的右下角
selectMarkElement.ref.y = itemHeight * 0.25;
selectMarkElement.ref.x = 100; // 相对于itemNode的右下角
selectMarkElement.ref.y = 50;
selectMarkElement.ref.scaleX = itemWidth / itemNode.ref.width;
selectMarkElement.ref.scaleY = itemHeight / itemNode.ref.height;
selectMarkElement.ref.visible = false; // 初始时隐藏
......@@ -639,8 +641,8 @@ export class PlayComponent implements OnInit, OnDestroy {
// 计算移动距离
const deltaX = this.g_clickX - startX;
// 如果移动距离超过阈值,标记为拖拽状态
if (Math.abs(deltaX) > 10) {
// 如果移动距离超过阈值,标记为拖拽状态,应用屏幕适配参数
if (Math.abs(deltaX) > 10 * this.g_mapScale) {
isDragging = true;
}
......@@ -674,572 +676,12 @@ export class PlayComponent implements OnInit, OnDestroy {
this.render(listItem.ref, zIndexMap.card);
}
/**
* 更新字母卡片的显示状态
* @param element 卡片元素
* @param letter 要显示的字母或单词
* @param isShow 是否默认显示
* @param audio 字母音频URL
* @param bgType 背景类型:'white'、'green'、'yellow'
* @param isSmall 是否使用小尺寸背景
* @param wordArr 单词数组,包含每个字母及其类型
*/
updateWordCard(element, letter, isShow, audio, bgType = 'white', isSmall = false, wordArr = null) {
// 根据背景类型和尺寸确定背景图片前缀
let bgPrefix = 'bg_' + bgType;
if (isSmall) {
bgPrefix = bgPrefix + '_small';
}
// 根据是否默认显示选择背景图片
const bgImage = isShow ? bgPrefix : bgPrefix + '_line';
// 初始化卡片背景
element.ref.init(this.g_cartoon.images.get(bgImage));
// 设置卡片显示状态标记
element.ref.isVisible = isShow || false;
// 判断是否为part3(黄色背景)且文字长度为3或4位
const isPart3 = bgType === 'yellow';
const isLongWord = letter && (letter.length === 3 || letter.length === 4);
const needSmallerFont = isPart3 && isLongWord;
if (isShow) {
// 如果是单个字母或没有wordArr数据,直接创建单个文本元素
if (!wordArr || letter.length <= 1) {
// 默认显示状态:创建字母文本
const textElement = this.g_cartoon.createCartoonElement(`letter_text_${letter}`, 'Label');
// 设置字母文本内容
textElement.ref.text = letter;
// 设置字母文本字体大小 - 对part3中的3位或4位文字缩小字号,并应用缩放
textElement.ref.fontSize = (needSmallerFont ? 60 : (isSmall ? 80 : 100)) * this.g_mapScale;
// 统一设置字体颜色为黑色,除非特别指定为高亮
textElement.ref.fontColor = '#010001';
// 设置字母文本位置(与卡片背景相同位置)
textElement.ref.x = element.ref.x;
textElement.ref.y = element.ref.y;
// 设置文本对齐方式为居中
textElement.ref.textAlign = 'center';
// 渲染字母文本元素
this.render(textElement.ref, zIndexMap.card + 1);
} else {
// 如果有wordArr数据,为每个字母创建单独的文本元素
const letterWidth = (isSmall ? 40 : 50) * this.g_mapScale; // 每个字母的宽度,应用缩放
const totalWidth = letterWidth * wordArr.length; // 所有字母的总宽度
const startX = element.ref.x - totalWidth / 2 + letterWidth / 2; // 第一个字母的X坐标
// 遍历wordArr数组,为每个字母创建文本元素
wordArr.forEach((item, index) => {
const textElement = this.g_cartoon.createCartoonElement(`letter_text_${letter}_${index}`, 'Label');
// 设置字母文本内容
textElement.ref.text = item.word;
// 设置字母文本字体大小 - 对part3中的3位或4位文字缩小字号,并应用缩放
textElement.ref.fontSize = (needSmallerFont ? 70 : (isSmall ? 80 : 100)) * this.g_mapScale;
// 统一所有部分的高亮逻辑:只有标记为'red'的字母显示为蓝色,其他显示为黑色
textElement.ref.fontColor = item.type === 'red' ? '#3c65f7' : '#010001';
// 设置字母文本位置
textElement.ref.x = startX + index * letterWidth;
textElement.ref.y = element.ref.y;
// 设置文本对齐方式为居中
textElement.ref.textAlign = 'center';
// 渲染字母文本元素
this.render(textElement.ref, zIndexMap.card + 1);
});
}
// 为显示状态的卡片添加点击事件(播放音频)
this.subscribeMapDownEvent(element.id, () => {
// 播放字母音频
if (audio) {
this.g_cartoon.playAudio(audio);
} else {
this.g_cartoon.playAudio('sm-click');
}
this.g_enableMapDown = true;
});
} else {
// 隐藏状态:为卡片添加点击事件(点击后显示内容)
this.subscribeMapDownEvent(element.id, () => {
// 播放点击音效
this.g_cartoon.playAudio('sm-click');
// 更新卡片为显示状态
element.ref.init(this.g_cartoon.images.get(bgPrefix));
// 更新卡片显示状态标记
element.ref.isVisible = true;
// 如果有wordArr数据,为每个字母创建单独的文本元素
if (wordArr && letter.length > 1) {
const letterWidth = (isSmall ? 40 : 50) * this.g_mapScale; // 每个字母的宽度,应用缩放
const totalWidth = letterWidth * wordArr.length; // 所有字母的总宽度
const startX = element.ref.x - totalWidth / 2 + letterWidth / 2; // 第一个字母的X坐标
// 遍历wordArr数组,为每个字母创建文本元素
wordArr.forEach((item, index) => {
const textElement = this.g_cartoon.createCartoonElement(`letter_text_${letter}_${index}`, 'Label');
// 设置字母文本内容
textElement.ref.text = item.word;
// 设置字母文本字体大小 - 对part3中的3位或4位文字缩小字号,并应用缩放
textElement.ref.fontSize = (needSmallerFont ? 70 : (isSmall ? 80 : 100)) * this.g_mapScale;
// 统一所有部分的高亮逻辑:只有标记为'red'的字母显示为蓝色,其他显示为黑色
textElement.ref.fontColor = item.type === 'red' ? '#3c65f7' : '#010001';
// 设置字母文本位置
textElement.ref.x = startX + index * letterWidth;
textElement.ref.y = element.ref.y;
// 设置文本对齐方式为居中
textElement.ref.textAlign = 'center';
// 渲染字母文本元素
this.render(textElement.ref, zIndexMap.card + 1);
});
} else {
// 创建字母文本
const textElement = this.g_cartoon.createCartoonElement(`letter_text_${letter}`, 'Label');
// 设置字母文本内容
textElement.ref.text = letter;
// 设置字母文本字体大小 - 对part3中的3位或4位文字缩小字号,并应用缩放
textElement.ref.fontSize = (needSmallerFont ? 70 : (isSmall ? 80 : 100)) * this.g_mapScale;
// 设置字母文本颜色为黑色
textElement.ref.fontColor = '#010001';
// 设置字母文本位置(与卡片背景相同位置)
textElement.ref.x = element.ref.x;
textElement.ref.y = element.ref.y;
// 设置文本对齐方式为居中
textElement.ref.textAlign = 'center';
// 渲染字母文本元素
this.render(textElement.ref, zIndexMap.card + 1);
}
// 更新点击事件为播放音频
this.m_mapDownQueue[element.id] = () => {
// 播放字母音频
if (audio) {
this.g_cartoon.playAudio(audio);
} else {
this.g_cartoon.playAudio('sm-click');
}
this.g_enableMapDown = true;
};
this.g_enableMapDown = true;
});
}
}
/**
* 初始化第一部分内容(字母块)
*/
initPart1(defaultX) {
// 获取转换后的表单数据
const formData = this.g_formData;
// 检查是否有part_1数据
if (formData.part_1 && formData.part_1.length > 0) {
// 获取画布高度的中心点
const centerY = this.g_canvasHeight / 2;
// 获取白色背景卡片的高度并应用缩放
const cardHeight = localImagesSize['bg_white'].height * this.g_mapScale;
// 计算垂直间距(卡片高度 + 10px间距 * 缩放比例)
const verticalSpacing = cardHeight + (10 * this.g_mapScale);
// 计算所有卡片总高度
const totalHeight = formData.part_1.length * cardHeight + (formData.part_1.length - 1) * (10 * this.g_mapScale);
// 计算第一个卡片的Y坐标(居中显示所有卡片)
const startY = centerY - totalHeight / 2 + cardHeight / 2;
// 遍历part_1数据创建字母块
formData.part_1.forEach((item, index) => {
// 创建字母卡片元素
const cardElement = this.g_cartoon.createCartoonElement(`part1_card_${index}`, 'MySprite');
// 设置卡片位置,应用g_mapScale确保在小屏幕上正确显示
cardElement.ref.x = defaultX;
cardElement.ref.y = startY + index * verticalSpacing;
cardElement.ref.scaleX = this.g_mapScale;
cardElement.ref.scaleY = this.g_mapScale;
// 设置卡片X方向和Y方向的缩放比例
cardElement.ref.scaleX = this.g_mapScale;
cardElement.ref.scaleY = this.g_mapScale;
// 设置卡片X方向和Y方向的缩放比例
cardElement.ref.scaleX = this.g_mapScale;
cardElement.ref.scaleY = this.g_mapScale;
// 渲染卡片元素
this.render(cardElement.ref, zIndexMap.card);
// 更新字母卡片显示状态(使用白色背景)
this.updateWordCard(cardElement, item.words, item.show, item.audio, 'white', false);
});
}
}
/**
* 初始化第二部分内容(字母块)
*/
initPart2(defaultX) {
// 获取转换后的表单数据
const formData = this.g_formData;
// 检查是否有part_2数据
if (formData.part_2 && formData.part_2.length > 0) {
// 获取画布高度的中心点
const centerY = this.g_canvasHeight / 2;
// 确定是否使用小尺寸背景(当数量大于3时使用小尺寸)
const useSmallBg = formData.part_2.length > 3;
// 获取绿色背景卡片的高度(根据是否使用小尺寸选择不同的背景)并应用缩放
const bgType = useSmallBg ? 'bg_green_small' : 'bg_green';
const cardHeight = localImagesSize[bgType].height * this.g_mapScale;
// 计算垂直间距(卡片高度 + 10px间距 * 缩放比例)
const verticalSpacing = cardHeight + (10 * this.g_mapScale);
// 计算所有卡片总高度
const totalHeight = formData.part_2.length * cardHeight + (formData.part_2.length - 1) * (10 * this.g_mapScale);
// 计算第一个卡片的Y坐标(居中显示所有卡片)
const startY = centerY - totalHeight / 2 + cardHeight / 2;
// 遍历part_2数据创建字母块
formData.part_2.forEach((item, index) => {
// 创建字母卡片元素
const cardElement = this.g_cartoon.createCartoonElement(`part2_card_${index}`, 'MySprite');
// 设置卡片位置
cardElement.ref.x = defaultX;
cardElement.ref.y = startY + index * verticalSpacing;
// 设置卡片X方向和Y方向的缩放比例
cardElement.ref.scaleX = this.g_mapScale;
cardElement.ref.scaleY = this.g_mapScale;
// 渲染卡片元素
this.render(cardElement.ref, zIndexMap.card);
// 更新字母卡片显示状态(使用绿色背景)
this.updateWordCard(cardElement, item.words, item.show, item.audio, 'green', useSmallBg, item.wordArr);
});
}
}
/**
* 初始化第三部分内容(字母块)
* 确保part3中的每个节点的y坐标与part2中对应的节点y坐标保持一致,以保证连接线为水平
*/
initPart3(defaultX) {
// 获取转换后的表单数据
const formData = this.g_formData;
// 检查是否有part_3数据
if (formData.part_3 && formData.part_3.length > 0) {
// 确定是否使用小尺寸背景(当数量大于3时使用小尺寸)
const useSmallBg = formData.part_3.length > 3;
// 获取黄色背景卡片的高度(根据是否使用小尺寸选择不同的背景)并应用缩放
const bgType = useSmallBg ? 'bg_yellow_small' : 'bg_yellow';
// 获取part2卡片的y坐标
const part2Cards = [];
for (let i = 0; i < formData.part_2.length; i++) {
const card = this.g_cartoon.getCartoonElement(`part2_card_${i}`);
if (card && card.ref) {
part2Cards.push(card.ref.y);
}
}
// 遍历part_3数据创建字母块
formData.part_3.forEach((item, index) => {
// 创建字母卡片元素
const cardElement = this.g_cartoon.createCartoonElement(`part3_card_${index}`, 'MySprite');
// 设置卡片位置
cardElement.ref.x = defaultX;
// 使用对应part2卡片的y坐标(如果存在),否则使用默认计算方式
if (index < part2Cards.length) {
cardElement.ref.y = part2Cards[index];
} else {
// 获取画布高度的中心点
const centerY = this.g_canvasHeight / 2;
const cardHeight = localImagesSize[bgType].height * this.g_mapScale;
// 计算垂直间距(卡片高度 + 10px间距 * 缩放比例)
const verticalSpacing = cardHeight + (10 * this.g_mapScale);
// 计算所有卡片总高度
const totalHeight = formData.part_3.length * cardHeight + (formData.part_3.length - 1) * (10 * this.g_mapScale);
// 计算第一个卡片的Y坐标(居中显示所有卡片)
const startY = centerY - totalHeight / 2 + cardHeight / 2;
cardElement.ref.y = startY + index * verticalSpacing;
}
// 设置卡片X方向和Y方向的缩放比例
cardElement.ref.scaleX = this.g_mapScale;
cardElement.ref.scaleY = this.g_mapScale;
// 渲染卡片元素
this.render(cardElement.ref, zIndexMap.card);
initFishGame() {
// 更新字母卡片显示状态(使用黄色背景)
this.updateWordCard(cardElement, item.words, item.show, item.audio, 'yellow', useSmallBg, item.wordArr);
});
}
}
initWords() {
this.g_formData.type;
}
/**
* 绘制节点之间的连接线
* 根据type属性决定连接逻辑
* 连接线从节点边缘开始,而不是从中心点
* 只有当连接的两端节点都处于显示状态时才绘制连接线
*/
drawConnectionLines() {
// 获取转换后的表单数据
const formData = this.g_formData;
const type = formData.type;
// 设置线条样式
const lineColor = '#3c65f7';
const lineWidth = 5 * this.g_mapScale; // 线条宽度应用缩放
const edgeOffset = 5 * this.g_mapScale; // 边缘偏移量应用缩放,从边缘向内5px
// 检查是否有足够的数据来绘制连接线
if (!formData.part_1 || !formData.part_2 || formData.part_1.length === 0 || formData.part_2.length === 0) {
return;
}
// 创建一个新的Canvas元素用于绘制线条
const lineElement = this.g_cartoon.createCartoonElement('connection_lines', 'MySprite');
lineElement.ref.x = 0;
lineElement.ref.y = 0;
lineElement.ref.width = this.g_canvasWidth;
lineElement.ref.height = this.g_canvasHeight;
// 检查卡片是否处于显示状态的函数
const isCardVisible = (card) => {
// 如果没有显示状态标记,则通过图像URL判断
return card.isVisible
};
// 自定义绘制函数
lineElement.ref.drawSelf = () => {
const ctx = this.g_ctx;
ctx.save();
// 设置线条样式
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
// 获取part1的节点
const part1Cards = [];
for (let i = 0; i < formData.part_1.length; i++) {
const card = this.g_cartoon.getCartoonElement(`part1_card_${i}`);
if (card && card.ref) {
part1Cards.push(card.ref);
}
}
// 获取part2的节点
const part2Cards = [];
for (let i = 0; i < formData.part_2.length; i++) {
const card = this.g_cartoon.getCartoonElement(`part2_card_${i}`);
if (card && card.ref) {
part2Cards.push(card.ref);
}
}
// 获取part3的节点
const part3Cards = [];
if (formData.part_3 && formData.part_3.length > 0) {
for (let i = 0; i < formData.part_3.length; i++) {
const card = this.g_cartoon.getCartoonElement(`part3_card_${i}`);
if (card && card.ref) {
part3Cards.push(card.ref);
}
}
}
// 计算连接点的函数
const calculateConnectionPoint = (sourceCard, targetCard) => {
// 获取卡片的宽度和高度,考虑缩放因子
let sourceWidth, sourceHeight, targetWidth, targetHeight;
// 计算实际尺寸(原始尺寸 * 缩放比例)
sourceWidth = sourceCard.width * sourceCard.scaleX;
sourceHeight = sourceCard.height * sourceCard.scaleY;
targetWidth = targetCard.width * targetCard.scaleX;
targetHeight = targetCard.height * targetCard.scaleY;
// 计算卡片中心点
const sourceCenter = {
x: sourceCard.x,
y: sourceCard.y
};
const targetCenter = {
x: targetCard.x,
y: targetCard.y
};
// 计算方向向量
const dx = targetCenter.x - sourceCenter.x;
const dy = targetCenter.y - sourceCenter.y;
// 确定连接点位置
let sourcePoint = { x: 0, y: 0 };
let targetPoint = { x: 0, y: 0 };
// 确定源卡片的连接点(在边缘)
if (Math.abs(dx) > Math.abs(dy)) {
// 水平方向连接
if (dx > 0) {
// 源卡片右边缘(向左偏移)
sourcePoint.x = sourceCenter.x + sourceWidth / 2 - edgeOffset;
sourcePoint.y = sourceCenter.y;
} else {
// 源卡片左边缘(向右偏移)
sourcePoint.x = sourceCenter.x - sourceWidth / 2 + edgeOffset;
sourcePoint.y = sourceCenter.y;
}
} else {
// 垂直方向连接
if (dy > 0) {
// 源卡片底边缘
sourcePoint.x = sourceCenter.x;
sourcePoint.y = sourceCenter.y + sourceHeight / 2 - edgeOffset;
} else {
// 源卡片顶边缘
sourcePoint.x = sourceCenter.x;
sourcePoint.y = sourceCenter.y - sourceHeight / 2 + edgeOffset;
}
}
// 确定目标卡片的连接点(在边缘)
if (Math.abs(dx) > Math.abs(dy)) {
// 水平方向连接
if (dx > 0) {
// 目标卡片左边缘(向右偏移)
targetPoint.x = targetCenter.x - targetWidth / 2 + edgeOffset;
targetPoint.y = targetCenter.y;
} else {
// 目标卡片右边缘(向左偏移)
targetPoint.x = targetCenter.x + targetWidth / 2 - edgeOffset;
targetPoint.y = targetCenter.y;
}
} else {
// 垂直方向连接
if (dy > 0) {
// 目标卡片顶边缘
targetPoint.x = targetCenter.x;
targetPoint.y = targetCenter.y - targetHeight / 2 + edgeOffset;
} else {
// 目标卡片底边缘
targetPoint.x = targetCenter.x;
targetPoint.y = targetCenter.y + targetHeight / 2 - edgeOffset;
}
}
return { sourcePoint, targetPoint };
};
// 根据type属性决定连接逻辑
if (type === 0) {
// type为0时:part1连接到每个part2,part2和part3一一对应
// 绘制part1到part2的连接线
part1Cards.forEach(part1Card => {
part2Cards.forEach(part2Card => {
// 只有当两端节点都处于显示状态时才绘制连接线
if (isCardVisible(part1Card) && isCardVisible(part2Card)) {
const { sourcePoint, targetPoint } = calculateConnectionPoint(part1Card, part2Card);
ctx.beginPath();
ctx.moveTo(sourcePoint.x, sourcePoint.y);
ctx.lineTo(targetPoint.x, targetPoint.y);
ctx.stroke();
}
});
});
// 绘制part2到part3的一一对应连接线
const minLength = Math.min(part2Cards.length, part3Cards.length);
for (let i = 0; i < minLength; i++) {
// 只有当两端节点都处于显示状态时才绘制连接线
if (isCardVisible(part2Cards[i]) && isCardVisible(part3Cards[i])) {
const { sourcePoint, targetPoint } = calculateConnectionPoint(part2Cards[i], part3Cards[i]);
ctx.beginPath();
ctx.moveTo(sourcePoint.x, sourcePoint.y);
ctx.lineTo(targetPoint.x, targetPoint.y);
ctx.stroke();
}
}
} else if (type === 1) {
// type为1时:part1连接到每个part2,part1连接到每个part3
// 绘制part1到part2的连接线
part1Cards.forEach(part1Card => {
part2Cards.forEach(part2Card => {
// 只有当两端节点都处于显示状态时才绘制连接线
if (isCardVisible(part1Card) && isCardVisible(part2Card)) {
const { sourcePoint, targetPoint } = calculateConnectionPoint(part1Card, part2Card);
ctx.beginPath();
ctx.moveTo(sourcePoint.x, sourcePoint.y);
ctx.lineTo(targetPoint.x, targetPoint.y);
ctx.stroke();
}
});
// 绘制part1到part3的连接线
part3Cards.forEach(part3Card => {
// 只有当两端节点都处于显示状态时才绘制连接线
if (isCardVisible(part1Card) && isCardVisible(part3Card)) {
const { sourcePoint, targetPoint } = calculateConnectionPoint(part1Card, part3Card);
ctx.beginPath();
ctx.moveTo(sourcePoint.x, sourcePoint.y);
ctx.lineTo(targetPoint.x, targetPoint.y);
ctx.stroke();
}
});
});
}
ctx.restore();
};
// 渲染线条元素(设置较低的zIndex确保线条在卡片下方)
this.render(lineElement.ref, zIndexMap.card - 1);
// 为所有卡片添加点击事件监听,当卡片状态变化时重新绘制连接线
const addCardClickListener = (cardId) => {
const originalHandler = this.m_mapDownQueue[cardId];
if (originalHandler) {
this.m_mapDownQueue[cardId] = (...args) => {
// 调用原始处理函数
originalHandler(...args);
// 延迟一帧后重新绘制连接线,确保卡片状态已更新
setTimeout(() => {
lineElement.ref.drawSelf();
}, 0);
};
}
};
// 为所有卡片添加点击事件监听
formData.part_1.forEach((_, index) => {
addCardClickListener(`part1_card_${index}`);
});
formData.part_2.forEach((_, index) => {
addCardClickListener(`part2_card_${index}`);
});
if (formData.part_3) {
formData.part_3.forEach((_, index) => {
addCardClickListener(`part3_card_${index}`);
});
}
}
/**
* 初始化退出按钮
......@@ -1269,53 +711,6 @@ export class PlayComponent implements OnInit, OnDestroy {
});
}
/**
* 初始化游戏标题
* 创建标题背景和标题文本
*/
initTitle() {
// 如果标题为空,则不渲染标题背景和文本
if (!this.g_formData.title) {
// 确保 m_titleBgElement 为 null,以便其他方法能正确处理
this.m_titleBgElement = null;
return;
}
// 创建标题背景元素
const bgElement = this.g_cartoon.createCartoonElement('title_bg', 'MySprite');
// 初始化背景图像,使用resources.js中的bg_title资源
bgElement.ref.init(this.g_cartoon.images.get('bg_title'));
// 设置背景X方向和Y方向的缩放比例
bgElement.ref.scaleX = this.g_mapScale;
bgElement.ref.scaleY = this.g_mapScale;
// 设置背景的X坐标位置(左上角坐标为50)
bgElement.ref.x = 50 * this.g_mapScale + bgElement.ref.width * bgElement.ref.scaleX / 2;
// 设置背景的Y坐标位置(左上角坐标为50)
bgElement.ref.y = 50 * this.g_mapScale + bgElement.ref.height * bgElement.ref.scaleY / 2;
// 渲染背景元素
this.render(bgElement.ref, zIndexMap.background + 1);
// 创建标题文本元素
const titleElement = this.g_cartoon.createCartoonElement('title_text', 'Label');
// 设置标题文本内容
titleElement.ref.text = this.g_formData.title;
// 设置标题文本字体大小并应用缩放
titleElement.ref.fontSize = 46 * this.g_mapScale;
// 设置标题文本颜色
titleElement.ref.fontColor = '#f0ffff';
// 设置标题文本位置(与背景相同位置,稍微调整以居中)
titleElement.ref.x = bgElement.ref.x;
titleElement.ref.y = bgElement.ref.y;
// 设置文本对齐方式为居中
titleElement.ref.textAlign = 'center';
// 保存标题背景元素的引用,供其他函数使用
this.m_titleBgElement = bgElement;
titleElement.ref.setOutline(6 * this.g_mapScale, '#3c65f7');
// 渲染标题文本元素
this.render(titleElement.ref, zIndexMap.background + 2);
}
......
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