Files
heros/assets/script/game/map/GoodsComp.ts
2024-12-24 13:46:46 +08:00

62 lines
2.4 KiB
TypeScript

import { _decorator, instantiate, Label, Prefab } from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { smc } from "../common/SingletonModuleComp";
import { Item } from "./Item";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { HChipComp } from "../hero/HChipComp";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('GoodsComp')
@ecs.register('GoodsComp', false)
export class GoodsComp extends CCComp {
type:number=0;
g_uuid:number=0;
num:number=0;
inventory:number=0;
cost:number=0;
smc_index:number=0;
/** 视图层逻辑代码分离演示 */
start() {
// var entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
// this.on(ModuleEvent.Cmd, this.onHandler, this);
}
update_data(){
let goods:any=smc.goods[this.smc_index]
let gnode=this.node.getChildByName("item")
switch (goods.type) {
case 0:
let item=ecs.getEntity<Item>(Item)
item.load(goods.uuid,goods.num,gnode)
this.node.getChildByName("inventory").getComponent(Label).string="库存:"+goods.inventory.toString()
console.log("item",item)
break;
case 1:
let path = "game/gui/hchip";
let prefab: Prefab = oops.res.get(path, Prefab)!;
let node = instantiate(prefab);
node.parent=gnode
let hc= node.getComponent(HChipComp)
hc.update_data(goods.uuid,goods.num)
break
}
if(goods.cost > 0){
this.node.getChildByName("free").active=false
this.node.getChildByName("buy").getChildByName("cost").getComponent(Label).string=goods.cost.toString()
}else{
this.node.getChildByName("free").active=true
}
}
buy(){
console.log("购买商品"+this.smc_index)
}
update_inventory(){
this.node.getChildByName("inventory").getComponent(Label).string="库存:"+smc.goods[this.smc_index].inventory
}
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.node.destroy();
}
}