Created
November 2, 2017 05:48
-
-
Save GMMan/4b2edfe19e8a02bd846b663d1b4687be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//============================================================================= | |
// cy_EventFade.js | |
//============================================================================= | |
/*: | |
* | |
* @plugindesc Fades all events out when any event activated, and all events in when all events finish running | |
* @author cyanic | |
* | |
* @param initialOpacity | |
* @text Initial Opacity | |
* @desc The initial opacity of events when the map loads | |
* @type number | |
* @default 0 | |
* @max 255 | |
* @min 0 | |
* | |
* @param fadeSpeed | |
* @text Fade Speed | |
* @desc The amount the opacity decreases or increases at each frame | |
* @type number | |
* @default 9 | |
* | |
*/ | |
var Imported = Imported || {}; | |
(function() { | |
var pluginName = "cy_EventFade"; | |
Imported[pluginName] = 1; | |
// Load parameters | |
var sysParams = PluginManager.parameters(pluginName); | |
var pluginParams = { | |
"fadeSpeed": Number(sysParams["fadeSpeed"]), | |
"initialOpacity": Number(sysParams["initialOpacity"]) | |
}; | |
// Setup plugin-specific variables | |
var globalOpacity = pluginParams.initialOpacity; | |
var fadeReferenceCount = 0; | |
var fadeEnabled = true; | |
// Plugin command handler | |
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand; | |
Game_Interpreter.prototype.pluginCommand = function(command, args) { | |
_Game_Interpreter_pluginCommand.call(this, command, args); | |
if (command.toLowerCase() === "eventfade" && args.length > 0) { | |
switch (args[0].toLowerCase()) { | |
case "enable": | |
fadeEnabled = true; | |
break; | |
case "disable": | |
fadeEnabled = false; | |
break; | |
case "toggle": | |
fadeEnabled = !fadeEnabled; | |
break; | |
} | |
} | |
}; | |
// Game Event overrides | |
var _Game_Event_opacity = Game_Event.prototype.opacity; | |
var _Game_Event_start = Game_Event.prototype.start; | |
var _Game_Event_update = Game_Event.prototype.update; | |
// This does the actual hiding | |
Game_Event.prototype.opacity = function() { | |
return Math.floor(_Game_Event_opacity.call(this) * (globalOpacity / 255)); | |
}; | |
// Increments reference count when event activated | |
Game_Event.prototype.start = function() { | |
_Game_Event_start.call(this); | |
this._fadeTracking = true; | |
++fadeReferenceCount; | |
}; | |
// Decrements reference count when event finishes | |
Game_Event.prototype.update = function() { | |
_Game_Event_update.call(this); | |
if (this._fadeTracking && !this._locked) { | |
--fadeReferenceCount; | |
this._fadeTracking = false; | |
} | |
}; | |
// Why not use lock() and unlock()? Because they seem a bit less reliable for some reason. | |
// Game Map overrides | |
var _Game_Map_setupEvents = Game_Map.prototype.setupEvents; | |
var _Game_Map_updateEvents = Game_Map.prototype.updateEvents; | |
Game_Map.prototype.setupEvents = function() { | |
_Game_Map_setupEvents.call(this); | |
globalOpacity = pluginParams.initialOpacity; | |
fadeReferenceCount = 0; | |
}; | |
Game_Map.prototype.updateEvents = function() { | |
if (fadeEnabled) { | |
if (fadeReferenceCount > 0) | |
{ | |
if (globalOpacity > 0) | |
globalOpacity = Math.max(0, globalOpacity - pluginParams.fadeSpeed); | |
} | |
else | |
{ | |
if (globalOpacity < 255) | |
globalOpacity = Math.min(255, globalOpacity + pluginParams.fadeSpeed); | |
} | |
} | |
_Game_Map_updateEvents.call(this); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment