Files
heros/assets/script/game/map/GoodsComp.ts
2025-08-20 23:27:32 +08:00

161 lines
5.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { _decorator, Component, Node, Label, Sprite, SpriteFrame, resources } from 'cc';
import { Goods, GType, CType } from '../common/config/Goods';
import { Items } from '../common/config/Items';
import { NumberFormatter } from '../common/config/BoxSet';
import { smc } from '../common/SingletonModuleComp';
import { oops } from 'db://oops-framework/core/Oops';
const { ccclass, property } = _decorator;
@ccclass('GoodsComp')
export class GoodsComp extends Component {
// 数据(仅用于更新显示)
private goodsData: any = null;
private itemData: any = null;
private currentUuid: number = 0;
goods_count_key:number=-1
/**
* 更新物品数据
* @param uuid 物品UUID
*/
update_data(uuid: number,key:number) {
this.goods_count_key=key
this.currentUuid = uuid;
this.goodsData = Goods[uuid];
if (!this.goodsData) {
console.error(`Goods data not found for uuid: ${uuid}`);
return;
}
this.itemData = Items[this.goodsData.i_uuid];
if (!this.itemData) {
console.error(`Item data not found for i_uuid: ${this.goodsData.i_uuid}`);
return;
}
this.updateIcon();
this.updateTexts();
this.update_btn()
this.update_max_num()
}
private update_btn( ){
if(smc.shop.goods_count[this.goods_count_key]<=0){
this.node.getChildByName("btn").getChildByName("ad").active=false
this.node.getChildByName("btn").getChildByName("free").active=false
this.node.getChildByName("btn").getChildByName("diamond").active=false
this.node.getChildByName("btn").getChildByName("gold").active=false
this.node.getChildByName("btn").getChildByName("nothing").active=true
this.node.getChildByName("btn").getComponent(Sprite).grayscale=true
return
}
this.node.getChildByName("btn").getChildByName("nothing").active=false
this.node.getChildByName("btn").getComponent(Sprite).grayscale=false
this.node.getChildByName("btn").getChildByName("ad").active=this.goodsData.c_type==CType.AD
this.node.getChildByName("btn").getChildByName("free").active=this.goodsData.c_type==CType.FREE
this.node.getChildByName("btn").getChildByName("diamond").getChildByName("num").getComponent(Label).string=NumberFormatter.formatNumber(this.goodsData.cast)
this.node.getChildByName("btn").getChildByName("gold").getChildByName("num").getComponent(Label).string=NumberFormatter.formatNumber(this.goodsData.cast)
this.node.getChildByName("btn").getChildByName("diamond").active=this.goodsData.c_type==CType.DIAMOND
this.node.getChildByName("btn").getChildByName("gold").active=this.goodsData.c_type==CType.GOLD
}
/**
* 更新图标
*/
private update_max_num(){
this.node.getChildByName("max").getComponent(Label).string=NumberFormatter.formatNumber(smc.shop.goods_count[this.goods_count_key])
}
private updateIcon() {
const iconSprite = this.node.getChildByName("icon")?.getComponent(Sprite);
if (!iconSprite) return;
const path = `gui/items/${this.itemData.path}`;
resources.load(path, SpriteFrame, (err, spriteFrame) => {
if (err) {
console.warn(`icon load failed: ${path}`, err);
return;
}
iconSprite.spriteFrame = spriteFrame;
});
}
/** 仅更新文字(名称与数量) */
private updateTexts() {
// 名称
const nameLabel = this.node.getChildByName("name")?.getComponent(Label);
if (nameLabel) nameLabel.string = this.itemData.name;
// 数量(根节点下的 num
const mainNumLabel = this.node.getChildByName("num")?.getComponent(Label);
if (mainNumLabel) mainNumLabel.string = NumberFormatter.formatNumber(this.goodsData.num);
}
to_buy(){
if(smc.shop.goods_count[this.goods_count_key]<=0){
oops.gui.toast("商品已售罄")
return
}
if(this.goodsData.c_type==CType.AD){
this.do_ad()
}else if(this.goodsData.c_type==CType.FREE){
this.do_free()
}else if(this.goodsData.c_type==CType.DIAMOND){
this.do_diamond_cast()
}else if(this.goodsData.c_type==CType.GOLD){
this.do_gold_cast()
}
}
do_buy(){
console.log("[GoodsComp]do_buy",this.goodsData,this.itemData)
switch(this.goodsData.type){
case GType.ITEM:
smc.addItem(this.itemData.uuid,this.goodsData.num) //添加物品
break
case GType.GOLD:
smc.addGold(this.goodsData.num) //添加金币
break
case GType.DIAMOND:
smc.addDiamond(this.goodsData.num) //添加钻石
break
case GType.MEAT: //添加肉
break
case GType.EXP: //添加经验
smc.addExp(this.goodsData.num)
break
}
smc.shop.goods_count[this.goods_count_key]--
this.update_btn()
this.update_max_num()
}
do_ad(){
if(this.goodsData.c_type==CType.AD){
this.do_ad_back(this.do_buy,this.ad_back_false)
}
}
ad_back_false(){
console.log("ad_back_false")
}
do_ad_back(success:Function,fail:Function){
success.call(this)
}
do_free(){
if(this.goodsData.c_type==CType.FREE){
this.do_buy()
}
}
do_diamond_cast(){
if(this.goodsData.c_type==CType.DIAMOND){
smc.spendDiamond(this.goodsData.cast)
this.do_buy()
}
}
do_gold_cast(){
if(this.goodsData.c_type==CType.GOLD){
smc.spendGold(this.goodsData.cast)
this.do_buy()
}
}
}