6a05cce4-3331-45d8-9bd4-8ac8bc7355e9.js 6.74 KB
"use strict";
cc._RF.push(module, '6a05czkMzFF2JvUisi8c1Xp', 'ObjBaseView');
// scripts/prefabs/ObjBaseView.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Global_1 = require("../Global");
var TableDefine_1 = require("../kernel/table/TableDefine");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var ObjBaseView = /** @class */ (function (_super) {
    __extends(ObjBaseView, _super);
    function ObjBaseView() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._owner = null;
        /**主体图像 */
        _this._img = null;
        _this._imgOffset = cc.v2();
        _this._needMove = false;
        _this._destroyed = false;
        return _this;
        //#endregion
        //#region private method
        //#endregion
    }
    Object.defineProperty(ObjBaseView.prototype, "owner", {
        get: function () { return this._owner; },
        enumerable: false,
        configurable: true
    });
    //#region interface
    ObjBaseView.prototype.onImgChanged = function () {
        this.updateImg();
    };
    ObjBaseView.prototype.onDroped = function () {
        this.setColliderEnable(false);
    };
    ObjBaseView.prototype.onDead = function () {
        this._img.destroy();
        this.node.destroy();
    };
    ObjBaseView.prototype.onHit = function () {
    };
    //#endregion
    //#region life cycle
    ObjBaseView.prototype.start = function () {
        this.schedule(this.onLazyUpdate, 1 / 20);
    };
    ObjBaseView.prototype.init = function (owner, 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);
    };
    ObjBaseView.prototype.onPreview = function () {
        //预览状态下,在init后被调用
        this._visible = true;
        this._img.active = true;
        this.setColliderEnable(false);
        this.onVisibleChange();
    };
    ObjBaseView.prototype.update = function (dt) {
        if (this._destroyed) {
            return;
        }
        // let needMove = (this.node.position.y != this._owner.pixelPos.y);
        this.onUpdateMoveState();
        if (this._needMove) {
            var speed = this._owner.droped ? 600 : Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 1).Value;
            var step = -speed * dt;
            var 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变量 */
    ObjBaseView.prototype.onUpdateMoveState = function () {
        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);
    //     }
    // }
    ObjBaseView.prototype.onBeginContact = function (contact, selfCollider, otherCollider) {
        if (otherCollider.node.name == "ball") {
            this._owner.onCollisonToBall(otherCollider);
        }
    };
    ObjBaseView.prototype.onDestroy = function () {
        this._destroyed = true;
        this._owner = null;
        this._img = null;
    };
    //#endregion
    //#region public method
    ObjBaseView.prototype.setColliderEnable = function (value) {
        this.node.getComponent(cc.PhysicsCollider).enabled = value;
    };
    //#endregion
    //#region children override 
    ObjBaseView.prototype.onLazyUpdate = function () {
    };
    ObjBaseView.prototype.onImageLoaded = function () {
    };
    ObjBaseView.prototype.updateImg = function () {
        var _this = this;
        if (this._owner.imgName != "") {
            // this._img.active = true;
            cc.resources.load("battle/" + this._owner.imgName, cc.SpriteFrame, function (err, spf) {
                // const frame = (spf as cc.SpriteAtlas).getSpriteFrame(this._owner.iconName);
                if (!err) {
                    if (!_this._destroyed) {
                        _this._img.getComponent(cc.Sprite).spriteFrame = spf;
                        _this.onImageLoaded();
                    }
                }
                else {
                    console.error(err.message);
                }
            });
        }
        else {
            this._img.active = false;
        }
    };
    ObjBaseView.prototype.onVisibleChange = function () {
    };
    ObjBaseView.prototype.onPositionChange = function (x, y) {
        this._img.setPosition(x + this._imgOffset.x, y + this._imgOffset.y);
        this.node.setPosition(x, y);
        var 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();
        }
    };
    ObjBaseView = __decorate([
        ccclass
    ], ObjBaseView);
    return ObjBaseView;
}(cc.Component));
exports.default = ObjBaseView;

cc._RF.pop();