Advertisement
TheSixth

Simple 1-Point Based HP HUD

Jul 5th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.84 KB | None | 0 0
  1. #===============================================================================
  2. # * [ACE] Simple 1-Point Based HP HUD
  3. #===============================================================================
  4. # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
  5. # * Version: 1.0
  6. # * Updated: 06/06/2016
  7. # * Requires: ---------
  8. #-------------------------------------------------------------------------------
  9. # * < Change Log >
  10. #-------------------------------------------------------------------------------
  11. # * Version 1.0 (06/06/2016)
  12. #   - Initial release.
  13. #-------------------------------------------------------------------------------
  14. # * < Description >
  15. #-------------------------------------------------------------------------------
  16. # * This script will make a HP HUD on the map which will, obviously, display
  17. #   the party leader's HP.
  18. # * This script is made for games which use 1-point based HP.
  19. #   What this means? Well, 1 point of HP is one picture displayed on the HUD.
  20. #   Imagine if you got 500 HP, that would be 500 HP image, so yeah...
  21. #   Only use this if your actors got way less HP in your game!
  22. # * Horizontal, vertical, and custom placement for your HP images!
  23. # * Flash effect on the affected HP images for gaining or losing HP (optional)!
  24. # * Show/hide the HUD with a switch!
  25. #-------------------------------------------------------------------------------
  26. # * < Installation >
  27. #-------------------------------------------------------------------------------
  28. # * Place this script between Materials and Main!
  29. #-------------------------------------------------------------------------------
  30. # * < Compatibility Info >
  31. #-------------------------------------------------------------------------------
  32. # * No known incompatibilities.
  33. #-------------------------------------------------------------------------------
  34. # * < Known Issues >
  35. #-------------------------------------------------------------------------------
  36. # * No known issues.
  37. #-------------------------------------------------------------------------------
  38. # * < Terms of Use >
  39. #-------------------------------------------------------------------------------
  40. # * Free to use for whatever purposes you want.
  41. # * Credit me (Sixth) in your game, pretty please! :P
  42. # * Posting modified versions of this script is allowed as long as you notice me
  43. #   about it with a link to it!
  44. #===============================================================================
  45. $imported = {} if $imported.nil?
  46. $imported["Sixth1PHPHUD"] = true
  47. #===============================================================================
  48. # Settings:
  49. #===============================================================================
  50. module HPHUD
  51.   #-----------------------------------------------------------------------------
  52.   # HUD Settings:
  53.   #-----------------------------------------------------------------------------
  54.   # This is the setting area. Who would have guessed, right? :P
  55.   #
  56.   # I won't explain the obvious settings, those will only get a one-liner
  57.   # layour explanation besides them.
  58.   #
  59.   # The not-so-obvious settings:
  60.   #
  61.   # - :spacing => value,
  62.   # Actually, this is quite obvious, but just to be sure...
  63.   # This only works if you set the :type setting to :horz or :vert!
  64.   # Will NOT work if the :type is set to :spec!
  65.   #
  66.   # - :type => :horz/:vert/:spec,
  67.   # This decides the layout of the HP images.
  68.   # :horz = Horizontally lined up images.
  69.   # :vert = Vertically lined up images.
  70.   # :spec = Specific position settings for each HP image.
  71.   #         Uses the extra :spec setting to get the position of the images!
  72.   # So, the :spec setting will let you place the HP images wherever you want,
  73.   # not following any pre-made layout.
  74.   #
  75.   # - :spec => { * specific position settings here *},
  76.   # And this is where the :spec type HUD gets the position for your HP images
  77.   # on the HUD. Each position setting here corresponds to 1 HP.
  78.   # You must make at least the same amount of position settings here than the
  79.   # maximum amount of HP your actors can get!
  80.   # The layout is simple:
  81.   #   index => [x,y],
  82.   # The index must start from 0 and increments by 1 for each HP shown on the
  83.   # HUD!
  84.   #
  85.   # - :flash => { * flash settings here *},
  86.   # These are the flash effect settings.
  87.   # When the player gains or lose HP, the affected HP images on the HUD can be
  88.   # set to flash for a brief (or long? o.o) time.
  89.   # The :gain setting is the flash effect for gained HP points, while the :lose
  90.   # setting is for the lost HP points.
  91.   # The :color setting is the color of the flash effect, uses RGBA codes!
  92.   # The :dur setting is the duration of the flash effect in frames.
  93.   # The duration setting for the :gain effect will directly set the flash effect
  94.   # to that duration.
  95.   # The duration setting for the :lose effect, however, will be divided by 3,
  96.   # and the result will decide the actual duration of the effect. This is
  97.   # because simply letting the fade effect to finish and make the HP images
  98.   # disappear after that looked weird, so I decided to gradually fade out the
  99.   # images, and they should disappear when the flash effect reaches it's highest
  100.   # color (so, during the peak of the flash effect).
  101.   # You can also disable the flash effects for both types (:gain/:lose) or for
  102.   # only one type. To disable the effect for only one type, comment out the
  103.   # line where the setting for it is. To disable all flash effects, either
  104.   # comment out both lines for both types, or comment out this whole setting.
  105.   #-----------------------------------------------------------------------------
  106.   HUD = {
  107.     :toggle => 10,     # Switch ID. This switch can be used to toggle the HUD.
  108.     :img => "hp_icon", # Image file name. Must be in "Graphics/Pictures/"!
  109.     :pos => [20,10],   # HUD position.
  110.     :spacing => 2,     # Spacing between each HP images.
  111.     :z => 200,         # The Z level of the HP images.
  112.     :type => :horz,    # Type of the HP image placement.
  113.     :flash => {        # Flash settings.
  114.       :gain => {:color => Color.new(100,250,100,255), :dur => 30},
  115.       :lose => {:color => Color.new(250,250,100,255), :dur => 40},
  116.     },
  117.     :spec => { # Specific HP image position settings. Used with the :spec type!
  118.       0 => [20,10],
  119.       1 => [43,14],
  120.       2 => [66,10],
  121.       3 => [89,14],
  122.       4 => [112,10],
  123.       5 => [135,14],
  124.       6 => [158,10],
  125.       7 => [181,14],
  126.       8 => [204,10],
  127.       9 => [227,14],
  128.      10 => [250,10],
  129.      11 => [273,10],
  130.      12 => [296,10],
  131.      # <-- Add more position setting here if needed!
  132.     },
  133.   }
  134.  
  135. end
  136. #===============================================================================
  137. # End of settings! O.o
  138. #===============================================================================
  139.  
  140. class HP_HUD
  141.  
  142.   attr_accessor :visible
  143.  
  144.   def initialize(data,id=0)
  145.     @data = data
  146.     @id = id
  147.     @actor = $game_party.members[id]
  148.     @old_hp = @actor.hp
  149.     @visible = $game_switches[HPHUD::HUD[:toggle]]
  150.     init_hp_imgs
  151.   end
  152.  
  153.   def init_hp_imgs
  154.     @imgs = {}; @disp = {}
  155.     @actor.hp.times do |i|
  156.       init_img(i,false)
  157.     end
  158.   end
  159.  
  160.   def init_img(i,flash=true)
  161.     @imgs[i] = Sprite.new
  162.     @imgs[i].visible = @visible
  163.     @imgs[i].bitmap = Cache.picture(@data[:img]).clone
  164.     @imgs[i].z = @data[:z]
  165.     case @data[:type]
  166.     when :horz
  167.       @imgs[i].x = @data[:pos][0] + i * (@imgs[i].bitmap.width + @data[:spacing])
  168.       @imgs[i].y = @data[:pos][1]
  169.     when :vert
  170.       @imgs[i].x = @data[:pos][0]
  171.       @imgs[i].y = @data[:pos][1] + i * (@imgs[i].bitmap.height + @data[:spacing])
  172.     when :spec
  173.       @imgs[i].x = @data[:pos][0] + @data[:spec][i][0]
  174.       @imgs[i].y = @data[:pos][1] + @data[:spec][i][1]
  175.     end
  176.     if flash && @data[:flash] && @data[:flash][:gain]
  177.       @imgs[i].flash(*@data[:flash][:gain].values)
  178.     end
  179.   end
  180.  
  181.   def toggle(state)
  182.     @imgs.each_value {|sp| sp.visible = state }
  183.     @visible = state
  184.   end
  185.  
  186.   def update
  187.     @imgs.each_value {|sp| sp.update }
  188.     if @old_hp < @actor.hp
  189.       change = @actor.hp - @old_hp
  190.       change.times do |i|
  191.         init_img(@imgs.size)
  192.       end
  193.       @old_hp = @actor.hp
  194.     end
  195.     if @old_hp > @actor.hp
  196.       change = @old_hp - @actor.hp
  197.       change.times do |i|
  198.         if @data[:flash] && @data[:flash][:lose]
  199.           @imgs[@imgs.size-1-i].flash(*@data[:flash][:lose].values)
  200.           @disp[@imgs.size-1-i] = @data[:flash][:lose][:dur]/3
  201.         else
  202.           dispose_img(@imgs.size-1)
  203.         end
  204.       end
  205.       @old_hp = @actor.hp
  206.     end
  207.     @disp.each do |key,val|
  208.       if @disp[key] > 0
  209.         @disp[key] -= 1
  210.         if @disp[key] <= 0
  211.           @imgs[key].opacity = 0
  212.         else
  213.           @imgs[key].opacity = 255 - 255/@disp[key]
  214.         end
  215.       else
  216.         dispose_img(key)
  217.         @disp.delete(key)
  218.       end
  219.     end
  220.   end
  221.  
  222.   def dispose_img(i)
  223.     return if @imgs[i].nil? || @imgs[i].disposed?
  224.     @imgs[i].bitmap.dispose
  225.     @imgs[i].dispose
  226.     @imgs.delete(i)
  227.   end
  228.  
  229.   def dispose
  230.     @imgs.each_value {|sp| sp.bitmap.dispose; sp.dispose}
  231.   end
  232.  
  233. end
  234.  
  235. class Spriteset_Map
  236.  
  237.   alias add_hp_hud7735 initialize
  238.   def initialize
  239.     add_hp_hud7735
  240.     init_hp_hud
  241.   end
  242.  
  243.   def init_hp_hud
  244.     @hp_hud = HP_HUD.new(HPHUD::HUD) if $game_party.members.size > 0
  245.   end
  246.  
  247.   alias update_hp_hud6625 update
  248.   def update
  249.     update_hp_hud6625
  250.     if @hp_hud
  251.       if @hp_hud.visible != $game_switches[HPHUD::HUD[:toggle]]
  252.         @hp_hud.toggle($game_switches[HPHUD::HUD[:toggle]])
  253.       end
  254.       @hp_hud.update
  255.     end
  256.   end
  257.  
  258.   alias disp_hp_hud8826 dispose
  259.   def dispose
  260.     @hp_hud.dispose if @hp_hud
  261.     disp_hp_hud8826
  262.   end
  263.  
  264. end
  265. #==============================================================================
  266. # !!END OF SCRIPT - OHH, NOES!!
  267. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement