Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Enemy HP Bars
#1
Enemy HP Bars 4.0
by El Conductor

Jul 29 2007

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


With this simple yet clever script you can see the enemies HP Bars during
battle. I added the players HP & SP bars as well. Just copy this script and
paste above main. Have fun!

UPDATE: System Rebuilt, new look for bars, new scrolling ability,
more customizable features, bars on menu screen is lost,
SDK compatible.



Code:
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Enemy/Actor HP & SP Bars +
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# By: El Conducter +
# Updated: January/08/08 +
# Version: 4.0 +
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Update History:
# Version 1.0 - Displays enemy & actor HP & SP bars in battle.
#
# Version 2.0 - Made the enemy HP bars more centered aligned with the
# enemy. Actor bars now also show in menu screen.
#
# Version 3.0 - Module added to change bar lengths easier.
#
# Version 4.0 - System Rebuilt, new look for bars, new scrolling ability,
# more customizable features, bars on menu screen is lost,
# SDK compatible
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# What it Does:
# See enemy and actor HP/SP bars during battle.
#
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# How it Works:
#
# This version of the script works similar to the previous versions,
# however it runs a lot differently. Rather than the invisible window
# to display the bars as in previous versions, sub-sprites are used
# instead. The same effects can be achieved this way with more efficeincy
# and less code. New fetures can be taken advantage of through this new
# system like the ability for visual filling and depletion of bars.
# Though not built directly for the SDK, this script will work with or
# without it.
#
# Lengths and settings for the bars are stored in module Bar_Config.
#
# New and altered scripts are:
#
# - module Bar_Length
# - Sprite_HP_Bar
# - Sprite_SP_Bar
# - Sprite_Battler
# - Game_Battler
# - Scene_Battle
#
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# How to Use This Script:
# Just copy it and paste it above Main. If using the SDK, paste below SDK
# and above Main.
#
# To change the bar lengths and other settings go to module Bar_Config
# and change the values there.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Comments:
# I hope my script is easy for you to use and modify. Study this script
# to learn the ways of RGSS and join the ranks of those known as 'Scripters'.
#----------------------------------------------------------------------------



#==============================================================================
# ** Module Bar_Config
#------------------------------------------------------------------------------
# Contains various configurations for bars. The lengths, colors, and speeds
# are assigned to constants that will be referenced later in the script.
#==============================================================================

module Bar_Config
# CONSTANTS

# Bar Size
LENGTH = 150
WIDTH = 9

# Bar Colors
# Full bar color
GREEN = Color.new(50, 200, 50, 255)
# Bar low color
RED = Color.new(200, 50, 50, 255)
# Colors for HP Bar
YELLOW = Color.new(200, 200, 0, 255)
ORANGE = Color.new(220, 150, 0, 255)
# Colors for SP Bar
INDIGO = Color.new(50, 50, 200, 255)
VIOLET = Color.new(120, 50, 180, 255)
# Color for bar border
BLACK = Color.new(0, 0, 0, 255)

# Bar fill & decrease speed
FASTEST = 10
FAST = Graphics.frame_rate / 2
MEDUIM = Graphics.frame_rate
SLOW = Graphics.frame_rate * 2
SLOWEST = 100
# Set this to the above desired speed
CURRENT_SPEED = MEDUIM

# Visibility for bars. Set which bars you want visible for battle.
ACTOR_HP_USED = true
ACTOR_SP_USED = true
ENEMY_HP_USED = true
ENEMY_SP_USED = false
end

#==============================================================================
# ** Sprite_HP_Bar
#------------------------------------------------------------------------------
# This sprite is used to display the HP_Bar for the battler. It inherits form
# RPG::Sprite.
#==============================================================================

class Sprite_HP_Bar < RPG::Sprite
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :battler
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(viewport, battler)
super(viewport)
# Assign battler and variables used in drawing bar
@battler = battler
@battler_hp = 0
@accu_damages = 0
@counter = 0
# Only call setup method if @battler is not equal to nothing
if @battler != nil
setup
end
end
#--------------------------------------------------------------------------
# * Setup: this prepares the bitmap
#--------------------------------------------------------------------------
def setup
@battler_hp = @battler.hp
self.bitmap = Bitmap.new(Bar_Config::LENGTH + 2, Bar_Config::WIDTH)
# Determine if bar is visible for Actor or Enemy
if @battler.is_a?(Game_Enemy)
self.visible = Bar_Config::ENEMY_HP_USED
elsif @battler.is_a?(Game_Actor)
self.visible = Bar_Config::ACTOR_HP_USED
end
# Set bar placement
self.x = @battler.screen_x
self.y = @battler.screen_y
self.ox = self.bitmap.width / 2
self.oy = self.bitmap.height
# Call method to draw bars
draw_bars
update
end
#--------------------------------------------------------------------------
# * Method that draws the bars
#--------------------------------------------------------------------------
def draw_bars
# Clear previous bars if any
self.bitmap.clear
# Use formula to get length ratio to health
length = Bar_Config::LENGTH * @battler_hp / @battler.maxhp
# Specify bar & border
bar_border = Rect.new(0, 0, Bar_Config::LENGTH + 2, Bar_Config::WIDTH)
bar = Rect.new(1, 1, length, Bar_Config::WIDTH - 2)
# Draw the bar with border
self.bitmap.fill_rect(bar_border, Bar_Config::BLACK)
self.bitmap.fill_rect(bar, get_color)
end
#--------------------------------------------------------------------------
# * Determine the appropriate color based on battler's health
#--------------------------------------------------------------------------
def get_color
# Variables
full = @battler.maxhp
half = @battler.maxhp / 2
quarter = @battler.maxhp / 4
# Change bar color depending on amount HP
case @battler.hp
when full
color = Bar_Config::GREEN # Green when full
when half...full
color = Bar_Config::YELLOW # Yellow when above half
when quarter...half
color = Bar_Config::ORANGE # Orange when below half
when 0...quarter
color = Bar_Config::RED # Red when below quarter
end
# Return the appropriate color
return color
end
#--------------------------------------------------------------------------
# * Process when battler receives damage
#--------------------------------------------------------------------------
def damage_dealing(damages)
# Don't do process if damage is a String "Miss"
unless damages.is_a?(String)
# Add new damage to accumulitive damages
@accu_damages += damages
# Counter will be how many times the bars will be refreshed
# for the 'fill' effect.
@counter = Bar_Config::CURRENT_SPEED
# Rate is how many points will be altered for every refresh
@rate = @accu_damages / @counter
end
end
#--------------------------------------------------------------------------
# * Refresh Method
#--------------------------------------------------------------------------
def refresh
# Do only while @battler is not equal to nothing
unless @battler == nil
# Do only if @counter isn't equal to 0
unless @counter == 0
# Minus variables by rate amount of rate
@accu_damages -= @rate
@battler_hp -= @rate
# Check for healing over the bar amount. If so, fix amount
if @battler_hp > @battler.maxhp
@battler_hp = @battler.maxhp
end
end
# Call method to draw bars
draw_bars
end
end
#--------------------------------------------------------------------------
# * Dispose
#--------------------------------------------------------------------------
def dispose
unless self.bitmap == nil
self.bitmap.dispose
end
super
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
# Clear if Battler is nil
if @battler.nil?
self.bitmap.clear unless self.bitmap.nil?
return
end
# If @counter is greater than zero, decrement @counter and refresh bars
if @counter > 0
# If counter is on last iteration
if @counter == 1
# Adjust variables in case of floating point number in-accuracy
@battler_hp = @battler.hp
@accu_damages = 0
end
@counter -= 1
refresh
end
end
end

#==============================================================================
# ** Sprite_SP_Bar
#------------------------------------------------------------------------------
# This sprite is used to display the SP Bar for the battler. It inherits form
# RPG::Sprite.
#==============================================================================

class Sprite_SP_Bar < RPG::Sprite
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :battler
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(viewport, battler)
super(viewport)
# Assign battler and variables used in drawing bar
@battler = battler
@battler_sp = 0
@accu_magic = 0
@counter = 0
# Only call setup method if @battler is not equal to nothing
if @battler != nil
setup
end
end
#--------------------------------------------------------------------------
# * Setup: this prepares the bitmap
#--------------------------------------------------------------------------
def setup
@battler_sp = @battler.sp
self.bitmap = Bitmap.new(Bar_Config::LENGTH + 2, 9)
# Determine if bar is visible for Actor or Enemy
if @battler.is_a?(Game_Enemy)
self.visible = Bar_Config::ENEMY_SP_USED
elsif @battler.is_a?(Game_Actor)
self.visible = Bar_Config::ACTOR_SP_USED
end
# Set bar placement
self.x = @battler.screen_x
self.y = @battler.screen_y + self.bitmap.height
self.ox = self.bitmap.width / 2
self.oy = self.bitmap.height
# Call method to draw bars
draw_bars
update
end
#--------------------------------------------------------------------------
# * Method that draws the bars
#--------------------------------------------------------------------------
def draw_bars
# Clear previous bars if any
self.bitmap.clear
# Use formula to get length ratio to SP
length = Bar_Config::LENGTH * @battler_sp / @battler.maxsp
# Specify bar & border
bar_border = Rect.new(0, 0, Bar_Config::LENGTH + 2, Bar_Config::WIDTH)
bar = Rect.new(1, 1, length, Bar_Config::WIDTH - 2)
# Draw the bar with border
self.bitmap.fill_rect(bar_border, Bar_Config::BLACK)
self.bitmap.fill_rect(bar, get_color)
end
#--------------------------------------------------------------------------
# * Determine the appropriate color based on battler's SP
#--------------------------------------------------------------------------
def get_color
# Variables
full = @battler.maxsp
half = @battler.maxsp / 2
quarter = @battler.maxsp / 4
# Change bar color depending on amount SP
case @battler.sp
when full
color = Bar_Config::GREEN # Green when full
when half...full
color = Bar_Config::INDIGO # Indigo when above half
when quarter...half
color = Bar_Config::VIOLET # Violet when below half
when 0...quarter
color = Bar_Config::RED # Red when below quarter
end
# Return the appropriate color
return color
end
#--------------------------------------------------------------------------
# * Process when battler uses skills
#--------------------------------------------------------------------------
def skill_use(magic_used)
# Add new magic_used to accumulitive magic_used
@accu_magic += magic_used
# Counter will be how many times the bars will be refreshed
# for the 'fill' effect.
@counter = Bar_Config::CURRENT_SPEED
# Rate is how many points will be altered for every refresh
@rate = @accu_magic / @counter
end
#--------------------------------------------------------------------------
# * Refresh Method
#--------------------------------------------------------------------------
def refresh
# Do only while @battler is not equal to nothing
unless @battler == nil
# Do only if @counter isn't equal to 0
unless @counter == 0
# Minus variables by rate amount of rate
@accu_magic -= @rate
@battler_sp -= @rate
# Check for recovering SP over the bar amount. If so, fix amount
if @battler_sp > @battler.maxsp
@battler_sp = @battler.maxsp
end
end
# Call method to draw bars
draw_bars
end
end
#--------------------------------------------------------------------------
# * Dispose
#--------------------------------------------------------------------------
def dispose
unless self.bitmap == nil
self.bitmap.dispose
end
super
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
# Clear if Battler is nil
if @battler.nil?
self.bitmap.clear unless self.bitmap.nil?
return
end
# If @counter is greater than zero, decrement @counter and refresh bars
if @counter > 0
# If counter is on last iteration
if @counter == 1
# Adjust variables in case of floating point number in-accuracy
@battler_sp = @battler.sp
@accu_magic = 0
end
@counter -= 1
refresh
end
end
end

#==============================================================================
# ** Sprite_Battler
#==============================================================================

class Sprite_Battler
#--------------------------------------------------------------------------
# * Alias Methods
#--------------------------------------------------------------------------
alias_method :bar_initialize_init, :initialize
alias_method :bar_dispose_dispose, :dispose
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(viewport, battler = nil)
# Original Initialization
bar_initialize_init(viewport, battler)
# Create bar sprites
@hp_bar = Sprite_HP_Bar.new(viewport, @battler)
@sp_bar = Sprite_SP_Bar.new(viewport, @battler)
end
#--------------------------------------------------------------------------
# * Dispose
#--------------------------------------------------------------------------
def dispose
# Original Method
bar_dispose_dispose
# Dispose Battler Bars
@hp_bar.dispose
@sp_bar.dispose
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
super
# If the HP bar sprite doesn't have a battler, assign one to it
if @battler != nil && @hp_bar.battler == nil
@hp_bar.battler = @battler
@hp_bar.setup
end
# If the SP bar sprite doesn't have a battler, assign one to it
if @battler != nil && @sp_bar.battler == nil
@sp_bar.battler = @battler
@sp_bar.setup
end
# If battler is changed, notify the bar sprites
if @battler != @hp_bar.battler
@hp_bar.battler = @battler
@sp_bar.battler = @battler
@hp_bar.setup
@sp_bar.setup
end
@hp_bar.update
@sp_bar.update
# If battler is nil
if @battler == nil
self.bitmap = nil
loop_animation(nil)
return
end
# If file name or hue are different than current ones
if @battler.battler_name != @battler_name or
@battler.battler_hue != @battler_hue
# Get and set bitmap
@battler_name = @battler.battler_name
@battler_hue = @battler.battler_hue
self.bitmap = RPG::Cache.battler(@battler_name, @battler_hue)
@width = bitmap.width
@height = bitmap.height
self.ox = @width / 2
self.oy = @height
# Change opacity level to 0 when dead or hidden
if @battler.dead? or @battler.hidden
self.opacity = 0
end
end
# If animation ID is different than current one
if @battler.damage == nil and
@battler.state_animation_id != @state_animation_id
@state_animation_id = @battler.state_animation_id
loop_animation($data_animations[@state_animation_id])
end
# If actor which should be displayed
if @battler.is_a?(Game_Actor) and @battler_visible
# Bring opacity level down a bit when not in main phase
if $game_temp.battle_main_phase
self.opacity += 3 if self.opacity < 255
else
self.opacity -= 3 if self.opacity > 207
end
end
# Blink
if @battler.blink
blink_on
else
blink_off
end
# If invisible
unless @battler_visible
# Appear
if not @battler.hidden and not @battler.dead? and
(@battler.damage == nil or @battler.damage_pop)
appear
@hp_bar.appear
@battler_visible = true
end
end
# If visible
if @battler_visible
# Escape
if @battler.hidden
$game_system.se_play($data_system.escape_se)
escape
@battler_visible = false
end
# White flash
if @battler.white_flash
whiten
@battler.white_flash = false
end
# Animation
if @battler.animation_id != 0
animation = $data_animations[@battler.animation_id]
#@hp_bar.duration = animation.frame_max
animation(animation, @battler.animation_hit)
@battler.animation_id = 0
end
# Damage
if @battler.damage_pop
damage(@battler.damage, @battler.critical)
@hp_bar.damage_dealing(@battler.damage)
@battler.damage = nil
@battler.critical = false
@battler.damage_pop = false
end
# Using Skill
if @battler.magic_casting
@sp_bar.skill_use(@battler.sp_used)
@battler.sp_used = nil
@battler.magic_casting = false
end
# Collapse
if @battler.damage == nil and @battler.dead?
if @battler.is_a?(Game_Enemy)
$game_system.se_play($data_system.enemy_collapse_se)
else
$game_system.se_play($data_system.actor_collapse_se)
end
collapse
@hp_bar.battler = nil
@hp_bar.collapse
@sp_bar.collapse
@battler_visible = false
end
end
# Set sprite coordinates
self.x = @battler.screen_x
self.y = @battler.screen_y
self.z = @battler.screen_z
end
end

#==============================================================================
# ** Game_Battler
#==============================================================================

class Game_Battler
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :magic_casting # sp bar update flag
attr_accessor :sp_used # skill points used
#--------------------------------------------------------------------------
# * Alias
#--------------------------------------------------------------------------
alias bar_initialize_init initialize
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize
# Added variables
@magic_casting = false
@sp_used = nil
# Original method execution
bar_initialize_init
end
end

#==============================================================================
# ** Scene_Battle
#==============================================================================

class Scene_Battle
#--------------------------------------------------------------------------
# * Make Skill Action Results
#--------------------------------------------------------------------------
def make_skill_action_result
# Get skill
@skill = $data_skills[@active_battler.current_action.skill_id]
# If not a forcing action
unless @active_battler.current_action.forcing
# If unable to use due to SP running out
unless @active_battler.skill_can_use?(@skill.id)
# Clear battler being forced into action
$game_temp.forcing_battler = nil
# Shift to step 1
@phase4_step = 1
return
end
end
# Use up SP
@active_battler.sp -= @skill.sp_cost
# Set amount used for use with sp bar
@active_battler.sp_used = @skill.sp_cost
# Refresh status window
@status_window.refresh
# Show skill name on help window
@help_window.set_text(@skill.name, 1)
# Set animation ID
@animation1_id = @skill.animation1_id
@animation2_id = @skill.animation2_id
# Set command event ID
@common_event_id = @skill.common_event_id
# Set target battlers
set_target_battlers(@skill.scope)
# Apply skill effect
for target in @target_battlers
target.skill_effect(@active_battler, @skill)
end
end
#--------------------------------------------------------------------------
# * Frame Update (main phase step 3 : animation for action performer)
#--------------------------------------------------------------------------
def update_phase4_step3
# Animation for action performer (if ID is 0, then white flash)
if @animation1_id == 0
@active_battler.white_flash = true
else
@active_battler.animation_id = @animation1_id
@active_battler.animation_hit = true
end
# Set flag to update sp bar sprite
if @active_battler.sp_used != nil
@active_battler.magic_casting = true
end
# Shift to step 4
@phase4_step = 4
end
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rotimikid's Enemy level Script ShockWave 0 2,357 08-24-2007, 01:00 PM
Last Post: ShockWave
  Enemy Detection System GubiD 0 2,024 08-12-2007, 01:00 PM
Last Post: GubiD
  Actor Enemy HP Bars Raziel 0 2,755 07-14-2006, 01:00 PM
Last Post: Raziel



Users browsing this thread: