//=============================================================================
// DiscoverThePlayer.js
//=============================================================================
/*:
* @plugindesc NPC视野算法插件
* @author 魏玉龙
* @since 2018.09.17
* @version 1.0
*
* @param range
* @desc 视野范围
* @default 5
*
* @param Enc-None
* @desc 开启不遇敌时,NPC是否逃离?true 逃离,false 不逃离
* @default true
*
* @param flee
* @desc 空闲时的状态 0:静止不动 1:随机移动
* @default 0
*
* @help
* 此插件为NPC提供了视野算法,当玩家出现在NPC视野内才会被发现并迅速靠近。
*
* 此插件提供以下插件命令
*
* DTP range number
* DTP encNone true/false
* DTP flee number
*
*/
(function () {
var parameters = PluginManager.parameters('DiscoverThePlayer');
var config = {
range: Number(parameters['range'] || 5),
encNone: JSON.parse(parameters['Enc-None']),
flee: Number(parameters['flee'] || 0)
};
var _Game_Event_initMembers = Game_Event.prototype.initMembers;
Game_Event.prototype.initMembers = function () {
_Game_Event_initMembers.call(this);
this._discover = false;
};
Game_Event.prototype.moveTypeTowardPlayer = function () {
var page = this.page();
var direction = this.direction();
if (!this._discover && this.isNearThePlayer() && this.isDiscoverThePlayer()) {
this._discover = true;
AudioManager.playSe({
name: 'Jump1',
volume: 75,
pitch: 85,
pan: 0
});
this.requestBalloon(1);
this.setMoveSpeed($gamePlayer.moveSpeed());
this.setMoveFrequency($gamePlayer.moveFrequency());
}
if (this._discover && this.isNearThePlayer()) {
if ($gameSystem.isEncounterEnabled()) {
this.moveTowardPlayer();
} else if (config.encNone) {
this.moveAwayFromPlayer();
}
} else {
this._discover = false
this.setMoveSpeed(page.moveSpeed);
this.setMoveFrequency(page.moveFrequency);
if (config.flee === 1) {
var d = [2,4,6,8].filter(v=>v!=(10-direction));
var i = Math.randomInt(3);
this.moveStraight(d[i]);
}
}
};
Game_Event.prototype.isNearThePlayer = function () {
var sx = Math.abs(this.deltaXFrom($gamePlayer.x));
var sy = Math.abs(this.deltaYFrom($gamePlayer.y));
return sx <= config.range && sy <= config.range;
};
Game_Event.prototype.isDiscoverThePlayer = function () {
var direction = this.direction();
var sx = this.deltaXFrom($gamePlayer.x);
var sy = this.deltaYFrom($gamePlayer.y);
switch (direction) {
case 2:
if (sy < 0 && sy >= -(config.range)) {
if (sx < Math.abs(sy) && sx > -(Math.abs(sy))) {
return true;
}
}
break;
case 4:
if (sx > 0 && sx <= config.range) {
if (sy < Math.abs(sx) && sy > -(Math.abs(sx))) {
return true;
}
}
break;
case 6:
if (sx < 0 && sx >= -(config.range)) {
if (sy < Math.abs(sx) && sy > -(Math.abs(sx))) {
return true;
}
}
break;
case 8:
if (sy > 0 && sy <= config.range) {
if (sx < Math.abs(sy) && sx > -(Math.abs(sy))) {
return true;
}
}
break;
}
return false;
};
var alias_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
alias_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === "DTP") {
switch (args[0]) {
case 'range':
config.range = Number(args[1] || 5)
break;
case 'encNone':
config.encNone = JSON.parse(args[1] || 5)
break;
case 'flee':
config.flee = Number(args[1] || 5)
break;
}
}
}
})();