Advertisement
modern_algebra

SelectItemCategories

Oct 31st, 2015
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. //  SelectItemCategories.js
  3. //=============================================================================
  4. //  Version: 1.0.0
  5. //  Date: 31 October 2015
  6. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. /*:
  8.  * @plugindesc Create additional categories for the Select Item event command
  9.  * @author Modern Algebra (rmrk.net)
  10.  * @help To set this script up, you first need to assign items to categories.
  11.  * To do that, enter the following code into the notebox of an item:
  12.  *
  13.  *      \select[x]
  14.  *          x : Category ID (must be an integer greater than 0)
  15.  *
  16.  * You can also set an item to more than one category by putting a space and
  17.  * adding more category IDs.
  18.  *
  19.  * Examples:
  20.  *
  21.  *      \select[1]  // This item will show up in category 1.
  22.  *      \select[3]  // This item will show up in category 3.
  23.  *      \select[1 2 14]   // This item shows up in categories 1, 2, and 14
  24.  *
  25.  * To select an item from one of these categories that you create, you need to
  26.  * use the following plugin command
  27.  *
  28.  *      MASelectItem x y
  29.  *          x : Variable ID - integer
  30.  *          y : Category ID - integer
  31.  *
  32.  * It will then work the same way as the regular Select Item command, and the
  33.  * ID of the item that the player selects will be assigned to the chosen
  34.  * variable.
  35.  *
  36.  * Examples:
  37.  *
  38.  *      MASelectItem 4 1
  39.  *          // Items from Category 1 will be shown, and Variable 4 will be set
  40.  *          // to the ID of the item that the player selects.
  41.  */
  42. //=============================================================================
  43.  
  44. (function() {
  45.  
  46.     // Plugin Command : MASelectItem x y
  47.     //      x : ID of the variable to set selection to (integer)
  48.     //      y : ID of the category to select from (integer)
  49.     var _ma_Game_Interpreter_pluginCommand =
  50.             Game_Interpreter.prototype.pluginCommand;
  51.     Game_Interpreter.prototype.pluginCommand = function(command, args) {
  52.         _ma_Game_Interpreter_pluginCommand.call(this, command, args);
  53.         if (command.toLowerCase() === 'maselectitem') {
  54.             return this.maCommandSelectItemByCategory(+args[0], +args[1]);
  55.         }
  56.     };
  57.    
  58.     // Setup Select Item by Category
  59.     Game_Interpreter.prototype.maCommandSelectItemByCategory = function(variableId, itemCategoryId) {
  60.         if (!$gameMessage.isBusy()) {
  61.             $gameMessage.maSelectItemByCategory(variableId, itemCategoryId)
  62.             this.setWaitMode('message');
  63.         }
  64.         return false
  65.     };
  66.    
  67.     // Clear - Initialize Item Category ID
  68.     var _ma_Game_Message_clear =
  69.             Game_Message.prototype.clear;
  70.     Game_Message.prototype.clear = function() {
  71.         _ma_Game_Message_clear.apply(this, arguments);
  72.         this._maSelectItemCategoryId = 0; // Defauult is 0
  73.     };
  74.    
  75.     // maSelectItemCategoryId - make category ID publicly accessible
  76.     Game_Message.prototype.maSelectItemCategoryId = function() {
  77.         return this._maSelectItemCategoryId;
  78.     };
  79.    
  80.     // Game_Message - Setup Item by Category Selection
  81.     Game_Message.prototype.maSelectItemByCategory = function(variableId, itemCategoryId) {
  82.         this._itemChoiceVariableId = variableId;
  83.         this._maSelectItemCategoryId = itemCategoryId;
  84.     };
  85.  
  86.     // Window_EventItem includes - Include if category right and selecting by category
  87.     var _ma_Window_EventItem_includes =
  88.             Window_EventItem.prototype.includes;
  89.     Window_EventItem.prototype.includes = function(item) {
  90.         var categoryId = $gameMessage.maSelectItemCategoryId();
  91.         if (categoryId > 0) { // If Category ID is setup (>0)
  92.             return (DataManager.isItem(item) && this.masicCategoryIds(item).contains(categoryId));
  93.         } else { // Default includes method if not using category ID
  94.             return _ma_Window_EventItem_includes.apply(this, arguments);
  95.         }
  96.     };
  97.    
  98.     // Window_EventItem masicCategoryIds - Get all categories for an item
  99.     Window_EventItem.prototype.masicCategoryIds = function(item) {
  100.         var arr = [];
  101.         var match = item.note.match(/\\SELECT\s*\[((?:\s*\d+[\s,;:]*)+)\]/i); // \select[x1 x2 x3 ... xn]
  102.         if (match) { arr = match[1].match(/\d+/g).map(Number); } // Map strings to integers
  103.         return arr;
  104.     }
  105.  
  106.  
  107. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement