Iavra

Iavra Achievement - Popup

Dec 8th, 2015 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Displays a popup, whenever a new achievement is completed.
  3.  * <Iavra Achievement Popup>
  4.  * @author Iavra
  5.  *
  6.  * @param Popup Width
  7.  * @desc Width of the popup window. Default: 500
  8.  * @default 500
  9.  *
  10.  * @param Fade In
  11.  * @desc How long it takes for the popup to fade in, in frames. Default: 30
  12.  * @default 30
  13.  *
  14.  * @param Fade Out
  15.  * @desc How long it takes for the popup to fade out, in frames. Default: 100
  16.  * @default 100
  17.  *
  18.  * @param Duration
  19.  * @desc How long the popup is visible after fading in and before starting to fade out. Default: 200
  20.  * @default 200
  21.  *
  22.  * @param Font Size
  23.  * @desc The font size to be used. Leave empty to use the global default. Default: (empty)
  24.  * @default
  25.  *
  26.  * @param Font Name
  27.  * @desc The font to be used. Leave empty to use the global default. Default: (empty)
  28.  * @default
  29.  *
  30.  * @param Padding
  31.  * @desc Window padding to be used. Leave empty to use the global default. Default: (empty)
  32.  * @default
  33.  *
  34.  * @param Windowskin
  35.  * @desc The windowskin to be used. Leave empty to use the global default. Default: (empty)
  36.  * @default
  37.  *
  38.  * @help
  39.  * This adds simple popups to the game, that get displayed, whenever an achievement gets completed.
  40.  *
  41.  * Everything about the popups is available for public, so it's easy to modify it or simple create new ones, since the whole
  42.  * achievement logic itself is contained in the core plugin.
  43.  */
  44.  
  45. var Imported = Imported || {};
  46. if(!Imported.iavra_achievement_core) { throw new Error("This plugin needs 'Iavra Achievement - Core' to work."); }
  47. Imported.iavra_achievement_popup = true;
  48.  
  49. //=============================================================================
  50. // namespace IAVRA
  51. //=============================================================================
  52.  
  53. var IAVRA = IAVRA || {};
  54.  
  55. (function() {
  56.     "use strict";
  57.    
  58.     /**
  59.      * Load plugin parameters independently from the plugin's file name.
  60.      */
  61.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Achievement Popup>'); })[0].parameters;
  62.     var _param_width = Math.max(parseInt(_params['Popup Width']) || 0, 0);
  63.     var _param_fadeIn = Math.max(parseInt(_params['Fade In']) || 0, 0);
  64.     var _param_fadeOut = Math.max(parseInt(_params['Fade Out']) || 0, 0);
  65.     var _param_duration = Math.max(parseInt(_params['Duration']) || 0, 0);
  66.     var _param_fontSize = (function($) { return $ === '' ? null : parseInt($) || 0; })(_params['Font Size'].trim());
  67.     var _param_fontName = _params['Font Name'];
  68.     var _param_padding = (function($) { return $ === '' ? null : parseInt($) || 0; })(_params['Padding'].trim());
  69.     var _param_windowskin = _params['Windowskin'];
  70.    
  71.     /**
  72.      * Instance of Sprite_Container, that holds all currently displaying popups.
  73.      */
  74.     var _container;
  75.    
  76.     //=============================================================================
  77.     // module IAVRA.ACHIEVEMENT.POPUP
  78.     //=============================================================================
  79.    
  80.     IAVRA.ACHIEVEMENT.POPUP = {
  81.         /**
  82.          * A simple container used to group all popups together. Currently, that's just a simple Sprite, but i decided to make
  83.          * it its own class, so it can be extended in other plugins.
  84.          */
  85.         Sprite_Container: function() { this.initialize.apply(this, arguments); },
  86.         /**
  87.          * Window used to display a popup.
  88.          */
  89.         Window_Popup: function() { this.initialize.apply(this, arguments); }
  90.     };
  91.    
  92.     //=============================================================================
  93.     // class IAVRA.ACHIEVEMENT.POPUP.Sprite_Container
  94.     //=============================================================================
  95.  
  96.     (function($) {
  97.         ($.prototype = Object.create(Sprite.prototype)).constructor = $;        
  98.     })(IAVRA.ACHIEVEMENT.POPUP.Sprite_Container);
  99.    
  100.     //=============================================================================
  101.     // class IAVRA.ACHIEVEMENT.POPUP.Window_Popup
  102.     //=============================================================================
  103.    
  104.     (function($) {
  105.         ($.prototype = Object.create(Window_Base.prototype)).constructor = $;
  106.        
  107.         /**
  108.          * Cretae a new popup with the given achievement.
  109.          */
  110.         $.prototype.initialize = function(achievement) {
  111.             Window_Base.prototype.initialize.call(this, 0, 0, _param_width, this.fittingHeight(1));
  112.             this.drawAchievementIcon(achievement.icon);
  113.             this.drawAchievementText(achievement.title);
  114.             this._fadeIn = _param_fadeIn;
  115.             this._fadeOut = _param_fadeOut;
  116.             this._duration = _param_duration;
  117.             this.opacity = this.contentsOpacity = 0;
  118.         };
  119.        
  120.         /**
  121.          * Draw the achievement icon.
  122.          */
  123.         $.prototype.drawAchievementIcon = function(icon) {
  124.             this.drawIcon(icon, 2, 2);
  125.         };
  126.        
  127.         /**
  128.          * Draw the achievement title.
  129.          */
  130.         $.prototype.drawAchievementText = function(text) {
  131.             this.drawText(text, 2 + this.textWidth(' ') + Window_Base._iconWidth, 0);
  132.         };
  133.        
  134.         /**
  135.          * On update, calculate the position of the popup. Afterwards, either fade in or out the popup or count down its
  136.          * remaining duration. Once a popup duration has expired, it is removed from the container.
  137.          */
  138.         $.prototype.update = function() {
  139.             Window_Base.prototype.update.call(this);
  140.             this.updatePosition();
  141.             this.updateFadeIn() || this.updateDuration() || this.updateFadeOut() || _container.removeChild(this);
  142.         };
  143.        
  144.         /**
  145.          * The position of a popup depends on all popups added afterwards. This way, new popups are added on top, pushing
  146.          * older ones down, so they don't overlap.
  147.          */
  148.         $.prototype.updatePosition = function() {
  149.             var children = _container.children, thisIndex = children.indexOf(this);
  150.             this.y = children.filter(function(child, index) { return index > thisIndex; }).reduce(function(sum, child) {
  151.                 return sum + child.height; },
  152.             0);
  153.         };
  154.        
  155.         /**
  156.          * Gradually fade in the popup over its fade in duration.
  157.          */
  158.         $.prototype.updateFadeIn = function() {
  159.             if(this._fadeIn-- > 0) {
  160.                 this.opacity = this.contentsOpacity = 255 * (1 - (this._fadeIn / _param_fadeIn));
  161.                 return true;
  162.             }
  163.             return false;
  164.         };
  165.        
  166.         /**
  167.          * Wait, until the popup duration has been completed. Also set the popup's opacity to 255 in case the fade in
  168.          * duration was specified as 0.
  169.          */
  170.         $.prototype.updateDuration = function() {
  171.             return this._duration-- > 0 && (this.opacity = this.contentsOpacity = 255);  
  172.         };
  173.        
  174.         /**
  175.          * Gradually fade out the popup over its fade out duration.
  176.          */
  177.         $.prototype.updateFadeOut = function() {
  178.             if(this._fadeOut-- > 0) {
  179.                 this.opacity = this.contentsOpacity = 255 - 255 * (1 - (this._fadeOut / _param_fadeOut));
  180.                 return true;
  181.             }
  182.             return false;
  183.         };
  184.        
  185.         $.prototype.standardFontSize = function() {
  186.             return _param_fontSize === null ? Window_Base.prototype.standardFontSize.call(this) : _param_fontSize;
  187.         };
  188.        
  189.         $.prototype.standardFontFace = function() {
  190.             return !_param_fontName ? Window_Base.prototype.standardFontFace.call(this) : _param_fontName;
  191.         };
  192.        
  193.         $.prototype.standardPadding = function() {
  194.             return _param_padding === null ? Window_Base.prototype.standardPadding.call(this) : _param_padding;
  195.         };
  196.        
  197.         $.prototype.standardPadding = function() {
  198.             return _param_padding === null ? Window_Base.prototype.standardPadding.call(this) : _param_padding;
  199.         };
  200.        
  201.         $.prototype.loadWindowskin = function() {
  202.             if(!_param_windowskin) {
  203.                 Window_Base.prototype.loadWindowskin.call(this);
  204.             } else {
  205.                 this.windowskin = ImageManager.loadSystem(_param_windowskin);
  206.             }
  207.         };
  208.        
  209.     })(IAVRA.ACHIEVEMENT.POPUP.Window_Popup);
  210.    
  211.     //=============================================================================
  212.     // class IAVRA.ACHIEVEMENT.Achievement
  213.     //=============================================================================
  214.  
  215.     (function($) {
  216.        
  217.         /**
  218.          * When an achievement gets completed, add a new popup to the container.
  219.          */
  220.         var alias_onComplete = $.prototype.onComplete;
  221.         $.prototype.onComplete = function() {
  222.             alias_onComplete.call(this);
  223.             if(_container) { _container.addChild(new IAVRA.ACHIEVEMENT.POPUP.Window_Popup(this)); }
  224.         };
  225.        
  226.     })(IAVRA.ACHIEVEMENT.Achievement);
  227.    
  228.     //=============================================================================
  229.     // Scene_Base
  230.     //=============================================================================
  231.    
  232.     (function($) {
  233.        
  234.         /**
  235.          * Popups are displayed above the WindowLayer.
  236.          */
  237.         var alias_createWindowLayer = $.prototype.createWindowLayer;
  238.         $.prototype.createWindowLayer = function() {
  239.             alias_createWindowLayer.call(this);
  240.             this.addChild(_container = new IAVRA.ACHIEVEMENT.POPUP.Sprite_Container);
  241.         };
  242.        
  243.         /**
  244.          * At the end of a scene, clear all popups.
  245.          */
  246.         var alias_terminate = $.prototype.terminate;
  247.         $.prototype.terminate = function() {
  248.             alias_terminate.call(this);
  249.             if(_container) { _container.removeChildren(); }
  250.         };
  251.        
  252.     })(Scene_Base);
  253.    
  254. })();
Add Comment
Please, Sign In to add comment