import { _decorator, CCInteger, CCString, Component, Node, Sprite, SpriteFrame, Texture2D, UITransform, v2 } from 'cc';
import { ImagePack } from '../ImagePack/ImagePack';
import { BaseSpriteFrame } from './BaseSpriteFrame';
const { ccclass, property } = _decorator;

@ccclass('BaseSprite')
export class BaseSprite extends Component {

    @property({ type: CCString, displayName: 'img路径', tooltip: "img路径" })
    ImgPath = "";

    @property({ type: CCInteger, displayName: 'img编号', tooltip: "img编号" })
    ImgIndex = 0;

    //精灵对象
    SpriteObj: Sprite;

    start() {
        //判断是否有精灵 如果没有 就给他搞一个
        if (this.node.getComponent(Sprite))
            this.SpriteObj = this.node.getComponent(Sprite);
        else
            this.SpriteObj = this.node.addComponent(Sprite);

        //设置节点锚点为左上角
        this.node.getComponent(UITransform).anchorPoint = v2(0, 1);
        //设置类型
        this.SpriteObj.sizeMode = Sprite.SizeMode.RAW;
        //设置
        this.SpriteObj.trim = false;

        new BaseSpriteFrame(this.ImgPath.toLocaleLowerCase(), this.ImgIndex, (_SpriteFrame) => {
            this.SpriteObj.spriteFrame = _SpriteFrame;
        });
    }

    update(deltaTime: number) {

    }
}


