Advertisement
Vlue

Basic Damage Popup

Jan 21st, 2014
3,491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.53 KB | None | 0 0
  1. #Basic Damage Popup v1.3b
  2. #----------#
  3. #Features: What the title says, very simple damage popup
  4. #
  5. #Usage:    Plug and play, customize as needed
  6. #
  7. #----------#
  8. #-- Script by: V.M of D.T
  9. #
  10. #- Questions or comments can be:
  11. #    given by email: sumptuaryspade@live.ca
  12. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  13. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  14. #
  15. #- Free to use in any project with credit given, donations always welcome!
  16.  
  17. $imported = {} if $imported.nil?
  18. $imported[:BasicDamagePopup] = true
  19.  
  20. class Damage_Popup < Sprite_Base
  21.  
  22.   #Font Details:
  23.   FONT_NAME = "Arial"
  24.   FONT_BOLD = false
  25.   FONT_SHADOW = false
  26.   FONT_BASE_SIZE = 24
  27.   #If you want to use a bitmap of numbers instead:
  28.   #Bitmap is to be named Numbers.png and placed in Graphics/System
  29.   USE_BITMAP = false
  30.   #Adjust the spacing between bitmap numbers
  31.   PADDING = 10
  32.   #Whether to change the size of the font based on weakness/resistance
  33.   CHANGE_SIZE = true
  34.   FONT_SIZE_CHANGE = 6
  35.   #Whether the popup should move along the x or y axis respectively
  36.   POPUP_MOVE = true
  37.   POPUP_BOUNCE = true
  38.   #Different colours for the popups
  39.   USE_ELEMENT_COLORS = true
  40.   NORMAL_COLOR = Color.new(255,255,255)
  41.   HEAL_COLOR = Color.new(0,255,0)
  42.   MISS_COLOR = Color.new(255,255,255)
  43.   STATE_COLOR = Color.new(255,100,255)
  44.  
  45.   #Set the colors for each element, it goes: element_id => Color.new(r,g,b),
  46.   ELEMENT_COLOR = {
  47.     3 => Color.new(255,0,0),
  48.     4 => Color.new(100,100,255),
  49.     5 => Color.new(255,255,0),
  50.     6 => Color.new(0,0,175),
  51.     7 => Color.new(255,175,50),
  52.     8 => Color.new(175,255,175),
  53.     9 => Color.new(255,255,255),
  54.    10 => Color.new(150,10,150),
  55.    }
  56.  
  57.   def initialize(viewport = nil)
  58.     super(viewport)
  59.     @damage = 0
  60.     @timer = 60
  61.     @x_speed = 0
  62.     @y_speed = 0
  63.     @element_id = 0
  64.   end
  65.   def setup_damage(target,item,subject)
  66.     @damage = target.result.hp_damage
  67.     @damage = "Failed" if !target.result.success
  68.     @damage = "Missed" if target.result.missed
  69.     @damage = "Evaded" if target.result.evaded
  70.     @damage = " " if @damage == 0
  71.     @element_id = item.damage.element_id
  72.     setup_position(target,item,subject)
  73.   end
  74.   def setup_position(target,item,subject)
  75.     font_size = FONT_BASE_SIZE
  76.     if @damage.is_a?(Integer)
  77.       val = @damage
  78.       val *= item_element_rate(subject, item, target)
  79.       if CHANGE_SIZE
  80.         font_size += FONT_SIZE_CHANGE
  81.         font_size += FONT_SIZE_CHANGE if val > @damage
  82.         font_size -= FONT_SIZE_CHANGE if val < @damage
  83.         font_size += FONT_SIZE_CHANGE if target.result.critical
  84.       end
  85.     end
  86.     fake_bitmap = Cache.battler(target.battler_name, target.battler_hue)
  87.     if $imported[:VlueAnimatedBattlers] && target.get_anim("Idle")
  88.       fake_bitmap = Cache.battler(target.get_anim("Idle")[0],0)
  89.       width = Animation::ANIMATION_FILES[target.get_anim("Idle")[0]][0]
  90.       width = fake_bitmap.width / width
  91.       height = Animation::ANIMATION_FILES[target.get_anim("Idle")[0]][1]
  92.       height = fake_bitmap.height / height
  93.       fake_bitmap = Bitmap.new(width,height)
  94.     end
  95.     fake_bitmap.font.size = font_size
  96.     self.bitmap = Bitmap.new(fake_bitmap.text_size(@damage).width*3+24,48*2)
  97.     self.bitmap.font.name = FONT_NAME
  98.     self.bitmap.font.size = font_size
  99.     self.bitmap.font.bold = FONT_BOLD
  100.     self.bitmap.font.shadow = FONT_SHADOW
  101.     self.x = target.screen_x - self.width / 2 - 24
  102.     self.y = target.screen_y - fake_bitmap.height
  103.     @x_speed = rand(3) - 1 if POPUP_MOVE
  104.     @y_speed = 2 if POPUP_BOUNCE
  105.   end
  106.   def setup_state(target, state)
  107.     self.visible = false if state.id == 1
  108.     @damage = state.name
  109.     @icon = state.icon_index
  110.     setup_position(target,nil,nil)
  111.   end
  112.   def update
  113.     update_regular if !USE_BITMAP
  114.     update_bitmap if USE_BITMAP
  115.   end
  116.   def update_color
  117.     bitmap.font.color = NORMAL_COLOR
  118.     bitmap.font.color = get_element_color if USE_ELEMENT_COLORS
  119.     bitmap.font.color = HEAL_COLOR if @damage.is_a?(Integer) && @damage < 0
  120.     bitmap.font.color = MISS_COLOR if @damage == "Missed"
  121.     bitmap.font.color = STATE_COLOR if @icon
  122.   end
  123.   def update_bounce
  124.     self.y -= @y_speed if @timer % 2 == 0 && POPUP_BOUNCE
  125.     @y_speed -= 0.1
  126.     @timer -= 1
  127.     self.x += @x_speed if @timer % 2 == 0
  128.     self.opacity -= 6 if @timer < 30
  129.   end
  130.   def update_regular
  131.     bitmap.clear
  132.     update_color
  133.     text = @damage.abs if @damage.is_a?(Integer)
  134.     text = @damage if @damage.is_a?(String)
  135.     draw_icon(@icon,self.width/4,self.height/2-12) if @icon
  136.     bitmap.draw_text(24,0,self.width,self.height,text,1)
  137.     update_bounce
  138.   end
  139.   def update_bitmap
  140.     bitmap.clear
  141.     update_color
  142.     text = @damage.abs if @damage.is_a?(Integer)
  143.     return update_regular unless text
  144.     self.color = bitmap.font.color
  145.     self.color.alpha = 75
  146.     text = text.to_s;xx = 0
  147.     dmg_bmp = SceneManager.scene.dam_bmps
  148.     rect = Rect.new(0,0,dmg_bmp[0].width,dmg_bmp[0].height)
  149.     text.length.times do |i|
  150.       bitmap.blt(bitmap.width / 2 + xx,bitmap.height/2,dmg_bmp[text[i].to_i],rect)
  151.       xx+=PADDING
  152.     end
  153.     update_bounce
  154.   end
  155.   def get_element_color
  156.     ELEMENT_COLOR[@element_id].nil? ? NORMAL_COLOR : ELEMENT_COLOR[@element_id]
  157.   end
  158.   def done
  159.     @timer < 0
  160.   end
  161.   def item_element_rate(user, item, subject)
  162.     if item.damage.element_id < 0
  163.       user.atk_elements.empty? ? 1.0 : subject.elements_max_rate(user.atk_elements)
  164.     else
  165.       subject.element_rate(item.damage.element_id)
  166.     end
  167.   end
  168.   def draw_icon(icon_index, x, y, enabled = true)
  169.     temp_bitmap = Cache.system("Iconset")
  170.     rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  171.     bitmap.blt(x, y, temp_bitmap, rect)
  172.   end
  173. end
  174.  
  175. class Scene_Battle
  176.   alias dam_pop_init start
  177.   alias dam_pop_update update_basic
  178.   def start(*args)
  179.     dam_pop_init(*args)
  180.     @damage_popups = []
  181.     return unless Damage_Popup::USE_BITMAP
  182.     @damage_number_bitmaps = [0]
  183.     bitmap = Cache.system("Numbers")
  184.     x = 0;y = 0;w = bitmap.width / 5;h = bitmap.height / 2
  185.     10.times do
  186.       rect = Rect.new(x,y,w,h)
  187.       bmp = Bitmap.new(w,h)
  188.       bmp.blt(0,0,bitmap,rect)
  189.       @damage_number_bitmaps.push(bmp)
  190.       @damage_number_bitmaps[0] = bmp
  191.       x += w
  192.       if x == w*5
  193.         x = 0;y += h
  194.       end
  195.     end
  196.   end
  197.   def dam_bmps
  198.     @damage_number_bitmaps
  199.   end
  200.   def add_damage_popup(target,item,subject)
  201.     @damage_popups.push(Damage_Popup.new(nil))
  202.     @damage_popups[-1].setup_damage(target,item,subject)
  203.   end
  204.   def add_state_popup(target, state)
  205.     @damage_popups.push(Damage_Popup.new(nil))
  206.     @damage_popups[-1].setup_state(target, state)
  207.   end
  208.   def update_basic(*args)
  209.     dam_pop_update(*args)
  210.     @damage_popups.each do |popup| popup.update end
  211.     @damage_popups.each_index do |index|
  212.       if @damage_popups[index].done
  213.         @damage_popups[index].bitmap.clear
  214.         @damage_popups[index] = nil
  215.       end
  216.     end
  217.     @damage_popups.compact!
  218.   end
  219.   def apply_item_effects(target, item)
  220.     target.item_apply(@subject, item)
  221.     refresh_status
  222.     if $imported[:VlueAnimatedBattlers]
  223.       add_damage_popup(target, item, @subject) if target.result.ignore.nil?
  224.     else
  225.       add_damage_popup(target, item, @subject)
  226.     end
  227.     target.result.added_state_objects.each do |state|
  228.       add_state_popup(target, state)
  229.     end
  230.     @log_window.display_action_results(target, item)
  231.   end
  232. end
  233.  
  234. class Game_Actor
  235.   def screen_x
  236.     return -50
  237.   end
  238.   def screen_y
  239.     return -50
  240.   end
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement