import { _decorator, Component, ProgressBar, Sprite, Vec3, tween, Color, Animation, Label, Node, UITransform, director, assetManager } from 'cc'; import { tgxUIAlert } from '../../core_tgx/tgx'; import { AreanNetMgr } from '../../module_arean/scripts/AreanNetMgr'; 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; // redScore: number = 0; blueScore: number = 0; gameover:boolean = false; private score: number = 0; // 积分 start() { // 初始化按钮状态 this.setButtonDark(); // 开始进度条增长动画 this.startProgressAnimation(); } onButtonClick() { if (this.canClick && !this.hasClick) { this.redScore += 2; this.getComponent(Animation)?.play("redSuccess"); this.hasClick = true; } else { this.text.string = "别着急"; this.redScore -= 1; } } changeGameScene() { director.loadScene("BallScene"); } setButtonDark() { // 将按钮设为变暗状态 this.buttonSprite.node.getComponent(Sprite)!.color = new Color(35, 35, 35); this.text.string = "保持住"; } setButtonLight() { // 将按钮设为变亮状态 this.buttonSprite.node.getComponent(Sprite)!.color = new Color(255, 66, 66); this.text.string = "用力拉"; } refreshScore() { tween(this.red.node.getComponent(UITransform)) .to(0.5, { width: this.redScore*4 }) .start(); tween(this.blue.node.getComponent(UITransform)) .to(0.5, { width: this.blueScore*4 }) .start(); this.redScoreLabel.string = this.redScore+""; this.blueScoreLabel.string = this.blueScore+""; const newPosition = new Vec3( this.ropeNode.position.x - this.score / 10, this.ropeNode.position.y, this.ropeNode.position.z); tween(this.ropeNode) .to(0.3, { position: newPosition }) .start(); if (this.ropeNode.position.x < -3.5) { this.winorlose.active = true; this.winorlose.getChildByName("Label")!.getComponent(Label)!.string = "己方获胜"; this.gameover = true; this.switchScene(); } else if (this.ropeNode.position.x > 3.5) { this.winorlose.active = true; this.winorlose.getChildByName("Label")!.getComponent(Label)!.string = "对手获胜"; this.gameover = true; this.switchScene(); } } switchScene() { console.log("switchScene"); tween(this.node).delay(5).call(() => { var bundle = assetManager.getBundle('level3'); if(bundle){ director.loadScene("scene/BallScene"); }else{ assetManager.loadBundle("level3",()=>{ director.loadScene("scene/BallScene"); }) } }).start(); } onClickBtnQuit() { tgxUIAlert.show(`是否确认退出 ? `, true).onClick(async b => { if (b) { AreanNetMgr.inst.sendMsg_exit(); } }); } startProgressAnimation() { if(this.gameover) return; 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); // 进度条归零延迟时间 } AiScoreChange() { const textArray1 = [ "无力了,无法继续了...", "没救了,我们...", "完了,真的完了...", "没指望了,无法挽回...", "我还不想死啊!" ]; const textArray2 = [ "我们还有一线生机", "要坚持到底", "还有机会扭转局势", "想想家人!不能轻言放弃!", "不能放弃希望!" ]; const textArray3 = [ "感受到了吗,他们再颤抖", "对手已经开始向我们投降的信号了", "看来我们要开始准备庆祝胜利了", ]; let rndText1 = [""]; let rndText2 = [""]; let randomIndex = 0; let rnd1 = getRandomNumber(Math.max(-2,4 - this.turn * 0.5), 4) this.redScore += rnd1; let rnd2 = getRandomNumber(Math.max(-2,5 - this.turn * 0.5), 5) this.blueScore += rnd2; this.score = this.redScore - this.blueScore; let rndString = ""; let rnd = getRandomNumber(0, 1); if(this.ropeNode.position.x < -2) { if(rnd==0) { rndText1=textArray3; this.redScore += 1; } else { rndText2=textArray1; this.blueScore -= 1; } }else if(this.ropeNode.position.x > 2) { if(rnd==0) { rndText2=textArray3; this.blueScore += 1; } else { rndText1=textArray1; this.redScore -= 1; } } else if(this.ropeNode.position.x <= -0.5) { rndText2=textArray2; this.blueScore += 1; } else if(this.ropeNode.position.x >= 0.5) { rndText1=textArray2; this.redScore += 1; } 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; } } function getRandomNumber(min: number, max: number) { // 使用 Math.random() 生成0到1之间的随机数,并将其映射到指定范围 return Math.floor(Math.random() * (max - min + 1)) + min; }