Rafael_Sol_Maker

Rafael_Sol_Maker's Timer Expansion MV v1.0

Jan 6th, 2016
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // TimerExpansion.js
  3. // Rafael_Sol_Maker's Timer Expansion MV v1.0
  4. // This work is licensed under a Creative Commons Attribution 4.0 International License.
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Expands the Game Timer Functions. Initial release (v1.0)
  9.  * @author Rafael_Sol_MAKER (www.condadobraveheart.com)
  10.  *
  11.  * @param timerPosition
  12.  * @desc 1:Top left 2:Top center 3:Top right 4:Bottom left 5:Bottom center 6:Bottom right
  13.  * @default 3
  14.  *
  15.  * @param showSecondsOnly
  16.  * @desc Put 1 if you want to show only seconds instead minutes and seconds. 0, otherwise.
  17.  * @default false
  18.  *
  19.  * @param showFractions
  20.  * @desc Put 1 if you want to show fractions of seconds, 0 otherwise.
  21.  * @default 0
  22.  *
  23.  * @param timerPrefix
  24.  * @desc To put some text before the timer.
  25.  * @default Time remaining:
  26.  *
  27.  * @param timerSuffix
  28.  * @desc To put some text after the the timer.
  29.  * @default s
  30.  *
  31.  * @param timerSeparator
  32.  * @desc Separator (for minutes and seconds exhibition only).
  33.  * @default :
  34.  *
  35.  * @param secondsToFlash
  36.  * @desc How many seconds before the time starts to flash. Put -1 to no flash at all.
  37.  * @default 30
  38.  *
  39.  * @param flashIntermittency
  40.  * @desc Frequency in frames that the flash will change its color.
  41.  * @default 20
  42.  *
  43.  * @param flashColor1
  44.  * @desc Color 1 for flash (Uses Windowskin color index)
  45.  * @default 17
  46.  *
  47.  * @param flashColor2
  48.  * @desc Color 2 for flash (Uses Windowskin color index)
  49.  * @default 18
  50.  *
  51.  * @help
  52.  * Plugin Command:
  53.  *   Timer add_time n       # Adds n seconds to timer. Negative values allowed.
  54.  *   Timer pause            # Pauses the timer countdown.
  55.  *   Timer resume           # Resumes the timer countdown, if paused.
  56.  *
  57.  * To start and stop functions, use the original event commands.
  58.  *
  59.  * ---------------------------------------------------------------
  60.  *
  61.  * Some new functions that can be useful for scripters were added as well.
  62.  *
  63.  * Scripting Commands:
  64.  *   $gameTimer.addTime(n); # Adds n seconds to timer. Negative values allowed.
  65.  *   $gameTimer.pause();    # Pauses the timer countdown.
  66.  *   $gameTimer.resume();   # Resumes the timer countdown, if paused.
  67.  *   $gameTimer.count();    # Return the total of frames in the countdown.
  68.  *   $gameTimer.isExpired();# If the Timer runs out, returns true. Returns false otherwise.
  69.  *  
  70.  *   $gameSystem.getWindowskinColor(n);  # The names says it all. n is the index, starting at 0.
  71.  *  
  72.  * (Please observe that now $gameTimer.isWorking() is tied with paused state instead stopped,
  73.  * so, it will be equal to true if the Timer is paused.)
  74.  */
  75.  
  76. /*
  77. #==============================================================================
  78. # ** PARAMETERS, COMMANDS & MISC
  79. #------------------------------------------------------------------------------
  80. #  Let's handle these values first
  81. #==============================================================================
  82. */
  83. (function() {
  84.  
  85.   // First let's have the parameters
  86.   var parameters = PluginManager.parameters('TimerExpansion');
  87.   var timerPrefix = parameters['timerPrefix'] || '';
  88.   var timerSuffix = parameters['timerSuffix'] || '';
  89.   var timerSeparator = parameters['timerSeparator'] || ':';
  90.   var timerPosition = Number(parameters['timerPosition'] || 3);
  91.   var showFractions = !!Number(parameters['showFractions']);
  92.   var showSecondsOnly = !!Number(parameters['showSecondsOnly']);
  93.   var flashIntermittency = Number(parameters['flashIntermittency'] || 20);
  94.   var secondsToFlash = Number(parameters['secondsToFlash'] || 30);
  95.   var flashColor1 = Number(parameters['flashColor1'] || 17);
  96.   var flashColor2 = Number(parameters['flashColor2'] || 18);
  97.  
  98.   // Dealing with our commands here
  99.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  100.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  101.       _Game_Interpreter_pluginCommand.call(this, command, args);
  102.  
  103.       if (command === 'Timer') {
  104.         // Plugin Commands Goes Here
  105.         switch (args[0]) {
  106.         case 'add_time':
  107.             $gameTimer.addTime(Number(args[1]));
  108.             break;
  109.         case 'pause':
  110.             $gameTimer.pause();
  111.             break;
  112.         case 'resume':
  113.             $gameTimer.resume();
  114.             break;
  115.         }
  116.       }
  117.   };
  118.  
  119.   // We will need this defined, anyway...
  120.   Game_System.prototype.getWindowskinColor = function(n) {
  121.     var px = 96 + (n % 8) * 12 + 6;
  122.     var py = 144 + Math.floor(n / 8) * 12 + 6;
  123.     var windowskin = ImageManager.loadSystem($gameSystem.windowskinName || 'Window');
  124.     return windowskin.getPixel(px, py);
  125.   }
  126.  
  127. /*
  128. #==============================================================================
  129. # ** Game_Timer (EXPANSION)
  130. #------------------------------------------------------------------------------
  131. #  Handles timers. Instances of this class are referenced by $gameTimer.
  132. #==============================================================================
  133. */
  134.   var _Game_Timer_initialize = Game_Timer.prototype.initialize
  135.   Game_Timer.prototype.initialize = function() {
  136.     _Game_Timer_initialize.call(this);
  137.     this._expired = false;
  138.     this._stopped = true;
  139.   };
  140.  
  141.   var _Game_Timer_start = Game_Timer.prototype.start;
  142.   Game_Timer.prototype.start = function(count) {
  143.       //_Game_Timer_start.call(this);
  144.       _Game_Timer_start.call(this, count);
  145.       this._stopped = false;
  146.   };
  147.  
  148.   Game_Timer.prototype.pause = function() {
  149.       this._working = false;
  150.   };
  151.  
  152.   Game_Timer.prototype.resume = function() {
  153.       this._working = true;
  154.   };
  155.  
  156.   Game_Timer.prototype.count = function() {
  157.       return this._frames;
  158.   };
  159.  
  160.   var _Game_Timer_stop = Game_Timer.prototype.stop;
  161.   Game_Timer.prototype.stop  = function() {
  162.       _Game_Timer_stop.call(this);
  163.       this._frames = 0;
  164.       this._stopped = true;
  165.   };
  166.  
  167.   Game_Timer.prototype.addTime = function(secs) {
  168.       console.log(Graphics.frameRate);
  169.       this._frames = this._frames + (secs * 60);
  170.   };
  171.  
  172.   Game_Timer.prototype.isExpired = function() {
  173.       return this._expired;
  174.   };
  175.  
  176.   Game_Timer.prototype.stopped = function() {
  177.       return this._stopped;
  178.   };
  179.  
  180.   var _Game_Timer_onExpire = Game_Timer.prototype.onExpire;
  181.   Game_Timer.prototype.onExpire = function() {
  182.     this._expired = true;
  183.     _Game_Timer_onExpire.call(this);      
  184.   };
  185.  
  186. /*
  187. #==============================================================================
  188. # ** Sprite_Timer (MODIFICATION)
  189. #------------------------------------------------------------------------------
  190. #  This sprite is for timer displays. It monitors $gameTimer and automatically
  191. # changes sprite states.
  192. #==============================================================================
  193. */
  194.   var _Sprite_Timer_initialize = Sprite_Timer.prototype.initialize;
  195.   Sprite_Timer.prototype.initialize = function() {
  196.     this._timerPosition = timerPosition;
  197.     this._flashIntermittency = flashIntermittency;
  198.     this._secondsToFlash = secondsToFlash;
  199.     this._flashColor1 = flashColor1;
  200.     this._flashColor2 = flashColor2;
  201.  
  202.     this._prefix = timerPrefix;
  203.     this._suffix = timerSuffix;
  204.     this._separator = timerSeparator;
  205.  
  206.     this._secondsOnly = showSecondsOnly;
  207.     this._showFractions = showFractions;
  208.  
  209.     //this._total_sec = 0;
  210.     this.__alt = false;
  211.  
  212.     _Sprite_Timer_initialize.call(this);
  213.   };
  214.  
  215.   Sprite_Timer.prototype.updateVisibility = function() {
  216.     this.visible = !$gameTimer.stopped();
  217.   };
  218.  
  219.   Sprite_Timer.prototype.updateBitmap = function() {
  220.     if ((Graphics.frameCount) % 4 !== 0) {
  221.       return;
  222.     }
  223.  
  224.     var flash = (Graphics.frameCount % this._flashIntermittency);
  225.  
  226.     if (($gameTimer.count() <= this._secondsToFlash * 60) && $gameTimer.isWorking()) { // if ($gameTimer.seconds() <= this._secondsToFlash) {
  227.       if (flash == 0) {
  228.         this.__alt = !this.__alt;
  229.         if (this.__alt == true) {
  230.           this.bitmap.textColor = $gameSystem.getWindowskinColor(this._flashColor1);
  231.         }
  232.         else{
  233.           this.bitmap.textColor = $gameSystem.getWindowskinColor(this._flashColor2);
  234.         }
  235.       }
  236.     }
  237.     else{
  238.       this.bitmap.textColor = $gameSystem.getWindowskinColor(0);
  239.     }
  240.     this._seconds = $gameTimer.seconds();
  241.     this.redraw();
  242.   };
  243.  
  244.   Sprite_Timer.prototype.timerText = function() {
  245.     var min = Math.floor(this._seconds / 60) % 60;
  246.     var sec = this._seconds % 60;
  247.     var fraction = Math.floor($gameTimer.count() % 100);
  248.  
  249.     if (this._secondsOnly){
  250.       if (this._showFractions){
  251.         return this._prefix + this._seconds + '.' +
  252.                fraction.padZero(2) + this._suffix;
  253.       }
  254.       else{
  255.         return this._prefix + this._seconds + this._suffix;
  256.       }
  257.     }
  258.     else {
  259.       if (this._showFractions){
  260.         return this._prefix + min.padZero(2) + this._separator + sec.padZero(2)
  261.                + '.' + fraction.padZero(2) + this._suffix;
  262.       }
  263.       else{
  264.         return this._prefix + min.padZero(2) + this._separator +
  265.                sec.padZero(2) + this._suffix;
  266.       }
  267.     }
  268.   };
  269.  
  270.   Sprite_Timer.prototype.createBitmap = function() {
  271.     this.bitmap = new Bitmap(96 + 180, 48);
  272.     this.bitmap.fontSize = 32;
  273.   };
  274.  
  275.   Sprite_Timer.prototype.updatePosition = function() {
  276.     // this._timerPosition = (this._timerPosition > 1 && this._timerPosition < 6) ?
  277.                            // this._timerPosition : 3;
  278.     switch (this._timerPosition) {
  279.     case 1:
  280.       this.x = 0;
  281.       this.y = 0;
  282.       break;
  283.     case 2:
  284.       this.x = (Graphics.width / 2) - (this.bitmap.width / 2);
  285.       this.y = 0;
  286.       break;
  287.     case 3:
  288.       this.x = Graphics.width - this.bitmap.width;
  289.       this.y = 0;
  290.       break;
  291.     case 4:
  292.       this.x = 0
  293.       this.y = Graphics.height  - this.bitmap.height;
  294.       break;
  295.     case 5:
  296.       this.x = (Graphics.width / 2) - (this.bitmap.width / 2);
  297.       this.y = Graphics.height  - this.bitmap.height;
  298.       break;
  299.     case 6:
  300.       this.x = Graphics.width - this.bitmap.width;
  301.       this.y = Graphics.height  - this.bitmap.height;  
  302.       break;
  303.     }
  304.   };
  305.  
  306. /*
  307. #==============================================================================
  308. # ** END OF CODE
  309. #==============================================================================
  310. */
  311. })(); //Don't remove this!
Advertisement
Add Comment
Please, Sign In to add comment