赞 | 0 |
VIP | 0 |
好人卡 | 0 |
积分 | 4 |
经验 | 0 |
最后登录 | 2024-10-14 |
在线时间 | 68 小时 |
Lv2.观梦者
- 梦石
- 0
- 星屑
- 356
- 在线时间
- 68 小时
- 注册时间
- 2020-7-17
- 帖子
- 8
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
但是这里并没有用什么诸如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);
- }
- }
- }
- };
- })();
复制代码 |
|