Advertisement
ezmash

Multi Timers (MV)

Nov 13th, 2015
2,890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Multi Timers
  3. // by Shaz
  4. // Last Updated: 2016.11.29
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows more than one timer running at once, with custom commands
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin allows you to set multiple timers at once, along with a command
  13.  * to be executed after the timer expires.  There is no timer display.
  14.  *
  15.  * Plugin Commands:
  16.  *
  17.  * AddTimer key seconds command - sets a timer with key
  18.  * DeleteTimer key - deletes timer with key
  19.  * HasTimer key switchId - turns switch on if a timer with that key is active
  20.  * TimeLeft key varId - sets a variable to the number of seconds remaining
  21.  *
  22.  * key = any numbers/text (no spaces) to identify unique timers
  23.  * seconds = seconds to run
  24.  * command = command to run on expiry (may be any command, and include spaces)
  25.  * switchId = id of switch to set on in HasTimer call if timer exists
  26.  * varId = id of variable to set to seconds remaining on timer
  27.  *
  28.  * The following codes can be used and substituted as follows:
  29.  * <thismap> - id of current map
  30.  * <thisevent> - id of current event
  31.  * \setSS - set self switch
  32.  * \setS - set switch
  33.  * \setV - set variable
  34.  * \ss - self switch value
  35.  * \s - switch value
  36.  * \v - variable value
  37.  *
  38.  * Examples:
  39.  * addTimer opendoor 300 \sets(10, true)
  40.  *    sets a timer for 5 minutes (300 seconds) to turn on switch 10, which
  41.  *    controls the opening of a door
  42.  * addTimer eggsHatched 600 \setv(8, \v(8) + 1)
  43.  *    sets a timer for 10 minutes (600 seconds) to increment variable 8 by 1,
  44.  *    which controls how many eggs have hatched
  45.  * addTimer <thismap>.<thisevent>.A 180 \setss([<thismap>, <thisevent>, 'A'], true)
  46.  *    sets a timer for 3 minutes to turn the current event's self switch A on
  47.  *    the name/key of the timer, if the event is EV008 on map 3, will be 3.8.A
  48.  * deleteTimer opendoor
  49.  *    cancels the timer previously called opendoor (case insensitive)
  50.  * hasTimer <thismap>.<thisevent>.A 15
  51.  *    if run on EV008 on map 3, turns switch 15 on if there is a timer for 3.8.A
  52.  * timeLeft eggsHatched 5
  53.  *    sets variable 5 to the number of seconds remaining before an egg hatches
  54.  *
  55.  *
  56.  * Revisions
  57.  * 2016.11.29  Fixed issue where default timer won't work properly
  58.  */
  59.  
  60. (function() {
  61.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  62.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  63.     switch(command.toUpperCase()) {
  64.       case 'ADDTIMER':
  65.         args = this.subst(args);
  66.         key = args.shift();
  67.         seconds = parseInt(args.shift());
  68.         command = args.join(' ');
  69.         $gameTimer.addTimer(key, seconds, command);
  70.         break;
  71.       case 'DELETETIMER':
  72.         args = this.subst(args);
  73.         key = args.shift();
  74.         $gameTimer.deleteTimer(key);
  75.         break;
  76.       case 'HASTIMER':
  77.         args = this.subst(args);
  78.         key = args.shift();
  79.         switchId = parseInt(args.shift());
  80.         $gameSwitches.setValue(switchId, $gameTimer.hasTimer(key));
  81.         break;
  82.       case 'TIMELEFT':
  83.         args = this.subst(args);
  84.         key = args.shift();
  85.         varId = parseInt(args.shift());
  86.         timeLeft = $gameTimer.hasTimer(key);
  87.         $gameVariables.setValue(varId, timeLeft ? timeLeft : 0);
  88.         break;
  89.       default:
  90.         _Game_Interpreter_pluginCommand.call(this, command, args);
  91.     }
  92.   };
  93.  
  94.   Game_Interpreter.prototype.subst = function(args) {
  95.     args = args.join(' ');
  96.     args = args.replace(/<thismap>/gi, this._mapId.toString());
  97.     args = args.replace(/<thisevent>/gi, this._eventId.toString());
  98.     args = args.replace(/\\setSS/gi, '$gameSelfSwitches.setValue');
  99.     args = args.replace(/\\setV/gi, '$gameVariables.setValue');
  100.     args = args.replace(/\\setS/gi, '$gameSwitches.setValue');
  101.     args = args.replace(/\\SS/gi, '$gameSelfSwitches.value');
  102.     args = args.replace(/\\V/gi, '$gameVariables.value');
  103.     args = args.replace(/\\S/gi, '$gameSwitches.value');
  104.     return args.split(' ');
  105.   };
  106.  
  107.   var _Game_Timer_initialize = Game_Timer.prototype.initialize;
  108.   Game_Timer.prototype.initialize = function() {
  109.     _Game_Timer_initialize.call(this);
  110.     this.initTimer();
  111.   };
  112.  
  113.   Game_Timer.prototype.initTimer = function() {
  114.     if (!this._timers) {
  115.       this._timers = {};
  116.     }
  117.   };
  118.  
  119.   var _Game_Timer_update = Game_Timer.prototype.update;
  120.   Game_Timer.prototype.update = function(sceneActive) {
  121.     _Game_Timer_update.call(this, sceneActive);
  122.     if (sceneActive && this._timers) {
  123.       keys = Object.keys(this._timers);
  124.       keys.forEach(function(timer) {
  125.         this._timers[timer][0]--;
  126.         if (this._timers[timer][0] === 0) {
  127.           eval(this._timers[timer][1]);
  128.           this.deleteTimer(timer);
  129.         }
  130.       }.bind(this));
  131.     }
  132.   };
  133.  
  134.   Game_Timer.prototype.addTimer = function(key, seconds, command) {
  135.     this.initTimer();
  136.     this._timers[key.toUpperCase()] = [seconds * 60, command];
  137.   };
  138.  
  139.   Game_Timer.prototype.deleteTimer = function(key) {
  140.     key = key.toUpperCase();
  141.     if (this._timers && this._timers[key]) {
  142.       delete this._timers[key.toUpperCase()];
  143.     }
  144.   };
  145.  
  146.   Game_Timer.prototype.hasTimer = function(key) {
  147.     key = key.toUpperCase();
  148.     if (this._timers && this._timers[key]) {
  149.       return Math.floor(this._timers[key][0] / 60);
  150.     } else {
  151.       return null;
  152.     }
  153.   };
  154. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement