Skip to content

Instantly share code, notes, and snippets.

@kentaromiura
Created November 1, 2015 13:56
Show Gist options
  • Save kentaromiura/183d46f8f3355a158a5f to your computer and use it in GitHub Desktop.
Save kentaromiura/183d46f8f3355a158a5f to your computer and use it in GitHub Desktop.
EnemyStats plugin for RPGMaker MV
/* Version 0.1.0 @kentaromiura
TODO: remove access to 'private' properties
TODO: find out if there's already an emitter
TODO: text now overlaps, check how to get the Sprite height/width properties
for the enemy to move the text in another position
TODO: make the text floating up and down (optional)
TODO: add a % bar below the text (optional)
*/
/*:
* @plugindesc Display HP of enemies
* @author Cristian Carlesso <kentaromiura>
*
* @param debug
* @desc opens chrome inspector for testing
* @default false
*/
!function(global){
var parameters = PluginManager.parameters('EnemyStats');
if ('' +parameters.debug === 'true') require('nw.gui').Window.get().showDevTools();
var slice = isNaN.call.bind([].slice);
function dispatchWhenDone(fn, event){
return function(){
fn.apply(this, slice(arguments));
global.dispatchEvent(new CustomEvent(event));
};
}
function addBattleEvents(){
var events = ['battleStarted', 'battleWon', 'battleEscaped'];
['onBattleStart', 'onBattleWin', 'onBattleEscape'].forEach(function(event, i){
$gameSystem[event] = dispatchWhenDone($gameSystem[event], events[i]);
})
}
// all the $* variable get set after DataManager.createGameObjects, the plugin though runs before that call.
DataManager.createGameObjects = dispatchWhenDone(
DataManager.createGameObjects,
'gameObjectsCreated'
);
var onGameObjectsCreated = function(){
addBattleEvents();
// pretty sure this flag exists somewhere else
var inBattle = false,
updateEnemyHP = function(){
$gameTroop._enemies.forEach(function(enemy){
var hpSprite = enemy._hpSprite || (enemy._hpSprite = new Sprite_Base());
var hp = '' + enemy._hp;
// I should get the font size and line height from the context somehow.
hpSprite.bitmap = new Bitmap(hp.length * 32, 32);
hpSprite.bitmap.drawText(hp, 0, 0, hp.length * 32, 32, 'left');
hpSprite.x = enemy._screenX;
hpSprite.y = enemy._screenY - 36;
if(hp === '0') hpSprite.hide();
SceneManager._scene.addChild(hpSprite);
});
if (inBattle) requestAnimationFrame(updateEnemyHP);
},
setBattleOn = function(){
inBattle = true;
requestAnimationFrame(updateEnemyHP);
},
setBattleOff = function(){
inBattle = false;
};
global.addEventListener('battleStarted', setBattleOn);
['battleWon', 'battleEscaped'].forEach(function(event){
global.addEventListener(event, setBattleOff);
})
};
global.addEventListener('gameObjectsCreated', onGameObjectsCreated);
}(Function('return this')());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment