const NullBalln = 19; 
let Nupu_PAdd_initMembers = Game_Event.prototype.initialize;
Game_Event.prototype.initialize = function (mapId, eventId) {
    Nupu_PAdd_initMembers.apply(this, arguments);
    this.BallnZureY = -1 * Number(EventMetaGet(eventId, 'NKBz', 0)); 
    this.TCkKyori = Number(EventMetaGet(eventId, 'NKr', 2.5)); 
    this.TKBKyori = Number(EventMetaGet(eventId, 'NKB', 0)); 
    if (this.TKBKyori == 0){
        this.TKBKyori = this.TCkKyori;
    }
    this.TCkTYM = Number(EventMetaGet(eventId, 'TYM', 0.5)); 
    if (this.TCkBlnNo == null) {
        this.TCkBlnNo = Number(EventMetaGet(eventId, 'TB', NullBalln)); 
    }
    this.TcOkFlg = false;
}
Sprite_Character.prototype.startBalloon = function () {
    if (!this._balloonSprite) {
        this._balloonSprite = new Sprite_Balloon();
        if (this._character.BallnZureY != undefined) {
            this._balloonSprite.anchor.y += this._character.BallnZureY;
        }
    }
    this._balloonSprite.setup(this._character.balloonId());
    this.parent.addChild(this._balloonSprite);
};
Game_Event.prototype.pd_TE_CheckTouchEvent = function () {
    this.TcOkFlg = false;
    if (this.__pd_TE.touchTriggerType >= 3) {
        if (TouchInput.isTriggered()) {
            if ($gameTemp._pd_TE_realTouchMapX >= this._realX && $gameTemp._pd_TE_realTouchMapX < this._realX + 1 &&
                $gameTemp._pd_TE_realTouchMapY >= this._realY && $gameTemp._pd_TE_realTouchMapY < this._realY + 1) {
                $gameTemp._pd_TE_touchTriggerTime = Date.now();
            }
        } else if (TouchInput.isReleased() && Date.now() - $gameTemp._pd_TE_touchTriggerTime <= 500) {
            if ($gameTemp._pd_TE_realTouchMapX >= this._realX && $gameTemp._pd_TE_realTouchMapX < this._realX + 1 &&
                $gameTemp._pd_TE_realTouchMapY >= this._realY && $gameTemp._pd_TE_realTouchMapY < this._realY + 1) {
                $gameTemp._pd_TE_touchTriggerTime = 0;
                return true;
            }
        }
    }
    else if (this.__pd_TE.touchTriggerType >= 1) {
        let _ckX = Math.abs(this._realX - PL_realX);
        let _ckY = Math.abs(this._realY - PL_realY);
        if (this.TCkBlnNo == 19 || this.TCkBlnNo == 16) {
            if (this.TKBKyori >= _ckX && this.TKBKyori >= _ckY) {
                this.TcOkFlg = true;
            } else {
                this.TcOkFlg = false;
            }
        } else {
            this.TcOkFlg = true;
        }
        if (this.TCkKyori >= _ckX && this.TCkKyori >= _ckY) {
            if (TouchInput.isTriggered()) {
                if ($gameTemp._pd_TE_realTouchMapX >= this._realX && $gameTemp._pd_TE_realTouchMapX < this._realX + 1 &&
                    $gameTemp._pd_TE_realTouchMapY >= this._realY - this.TCkTYM && $gameTemp._pd_TE_realTouchMapY < this._realY + 1) {
                    if (this._directionFix == false) {
                        this._clkRtnDirection = this.direction(); 
                        this.turnTowardCharacter($gamePlayer);
                    }
                    if ($gamePlayer._directionFix == false) {
                        $gamePlayer.turnTowardCharacter(this);
                    }
                    return true;
                }
            }
        }
    }
    return false;
};
Game_Event.prototype.unlock = function (_TEFlg) { 
    _TEFlg = typeof _TEFlg !== 'undefined' ? _TEFlg : true;
    if (this._locked) {
        this._locked = false;
        this.setDirection(this._prelockDirection);
    }
    if (this._clkRtnDirection != null && _TEFlg) {
        this.setDirection(this._clkRtnDirection);
        this._clkRtnDirection = null;
    }
};
let Nupu_Game_CharacterBase_update = Game_CharacterBase.prototype.update;
Game_CharacterBase.prototype.update = function () {
    Nupu_Game_CharacterBase_update.call(this);
    if (this.TcOkFlg && !$gameMap.isEventRunning()){
        this.requestBalloon(this.TCkBlnNo); 
    }
};
Game_Interpreter.prototype._吹き出しバルーン変更 = function (_バルーン番号) {
    try {
        this.character(0).TCkBlnNo = _バルーン番号;
    } catch (e) { }
}
