BlockData.ts 3.65 KB
import { SoundManager } from "../../../component/SoundManager";
import { SoundName } from "../../../Global";
import { BlockColorTableMgr } from "../../table/BlockColorTableMgr";
import { ObjType } from "../BattleConst";
import { BlockSkillBase } from "./blockSkill/BlockSkillBase";
import { MapData } from "./MapData";
import { ObjData } from "./ObjData";

export class BlockData extends ObjData {

    protected _hp: number = 1;
    get hp(): number { return this._hp; }

    protected _contentY: number = 0;
    /**最大内容范围内的Y坐标 */
    get contentY(): number { return this._contentY; }

    skill: BlockSkillBase = null;

    protected _isProtected: boolean = false;
    /**是否在被闸门保护状态 */
    get isProtected(): boolean { return this._isProtected; }

    //#region life cycle

    init(map: MapData, type: ObjType, gridX: number, gridY: number): void {
        //屏蔽,调用initBlock
        return;
    }

    initBlock(map: MapData, type: ObjType, hp: number, gridX: number, gridY: number) {
        super.init(map, type, gridX, gridY);

        this._contentY = gridY;
        this._hp = hp;
        this._imgName = this.getImgName();
    }

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

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

        if (!this._deaded && !this.isProtected) {
            super.onCollisonToBall(collider);

            this.skill && this.skill.onCollisionToBall();
            if (this._hp >= 1) {
                this.adJustHp(-1);
            }
        }
    }

    onDead(): void {

        super.onDead();
        this.skill && this.skill.onDead();

        SoundManager.ins.playEffect(SoundName.BREAK);
    }

    protected onDestroy(): void {
        this.skill && this.skill.destroy();
    }

    //#endregion

    //#region public method

    addSkill(skill: BlockSkillBase) {
        this.skill = skill;
        skill.onAddedToBlock();
    }

    adJustHp(delta: number) {

        if (delta < 0) {
            //先只处理扣血

            this._hp += delta;

            if (this._hp > 0) {
                let imgName = this.getImgName();
                if (this._imgName != imgName) {
                    this._imgName = imgName;
                    this._IView.onImgChanged();
                }

                SoundManager.ins.playEffect(SoundName.HIT);
            }

            if (this._hp <= 0) {
                this._hp = 0;
                this.dead();
            }
        }
    }

    setIsProtected(value: boolean) {
        if (this._isProtected != value) {
            this._isProtected = value;
        }
    }

    //#endregion

    //#region child override

    protected cloneValue(target: BlockData): BlockData {
        super.cloneValue(target);
        target._hp = this._hp;
        // target._skillImgName = this._skillImgName;
        target._contentY = this._contentY;
        if (this.skill) {
            target.skill = this.skill;
            target.skill.owner = target; //指向新的格子
            target.skill.onAddedToBlock(); //重新触发一下
        }
        return target;
    }

    protected getImgName(): string {
        return BlockColorTableMgr.ins.getByHp(this._hp).Img;
    }

    setGridPos(gridX: number, gridY: number): void {
        this.map.blockData.delete(this.index); //删除旧索引
        // super.setGridPos(gridX, gridY, false);
        super.setGridPos(gridX, gridY);
        this.map.blockData.set(this.index, this); //增加新索引
        this._contentY = this.gridPos.y;
    }

    //#endregion

}