squidGame/tgx-games-client/assets/module_arean/level2/Script/CircularButton.ts

363 lines
11 KiB
TypeScript
Raw Normal View History

2025-03-06 22:47:02 +08:00
import { _decorator, Component, ProgressBar, Sprite, Vec3, tween, Color, Animation, Label, Node, UITransform, director, assetManager, instantiate, Prefab, TweenSystem } from 'cc';
import { resLoader } from 'db://assets/core_tgx/base/utils/ResLoader';
import { tgxUIAlert } from 'db://assets/core_tgx/tgx';
import { ModuleDef } from 'db://assets/scripts/ModuleDef';
import { AreanNetMgr } from '../../scripts/AreanNetMgr';
2025-02-17 21:36:37 +08:00
const { ccclass, property } = _decorator;
@ccclass('CircularButton')
export class CircularButton extends Component {
@property({ type: ProgressBar })
progressBar: ProgressBar = null!; // 圆形进度条
@property({ type: Sprite })
buttonSprite: Sprite = null!; // 按钮精灵
@property({ type: Label })
text: Label = null!;
@property({ type: Node })
ropeNode: Node = null!;
@property({ type: Node })
bubble1: Node = null!;
@property({ type: Node })
bubble2: Node = null!;
@property({ type: Node })
winorlose: Node = null!;
@property({ type: Sprite })
red: Sprite = null!;
@property({ type: Sprite })
blue: Sprite = null!;
@property({ type: Label })
redScoreLabel: Label = null!;
@property({ type: Label })
blueScoreLabel: Label = null!;
hasClick: boolean = false;
canClick: boolean = false;
progressTime: number = 3; // 进度增长时长
resetDelay: number = 0.5; // 进度条归零延迟时间
turn: number = 0; //
2025-03-07 09:08:47 +08:00
redScore: number = 0;
blueScore: number = 0;
2025-02-17 21:36:37 +08:00
2025-03-07 09:08:47 +08:00
gameover: boolean = false;
2025-02-17 21:36:37 +08:00
private score: number = 0; // 积分
2025-03-06 22:47:02 +08:00
2025-03-07 09:08:47 +08:00
2025-03-06 22:47:02 +08:00
@property(Node)
guiNode: Node = null;
2025-02-17 21:36:37 +08:00
start() {
// 初始化按钮状态
this.setButtonDark();
// 开始进度条增长动画
this.startProgressAnimation();
}
onButtonClick() {
if (this.canClick && !this.hasClick) {
this.redScore += 2;
this.hasClick = true;
2025-03-06 22:47:02 +08:00
this.getComponent(Animation)?.play("redSuccess");
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
} else {
2025-03-01 02:24:46 +08:00
this.text.string = "Don't worry";
2025-02-17 21:36:37 +08:00
this.redScore -= 1;
}
}
2025-03-07 09:08:47 +08:00
changeGameScene() {
2025-02-17 21:36:37 +08:00
director.loadScene("BallScene");
}
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
setButtonDark() {
// 将按钮设为变暗状态
this.buttonSprite.node.getComponent(Sprite)!.color = new Color(35, 35, 35);
2025-03-01 02:24:46 +08:00
this.text.string = "Hode on";
2025-02-17 21:36:37 +08:00
}
setButtonLight() {
// 将按钮设为变亮状态
this.buttonSprite.node.getComponent(Sprite)!.color = new Color(255, 66, 66);
2025-03-01 02:24:46 +08:00
this.text.string = "Click it";
2025-02-17 21:36:37 +08:00
}
refreshScore() {
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
tween(this.red.node.getComponent(UITransform))
2025-03-07 09:08:47 +08:00
.to(0.5, { width: this.redScore * 3 })
2025-02-17 21:36:37 +08:00
.start();
tween(this.blue.node.getComponent(UITransform))
2025-03-07 09:08:47 +08:00
.to(0.5, { width: this.blueScore * 3 })
.start();
this.redScoreLabel.string = this.redScore + "";
this.blueScoreLabel.string = this.blueScore + "";
2025-02-17 21:36:37 +08:00
2025-03-07 09:08:47 +08:00
const newPosition = new Vec3(this.ropeNode.position.x - this.score / 10, this.ropeNode.position.y, this.ropeNode.position.z);
2025-02-17 21:36:37 +08:00
tween(this.ropeNode)
.to(0.3, { position: newPosition })
.start();
if (this.ropeNode.position.x < -3.5) {
2025-03-06 22:47:02 +08:00
// this.winorlose.active = true;
// this.winorlose.getChildByName("Label")!.getComponent(Label)!.string = "己方获胜";
resLoader.load(ModuleDef.Arean, 'common/prefabs/settlement', (err: Error | null, prefab: Prefab) => {
if (!err && prefab) {
const n = instantiate(prefab);
this.guiNode.addChild(n);
n.getComponent(Animation).play("showWin");
}
});
this.showGameOver(true);
2025-02-17 21:36:37 +08:00
}
else if (this.ropeNode.position.x > 3.5) {
2025-03-06 22:47:02 +08:00
// this.winorlose.active = true;
// this.winorlose.getChildByName("Label")!.getComponent(Label)!.string = "对手获胜";
resLoader.load(ModuleDef.Arean, 'common/prefabs/settlement', (err: Error | null, prefab: Prefab) => {
if (!err && prefab) {
const n = instantiate(prefab);
this.guiNode.addChild(n);
n.getComponent(Animation).play("showLose");
}
});
this.showGameOver(false);
2025-02-17 21:36:37 +08:00
}
}
2025-03-07 09:08:47 +08:00
showGameOver(isWin: boolean) {
2025-03-06 22:47:02 +08:00
this.gameover = true;
this.scheduleOnce(() => {
resLoader.load(ModuleDef.Arean, 'common/prefabs/settlement', (err: Error | null, prefab: Prefab) => {
if (!err && prefab) {
const n = instantiate(prefab);
this.guiNode.addChild(n);
n.getComponent(Animation).play("showSettlement");
2025-03-07 09:08:47 +08:00
2025-03-06 22:47:02 +08:00
// Lives从16滚动到8
const livesLabel = n.getChildByPath("settlementNode/Node/lives").getComponent(Label);
2025-03-07 09:08:47 +08:00
let lives = { value: 10 };
2025-03-06 22:47:02 +08:00
tween(lives)
2025-03-07 09:08:47 +08:00
.to(1, { value: 5 })
2025-03-06 22:47:02 +08:00
.call(() => {
livesLabel.string = Math.floor(lives.value).toString();
})
.start();
// Money从400w滚动到480w
const moneyLabel = n.getChildByPath("settlementNode/Node/money").getComponent(Label);
2025-03-07 09:08:47 +08:00
let money = { value: 446 };
2025-03-06 22:47:02 +08:00
tween(money)
2025-03-07 09:08:47 +08:00
.to(1, { value: 451 }, {
2025-03-06 22:47:02 +08:00
easing: 'linear',
onUpdate: () => {
moneyLabel.string = Math.floor(money.value) + "w";
}
})
.start();
2025-03-07 09:08:47 +08:00
n.getChildByPath("settlementNode/btnOK/label").getComponent(Label).string = isWin ? "NEXT" : "QUIT";
2025-03-06 22:47:02 +08:00
n.getChildByPath("settlementNode/btnOK").on(Node.EventType.TOUCH_START, () => {
2025-03-07 09:08:47 +08:00
if (isWin) {
director.loadScene("level3/scene/BallScene");
2025-03-06 22:47:02 +08:00
}
2025-03-07 09:08:47 +08:00
else {
director.loadScene("lobby_arean");
2025-03-06 22:47:02 +08:00
}
});
}
});
2025-03-07 09:08:47 +08:00
}, 3);
2025-03-06 22:47:02 +08:00
}
2025-03-07 09:08:47 +08:00
2025-03-06 22:47:02 +08:00
2025-02-17 21:36:37 +08:00
switchScene() {
console.log("switchScene");
tween(this.node).delay(5).call(() => {
var bundle = assetManager.getBundle('level3');
2025-03-07 09:08:47 +08:00
if (bundle) {
2025-02-17 21:36:37 +08:00
director.loadScene("scene/BallScene");
2025-03-07 09:08:47 +08:00
} else {
assetManager.loadBundle("level3", () => {
director.loadScene("scene/BallScene");
})
}
2025-02-17 21:36:37 +08:00
}).start();
}
2025-03-07 09:08:47 +08:00
onClickBtnQuit() {
tgxUIAlert.show(`是否确认退出 ? `, true).onClick(async b => {
if (b) {
AreanNetMgr.inst.sendMsg_exit();
}
});
}
2025-02-17 21:36:37 +08:00
startProgressAnimation() {
2025-03-07 09:08:47 +08:00
if (this.gameover) return;
2025-02-17 21:36:37 +08:00
this.setButtonDark();
this.progressBar.progress = 0;
this.newTurn();
// 开始新的动画
tween(this.progressBar)
.to(this.progressTime, { progress: 1 }) // 设置进度条的目标进度
.call(() => {
// 如果按钮变亮,重新设为变暗状态
this.setButtonLight();
this.hasClick = false;
this.progressBar.progress = 0;
this.canClick = true;
this.AiScoreChange();
// 等待一段时间后归零进度条
tween(this.progressBar)
.to(this.resetDelay, { progress: 1 }) // 设置进度条的目标进度
.call(() => {
this.canClick = false;
this.progressBar.progress = 0;
this.refreshScore();
this.startProgressAnimation();
})
.start();
})
.start();
}
newTurn() {
this.turn++;
this.progressTime = Math.max(1.5, 3 - this.turn * 0.1); // 进度增长时长
this.resetDelay = Math.max(0.2, 1 - this.turn * 0.03); // 进度条归零延迟时间
}
2025-03-06 22:47:02 +08:00
2025-02-17 21:36:37 +08:00
AiScoreChange() {
const textArray1 = [
2025-03-01 02:24:46 +08:00
"I'm out of strength, I can't go on...",
"It's hopeless, we're...",
"It's over, really over...",
"No hope, it's irreparable...",
"I don't want to die yet!"
2025-02-17 21:36:37 +08:00
];
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
const textArray2 = [
2025-03-01 02:24:46 +08:00
"We still have a glimmer of hope",
"We must persevere to the end",
"There's still a chance to turn things around",
"Think of your family! Don't give up so easily!",
"Don't give up hope!"
2025-02-17 21:36:37 +08:00
];
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
const textArray3 = [
2025-03-01 02:24:46 +08:00
"Can you feel it? They're trembling",
"The opponents are already sending signals of surrender",
"Looks like we need to start preparing to celebrate our victory",
2025-02-17 21:36:37 +08:00
];
let rndText1 = [""];
let rndText2 = [""];
let randomIndex = 0;
2025-03-07 09:08:47 +08:00
let rnd1 = getRandomNumber(Math.max(-2, 4 - this.turn * 0.5), 4)
2025-02-17 21:36:37 +08:00
this.redScore += rnd1;
2025-03-07 09:08:47 +08:00
let rnd2 = getRandomNumber(Math.max(-2, 5 - this.turn * 0.5), 5)
2025-02-17 21:36:37 +08:00
this.blueScore += rnd2;
this.score = this.redScore - this.blueScore;
let rndString = "";
let rnd = getRandomNumber(0, 1);
2025-03-07 09:08:47 +08:00
if (this.ropeNode.position.x < -2) {
if (rnd == 0) {
rndText1 = textArray3;
2025-02-17 21:36:37 +08:00
this.redScore += 1;
}
2025-03-07 09:08:47 +08:00
else {
rndText2 = textArray1;
2025-02-17 21:36:37 +08:00
this.blueScore -= 1;
}
2025-03-07 09:08:47 +08:00
} else if (this.ropeNode.position.x > 2) {
if (rnd == 0) {
rndText2 = textArray3;
2025-02-17 21:36:37 +08:00
this.blueScore += 1;
}
2025-03-07 09:08:47 +08:00
else {
rndText1 = textArray1;
2025-02-17 21:36:37 +08:00
this.redScore -= 1;
}
}
2025-03-07 09:08:47 +08:00
else if (this.ropeNode.position.x <= -0.5) {
rndText2 = textArray2;
2025-02-17 21:36:37 +08:00
this.blueScore += 1;
}
2025-03-07 09:08:47 +08:00
else if (this.ropeNode.position.x >= 0.5) {
rndText1 = textArray2;
2025-02-17 21:36:37 +08:00
this.redScore += 1;
}
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
randomIndex = Math.floor(Math.random() * rndText1.length);
rndString = rndText1[randomIndex];
this.bubble1.getComponent(Animation)?.play("bubble");
this.bubble1.getChildByName("Label")!.getComponent(Label)!.string = rndString;
randomIndex = Math.floor(Math.random() * rndText2.length);
rndString = rndText2[randomIndex];
this.bubble2.getComponent(Animation)?.play("bubble");
this.bubble2.getChildByName("Label")!.getComponent(Label)!.string = rndString;
2025-03-07 09:08:47 +08:00
2025-02-17 21:36:37 +08:00
}
}
function getRandomNumber(min: number, max: number) {
// 使用 Math.random() 生成0到1之间的随机数并将其映射到指定范围
return Math.floor(Math.random() * (max - min + 1)) + min;
}