[不怎么成功的尝试]增强接近玩家使其能绕开障碍
但是这里并没有用什么诸如B*之类寻路算法,一方面是我怕影响性能,但更重要的是我很懒(我真的不想探究Game_Map.prototype.checkPassage这个里面的东西了www)...请注意,这个虽然能一定程度上使其绕过障碍,但如果你和事件离得很远,或者你们之间直线距离很短但实际距离很长,那这个事件很有可能转圈圈或者左右横跳。
个人第一次写给别人看的代码,我得承认里面确实有很多地方考虑不够,比如那些var究竟会不会有什么坏影响我也吃不准,各位大佬如果要用的话还是可以继续改进的。
//=============================================================================
// RPG Maker MZ - Moving Toward Player More Intelligent
//=============================================================================
/*:
* @target MZ
* @plugindesc Moving toward player more intelligent.
* @author 那只野生loli
*
* @help
*
* This plugin make character moving toward player more intelligent.
* It was rewrited Game_Character.prototype.moveTowardCharacter.
*
*
* It does not provide plugin commands.
*/
(() => {
var passedXs = new Array();
var passedYs = new Array();
var dirOfNext;
var _is_passedX;
var _is_passedY;
var _xNow;
var _yNow;
var _leftOrRight;
var _upOrDown;
//var _Markov;
Game_Character.prototype.addPassedArray = function(x,y)
{
passedXs = x;
passedYs = y;
if (passedXs.length > 3)
passedXs.shift();
if (passedYs.length > 3)
passedYs.shift();
};
Game_Character.prototype.isPassed = function(_x,_y,d)
{
const x2 = $gameMap.roundXWithDirection(_x, d);
const y2 = $gameMap.roundYWithDirection(_y, d);
for (var i = 0; i < passedYs.length; i++)
{
if (passedXs == x2 && passedYs == y2)
{
return true;
}
}
return false;
};
Game_Character.prototype.hasDir = function(dir)
{
if (dir == 2 || dir == 4 || dir == 6|| dir == 8)
return true;
else
return false;
}
Game_Character.prototype.newPath = function(_directOfLength,sx,sy,_x,_y)
{
var dirX = sx > 0 ? 6 : 4
var dirY = sy > 0 ? 2 : 8
var dirOfNextX = sx > 0 ? 4 : 6
var dirOfNextY = sy > 0 ? 8 : 2
if (_directOfLength)
{
this.moveStraight(dirX);
if (!this.isMovementSucceeded())
{
this.moveStraight(dirY);
this.addPassedArray(_x,_y);
return dirOfNextX
}
else
{
this.addPassedArray(_x,_y);
return dirOfNextY
}
}
else
{
this.moveStraight(dirY);
if (!this.isMovementSucceeded())
{
this.moveStraight(dirX);
this.addPassedArray(_x,_y);
return dirOfNextY
}
else
{
this.addPassedArray(_x,_y);
return dirOfNextX
}
}
};
Game_Character.prototype.moveTowardCharacter = function(character)
{
/*
if (Math.random() < 0.05)
{
_Markov = Math.random();
if (_Markov < 0.25)
dirOfNext = 2;
else if(_Markov < 0.5)
dirOfNext = 4;
else if(_Markov < 0.5)
dirOfNext = 6;
else if(_Markov < 0.5)
dirOfNext = 8;
}
*/
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
//console.log(dirOfNext);
//console.log(passedXs);
//console.log(passedYs);
if (this.hasDir(dirOfNext))
{
console.log("0000000000000");
if (this.canPass(this._x, this._y, dirOfNext))
this.moveStraight(dirOfNext);
dirOfNext = 0;
}
else if (sx == 0)
{
_is_passedY = sy > 0 ? 8 : 2;
if (this.isPassed(this._x,this._y,_is_passedY))
{
_is_passedY = sy > 0 ? 2 : 8;
this.addPassedArray(this._x,this._y);
_leftOrRight = Math.random();
if(_leftOrRight 0 ? 8 : 2;
this.moveStraight(6);
if (this.isMovementSucceeded())
{
this.addPassedArray(_xNow,_yNow);
}
}
}
}
else if (sy == 0)
{
_is_passedX = sx > 0 ? 4 : 6;
if (this.isPassed(this._x,this._y,_is_passedX))
{
_is_passedX = sx > 0 ? 6 : 4;
this.addPassedArray(this._x,this._y);
_upOrDown = Math.random();
if(_upOrDown 0 ? 4 : 6;
this.moveStraight(2);
if (this.isMovementSucceeded())
{
this.addPassedArray(_xNow,_yNow);
}
}
}
}
else if (Math.abs(sx) > Math.abs(sy))
{
_is_passedX = sx > 0 ? 4 : 6;
if (this.isPassed(this._x,this._y,_is_passedX))
{
_is_passedX = sx > 0 ? 6 : 4;
this.addPassedArray(this._x,this._y);
dirOfNext = sy > 0 ? 8 : 2;
}
this.moveStraight(_is_passedX);
if (!this.isMovementSucceeded()&& sy !== 0)
{
_is_passedY = (sy > 0 ? 8 : 2);
if (this.isPassed(this._x,this._y,_is_passedY))
{
_is_passedY = sy > 0 ? 2 : 8;
this.addPassedArray(this._x,this._y);
dirOfNext = sx > 0 ? 4 : 6;
}
this.moveStraight(_is_passedY);
if (!this.isMovementSucceeded())
{
dirOfNext = this.newPath(false, sx, sy, this._x, this._y);//强制离开此格
}
}
}
else if (sy !== 0)
{
_is_passedY = sy > 0 ? 8 : 2;
if (this.isPassed(this._x,this._y,_is_passedY))
{
_is_passedY = sy > 0 ? 2 : 8;
this.addPassedArray(this._x,this._y);
dirOfNext = sx > 0 ? 4 : 6;
console.log(dirOfNext);
}
this.moveStraight(_is_passedY);
if (!this.isMovementSucceeded() && sx !== 0)
{
_is_passedX = sx > 0 ? 4 : 6;
if (this.isPassed(this._x,this._y,_is_passedX))
{
_is_passedX = sx > 0 ? 6 : 4;
this.addPassedArray(this._x,this._y);
dirOfNext = sy > 0 ? 8 : 2;
}
this.moveStraight(_is_passedX);
if (!this.isMovementSucceeded())
{
dirOfNext = this.newPath(true,sx, sy,this._x,this._y);
}
}
}
};
})();复制代码
本帖来自P1论坛作者wtyliangting,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=483088若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页:
[1]