Files
Alien_Sniper/assets/scripts/ui/WinPanel.ts
T
2026-07-14 11:44:07 +08:00

257 lines
7.7 KiB
TypeScript

import { _decorator, Button, Component, instantiate, Label, math, Node, Prefab, randomRange, Sprite, Tween, tween, Vec3 } from 'cc';
import { LocalSaveMgr } from '../framework/data/LocalSaveMgr';
import { UIManager } from '../framework/ui/UIManager';
import { UISwitcher } from '../framework/ui/UISwitcher';
import { AudioMgr } from '../framework/audio/AudioMgr';
import { PanelBase } from '../framework/ui/PanelBase';
import { AssetController } from '../AssetController';
import { GameController } from '../GameController';
import { BattleResult } from '../BattleLevel';
import { SdkMgr } from '../framework/sdk/SdkMgr';
const { ccclass, property } = _decorator;
@ccclass('WinPanel')
export class WinPanel extends PanelBase {
static instance: WinPanel;
@property(Node)
pointer: Node = null; // 指针节点
@property
amplitude: number = 450; // 运动幅度
@property
speed: number = 2; // 运动速度
@property(Button)
rewardBtn: Button = null;
@property(Button)
continueBtn: Button = null;
@property(Label)
remainTimer: Label = null;
@property(Label)
killEnemyCount: Label = null;
@property(Prefab)
flyMoneyPrefab: Prefab = null;
@property(Label)
coinCount: Label = null;
@property(Label)
rewardCoin: Label = null;
@property(Label)
rewardTimerCoin: Label = null;
@property(Label)
rewardKillCoin: Label = null;
@property(Node)
flyMoneyContent: Node = null;
@property(Label)
coinLabel: Label = null;
@property(Label)
continueCoinLabel: Label = null;
private time: number = 0;
runingPoint: boolean = false;
rewardDouble: number = 1;
onLoad() {
WinPanel.instance = this;
this.rewardBtn.node.on(Button.EventType.CLICK, this.onClickRewardBtn, this);
this.continueBtn.node.on(Button.EventType.CLICK, this.onClickContinueBtn, this);
}
onClickRewardBtn() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
if (Math.abs(this.pointPos) < 50) {
this.rewardDouble = 5
} else if (Math.abs(this.pointPos) < 150) {
this.rewardDouble = 4
} else if (Math.abs(this.pointPos) < 250) {
this.rewardDouble = 3
} else if (Math.abs(this.pointPos) < 350) {
this.rewardDouble = 2
} else if (Math.abs(this.pointPos) < 450) {
this.rewardDouble = 1
}
this.runingPoint = false
SdkMgr.instance.playerRewardAds(() => {
let addPoint = Math.ceil((this.curResult.coinCount * this.rewardDouble))
let fromeMoney = LocalSaveMgr.instance.playerData.coin
LocalSaveMgr.instance.addCoin(addPoint)
let endMoney = LocalSaveMgr.instance.playerData.coin
this.flyMoney(fromeMoney, endMoney)
})
}
onClickContinueBtn() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
GameController.getInstance().backMain()
SdkMgr.instance.showInterstitial()
}
backMain() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
GameController.getInstance().backMain()
}
start() {
this.time = 0;
}
curResult: BattleResult;
winTween1 = null
winTween2 = null
winTween3 = null
refresh() {
let result = GameController.getInstance().curLevel.levelResult
this.curResult = result
LocalSaveMgr.instance.addCoin(result.coinCount)
this.killEnemyCount.string = result.enemyCount.toString()
this.remainTimer.string = result.timer.toString()
this.coinLabel.string = LocalSaveMgr.instance.playerData.coin.toString()
this.coinCount.string = "0"
let startValue = 0;
let endValue = result.coinCount;
let duration = 1; // 1秒
let startValue2 = 0;
let endValue2 = result.timerCoin;
let startValue3 = 0;
let endValue3 = result.enemyCoin;
this.continueCoinLabel.string = result.coinCount.toString()
this.winTween1 = new Tween({ value: startValue })
.to(duration, { value: endValue }, {
onUpdate: (target, ratio) => {
// 在每一帧更新时调用
let currentValue = Math.floor(target.value);
this.coinCount.string = currentValue.toString();
}
})
.start();
this.winTween2 = new Tween({ value: startValue2 })
.to(duration, { value: endValue2 }, {
onUpdate: (target, ratio) => {
// 在每一帧更新时调用
let currentValue = Math.floor(target.value);
this.rewardTimerCoin.string = currentValue.toString();
}
})
.start();
this.winTween3 = new Tween({ value: startValue3 })
.to(duration, { value: endValue3 }, {
onUpdate: (target, ratio) => {
// 在每一帧更新时调用
let currentValue = Math.floor(target.value);
this.rewardKillCoin.string = currentValue.toString();
}
})
.start();
this.runPoint()
}
flyMoney(fromMoney: number, endMoney: number) {
this.rewardBtn.interactable = false
this.continueBtn.interactable = false
AudioMgr.inst.playOneShot(AssetController.instance.getCoin)
for (let index = 0; index < 10; index++) {
let item = instantiate(this.flyMoneyPrefab)
item.setParent(this.flyMoneyContent)
item.setPosition(this.rewardBtn.node.getPosition())
let delay = index * 0.1
tween(item)
.by(0.3, { position: new Vec3(randomRange(-150, 150), randomRange(-150, 150), 0) })
.delay(delay)
.to(0.6, { worldPosition: this.coinLabel.node.getWorldPosition() })
.call(() => {
item.destroy()
})
.start()
}
new Tween({ value: fromMoney })
.delay(0.9)
.to(1, { value: endMoney }, {
onUpdate: (target, ratio) => {
// 在每一帧更新时调用
let currentValue = Math.floor(target.value);
this.coinLabel.string = currentValue.toString();
}
}).call(() => {
// this.rewardBtn.node.active = false
// this.continueBtn.interactable = true
this.backMain()
})
.start()
}
runPoint() {
this.runingPoint = true
}
pointPos: number
update(dt: number) {
if (!this.runingPoint) return
// 更新时间
this.time += dt * this.speed;
// 使用正弦函数计算指针的位置
const x = this.amplitude * Math.sin(this.time);
this.pointPos = x
// 设置指针的位置
this.pointer.setPosition(new Vec3(x, 65, 0))
if (Math.abs(x) < 93.079) {
this.rewardDouble = 5
} else if (Math.abs(x) < 224.786) {
this.rewardDouble = 4
} else if (Math.abs(x) < 341.818) {
this.rewardDouble = 3
} else if (Math.abs(x) < 430) {
this.rewardDouble = 2
}
this.rewardCoin.string = Math.ceil((this.curResult.coinCount * this.rewardDouble)).toString()
}
protected onDestroy(): void {
this.winTween1.stop()
this.winTween2.stop()
this.winTween3.stop()
}
protected onOpen(): void {
AudioMgr.inst.playOneShot(AssetController.instance.win)
WinPanel.instance.time = 0;
WinPanel.instance.refresh()
}
}