183 lines
7.2 KiB
TypeScript
183 lines
7.2 KiB
TypeScript
import NumberToImage from "./NumberToImage";
|
|
|
|
|
|
const { ccclass, property, requireComponent } = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class Reward extends cc.Component {
|
|
|
|
|
|
@property(cc.Prefab)
|
|
rewardNode: cc.Prefab = null;
|
|
actionOver: boolean;
|
|
speedTime: number = 50;
|
|
private originalTimeScale: number = 1; // 记录原始时间缩放比例
|
|
|
|
|
|
onLoad() {
|
|
// 为节点添加点击事件监听器
|
|
this.speedTime = 50;
|
|
}
|
|
|
|
start() {
|
|
// ... 已有代码 ...
|
|
this.node.on(cc.Node.EventType.TOUCH_END, this.onNodeClick, this);
|
|
|
|
}
|
|
|
|
/**
|
|
* 节点点击事件处理函数
|
|
*/
|
|
onNodeClick() {
|
|
console.log('Reward 节点被点击了', this.actionOver);
|
|
// 这里可以添加点击后的具体逻辑
|
|
if (this.actionOver == true) {
|
|
this.node.destroy();
|
|
}
|
|
else {
|
|
// 游戏整体加速
|
|
// this.speedUpGame();
|
|
}
|
|
}
|
|
|
|
|
|
speedUpGame() {
|
|
this.speedTime = 0;
|
|
this.originalTimeScale = cc.director.getScheduler().getTimeScale(); // 记录原始时间缩放比例
|
|
cc.director.getScheduler().setTimeScale(10); // 加速到 2 倍速
|
|
// 在奖励节点创建完成后恢复正常速度
|
|
const checkCompletion = () => {
|
|
if (this.actionOver) {
|
|
cc.director.getScheduler().setTimeScale(this.originalTimeScale); // 恢复原始时间缩放比例
|
|
} else {
|
|
setTimeout(checkCompletion, 50); // 每隔 100 毫秒检查一次
|
|
}
|
|
};
|
|
checkCompletion();
|
|
}
|
|
|
|
/**
|
|
* 初始化
|
|
* @param data 数据
|
|
*/
|
|
init(data) {
|
|
this.actionOver = false;
|
|
const num = Math.min(data.length, 9); // 确保 num 不超过 9
|
|
const spacing = 10; // 间隔,默认 10
|
|
if (this.rewardNode && num > 0) {
|
|
// 获取单个 rewardNode 的宽度和高度
|
|
const tempNode = cc.instantiate(this.rewardNode);
|
|
const nodeWidth = tempNode.width;
|
|
const nodeHeight = tempNode.height;
|
|
tempNode.destroy();
|
|
|
|
// 定义每一行的节点数量
|
|
const rowCounts: number[] = [];
|
|
if (num <= 3) {
|
|
rowCounts.push(num);
|
|
} else if (num === 4) {
|
|
rowCounts.push(2, 2);
|
|
} else if (num === 5) {
|
|
rowCounts.push(3, 2);
|
|
} else if (num === 6) {
|
|
rowCounts.push(3, 3);
|
|
} else if (num === 7) {
|
|
rowCounts.push(3, 3, 1);
|
|
} else if (num === 8) {
|
|
rowCounts.push(3, 3, 2);
|
|
} else if (num === 9) {
|
|
rowCounts.push(3, 3, 3);
|
|
}
|
|
|
|
// 计算总高度
|
|
const rows = rowCounts.length;
|
|
const totalHeight = (nodeHeight * rows) + (spacing * (rows - 1));
|
|
|
|
// 计算起始位置的 y 坐标
|
|
const startY = totalHeight / 2 - nodeHeight / 2;
|
|
|
|
let index = 0;
|
|
const createNodeWithDelay = (row: number, col: number) => {
|
|
setTimeout(() => {
|
|
const rewardNode = cc.instantiate(this.rewardNode);
|
|
rewardNode.parent = this.node;
|
|
// 计算当前行的总宽度
|
|
// let startX = 0;
|
|
// if (row === 0) {
|
|
// const totalWidth = (nodeWidth * rowCounts[row]) + (spacing * (rowCounts[row] - 1));
|
|
// startX = -totalWidth / 2 + nodeWidth / 2;
|
|
// } else {
|
|
// startX = -((nodeWidth * 3) + (spacing * 2)) / 2 + nodeWidth / 2;
|
|
// }
|
|
// 修复:统一使用当前行的节点数量计算起始位置
|
|
const totalWidth = (nodeWidth * rowCounts[row]) + (spacing * (rowCounts[row] - 1));
|
|
const startX = -totalWidth / 2 + nodeWidth / 2;
|
|
|
|
// 计算每个节点的 x 和 y 位置
|
|
const xPos = startX + (col * (nodeWidth + spacing));
|
|
const yPos = startY - (row * (nodeHeight + spacing));
|
|
rewardNode.setPosition(xPos, yPos);
|
|
// 查找 rewardNode 的子节点 icon
|
|
const iconNode = rewardNode.getChildByName('icon');
|
|
iconNode.getChildByName(data[index].type).active = true;
|
|
let label = rewardNode.getChildByName('label')
|
|
if (data[index].type == 'coin')
|
|
NumberToImage.numberToImageNodes(data[index].count, 45, 5, "coins", label, true);
|
|
else if (data[index].type == 'infinite_health') {
|
|
NumberToImage.getTimeMargin2(data[index].count, 45, "coins", label, true);
|
|
if (data[index].count > 3600) label.x -= 10;
|
|
rewardNode.getChildByName('xnode').opacity = 1;
|
|
}
|
|
else {
|
|
NumberToImage.numberToImageNodes(data[index].count, 50, 15, "coins", label, true);
|
|
rewardNode.getChildByName('xnode').opacity = 0;
|
|
label.x += 20;
|
|
}
|
|
rewardNode.getChildByName('label').active = false;
|
|
if (iconNode) {
|
|
cc.tween(iconNode)
|
|
.to(0.2, { scale: 1.1 })
|
|
.to(0.1, { scale: 0.9 })
|
|
.to(0.05, { scale: 1 })
|
|
.delay(0.1)
|
|
.call(() => {
|
|
rewardNode.getChildByName('label').active = true;
|
|
if (rewardNode.getChildByName('xnode').opacity == 1) {
|
|
rewardNode.getChildByName('xnode').opacity = 0;
|
|
let name = "icon1";
|
|
if (rewardNode.getChildByName('label').children.length > 4) name = "icon2";
|
|
iconNode.getChildByName('infinite_health').getChildByName(name).active = true;
|
|
}
|
|
else if (rewardNode.getChildByName('xnode').opacity != 255) {
|
|
rewardNode.getChildByName('xnode').active = true;
|
|
rewardNode.getChildByName('xnode').opacity = 255;
|
|
}
|
|
})
|
|
.start();
|
|
}
|
|
|
|
index++;
|
|
if (index == data.length) {
|
|
setTimeout(() => {
|
|
this.actionOver = true;
|
|
}, 500);
|
|
|
|
}
|
|
if (index < data.length) {
|
|
const nextCol = col + 1;
|
|
if (nextCol < rowCounts[row]) {
|
|
createNodeWithDelay(row, nextCol);
|
|
} else {
|
|
createNodeWithDelay(row + 1, 0);
|
|
}
|
|
}
|
|
}, index * this.speedTime); // 每个节点间隔 0.1 秒
|
|
};
|
|
createNodeWithDelay(0, 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|