Advertisement
ezmash

State Add/Remove Commands (MV)

Nov 23rd, 2015
1,609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //==============================================================================
  2. // State Add/Remove commands
  3. // by Shaz
  4. // Last Updated: 2015.11.24
  5. //==============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Execute commands when states are added or removed
  9.  * @author Shaz
  10.  *
  11.  * @help This plugin does not provide plugin commands.
  12.  *
  13.  * State Notes:
  14.  *  onadd: command      - runs command when state is added
  15.  *  onremove: command   - runs command when state is removed
  16.  *
  17.  *  command is a Javascript command that will be evaluated.  The context is
  18.  *  Game_Battler, so any properties and functions available to Game_Battler
  19.  *  and Game_BattlerBase can be used.  Use 'this' to refer to the battler
  20.  *
  21.  * Examples:
  22.  *  onadd: this.gainHp(-50)
  23.  *         to lower the battler's HP by 50 when the state is added
  24.  *  onremove: this.addState(5)
  25.  *         to add state 5 to the battler when the current state is removed
  26.  */
  27.  
  28. (function() {
  29.   var _Game_BattlerBase_clearStates = Game_BattlerBase.prototype.clearStates;
  30.   Game_BattlerBase.prototype.clearStates = function() {
  31.     if (this._states) {
  32.       this._states.forEach(function(stateId) {
  33.         this.stateRemoveCommand(stateId);
  34.       }.bind(this));
  35.     }
  36.     _Game_BattlerBase_clearStates.call(this);
  37.   };
  38.  
  39.   var _Game_BattlerBase_eraseState = Game_BattlerBase.prototype.eraseState;
  40.   Game_BattlerBase.prototype.eraseState = function(stateId) {
  41.     var index = this._states.indexOf(stateId);
  42.     if (index >= 0) {
  43.       this.stateRemoveCommand(stateId);
  44.     }
  45.     _Game_BattlerBase_eraseState.call(this, stateId);
  46.   };
  47.  
  48.   var _Game_BattlerBase_addNewState = Game_BattlerBase.prototype.addNewState;
  49.   Game_BattlerBase.prototype.addNewState = function(stateId) {
  50.     var hadStateBefore = this._states.indexOf(stateId) >= 0;
  51.     _Game_BattlerBase_addNewState.call(this, stateId);
  52.     var hasStateNow = this._states.indexOf(stateId) >= 0;
  53.     if (hasStateNow && !hadStateBefore) {
  54.       this.stateAddCommand(stateId);
  55.     }
  56.   };
  57.  
  58.   Game_BattlerBase.prototype.stateAddCommand = function(stateId) {
  59.     var st = $dataStates[stateId];
  60.     var note = st.note;
  61.     var lines = note.split(/[\r\n]+/);
  62.     lines.forEach(function(line) {
  63.       cmd = /onadd:\s*(.*)/i.exec(line);
  64.       if (cmd) {
  65.         eval(cmd[1]);
  66.       }
  67.     }.bind(this));
  68.   };
  69.  
  70.   Game_BattlerBase.prototype.stateRemoveCommand = function(stateId) {
  71.     var st = $dataStates[stateId];
  72.     var note = st.note;
  73.     var lines = note.split(/[\r\n]+/);
  74.     lines.forEach(function(line) {
  75.       cmd = /onremove:\s*(.*)/i.exec(line);
  76.       if (cmd) {
  77.         eval(cmd[1]);
  78.       }
  79.     }.bind(this));
  80.   };
  81. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement