Project1
标题:
[不怎么成功的尝试]增强接近玩家使其能绕开障碍
[打印本页]
作者:
wtyliangting
时间:
2020-8-28 22:39
标题:
[不怎么成功的尝试]增强接近玩家使其能绕开障碍
但是这里并没有用什么诸如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[passedXs.length] = x;
passedYs[passedYs.length] = 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[i] == x2 && passedYs[i] == 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.5)
dirOfNext = 4;
else
dirOfNext = 6;
console.log(dirOfNext);
}
this.moveStraight(_is_passedY);
if (!this.isMovementSucceeded())
{
_xNow = this._x;
_yNow = this._y;
_leftOrRight = Math.random();
if(_leftOrRight<0.5)
{
dirOfNext = sy > 0 ? 8 : 2;
this.moveStraight(4);
if (this.isMovementSucceeded())
{
this.addPassedArray(_xNow,_yNow);
}
}
else
{
dirOfNext = sy > 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.5)
dirOfNext = 8;
else
dirOfNext = 2;
}
this.moveStraight(_is_passedX);
if (!this.isMovementSucceeded())
{
_xNow = this._x;
_yNow = this._y;
_upOrDown = Math.random();
if(_upOrDown<0.5)
{
dirOfNext = sx > 0 ? 4 : 6;
this.moveStraight(8);
if (this.isMovementSucceeded())
{
this.addPassedArray(_xNow,_yNow);
}
}
else
{
dirOfNext = sx > 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);
}
}
}
};
})();
复制代码
MovingTowardPlayerMoreIntelligent.zip
2020-8-28 22:29 上传
点击文件名下载附件
1.54 KB, 下载次数: 60
作者:
汪汪
时间:
2020-9-10 00:19
其实直接用mz默认的寻路就不错,
this.findDirectionTo(x,y)
返回的是一个方向值
之前写过一个mv的事件向实现应该可以参考
https://rpg.blue/thread-405972-1-1.html
并行时
this.moveStraight(this.findDirectionTo(10, 5))
this.pos(10,5)&&this.setMoveRoute(null)
非并行时防卡(卡时跳出)
this.moveStraight(this.findDirectionTo(10, 5));!this.isMovementSucceeded()&&this.setMoveRoute(null)
this.pos(10,5)&&this.setMoveRoute(null)
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1