Files
Alien_Sniper/assets/scripts/ui/MainPanel.ts
T
2026-07-15 11:38:16 +08:00

345 lines
12 KiB
TypeScript

import { _decorator, Asset, Button, Component, debug, instantiate, Label, 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 { DataMgr } from '../framework/data/DataMgr';
import { Weapon } from '../data/ConfigData';
import { AddHeartPanel } from './AddHeartPanel';
import { SdkMgr } from '../framework/sdk/SdkMgr';
import { EventCenter } from '../EventCenter';
const { ccclass, property } = _decorator;
@ccclass('MainPanel')
export class MainPanel extends PanelBase {
static instance: MainPanel;
@property({ type: Button })
startBtn: Button = null;
@property({ type: Button })
leftBtn: Button = null;
@property({ type: Button })
right: Button = null;
@property({ type: Button })
equip: Button = null;
@property({ type: Button })
clear: Button = null;
@property({ type: Button })
addMoneyBtn: Button = null;
@property({ type: Button })
buy: Button = null;
@property({ type: Node })
equiped: Node = null;
@property({ type: Label })
weaponName: Label = null;
@property({ type: Label })
gunPrice: Label = null;
@property({ type: Label })
heart: Label = null;
@property({ type: Label })
heartTimer: Label = null;
@property({ type: Label })
coin: Label = null;
@property({ type: Button })
addHeartBtn: Button = null;
@property({ type: Prefab })
weaponSelectPrefab: Prefab = null;
@property(Prefab)
flyMoneyPrefab: Prefab = null;
@property(Node)
flyMoneyContent: Node = null;
@property(Node)
fingerNode: Node = null;
weaponDatas: Array<Weapon> = []
private weaponOffset: number = 1000
private curSelectIndex: number = 0
@property({ type: Node })
weaponContent: Node = null;
protected onOpen(): void {
MainPanel.instance.refresh()
}
refresh() {
var weapons = DataMgr.getInstance().GetDatas(Weapon)
this.weaponDatas = weapons
let equipedIndex: number = 0
for (let index = 0; index < weapons.length; index++) {
let data = weapons[index]
var item = instantiate(this.weaponSelectPrefab)
item.setParent(this.weaponContent)
let sprite = item.getChildByPath("Sprite").getComponent(Sprite)
AssetController.instance.loadSetSprite(`weaponSprite/${data.spriteName}`, sprite);
item.getChildByPath("Label").getComponent(Label).string = data.gunName
if (data.id == LocalSaveMgr.instance.playerData.equipedWeapon) {
equipedIndex = index
}
}
this.fingerNode.active = !LocalSaveMgr.instance.playerData.haveTutorial
this.heart.string = LocalSaveMgr.instance.playerData.power.toString()
this.coin.string = LocalSaveMgr.instance.playerData.coin.toString()
this.setToIndex(equipedIndex, false)
}
protected update(dt: number): void {
if (LocalSaveMgr.instance.playerData.isOnTimerRun) {
let totalSeconds = Math.ceil(LocalSaveMgr.instance.getPowerCD())
totalSeconds = Math.ceil(totalSeconds);
let minutes = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
// 将分钟和秒格式化为两位数
let formattedMinutes = minutes < 10 ? `0${minutes}` : `${minutes}`;
let formattedSeconds = seconds < 10 ? `0${seconds}` : `${seconds}`;
// 组合成 00:00 的格式
let formattedTime = `${formattedMinutes}:${formattedSeconds}`;
this.heartTimer.string = formattedTime
} else {
this.heartTimer.string = "已满"
}
this.heart.string = LocalSaveMgr.instance.playerData.power.toString()
}
setToIndex(index: number, widthAim: boolean) {
this.curSelectIndex = index
if (!widthAim) {
this.weaponContent.setPosition(-this.weaponOffset * index, 0, 0)
} else {
let newPos = new Vec3(-this.weaponOffset * index, 0, 0)
tween(this.weaponContent)
.to(0.3, { position: newPos })
.start()
}
this.refreshToIndex()
}
refreshToIndex() {
let ownedWeapons = LocalSaveMgr.instance.playerData.ownWeapon
let owned: boolean = false
for (let index = 0; index < ownedWeapons.length; index++) {
let element = ownedWeapons[index];
console.log("刷新", this.weaponDatas[this.curSelectIndex].id, element, ownedWeapons.length, index)
if (element == this.weaponDatas[this.curSelectIndex].id) {
owned = true
break
}
}
let equiped: boolean = LocalSaveMgr.instance.playerData.equipedWeapon == this.weaponDatas[this.curSelectIndex].id
// this.weaponName.string = this.weaponDatas[this.curSelectIndex].gunName
this.gunPrice.string = this.weaponDatas[this.curSelectIndex].coin.toString()
this.equiped.active = false
this.buy.node.active = false
this.equip.node.active = false
if (equiped) {
this.equiped.active = true
} else if (owned) {
this.equip.node.active = true
} else {
this.buy.node.active = true
if (LocalSaveMgr.instance.playerData.coin >= this.weaponDatas[this.curSelectIndex].coin) {
this.buy.interactable = true;
this.buy.getComponent(Sprite).grayscale = false
} else {
this.buy.interactable = false;
this.buy.getComponent(Sprite).grayscale = true
}
}
if (this.curSelectIndex == 0) {
this.leftBtn.node.active = false
} else {
this.leftBtn.node.active = true
}
if (this.curSelectIndex == this.weaponDatas.length - 1) {
this.right.node.active = false
} else {
this.right.node.active = true
}
}
clickLeft() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
if (this.curSelectIndex > 0) {
this.setToIndex(this.curSelectIndex - 1, true)
}
}
clickRight() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
if (this.curSelectIndex < this.weaponDatas.length - 1) {
this.setToIndex(this.curSelectIndex + 1, true)
}
}
clickEquipBtn() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
LocalSaveMgr.instance.equipWeapon(this.weaponDatas[this.curSelectIndex].id)
this.refreshToIndex()
}
clickBuyBtn() {
LocalSaveMgr.instance.addCoin(-this.weaponDatas[this.curSelectIndex].coin)
AudioMgr.inst.playOneShot(AssetController.instance.click)
LocalSaveMgr.instance.buyWeapon(this.weaponDatas[this.curSelectIndex].id)
this.refreshToIndex()
}
clickClear() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
LocalSaveMgr.instance.clearData()
}
clickAddHeartBtn() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
UIManager.instance.openPanel(AddHeartPanel)
}
clickAddMoneyBtn() {
AudioMgr.inst.playOneShot(AssetController.instance.click)
SdkMgr.instance.requestReward(() => {
let addPoint = 200
let fromeMoney = LocalSaveMgr.instance.playerData.coin
LocalSaveMgr.instance.addCoin(addPoint)
let endMoney = LocalSaveMgr.instance.playerData.coin
this.flyMoney(fromeMoney, endMoney)
})
}
flyRewardMoney(fromMoney: number, endMoney: number) {
AudioMgr.inst.playOneShot(AssetController.instance.getCoin)
for (let index = 0; index < 10; index++) {
let item = instantiate(this.flyMoneyPrefab)
item.setParent(this.flyMoneyContent)
item.setPosition(new Vec3(0, 0, 0))
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.coin.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);
if (this.coin != null)
this.coin.string = currentValue.toString();
}
}).call(() => {
if (this.addMoneyBtn != null)
this.addMoneyBtn.interactable = true
})
.start();
}
flyMoney(fromMoney: number, endMoney: number) {
this.addMoneyBtn.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.addMoneyBtn.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.coin.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);
if (this.coin != null)
this.coin.string = currentValue.toString();
}
}).call(() => {
if (this.addMoneyBtn != null)
this.addMoneyBtn.interactable = true
})
.start();
}
onCoinCountChange() {
MainPanel.instance.coin.string = LocalSaveMgr.instance.playerData.coin.toString()
MainPanel.instance.refreshToIndex()
}
protected onDestroy(): void {
EventCenter.coinCountChange.off("coinCountChange", this.onCoinCountChange)
}
onLoad() {
MainPanel.instance = this;
this.startBtn.node.on(Button.EventType.CLICK, this.startBtnCallBack, this);
this.leftBtn.node.on(Button.EventType.CLICK, this.clickLeft, this);
this.right.node.on(Button.EventType.CLICK, this.clickRight, this);
this.equip.node.on(Button.EventType.CLICK, this.clickEquipBtn, this);
this.buy.node.on(Button.EventType.CLICK, this.clickBuyBtn, this);
this.clear.node.on(Button.EventType.CLICK, this.clickClear, this);
this.addHeartBtn.node.on(Button.EventType.CLICK, this.clickAddHeartBtn, this);
this.addMoneyBtn.node.on(Button.EventType.CLICK, this.clickAddMoneyBtn, this);
EventCenter.coinCountChange.on("coinCountChange", this.onCoinCountChange).bind(this)
}
startBtnCallBack() {
GameController.getInstance().gameStart()
}
}