SHARE
TWEET

Save Event Position (MV)

ezmash Nov 12th, 2015 (edited) 721 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Save Event Position
  3. // by Shaz
  4. // Last Updated: 2015.11.12
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows you to save an event's position and direction
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin lets you set the position and direction where an event will
  13.  * appear the next time the map is loaded.  The commands can also be called
  14.  * via a script call inside of move routes to save/forget the position of
  15.  * the event being moved.
  16.  *
  17.  * This plugin affects events only, not the player or followers, or vehicles.
  18.  *
  19.  * This does not move the event to the specified location - it simply sets the
  20.  * location for the next time the map is loaded.
  21.  *
  22.  * Arguments may be values or formulae to be evaluated (do not include any
  23.  * spaces).  Use this._mapId for the current map, or this._eventId for the
  24.  * current event.
  25.  *
  26.  * Plugin Command:
  27.  * ---------------
  28.  * SavePos - save position for current event
  29.  * SavePos x y
  30.  * SavePos x y dir
  31.  *
  32.  * SaveOtherPos eventId - save position for other event (including other map)
  33.  * SaveOtherPos eventId x y
  34.  * SaveOtherPos eventId x y dir
  35.  * SaveOtherPos mapId eventId x y dir
  36.  *
  37.  * ForgetPos - forget current event's position
  38.  * ForgetOtherPos eventId - forget another event's position (incl other map)
  39.  * ForgetOtherPos mapId eventId
  40.  *
  41.  * Script Call within Move Route (current event only):
  42.  * ---------------------------------------------------
  43.  * this.savePos()
  44.  * this.savePos(x, y)
  45.  * this.savePos(x, y, dir)
  46.  *
  47.  * this.forgetPos()
  48.  */
  49.  
  50. (function() {
  51.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  52.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  53.  
  54.     switch(command.toUpperCase()) {
  55.       case 'SAVEPOS':
  56.         this.savePos(args);
  57.         break;
  58.       case 'SAVEOTHERPOS':
  59.         this.saveOtherPos(args);
  60.         break;
  61.       case 'FORGETPOS':
  62.         this.forgetPos(args);
  63.         break;
  64.       case 'FORGETOTHERPOS':
  65.         this.forgetOtherPos(args);
  66.         break;
  67.       default:
  68.         _Game_Interpreter_pluginCommand.call(this, command, args);
  69.     }
  70.   };
  71.  
  72.   Game_Interpreter.prototype.savePos = function(args) {
  73.     args.unshift(this._eventId);
  74.     this.saveOtherPos(args);
  75.   };
  76.  
  77.   Game_Interpreter.prototype.saveOtherPos = function(args) {
  78.     // evaluate any formulae
  79.     for (arg = 0; arg < args.length; arg++) {
  80.       args[arg] = eval(args[arg]);
  81.     }
  82.  
  83.     // default any missing arguments
  84.     if (args.length < 5) {
  85.       evt = $gameMap.event(args[0]);
  86.       args.unshift(this._mapId);
  87.     }
  88.  
  89.     mapId = args[0];
  90.     eventId = args[1];
  91.     x = args.length > 2 ? args[2] : evt.x;
  92.     y = args.length > 2 ? args[3] : evt.y;
  93.     dir = args.length > 4 ? args[4] : evt.direction();
  94.  
  95.     // and save
  96.     $gameSystem.saveEventPosition(mapId, eventId, x, y, dir);
  97.   };
  98.  
  99.   Game_Interpreter.prototype.forgetPos = function(args) {
  100.     args.unshift(this._eventId);
  101.     this.forgetOtherPos(args);
  102.   };
  103.  
  104.   Game_Interpreter.prototype.forgetOtherPos = function(args) {
  105.     // evaluate any formulae
  106.     for (arg = 0; arg < args.length; arg++) {
  107.       args[arg] = eval(args[arg]);
  108.     }
  109.  
  110.     // default any missing arguments
  111.     mapId = args.length > 1 ? args[0] : this._mapId;
  112.     eventId = args[args.length - 1];
  113.  
  114.     // and forget
  115.     $gameSystem.forgetEventPosition(args);
  116.   };
  117.  
  118.   Game_System.prototype.saveEventPosition = function(mapId, eventId, x, y, dir) {
  119.     this.checkEventPositionsExist();
  120.     key = [mapId, eventId];
  121.     value = [x, y, dir];
  122.     this._eventPositions[key] = value;
  123.   };
  124.  
  125.   Game_System.prototype.forgetEventPosition = function(mapId, eventId) {
  126.     this.checkEventPositionsExist();
  127.     key = [mapId, eventId];
  128.     if (this._eventPositions[key]) {
  129.       delete this._eventPositions[key];
  130.     }
  131.   };
  132.  
  133.   Game_System.prototype.eventPosition = function(mapId, eventId) {
  134.     this.checkEventPositionsExist();
  135.     key = [mapId, eventId];
  136.     return this._eventPositions[key] || null;
  137. };
  138.  
  139.   Game_System.prototype.checkEventPositionsExist = function() {
  140.     if (!this._eventPositions) {
  141.       this._eventPositions = {};
  142.     }
  143.   };
  144.  
  145.   Game_Character.prototype.savePos = function(x, y, dir) {
  146.     // only usable by events
  147.     if (this.constructor === Game_Event) {
  148.       // default any missing arguments
  149.       mapId = $gameMap.mapId();
  150.       eventId = this._eventId;
  151.       if (!x) { x = this._x };
  152.       if (!y) { y = this._y };
  153.       if (!dir) { dir = this._direction };
  154.  
  155.       // and save
  156.       $gameSystem.saveEventPosition(mapId, eventId, x, y, dir);
  157.     }
  158.   };
  159.  
  160.   Game_Character.prototype.forgetPos = function() {
  161.     // only usable by events
  162.     if (this.constructor === Game_Event) {
  163.       $gameSystem.forgetEventPosition($gameMap.mapId(), this._eventId);
  164.     }
  165.   };
  166.  
  167.   var _Game_Event_initialize = Game_Event.prototype.initialize;
  168.   Game_Event.prototype.initialize = function(mapId, eventId) {
  169.     _Game_Event_initialize.call(this, mapId, eventId);
  170.     savedPosition = $gameSystem.eventPosition(mapId, eventId);
  171.     if (savedPosition) {
  172.       this.setPosition(savedPosition[0], savedPosition[1]);
  173.       this.setDirection(savedPosition[2]);
  174.     }
  175.   };
  176. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top