MOG_GoldHud.js【中文注解】【金币展示】

//=============================================================================
// MOG_GoldHud.js
// 作用:金币展示,指定位置在金币显示框显示金币数量,增减金币时即时变化
// 提供插件命令:显示、隐藏金币展示框:show_gold_hud、hide_gold_hud
//=============================================================================

/*:
 * @plugindesc (v1.3) 在窗口显示金钱.
 * @author Moghunter
 *
 * @param Initial Visible
 * @desc Ativar a Hud no inicio do jogo. //是否初始可见
 * @default true 
 *
 * @param Hud X-Axis
 * @desc Definição da posição X-Axis da Hud. //显示框X轴
 * @default 555
 *
 * @param Hud Y-Axis
 * @desc Definição da posição Y-Axis da Hud. //显示框Y轴
 * @default 560
 *
 * @param Number X-Axis
 * @desc Definição da posição X-Axis da Numero. //金币数量X轴
 * @default 240
 *
 * @param Number Y-Axis
 * @desc Definição da posição Y-Axis da Numero. //金币数量Y轴
 * @default 24
 *
 * @param Fade Limit
 * @desc Definição do limite do fade. //
 * @default 60
 *
 * @help  
 * =============================================================================
 * +++ MOG Gold Hud (v1.3) +++
 * By Moghunter 
 * https://atelierrgss.wordpress.com/
 * =============================================================================
 * Apresenta uma Hud com a quantidade de dinheiro.
 * Serão necessários os arquivos. (img/system/)
 *
 * Gold_A.png //显示框
 * Gold_B.png //数字
 * =============================================================================
 * Para ocultar ou apresentar a hud use os códigos abaixo através do
 * PLUGIN COMMAND
 *
 * hide_gold_hud
 * show_gold_hud
 * 
 * =============================================================================
 * HISTÓRICO
 * =============================================================================
 * (v1.3) - Adição de ocultar a hud no inicio do jogo. 
 * (v1.2) - Correção de piscar a hud no modo ocultar a hud.
 * (v1.1) - Correção na posição da HUD através do setup.
 *        
 */

//=============================================================================
// ** PLUGIN PARAMETERS
//=============================================================================

//=============================================================================
// ** PLUGIN PARAMETERS
//=============================================================================
  var Imported = Imported || {};
  Imported.MOG_GoldHud = true;
  var Moghunter = Moghunter || {}; 

   Moghunter.parameters = PluginManager.parameters('MOG_GoldHud');
   
    // HUD POSITION
    Moghunter.ghud_pos_x = Number(Moghunter.parameters['Hud X-Axis'] || 555);
    Moghunter.ghud_pos_y = Number(Moghunter.parameters['Hud Y-Axis'] || 560);
    // 数字位置
    Moghunter.ghud_number_pos_x = Number(Moghunter.parameters['Number X-Axis'] || 240);
    Moghunter.ghud_number_pos_y = Number(Moghunter.parameters['Number Y-Axis'] || 24);
    
    Moghunter.ghud_fade_limit = Number(Moghunter.parameters['Fade Max'] || 60);
    Moghunter.ghud_hudvisible = String(Moghunter.parameters['Initial Visible'] || "true");
    
//=============================================================================
// ** Game_System
//=============================================================================

//==============================
// * Initialize
// * Game_System初始化时,添加_ghud_visible
//==============================
var _alias_mog_ghud_sys_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function() {
    _alias_mog_ghud_sys_initialize.call(this);
    this._ghud_visible = String(Moghunter.ghud_hudvisible) === "true" ? true : false;
};

//=============================================================================
// ** Game_Interpreter
//============================================================================= 

//==============================
// * PluginCommand
// * 插件命令
//==============================
var _alias_mog_goldhud_pluginCommand = Game_Interpreter.prototype.pluginCommand
Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _alias_mog_goldhud_pluginCommand.call(this,command, args)
    if (command === "show_gold_hud")  { $gameSystem._ghud_visible = true};
    if (command === "hide_gold_hud")  { $gameSystem._ghud_visible = false};
    return true;
};

//=============================================================================
// ** Game Character Base 
//=============================================================================

//==============================
// * Screen RealX
//==============================
Game_CharacterBase.prototype.screen_realX = function() {
    return this.scrolledX() * $gameMap.tileWidth()
};

//==============================
// * Screen RealY
//==============================
Game_CharacterBase.prototype.screen_realY = function() {
    return this.scrolledY() * $gameMap.tileHeight()
};

//=============================================================================
// ** Spriteset Map
//=============================================================================

//==============================
// * Create Upper Layer
//==============================
var _alias_mog_ghud_sprmap_createUpperLayer = Spriteset_Map.prototype.createUpperLayer;
Spriteset_Map.prototype.createUpperLayer = function() {
    _alias_mog_ghud_sprmap_createUpperLayer.call(this);
    this.create_gold_hud();
};

//==============================
// * Create Gold Hud
//==============================
Spriteset_Map.prototype.create_gold_hud = function() {
    this._gold_hud = new Gold_Hud();
    this.addChild(this._gold_hud);
}; 

//=============================================================================
// * Actor_Hud
//=============================================================================
function Gold_Hud() {
    this.initialize.apply(this, arguments);
};

Gold_Hud.prototype = Object.create(Sprite.prototype);
Gold_Hud.prototype.constructor = Gold_Hud;

//==============================
// * Initialize
//==============================
Gold_Hud.prototype.initialize = function() {    
    Sprite.prototype.initialize.call(this); 
    this._hud_size = [-1,-1,-1,-1];
    this.load_img();
    this.opacity = 255;
};

//==============================
// * Load Img
// * 加载图片资源
//==============================
Gold_Hud.prototype.load_img = function() {
    this._layout_img = ImageManager.loadSystem("Gold_A");
    this._number_img = ImageManager.loadSystem("Gold_B");
};

//==============================
// * Create Layout
//==============================
Gold_Hud.prototype.create_layout = function() {
    this._layout = new Sprite(this._layout_img);
    this._layout.x = this._pos_x;
    this._layout.y = this._pos_y;
    this.addChild(this._layout);
};
    
//==============================
// * Refresh Data
//==============================
Gold_Hud.prototype.refresh_data = function() {
     this._hud_size[0] = Moghunter.ghud_pos_x - ($gameMap.tileWidth() / 2);
     this._hud_size[1] = Moghunter.ghud_pos_y - $gameMap.tileHeight();
     this._hud_size[2] = Moghunter.ghud_pos_x + this._layout_img.width - $gameMap.tileWidth();
     this._hud_size[3] = Moghunter.ghud_pos_y + this._layout_img.height;     
     this._pos_x = Moghunter.ghud_pos_x;
     this._pos_y = Moghunter.ghud_pos_y;
     this.create_layout();
     this.create_number();   
};

//==============================
// * Create Number
//==============================
Gold_Hud.prototype.create_number = function() {
    this._number = [];
    //this._number_img.width / 10  0`9每个数字的宽度
    this._number_img_data = [this._number_img.width,
                            this._number_img.height,
                            this._number_img.width / 10, 
                            this._number_img.height / 2,
                            this._pos_x + Moghunter.ghud_number_pos_x,
                            this._pos_y + Moghunter.ghud_number_pos_y,
                          ];
    for (var i = 0; i < 8; i++) { //8张数字照片,这里应该就限制了最多显示几位数,目前到千万级
       this._number[i] = new Sprite(this._number_img);
       this._number[i].visible = false;
       this._number[i].x = this._number_img_data[4]; //this._pos_x + Moghunter.ghud_number_pos_x
       this._number[i].y = this._number_img_data[5]; //this._pos_y + Moghunter.ghud_number_pos_y
       this.addChild(this._number[i]);
    };  
    this._number_old = $gameParty.gold();   
    this.refresh_number(this._number,this._number_old,this._number_img_data,this._number_img_data[4],0);    
};

//==============================
// * Update Dif
// * value:上一次更新时数量
// * real_value:本次更新数量
// * 根据一定的递增\减速度,计算差值,每次大概递增\减10左右
// * 为什么不一次性直接展示最新的数字?
// * 为了在更新过程中,形成连续增加的效果,不至于过于突兀
//==============================
Gold_Hud.prototype.update_dif = function(value,real_value,speed) {
    if (value == real_value) {return value}; //相等不变

    var dnspeed = 1 + (Math.abs(value - real_value) / speed);

    if (value > real_value) { //减少
        value -= dnspeed;
        if (value < real_value) {
            value = real_value
        };
    } else if (value < real_value) { //增加
        value  += dnspeed;
        if (value  > real_value) {
            value  = real_value
        };      
    };
    return Math.floor(value);
};

//==============================
// * Refresh Number
// * _number
// * _number_old
// * _number_img_data
// * this._pos_x + Moghunter.ghud_number_pos_x
// * 0
//==============================
Gold_Hud.prototype.refresh_number = function(sprites,value,img_data,x,center) {
    numbers = Math.abs(value).toString().split("");  //金币数量绝对值转字符串分隔
    for (var i = 0; i < sprites.length ; i++) { //sprites.length 8
        sprites[i].visible = false; //先不可见
        if (i > numbers.length) {return};
        var n = Number(numbers[i]);
        sprites[i].setFrame(n * img_data[2], 0, img_data[2], img_data[1]); //选取数字
        sprites[i].visible = true;
        if (center === 0) {
           var nx = -(img_data[2] * i) + (img_data[2] * numbers.length);
        } else {
           var nx = -(img_data[2] * i) + ((img_data[2] / 2) * numbers.length);
        };
        sprites[i].x = x - nx; //计算数字X轴位置
    };  
};

//==============================
// * Update Number
//==============================
Gold_Hud.prototype.update_number = function() {
     var dif_number = this.update_dif(this._number_old,$gameParty.gold(),10)
     if (this._number_old != dif_number) {this._number_old = dif_number;
     this.refresh_number(this._number,this._number_old,this._number_img_data,this._number_img_data[4],0);};
};

//==============================
// * Update visible
//==============================
Gold_Hud.prototype.update_visible = function() {
    this.visible = $gameSystem._ghud_visible;
    if (this.is_hud_visible()) {this.opacity += 10}  
    else {this.opacity -= 10};
};

//==============================
// * Is Hud Visible
//==============================
Gold_Hud.prototype.is_hud_visible = function() {
    if ($gameMessage.isBusy()) {return false}; //有消息框
    if (!$gameSystem._ghud_visible) {return false}; //未设置为可见
    /**
     * 角色位置不在显示框内
     */
    if ($gamePlayer.screen_realX() < this._hud_size[0]) {return true};
    if ($gamePlayer.screen_realX() > this._hud_size[2]) {return true};
    if ($gamePlayer.screen_realY() < this._hud_size[1]) {return true};
    if ($gamePlayer.screen_realY() > this._hud_size[3]) {return true};
    
    if (this.opacity < Moghunter.ghud_fade_limit) {return true}; //透明度小于设定值
    return false;
};

//==============================
// * Update
//==============================
Gold_Hud.prototype.update = function() {    
    Sprite.prototype.update.call(this); 
    if (this._hud_size[0] === -1 && this._layout_img.isReady()) {this.refresh_data()};
    if (this._hud_size[0] === -1) {return};
    this.update_visible();
    this.update_number();
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355