Iavra

Iavra Achievement - Menu

Dec 2nd, 2015 (edited)
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Adds a new scene, that provides an overview over all achievements.
  3.  * <Iavra Achievement Menu>
  4.  * @author Iavra
  5.  *
  6.  * @param Title Menu Option
  7.  * @desc Label to display in the title menu. If left empty, the menu won't be accessible from there. Default: Achievements
  8.  * @default Achievements
  9.  *
  10.  * @param Game Menu Option
  11.  * @desc Label to display in the game menu. If left empty, the menu won't be accessible from there. Default: Achievements
  12.  * @default Achievements
  13.  *
  14.  * @param Secret Placeholder
  15.  * @desc Placeholder to be used for titles of secret achievements. Leave empty to completely hide them. Default: ???
  16.  * @default ???
  17.  *
  18.  * @param Variable Gauge Background
  19.  * @desc Background Color to be used for the progress gauge of variable achievements, in hex: Default: #000
  20.  * @default #000
  21.  *
  22.  * @param Variable Gauge Fill
  23.  * @desc Color to be used for the filled part of the progress gauge of variable achievements, in hex. Default: #FF0000
  24.  * @default #FF0000
  25.  *
  26.  * @help
  27.  * This adds a simple menu to the game, that can be used to view achievement progress.
  28.  *
  29.  * Everything about the menu is available for public, so it's easy to modify it or simple create a new one, since the whole
  30.  * achievement logic itself is contained in the core plugin.
  31.  */
  32.  
  33. var Imported = Imported || {};
  34. if(!Imported.iavra_achievement_core) { throw new Error("This plugin needs 'Iavra Achievement - Core' to work."); }
  35. Imported.iavra_achievement_menu = true;
  36.  
  37. //=============================================================================
  38. // namespace IAVRA
  39. //=============================================================================
  40.  
  41. var IAVRA = IAVRA || {};
  42.  
  43. (function() {
  44.     "use strict";
  45.    
  46.     /**
  47.      * Load plugin parameters independently from the plugin's file name.
  48.      */
  49.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Achievement Menu>'); })[0].parameters;
  50.     var _param_titleMenu = _params['Title Menu Option'];
  51.     var _param_gameMenu = _params['Game Menu Option'];
  52.     var _param_secretPlaceholder = _params['Secret Placeholder'];
  53.     var _param_gaugeBackground = (/(#(?:[A-F0-9]{6}|[A-F0-9]{3}))/i.exec(_params['Variable Gauge Background']) || [])[1];
  54.     var _param_gaugeFill = (/(#(?:[A-F0-9]{6}|[A-F0-9]{3}))/i.exec(_params['Variable Gauge Fill']) || [])[1];
  55.    
  56.     /**
  57.      * Width and height of the gauge used to display the progress of certain achievements.
  58.      */
  59.     var _variableGauge = {w: 200, h: 10};
  60.    
  61.     //=============================================================================
  62.     // module IAVRA.ACHIEVEMENT.MENU
  63.     //=============================================================================
  64.    
  65.     IAVRA.ACHIEVEMENT.MENU = {
  66.         /**
  67.          * The scene used to display the menu on screen.
  68.          */
  69.         Scene_Menu: function() { this.initialize.apply(this, arguments); },
  70.         /**
  71.          * A window containing all achievements.
  72.          */
  73.         Window_AchievementList: function() { this.initialize.apply(this, arguments); }
  74.     };
  75.    
  76.     //=============================================================================
  77.     // class IAVRA.ACHIEVEMENT.MENU.Scene_Menu
  78.     //=============================================================================
  79.    
  80.     (function($) {
  81.         ($.prototype = Object.create(Scene_MenuBase.prototype)).constructor = $;
  82.        
  83.         /**
  84.          * The scene consists of 2 windows: The achievement list and a help window displaying the achievement description.
  85.          */
  86.         $.prototype.create = function() {
  87.             Scene_MenuBase.prototype.create.call(this);
  88.             this.createHelpWindow();
  89.             this.createAchievementWindow();
  90.         };
  91.        
  92.         /**
  93.          * The window is simple, since there is no action involved, so we only need to register the help window.
  94.          */
  95.         $.prototype.createAchievementWindow = function() {
  96.             var window = new IAVRA.ACHIEVEMENT.MENU.Window_AchievementList(this._helpWindow.height);
  97.             window.setHelpWindow(this._helpWindow);
  98.             window.setHandler('cancel', this.popScene.bind(this));
  99.             this.addWindow((this._windowAchievementList = window));
  100.         };
  101.    
  102.     })(IAVRA.ACHIEVEMENT.MENU.Scene_Menu);
  103.    
  104.     //=============================================================================
  105.     // class IAVRA.ACHIEVEMENT.MENU.Window_AchievementList
  106.     //=============================================================================
  107.    
  108.     (function($) {
  109.         ($.prototype = Object.create(Window_Command.prototype)).constructor = $;
  110.        
  111.         /**
  112.          * Position the window relative to the help window, so they don't overlap.
  113.          */
  114.         $.prototype.initialize = function(offset) {
  115.             this._offset = offset;
  116.             Window_Command.prototype.initialize.call(this, 0, offset);
  117.         };
  118.        
  119.         /**
  120.          * Fill out the whole screen.
  121.          */
  122.         $.prototype.windowWidth = function() {
  123.             return Graphics.boxWidth;
  124.         };
  125.        
  126.         /**
  127.          * Fill out the remaining screen.
  128.          */
  129.         $.prototype.windowHeight = function() {
  130.             return Graphics.boxHeight - this._offset;
  131.         };
  132.        
  133.         /**
  134.          * Register a command for each achievement. I the parameter "Secret Placeholder" is empty, leave out secret, not
  135.          * yet completed ones.
  136.          */
  137.         $.prototype.makeCommandList = function() {
  138.             var achievements = IAVRA.ACHIEVEMENT.achievements(), achievement;
  139.             for(var i = 0, max = achievements.length; i < max; ++i) {
  140.                 achievement = achievements[i];
  141.                 if(!_param_secretPlaceholder && achievement.secret && !achievement.completed) { continue; }
  142.                 this.addCommand(achievement.title, 'achievement', achievement.completed, achievement);
  143.             };
  144.         };
  145.        
  146.         /**
  147.          * An achievement consists of an icon, a title and maybe a progress gauge, depending on the trigger type.
  148.          */
  149.         $.prototype.drawItem = function(index) {
  150.             var rect = this.itemRectForText(index), achievement = this._list[index].ext;
  151.             this.resetTextColor();
  152.             this.changePaintOpacity(this.isCommandEnabled(index));
  153.             this.drawAchievementIcon(achievement, rect);
  154.             this.drawAchievementTitle(achievement, rect);
  155.             this.drawAchievementProgress(achievement, rect);
  156.         };
  157.        
  158.         /**
  159.          * Simply draw the icon belonging to that achievement.
  160.          */
  161.         $.prototype.drawAchievementIcon = function(achievement, rect) {
  162.             this.drawIcon(achievement.icon, rect.x + 2, rect.y + 2);
  163.         };
  164.        
  165.         /**
  166.          * Draw the achievement title or our placeholder text, if the achievement is secret and not yet completed.
  167.          */
  168.         $.prototype.drawAchievementTitle = function(achievement, rect) {
  169.             var text = achievement.secret && !achievement.completed ? _param_secretPlaceholder : achievement.title;
  170.             this.drawText(text, rect.x + 2 + this.textWidth(' ') + Window_Base._iconWidth, rect.y);
  171.         };
  172.        
  173.         /**
  174.          * If we are dealing with a non-secret, variable achievement, draw a progress gauge.
  175.          */
  176.         $.prototype.drawAchievementProgress = function(achievement, rect) {
  177.             if(achievement.secret && !achievement.completed) { return; }
  178.             if(achievement instanceof IAVRA.ACHIEVEMENT.Achievement_Variable) {
  179.                 var x = rect.width - _variableGauge.w, y = rect.y + (rect.height - _variableGauge.h) / 2;
  180.                 var progress = achievement.completed ? 1 : $gameVariables.value(achievement._variableId) / achievement._variableValue;
  181.                 this.contents.fillRect(x, y, _variableGauge.w, _variableGauge.h, _param_gaugeBackground);
  182.                 this.contents.fillRect(x, y, Math.floor(_variableGauge.w * progress), _variableGauge.h, _param_gaugeFill);
  183.             }
  184.         };
  185.        
  186.         /**
  187.          * Display the description for the currently selected achievement or our placeholder, if it's secret and not yet completed.
  188.          */
  189.         $.prototype.updateHelp = function() {
  190.             Window_Command.prototype.updateHelp.call(this);
  191.             var achievement = this.currentExt();
  192.             var text = achievement.secret && !achievement.completed ? _param_secretPlaceholder : achievement.description;
  193.             if(this._helpWindow) { this._helpWindow.setText(text); }
  194.         };
  195.    
  196.     })(IAVRA.ACHIEVEMENT.MENU.Window_AchievementList);
  197.    
  198.     //=============================================================================
  199.     // class Scene_Title
  200.     //=============================================================================
  201.    
  202.     (function($) {
  203.    
  204.         /**
  205.          * If the parameter was left empty, we don't have to do anything, here.
  206.          */
  207.         if(!_param_titleMenu) { return; }
  208.        
  209.         /**
  210.          * Register the command to show our menu scene.
  211.          */
  212.         var alias_createCommandWindow = $.prototype.createCommandWindow;
  213.         $.prototype.createCommandWindow = function() {
  214.             alias_createCommandWindow.call(this);
  215.             this._commandWindow.setHandler('_iavra_achievement', this._iavra_commandAchievements.bind(this));
  216.         };
  217.        
  218.         /**
  219.          * The actual command function.
  220.          */
  221.         $.prototype._iavra_commandAchievements = function() {
  222.             this._commandWindow.close();
  223.             SceneManager.push(IAVRA.ACHIEVEMENT.MENU.Scene_Menu);
  224.         };
  225.    
  226.     })(Scene_Title);
  227.    
  228.     //=============================================================================
  229.     // class Window_TitleCommand
  230.     //=============================================================================
  231.    
  232.     (function($) {
  233.    
  234.         /**
  235.          * If the parameter was left empty, we don't have to do anything, here.
  236.          */
  237.         if(!_param_titleMenu) { return; }
  238.        
  239.         /**
  240.          * Actually draw the command on-screen.
  241.          */
  242.         var alias_makeCommandList = $.prototype.makeCommandList;
  243.         $.prototype.makeCommandList = function() {
  244.             alias_makeCommandList.call(this);
  245.             this.addCommand(_param_titleMenu, '_iavra_achievement');
  246.         };
  247.        
  248.     })(Window_TitleCommand);
  249.    
  250.     //=============================================================================
  251.     // class Scene_Menu
  252.     //=============================================================================
  253.    
  254.     (function($) {
  255.    
  256.         /**
  257.          * If the parameter was left empty, we don't have to do anything, here.
  258.          */
  259.         if(!_param_gameMenu) { return; }
  260.    
  261.         /**
  262.          * Register the command to show our menu scene.
  263.          */
  264.         var alias_createCommandWindow = $.prototype.createCommandWindow;
  265.         $.prototype.createCommandWindow = function() {
  266.             alias_createCommandWindow.call(this);
  267.             this._commandWindow.setHandler('_iavra_achievement', this._iavra_commandAchievements.bind(this));
  268.         };
  269.    
  270.         /**
  271.          * The actual command function.
  272.          */
  273.         $.prototype._iavra_commandAchievements = function() {
  274.             SceneManager.push(IAVRA.ACHIEVEMENT.MENU.Scene_Menu);
  275.         };
  276.    
  277.     })(Scene_Menu);
  278.    
  279.     //=============================================================================
  280.     // Window_MenuCommand
  281.     //=============================================================================
  282.    
  283.     (function($) {
  284.    
  285.         /**
  286.          * If the parameter was left empty, we don't have to do anything, here.
  287.          */
  288.         if(!_param_gameMenu) { return; }
  289.    
  290.         /**
  291.          * Actually draw the command on-screen.
  292.          */
  293.         var alias_addOptionsCommand = $.prototype.addOptionsCommand;
  294.         $.prototype.addOptionsCommand = function() {
  295.             alias_addOptionsCommand.call(this);
  296.             this.addCommand(_param_gameMenu, '_iavra_achievement');
  297.         };
  298.    
  299.     })(Window_MenuCommand);
  300.    
  301. })();
Add Comment
Please, Sign In to add comment