ObjData.ts 4.87 KB
import { ObjType } from "../BattleConst";
import { IObjView } from "../inter/IObjView";
import { MapData } from "./MapData";
import { ObjDataUtil } from "./ObjDataUtil";

export class ObjData {

    private static UID: number = 0;
    // private static getIndex(gridX: number, gridY: number): number {
    //     return gridX * 10000 + gridY;
    // }

    protected _gridPos: cc.Vec2 = cc.v2(0, 0);
    get gridPos(): cc.Vec2 { return this._gridPos; }
    protected _pixelPos: cc.Vec2 = cc.v2(0, 0);
    get pixelPos(): cc.Vec2 { return this._pixelPos; }
    /**所属地图引用 */
    map: MapData = null;

    canSlide: boolean = true;

    protected _imgName: string = '';
    /**图标层要显示的图片名 */
    get imgName(): string { return this._imgName; }

    private _uid: number = 0;
    get uid(): number { return this._uid; }

    protected _droped: boolean = false;
    get droped(): boolean { return this._droped; }

    private _type: ObjType = ObjType.NONE;
    get type(): ObjType { return this._type; }

    private _index: number = 0;
    /**根据格子位置产生的索引值 */
    get index(): number { return this._index; }

    protected _deaded: boolean = false;
    get isDead(): boolean { return this._deaded; }

    protected _IView: IObjView;
    /**是否可见(未降到可视区域的格子不可见) */
    get isVisible(): boolean { return (this.gridPos.y > this.map.viewSplitRow); }

    //#region public method

    /**绑定显示对象 */
    bindView(view: IObjView) {
        this._IView = view;
    }

    /**掉落到地图最下端 */
    dropDown() {
        this._droped = true;
        this.setGridPos(this.gridPos.x, this.map.mapGridSize.y - 1);

        this._IView && this._IView.onDroped();
    }

    setGridPos(gridX: number, gridY: number, syncToMapData: boolean = true) {

        // let oldIndex = ObjData.getIndex(this._gridPos.x, this._gridPos.y);

        if (syncToMapData && this.map.objData.has(this._index)) {
            this.map.objData.delete(this._index);
        }

        this._gridPos.x = gridX;
        this._gridPos.y = gridY;
        this._pixelPos.set(this.map.getPixelPosByGridPos(this._gridPos));
        this._index = this.droped ? -this._uid : ObjDataUtil.getIndex(this._gridPos.x, this._gridPos.y);

        if (syncToMapData && !this.map.objData.has(this._index)) {
            this.map.objData.set(this._index, this);
        }
    }

    dead() {

        if (!this._deaded) {
            this._deaded = true;

            this.map.onObjDead(this); //有可能导致回合结束, 移动此格子,在移动时加判断死亡
            this.onDead();
            this._IView && this._IView.onDead();
            this._IView = null;
        }

    }

    /**向下滑动一格, 当物件未达到最下沿时可以滑动
     * @returns 是否产生了滑动
     */
    slideDown(): boolean {

        if (this._deaded) {
            return false; //死亡后 
        }

        if (this._gridPos.y < this.map.mapGridSize.y - 1) {
            this.setGridPos(this._gridPos.x, this._gridPos.y + 1);
            return true;
        }

        return false;
    }

    /**是否到达了地图最下端 */
    isReachedDownBorder(): boolean {
        if (!this._droped && this.gridPos.y >= (this.map.mapGridSize.y - 1)) {
            return true;
        }
        return false;
    }

    //#endregion

    //#region life cycle

    init(map: MapData, type: ObjType, gridX: number, gridY: number) {
        // ObjData.UID++;
        this._uid = ObjData.UID++;
        this.map = map;
        this._type = type;
        this._index = ObjDataUtil.getIndex(gridX, gridY);
        this.setGridPos(gridX, gridY);
    }

    /**子类需覆写,返回对应的类型 */
    clone(): ObjData {
        //XXX 新增属性需要在此处增加处理
        return this.cloneValue(new ObjData());
    }

    destroy() {
        this.onDestroy();
        this._gridPos = null;
        this._pixelPos = null;
        this.map = null;
        this._IView = null;
    }

    /**和球发生碰撞 */
    onCollisonToBall(collider: cc.Collider) {

        if (!this._deaded) { //销毁后有物理组件没有销毁,还会进入回调

            this._IView.onHit();
        }

    }

    onDead() {
        // this.map.onObjDead(this);
    }

    /**实际战斗时会调用, 预览时不会,在 BattleEvent.BATTLE_START 之前 */
    onBattleStart() {

    }

    protected onDestroy() {

    }

    //#endregion

    //#region child override

    protected cloneValue(target: ObjData): ObjData {
        target._uid = ObjData.UID++;
        target._gridPos.set(this._gridPos);
        target._pixelPos.set(this._pixelPos);
        target._imgName = this._imgName;
        target._type = this.type;
        target._index = this._index;
        target._droped = this._droped;
        target.canSlide = this.canSlide;

        // console.log();
        return target;
    }

    //#endregion
}