import { tMgr } from "../Global"; import { IObjView } from "../kernel/battle/inter/IObjView"; import { MapView } from "../kernel/battle/map/MapView"; import { ObjData } from "../kernel/battle/map/ObjData"; import { TableName, GamePlayTable } from "../kernel/table/TableDefine"; const { ccclass, property } = cc._decorator; @ccclass export default class ObjBaseView extends cc.Component implements IObjView { protected _owner: ObjData = null; get owner(): ObjData { return this._owner; } /**主体图像 */ protected _img: cc.Node = null; protected _imgOffset: cc.Vec2 = cc.v2(); protected _visible: boolean; protected _needMove: boolean = false; protected _destroyed: boolean = false; //#region interface onImgChanged() { this.updateImg(); } onDroped() { this.setColliderEnable(false); } onDead() { this._img.destroy(); this.node.destroy(); } onHit() { } //#endregion //#region life cycle protected start(): void { this.schedule(this.onLazyUpdate, 1 / 20); } init(owner: ObjData, mapView: MapView) { this._owner = owner; this._owner.bindView(this); this._img = this.node.getChildByName('img'); this._imgOffset.x = this._img.x; this._imgOffset.y = this._img.y; //分层显示 this._img.removeFromParent(); mapView.colliderLayer.addChild(this.node); this.node.active = true; mapView.blockImgLayer.addChild(this._img); // this.updateImg(); this.onImgChanged(); this.onPositionChange(this._owner.pixelPos.x, this._owner.pixelPos.y); } onPreview() { //预览状态下,在init后被调用 this._visible = true; this._img.active = true; this.setColliderEnable(false); this.onVisibleChange(); } protected update(dt: number): void { if (this._destroyed) { return; } // let needMove = (this.node.position.y != this._owner.pixelPos.y); this.onUpdateMoveState(); if (this._needMove) { let speed = this._owner.droped ? 600 : (tMgr.getConfig(TableName.GAME_PLAY, 1) as GamePlayTable).Value; let step = -speed * dt; let dis = this._owner.pixelPos.y - this.node.y; if (Math.abs(step) >= Math.abs(dis)) { step = dis; } this.onPositionChange(this._owner.pixelPos.x, this.node.position.y + step); } } /**更改是否需要移动的接口,更改 _needMove变量 */ protected onUpdateMoveState() { this._needMove = (this.node.position.y != this._owner.pixelPos.y); } // onEndContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) { // if (otherCollider.node.name == "ball") { // contact.disabled = true; // this._owner.onCollisonToBall(otherCollider); // } // } onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) { if (otherCollider.node.name == "ball") { this._owner.onCollisonToBall(otherCollider); } } protected onDestroy(): void { this._destroyed = true; this._owner = null; this._img = null; } //#endregion //#region public method setColliderEnable(value: boolean) { this.node.getComponent(cc.PhysicsCollider).enabled = value; } //#endregion //#region children override protected onLazyUpdate() { } protected onImageLoaded() { } protected updateImg() { if (this._owner.imgName != "") { // this._img.active = true; cc.resources.load("battle/" + this._owner.imgName, cc.SpriteFrame, (err, spf) => { // const frame = (spf as cc.SpriteAtlas).getSpriteFrame(this._owner.iconName); if (!err) { if (!this._destroyed) { this._img.getComponent(cc.Sprite).spriteFrame = spf as cc.SpriteFrame; this.onImageLoaded(); } } else { console.error(err.message); } }); } else { this._img.active = false; } } protected onVisibleChange() { } protected onPositionChange(x: number, y: number) { this._img.setPosition(x + this._imgOffset.x, y + this._imgOffset.y); this.node.setPosition(x, y); let visible = (this._owner.gridPos.y > this._owner.map.viewSplitRow); if (this._visible != visible) { this._visible = visible; this.setColliderEnable(visible); this._img.active = visible; this.onVisibleChange(); } } //#endregion //#region private method //#endregion }