ezmash

Smart Path (MV)

Oct 24th, 2015
8,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Smart Pathfinding
  3. // by Shaz
  4. // Last Updated: 2015.10.21
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows events or players to do smart Pathfinding
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  *
  13.  * Plugin Command:
  14.  *  SmartPath eventId1 eventId2      # Makes event 1 find path to event 2
  15.  *  SmartPath eventId x y            # Makes event find a path to location x, y
  16.  *  SmartPath eventId cancel         # Cancel Pathfinding for event
  17.  *
  18.  *  event = number for specific event
  19.  *  event = 0 for "this" event
  20.  *  event = -1 for player
  21.  *  event = $gameVariables.value(x) to get the event id from variable x
  22.  *
  23.  *  x, y = coordinates or $gameVariables.value(#) to use a variable coordinate
  24.  *
  25.  */
  26.  
  27. (function() {
  28.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  29.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  30.     _Game_Interpreter_pluginCommand.call(this, command, args);
  31.  
  32.     if (command.toUpperCase() === 'SMARTPATH') {
  33.       subject = this.character(eval(args[0]));
  34.       if (args[1].toUpperCase() === 'CANCEL') {
  35.         subject.clearTarget();
  36.       } else if (args.length > 2) {
  37.         subject.setTarget(null, eval(args[1]), eval(args[2]));
  38.       } else {
  39.         subject.setTarget(this.character(eval(args[1])));
  40.       }
  41.     }
  42.   };
  43.  
  44.   var _Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
  45.   Game_CharacterBase.prototype.initMembers = function() {
  46.     _Game_CharacterBase_initMembers.call(this);
  47.     this._target = null;
  48.     this._targetX = null;
  49.     this._targetY = null;
  50.   };
  51.  
  52.   Game_CharacterBase.prototype.setTarget = function(target, targetX, targetY) {
  53.     this._target = target;
  54.     if (this._target) {
  55.       this._targetX = this._target.x;
  56.       this._targetY = this._target.y;
  57.     } else {
  58.       this._targetX = targetX;
  59.       this._targetY = targetY;
  60.     }
  61.   };
  62.  
  63.   Game_CharacterBase.prototype.clearTarget = function() {
  64.     this._target = null;
  65.     this._targetX = null;
  66.     this._targetY = null;
  67.   };
  68.  
  69.   var _Game_CharacterBase_updateStop = Game_CharacterBase.prototype.updateStop;
  70.   Game_CharacterBase.prototype.updateStop = function() {
  71.     _Game_CharacterBase_updateStop.call(this);
  72.  
  73.     if (this._target) {
  74.       this._targetX = this._target.x;
  75.       this._targetY = this._target.y;
  76.     }
  77.  
  78.     if (this._targetX != null) {
  79.       direction = this.findDirectionTo(this._targetX, this._targetY);
  80.       if (direction > 0)
  81.       {
  82.         this.moveStraight(direction);
  83.       }
  84.     }
  85.   };
  86. })();
Advertisement
Add Comment
Please, Sign In to add comment