Localize and color NPC hit labels

This commit is contained in:
WorldBosse committed 2026-10-31 03:07:31 +04:00
1 parent 597edd72a9
commit 7e60dbd70a
1 file changed
+67 -20
+67 -20
View File
@@ -6,9 +6,9 @@
module MobHitBars module MobHitBars
WIDTH = 42 WIDTH = 42
INNER_WIDTH = WIDTH - 2 INNER_WIDTH = WIDTH - 2
BITMAP_WIDTH = 220 BITMAP_WIDTH = 160
BAR_X = (BITMAP_WIDTH - WIDTH) / 2 BAR_X = (BITMAP_WIDTH - WIDTH) / 2
HEIGHT = 37 HEIGHT = 33
VISIBLE_FRAMES = 150 VISIBLE_FRAMES = 150
FADE_FRAMES = 30 FADE_FRAMES = 30
Y_OFFSET = 7 Y_OFFSET = 7
@@ -17,9 +17,14 @@ module MobHitBars
BORDER_COLOR = Color.new(210, 210, 210, 220) BORDER_COLOR = Color.new(210, 210, 210, 220)
STAMINA_COLOR = Color.new(255, 220, 20, 255) STAMINA_COLOR = Color.new(255, 220, 20, 255)
HEALTH_COLOR = Color.new(225, 35, 35, 255) HEALTH_COLOR = Color.new(225, 35, 35, 255)
LABEL_BACK_COLOR = Color.new(0, 0, 0, 180)
LABEL_FONT = "AuX DotBitC Xtra Bold" LABEL_FONT = "AuX DotBitC Xtra Bold"
LABEL_FONT_SIZE = 20 LABEL_FONT_SIZE = 18
MAX_NPC_LEVEL = 100
YELLOW_NPC_LEVEL = 50
NORMAL_LEVEL_COLOR = Color.new(255, 255, 255, 255)
MEDIUM_LEVEL_COLOR = Color.new(255, 220, 20, 255)
MAX_LEVEL_COLOR = Color.new(255, 55, 55, 255)
def self.npc_actor(character) def self.npc_actor(character)
return nil unless character.is_a?(Game_Event) return nil unless character.is_a?(Game_Event)
@@ -49,18 +54,62 @@ module MobHitBars
def self.display_name(actor) def self.display_name(actor)
raw = actor.respond_to?(:npc_name) ? actor.npc_name.to_s : "NPC" raw = actor.respond_to?(:npc_name) ? actor.npc_name.to_s : "NPC"
key = "DataNpcName:name/#{raw}" key = "DataNpcName:name/#{raw}"
translated = $game_text[key].to_s rescue "" translated = $game_text[key, true].to_s rescue ""
return raw if translated.empty? || translated == key return translated unless translated.empty?
translated
roles = [
[/Guard/i, "Стражник"],
[/(Bow|Archer)/i, "Лучник"],
[/Scout/i, "Разведчик"],
[/Warrior/i, "Воин"],
[/Raider/i, "Налётчик"],
[/Bandit/i, "Бандит"],
[/Miner/i, "Шахтёр"],
[/Shaman/i, "Шаман"],
[/(Mage|Witch)/i, "Маг"],
[/Slave/i, "Раб"],
[/Wolf/i, "Волк"],
[/Dog/i, "Собака"]
]
role = roles.find { |pair| raw =~ pair[0] }
return role[1] if role
race = actor.respond_to?(:race) ? actor.race.to_s : ""
race_name = $game_text["DataNpcName:race/#{race}", true].to_s rescue ""
race_name.empty? ? "NPC" : race_name
rescue rescue
"NPC" "NPC"
end end
def self.fit_label(bitmap, npc_name, npc_level)
suffix = " Ур.#{npc_level}"
name = npc_name.to_s
shortened = false
while name.length > 3 && bitmap.text_size("#{name}#{shortened ? "." : ""}#{suffix}").width > BITMAP_WIDTH - 8
name = name[0...-1]
shortened = true
end
"#{name}#{shortened ? "." : ""}#{suffix}"
rescue
"NPC#{suffix}"
end
def self.level(actor) def self.level(actor)
actor.respond_to?(:level) ? actor.level.to_i : 1 data = actor.respond_to?(:npc) ? actor.npc : nil
value = data && data.respond_to?(:combat) ? data.combat.to_i : 0
value = stat(actor, "combat").to_i if value <= 0
[[value, 1].max, MAX_NPC_LEVEL].min
rescue rescue
1 1
end end
def self.level_color(npc_level)
return MAX_LEVEL_COLOR if npc_level.to_i >= MAX_NPC_LEVEL
return MEDIUM_LEVEL_COLOR if npc_level.to_i >= YELLOW_NPC_LEVEL
NORMAL_LEVEL_COLOR
rescue
NORMAL_LEVEL_COLOR
end
end end
class Sprite_Character < Sprite_Base class Sprite_Character < Sprite_Base
@@ -152,23 +201,21 @@ class Sprite_Character < Sprite_Base
bitmap.font.size = MobHitBars::LABEL_FONT_SIZE bitmap.font.size = MobHitBars::LABEL_FONT_SIZE
bitmap.font.bold = false bitmap.font.bold = false
bitmap.font.outline = false bitmap.font.outline = false
bitmap.font.color = Color.new(255, 255, 255, 255) bitmap.font.shadow = true
label = "#{npc_name} Ур. #{npc_level}" bitmap.font.color = MobHitBars.level_color(npc_level)
label_width = [bitmap.text_size(label).width + 10, MobHitBars::BITMAP_WIDTH].min label = MobHitBars.fit_label(bitmap, npc_name, npc_level)
label_x = (MobHitBars::BITMAP_WIDTH - label_width) / 2 bitmap.draw_text(0, 0, MobHitBars::BITMAP_WIDTH, 21, label, 1)
bitmap.fill_rect(label_x, 1, label_width, 24, MobHitBars::LABEL_BACK_COLOR)
bitmap.draw_text(0, 0, MobHitBars::BITMAP_WIDTH, 25, label, 1)
x = MobHitBars::BAR_X x = MobHitBars::BAR_X
bitmap.fill_rect(x, 28, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR) bitmap.fill_rect(x, 24, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR)
bitmap.fill_rect(x + 1, 29, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR) bitmap.fill_rect(x + 1, 25, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR)
sta_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(sta, sta_max)).round sta_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(sta, sta_max)).round
bitmap.fill_rect(x + 1, 29, sta_width, 2, MobHitBars::STAMINA_COLOR) if sta_width > 0 bitmap.fill_rect(x + 1, 25, sta_width, 2, MobHitBars::STAMINA_COLOR) if sta_width > 0
bitmap.fill_rect(x, 33, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR) bitmap.fill_rect(x, 29, MobHitBars::WIDTH, 4, MobHitBars::BORDER_COLOR)
bitmap.fill_rect(x + 1, 34, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR) bitmap.fill_rect(x + 1, 30, MobHitBars::INNER_WIDTH, 2, MobHitBars::BACK_COLOR)
hp_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(hp, hp_max)).round hp_width = (MobHitBars::INNER_WIDTH * MobHitBars.ratio(hp, hp_max)).round
bitmap.fill_rect(x + 1, 34, hp_width, 2, MobHitBars::HEALTH_COLOR) if hp_width > 0 bitmap.fill_rect(x + 1, 30, hp_width, 2, MobHitBars::HEALTH_COLOR) if hp_width > 0
end end
def reset_mob_hit_bars_tracking def reset_mob_hit_bars_tracking