RectBlockData.ts 3.17 KB
import { ObjType } from "../BattleConst";
import { BlockData } from "./BlockData";
import { MapData } from "./MapData";
import { ObjDataUtil } from "./ObjDataUtil";

export class RectBlockData extends BlockData {

    private _widthGrid: number;
    get widthGrid(): number { return this._widthGrid; }
    get widthPixel(): number { return this._widthGrid * this.map.gridSize.x; }
    private _heightGrid: number;
    get heightGrid(): number { return this._heightGrid; }
    get heightPixel(): number { return this._heightGrid * this.map.gridSize.y; }

    get viewPixelPosX(): number { return this._pixelPos.x + (this._widthGrid) / 2 * this.map.gridSize.x - this.map.gridSize.x / 2; }
    get viewPixelPosY(): number { return this._pixelPos.y - (this._heightGrid) / 2 * this.map.gridSize.y + this.map.gridSize.y / 2 }

    //#region life cycle

    initBlock(map: MapData, type: ObjType, hp: number, gridX: number, gridY: number, width: number = 1, height: number = 1): void {

        this._widthGrid = width;
        this._heightGrid = height;

        super.initBlock(map, type, hp, gridX, gridY);

        for (let i = this._gridPos.x; i < this._gridPos.x + this._widthGrid; ++i) {

            for (let j = this._gridPos.y; j < this._gridPos.y + this._heightGrid; ++j) {

                if (i == this._gridPos.x && j == this._gridPos.y) {
                    continue;
                }

                let index = ObjDataUtil.getIndex(i, j);
                this.map.objData.delete(index);//删除范围内的其他格子
                this.map.blockData.delete(index);
            }
        }

        this._contentY = this._gridPos.y + this._heightGrid - 1;
    }

    clone(): RectBlockData {
        return this.cloneValue(new RectBlockData())
    }

    protected cloneValue(target: RectBlockData): RectBlockData {
        super.cloneValue(target);
        target._widthGrid = this._widthGrid;
        target._heightGrid = this._heightGrid;
        return target;
    }

    //#endregion

    //#region public method

    setGridPos(gridX: number, gridY: number): void {
        // this.clearFromMap();
        super.setGridPos(gridX, gridY);
        this._contentY = this._gridPos.y + this._heightGrid - 1;
        // this.addToMap();
    }

    isReachedDownBorder(): boolean {
        return !this._droped && (this._gridPos.y + this._heightGrid - 1 >= this.map.mapGridSize.y - 1);
    }

    // private clearFromMap() {
    //     for (let i = this._gridPos.x; i < this._gridPos.x + this._widthGrid; ++i) {

    //         for (let j = this._gridPos.y; j < this._gridPos.y + this._heightGrid; ++j) {

    //             let index = ObjDataUtil.getIndex(i, j);
    //             this.map.blockData.delete(index);
    //         }
    //     }
    // }

    // private addToMap() {
    //     for (let i = this._gridPos.x; i < this._gridPos.x + this._widthGrid; ++i) {

    //         for (let j = this._gridPos.y; j < this._gridPos.y + this._heightGrid; ++j) {

    //             let index = ObjDataUtil.getIndex(i, j);
    //             this.map.blockData.set(index, this);
    //         }
    //     }
    // }

    //#endregion

    //#region event

    //#endregion

    //#region private method

    //#endregion
}