Advertisement
DarkSoul144

DSI-Busts.js

Feb 18th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=======================================================================================================================================
  2. // * DSI-Busts.js
  3. // -- Last updated: 2/17/18
  4. // -- Author: dsiver144
  5. // -- Commission by: Dark Horseman
  6. //=======================================================================================================================================
  7. // Change log:
  8. //  + 2/17/18: Finish the plugin!
  9. //  + 2/18/18: Update the script. Fix Change Bust issue. Remake Collapse Special Code. Add centerL, centerR position for pos.
  10. //=======================================================================================================================================
  11. /*:
  12. * @plugindesc Provide an ability for display busts on messages with lots of features similiar to Fire Emblem series. <DSI-Busts>
  13. * @author dsiver144
  14. * @help
  15. * -- Plugin Commands --
  16. * Create_Bust <FileName> <BustIndex> <Pos> <flipped?> <Active?> <effect>
  17. * example: Create_Bust Create_Bust Listener 1 left false true fadeIn
  18. *          Create_Bust Create_Bust Custom 1 right true false none
  19. *          Create_Bust Create_Bust Custom 1 centerL true false none
  20. *          Create_Bust Create_Bust Custom 1 centerR true false none
  21. * NOTE: "centerL" mean position is center but the direction of the bust is left. Same logic for "centerR".
  22. * -- New Plugin Commands --
  23. * Set_Collapse_SE <bust_id> <SE Name> <Volume> <Pitch> <Pan> : Set collapse se for bust.
  24. * Set_Damage_SE <bust_id> <SE Name> <Volume> <Pitch> <Pan> : Set damage se for bust when shaking.
  25. * Clear_All_Busts : Remember to put this in when end an conversation.
  26. * -- Message Special Text Codes --
  27. * \FadeInB[bust_id] : Perform Fade in effect for the bust with specific #bust_id.
  28. * \FadeOutB[bust_id] : Perform Fade out effect for the bust with specific #bust_id.
  29. * \ActB[bust_id] : Active bust for talking. Remove Dim Bust effect.
  30. * \DeactB[bust_id] : Deactive bust. Add Dim Bust effect.
  31. * \ChB[bust_id,bust_index,delayFrame] : Change bust image. * See demo for more details.
  32. * \SldB[bust_id,slideX,slideY] : Slide bust. * See demo for more details.
  33. * \ShkB[bust_id, power] : Shake a bust.
  34. * \CollapseB[bust_id, duration] : Pefrom collapse effect on bust with duration.
  35. */
  36. //=======================================================================================================================================
  37. (function(){
  38.     'use strict';
  39.     var DSIVER144 = DSIVER144 || {};
  40.     var params = $plugins.filter(function(p) {return p.description.contains('<DSI-Busts>')})[0].parameters;
  41.    
  42.     var windowMessageStartMessageAlias  = Window_Message.prototype.startMessage;
  43.     Window_Message.prototype.startMessage = function() {
  44.         windowMessageStartMessageAlias.call(this);
  45.         //SceneManager._scene._spriteset.createBusts();
  46.     };
  47.    
  48.     var SpriteSet_Base_createUpperLayer_alias = Spriteset_Base.prototype.createUpperLayer;
  49.     Spriteset_Base.prototype.createUpperLayer = function() {
  50.         SpriteSet_Base_createUpperLayer_alias.call(this);
  51.         this._bust_sprites = [];
  52.     };
  53.    
  54.     var alias_convertEscapeCharacters_dsiBust = Window_Base.prototype.convertExtraEscapeCharacters;
  55.     Window_Base.prototype.convertExtraEscapeCharacters = function(text) {
  56.         var new_text = alias_convertEscapeCharacters_dsiBust.call(this, text);
  57.         new_text = new_text.replace(/\x1bFadeInB/gi, '\x1bBFI');
  58.         new_text = new_text.replace(/\x1bFadeOutB/gi, '\x1bBFO');
  59.         new_text = new_text.replace(/\x1bCollapseB/gi, '\x1bBCL');
  60.         new_text = new_text.replace(/\x1bActB/gi, '\x1bBAT');
  61.         new_text = new_text.replace(/\x1bDeactB/gi, '\x1bBDAT');
  62.         new_text = new_text.replace(/\x1bChB/gi, '\x1bCAB');
  63.         new_text = new_text.replace(/\x1bSldB/gi, '\x1bSLB');
  64.         new_text = new_text.replace(/\x1bShkB/gi, '\x1bSHKB');
  65.         return new_text;
  66.     };
  67.    
  68.     var alias_processEscapeCharacter_dsiBust = Window_Message.prototype.processEscapeCharacter;
  69.     Window_Message.prototype.processEscapeCharacter = function(code, textState) {
  70.         switch (code) {
  71.         case 'BFI':
  72.             var bust_id = this.obtainEscapeParam(textState);
  73.             console.log('Here');
  74.             $gameSystem._bustSettings[bust_id - 1].phase = 'fadeIn';
  75.             break;
  76.         case 'BFO':
  77.             var bust_id = this.obtainEscapeParam(textState);
  78.             $gameSystem._bustSettings[bust_id - 1].phase = 'fadeOut';
  79.             break;
  80.         case 'BCL':
  81.             this.obtainSpecialParamCollapse(textState);
  82.             break;
  83.         case 'BAT':
  84.             var bust_id = this.obtainEscapeParam(textState);
  85.             $gameSystem._bustSettings[bust_id - 1].IsActive = true;
  86.             break;
  87.         case 'BDAT':
  88.             var bust_id = this.obtainEscapeParam(textState);
  89.             $gameSystem._bustSettings[bust_id - 1].IsActive = false;
  90.             break;
  91.         case 'CAB':
  92.             this.obtainSpecialParamChangeBust(textState);
  93.             break;
  94.         case 'SLB':
  95.             this.obtainSpecialParamSliding(textState);
  96.             break;
  97.          case 'SHKB':
  98.             this.obtainSpecialParamShake(textState);
  99.             break;
  100.         }
  101.         alias_processEscapeCharacter_dsiBust.call(this, code, textState);
  102.     };
  103.    
  104.     Window_Message.prototype.obtainSpecialParamChangeBust = function(textState) {
  105.         var arr = /^\[(\d+),\s*(\d+)\,\s*(\d+)\]/.exec(textState.text.slice(textState.index));
  106.         if (arr) {
  107.             textState.index += arr[0].length;
  108.             var txt = arr[0].slice(1).slice(0, - 1);
  109.             var array = txt.split(",");
  110.             var bust_id = Number(array[0]);
  111.             var new_name = $gameSystem._bustSettings[bust_id - 1].originalName + '_' + array[1];
  112.             $gameSystem._bustSettings[bust_id - 1].filename = new_name;
  113.             $gameSystem._bustSettings[bust_id - 1].changeBust = true;
  114.             $gameSystem._bustSettings[bust_id - 1].changeBustDelay = Number(array[2]);
  115.         } else {
  116.             return '';
  117.         }
  118.     };
  119.    
  120.     Window_Message.prototype.obtainSpecialParamSliding = function(textState) {
  121.         var arr = /^\[(\d+),(\-*\d+),(\-*\d+)\]/.exec(textState.text.slice(textState.index));
  122.         if (arr) {
  123.             textState.index += arr[0].length;
  124.             var txt = arr[0].slice(1).slice(0, - 1);
  125.             var array = txt.split(",");
  126.             var bust_id = Number(array[0]);
  127.             $gameSystem._bustSettings[bust_id - 1].phase = 'sliding';
  128.             $gameSystem._bustSettings[bust_id - 1].slideX = Number(array[1]);
  129.             $gameSystem._bustSettings[bust_id - 1].slideY = Number(array[2]);
  130.         } else {
  131.             return '';
  132.         }
  133.     };
  134.    
  135.     Window_Message.prototype.obtainSpecialParamCollapse = function(textState) {
  136.         var arr = /^\[(\d+),(\d+)\]/.exec(textState.text.slice(textState.index));
  137.         if (arr) {
  138.             textState.index += arr[0].length;
  139.             var txt = arr[0].slice(1).slice(0, - 1);
  140.             var array = txt.split(",");
  141.             var bust_id = Number(array[0]);
  142.             $gameSystem._bustSettings[bust_id - 1].phase = 'collapse';
  143.             $gameSystem._bustSettings[bust_id - 1].collapse_duration = Number(array[1]);
  144.             if ($gameSystem._bustSettings[bust_id - 1].collapse_se) {
  145.                 AudioManager.playStaticSe($gameSystem._bustSettings[bust_id - 1].collapse_se)
  146.             } else {
  147.                 SoundManager.playEnemyCollapse();
  148.             }
  149.         } else {
  150.             return '';
  151.         }
  152.     };
  153.    
  154.     Window_Message.prototype.obtainSpecialParamShake = function(textState) {
  155.         var arr = /^\[(\d+),(\-*\d+)\]/.exec(textState.text.slice(textState.index));
  156.         if (arr) {
  157.             textState.index += arr[0].length;
  158.             var txt = arr[0].slice(1).slice(0, - 1);
  159.             var array = txt.split(",");
  160.             var bust_id = Number(array[0]);
  161.             $gameSystem._bustSettings[bust_id - 1].phase = 'shake';
  162.             $gameSystem._bustSettings[bust_id - 1].shakeRadius = Number(array[1]);
  163.             if ($gameSystem._bustSettings[bust_id - 1].damage_se) {
  164.                 AudioManager.playStaticSe($gameSystem._bustSettings[bust_id - 1].damage_se)
  165.             } else {
  166.                 SoundManager.playEnemyDamage();
  167.             }
  168.         } else {
  169.             return '';
  170.         }
  171.     };
  172.    
  173.    
  174.     function Sprite_DSIBust() {
  175.         this.initialize.apply(this, arguments);
  176.     };
  177.    
  178.     var dsiPluginCommandAliasBustSystem = Game_Interpreter.prototype.pluginCommand;
  179.     Game_Interpreter.prototype.pluginCommand = function(command, args) {
  180.         if (command === 'Create_Bust') {
  181.             var bust = {};
  182.             bust.originalName = args[0];
  183.             bust.filename = args[0] + '_' + args[1];
  184.             bust.pos = args[2];
  185.             bust.IsFlipped = (args[3] === "true") ? true : false;
  186.             bust.IsActive = (args[4] === "true") ? true : false;
  187.             bust.phase = args[5];
  188.             bust.hide = false;
  189.             bust.slideSpeed = 2;
  190.             bust.hasSprite = false;
  191.             bust.changeBust = true;
  192.             bust.changeBustDelay = 0;
  193.             bust.slideX = 0;
  194.             bust.newX = null;
  195.             bust.newY = null;
  196.             $gameSystem._bustSettings.push(bust);
  197.             SceneManager._scene._spriteset.createBusts();
  198.             //console.log($gameSystem._bustSettings);
  199.         }
  200.         if (command === 'Slide_Bust') {
  201.             var bust_id = parseInt(args[0]);
  202.             $gameSystem._bustSettings[bust_id - 1].slideX = parseInt(args[1]);
  203.             $gameSystem._bustSettings[bust_id - 1].phase = 'sliding';
  204.         }
  205.         if (command === 'Shake_Bust') {
  206.             var bust_id = parseInt(args[0]);
  207.             $gameSystem._bustSettings[bust_id - 1].shakeRadius = parseInt(args[1]);
  208.             $gameSystem._bustSettings[bust_id - 1].phase = 'shake';
  209.             SoundManager.playEnemyDamage();
  210.         }
  211.         if (command === 'Set_Collapse_SE') {
  212.             var bust_id = parseInt(args[0]);
  213.             var se = {
  214.                 name : args[1],
  215.                 volume : parseInt(args[2]),
  216.                 pitch : parseInt(args[3]),
  217.                 pan : parseInt(args[4])
  218.             }
  219.             $gameSystem._bustSettings[bust_id - 1].collapse_se = se;
  220.         }
  221.         if (command === 'Set_Damage_SE') {
  222.             var bust_id = parseInt(args[0]);
  223.             var se = {
  224.                 name : args[1],
  225.                 volume : parseInt(args[2]),
  226.                 pitch : parseInt(args[3]),
  227.                 pan : parseInt(args[4])
  228.             }
  229.             $gameSystem._bustSettings[bust_id - 1].damage_se = se;
  230.         }
  231.         if (command === 'Clear_All_Busts') {
  232.             $gameSystem._bustSettings.forEach(function(bust){
  233.                bust.phase = 'fadeOut';
  234.                bust.delete = true;
  235.             });
  236.             $gameSystem._bustSettings.splice(0, $gameSystem._bustSettings.length); // Clear Bust Settings Array
  237.         }
  238.         dsiPluginCommandAliasBustSystem.call(this, command, args);
  239.     }
  240.    
  241.     Spriteset_Base.prototype.createBusts = function() {
  242.         for (var i = 0; i < $gameSystem._bustSettings.length; i++) {
  243.             var bust = $gameSystem._bustSettings[i];
  244.             if (bust.hasSprite) continue;
  245.             var new_sprite = new Sprite_DSIBust(bust);
  246.             bust.hasSprite = true;
  247.             this._bust_sprites.push(new_sprite);
  248.             this.addChild(new_sprite);
  249.         }
  250.     }
  251.    
  252.     var dsiGameSystemInitializeBustInfo = Game_System.prototype.initialize;
  253.     Game_System.prototype.initialize = function() {
  254.         dsiGameSystemInitializeBustInfo.call(this);
  255.         this._bustSettings = [];
  256.     };
  257.    
  258.     Sprite_DSIBust.prototype = Object.create(Sprite.prototype);
  259.     Sprite_DSIBust.prototype.constructor = Sprite_DSIBust;
  260.  
  261.     Sprite_DSIBust.prototype.initialize = function(bust) {
  262.         Sprite.prototype.initialize.call(this);
  263.         this._bust = bust;
  264.         if (bust.IsFlipped) {
  265.             this.anchor.x = 1;
  266.             this.scale.x *= -1;
  267.         }
  268.         this.moveX = 0;
  269.         this.opacity = 0;
  270.         this.update();
  271.         this.hideFlag = false;
  272.         this.targetX = null;
  273.         this.targetY = null;
  274.     };
  275.    
  276.     Sprite_DSIBust.prototype.loadBustBitmap = function() {
  277.         var name = this._bust.filename;
  278.         var img = ImageManager.loadPicture(name);
  279.         if (img.isReady()) {
  280.             this.y = Graphics._height - SceneManager._scene._messageWindow.windowHeight() - img.height;
  281.             if (this._bust.newY) this.y = this._bust.newY;
  282.             if (this._bust.pos === 'left') {
  283.                 this.x = 10;
  284.                 if (this._bust.newX) this.x = this._bust.newX;
  285.             }
  286.             if (this._bust.pos === 'right') {
  287.                 this.x = Graphics._width - img.width - 10;
  288.                 if (this._bust.newX) this.x = this._bust.newX;
  289.             }
  290.             if (this._bust.pos === 'centerL') {
  291.                 this.x = (Graphics._width - img.width) * 0.5;
  292.                 if (this._bust.newX) this.x = this._bust.newX;
  293.             }
  294.             if (this._bust.pos === 'centerR') {
  295.                 this.x = (Graphics._width - img.width) * 0.5;
  296.                 if (this._bust.newX) this.x = this._bust.newX;
  297.             }
  298.             this.bitmap = img;
  299.             this._bust.changeBust = false;
  300.         }
  301.     };
  302.    
  303.     Sprite_DSIBust.prototype.update = function() {
  304.         Sprite.prototype.update.call(this);
  305.         this.update_bust();
  306.     };
  307.    
  308.     Sprite_DSIBust.prototype.update_bust = function() {
  309.         if (this.hideFlag) {
  310.             return;
  311.         }
  312.         if (this._bust.changeBust) {
  313.             if (this._bust.changeBustDelay > 0) this._bust.changeBustDelay--;
  314.             if (this._bust.changeBustDelay === 0) this.loadBustBitmap();
  315.         }
  316.         if (!this._bust.IsActive) {
  317.             this.setColorTone([-90,-90,-90,60]);
  318.         } else {
  319.             this.setColorTone([0,0,0,0]);
  320.         }
  321.         // Fade In
  322.         if (this._bust.phase === 'fadeIn') {
  323.             if (this.opacity < 255) {
  324.                 this.opacity += 10;
  325.             }
  326.             if (this.moveX < 30) {
  327.                 this.x += (this._bust.pos === 'left' || this._bust.pos === 'centerR') ? 1 : -1;
  328.                 this.moveX += this._bust.slideSpeed;
  329.             }
  330.             if (this.moveX >= 30 && this.opacity === 255) {
  331.                 this.moveX = 0;
  332.                 this._bust.newY = this.y;
  333.                 this._bust.newX = this.x;
  334.                 this._bust.phase = 'none';
  335.             }
  336.         }
  337.         // Fade Out
  338.         if (this._bust.phase === 'fadeOut') {
  339.             if (this.opacity > 0) {
  340.                 this.opacity -= 10;
  341.             }
  342.             if (this.moveX <= 30) {
  343.                 this.x += (this._bust.pos === 'left' || this._bust.pos === 'centerR') ? -1 : 1;
  344.                 this.moveX += this._bust.slideSpeed;
  345.             }
  346.             if (this.moveX >= 30 && this.opacity === 0) {
  347.                 this.moveX = 0;
  348.                 this._bust.newY = this.y;
  349.                 this._bust.newX = this.x;
  350.                 this._bust.phase = 'none';
  351.                 if (this._bust.delete) {
  352.                     this.hideFlag = true;
  353.                 }
  354.             }
  355.         }
  356.         // Collapse
  357.         if (this._bust.phase === 'collapse') {
  358.             this.blendMode = Graphics.BLEND_ADD;
  359.             console.log('Colappse');
  360.         this.setBlendColor([255, 128, 128, 128]);
  361.             if (this.opacity > 0) {
  362.                 this.opacity -= 255 / this._bust.collapse_duration;
  363.             }
  364.             if (this.opacity === 0) {
  365.                 this._bust.phase = 'none';
  366.                 this.hideFlag = true;
  367.             }
  368.         }
  369.         // Sliding
  370.         if (this._bust.phase === 'sliding') {
  371.             if (!this.targetX) {
  372.                 this.targetX = this.x + this._bust.slideX * ((this._bust.pos === 'left' || this._bust.pos === 'centerR') ? 1 : -1);
  373.                 this._bust.newX = this.targetX;
  374.             }
  375.             if (!this.targetY) {
  376.                 this.targetY = this.y + this._bust.slideY
  377.                 this._bust.newY = this.targetY;
  378.             }
  379.             this.x += (this.targetX - this.x) / 10;
  380.             this.y += (this.targetY - this.y) / 10;
  381.             if (Math.abs(this.x - this.targetX) <= 0.01 && Math.abs(this.y - this.targetY) <= 0.01)  {
  382.                 this._bust.newY = this.y;
  383.                 this._bust.newX = this.x;
  384.                 this._bust.phase = 'none';
  385.                 this._bust.slideY = 0;
  386.                 this.targetY = null;
  387.                 this._bust.slideX = 0;
  388.                 this.targetX = null;
  389.                 return;
  390.             }
  391.            
  392.         }
  393.         // Damage Shake
  394.         if (this._bust.phase === 'shake') {
  395.             if (this._bust.shakeRadius > 0) {
  396.                 if (!this._bust.originX) {
  397.                     this._bust.originX = this.x;
  398.                 }
  399.                 this.x = this._bust.originX + Math.cos(Graphics.frameCount) * this._bust.shakeRadius;
  400.                 this._bust.shakeRadius -= 0.5
  401.                 if (this._bust.shakeRadius <= 0) {
  402.                     this._bust.phase = 'none';
  403.                     this.x = this._bust.originX;
  404.                     this._bust.originX = null;
  405.                 }
  406.             }
  407.         }
  408.     };
  409.        
  410. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement