squidGame/tgx-games-client/assets/module_arean/ui_bag/BagItem.ts

91 lines
3.1 KiB
TypeScript

import { _decorator, Component, Label, Sprite, SpriteFrame, Prefab, instantiate, v3, sp } from 'cc';
import {areanMgr} from "db://assets/module_arean/scripts/AreanManager";
import {EAreanItemType, ItemAttributes} from "db://assets/module_basic/shared/protocols/public/arean/AreanTypeDef";
import {IPropCfg} from "db://assets/module_basic/shared/configs/interface/IPropCfg";
import {resLoader} from "db://assets/core_tgx/base/utils/ResLoader";
import {ModuleDef} from "db://assets/scripts/ModuleDef";
import {CommonFun} from "db://assets/module_arean/scripts/CommonFun";
import {UserMgr} from "db://assets/module_basic/scripts/UserMgr";
const { ccclass, property } = _decorator;
@ccclass('BagItem')
export class BagItem extends Component {
@property(Label)
count: Label;
@property(Sprite)
rare: Sprite;
@property(Sprite)
kuang: Sprite;
@property(Sprite)
icon: Sprite;
@property(Sprite)
iconShadow: Sprite;
/** 当前选中的道具 */
private _curChooseItemID : number = null;
private _itemData : ItemAttributes = null;
private _callBack : Function = null;
public getItemId() : number{
return this._itemData.itemId;
}
/**
*
* @param item 服务端数据
* @param chooseItemId
* @param callBack
*/
public setData(item : ItemAttributes,chooseItemId : number,callBack : Function) {
this._curChooseItemID = chooseItemId;
this._itemData = item;
this._callBack = callBack;
let data : IPropCfg = areanMgr.cfgMgr.PropData.getData(item.itemId);
if(data){
if(data.type === EAreanItemType.Hero){
let skinId = data.icon
resLoader.load<Prefab>(ModuleDef.Arean,`res/Prefebs/heroSpine/${skinId}`,(err: Error, prefab: Prefab) => {
if(err){
console.error(err);
return;
}else {
let spineNode = instantiate(prefab);
this.icon.node.destroyAllChildren();
this.icon.node.addChild(spineNode);
spineNode.scale = v3(0.8,0.8,1);
spineNode.position = v3(0,-60,0);
spineNode.getComponent(sp.Skeleton).animation = null;
}
})
}else{
let res : string = CommonFun.getInstance().getIcon(item.itemId);
let sp : SpriteFrame = resLoader.getSpriteFrame(res,ModuleDef.Arean);
if(sp) this.icon.getComponent(Sprite).spriteFrame = sp;
}
}
this.updateChooseState(this._curChooseItemID === this._itemData.itemId);
this.updateCount(item.itemCount)
}
public updateCount(value : number) : void{
this.count.string = `${value}`;
}
/** 刷新选中状态 */
public updateChooseState(show : boolean) : void{
this.kuang.node.active = show;
}
public click() : void{
this._callBack && this._callBack(this._itemData);
this.updateChooseState(true);
}
}