Advertisement
DrDhoom

[RGSS3] Sprite Enhancement

Jun 3rd, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.55 KB | None | 0 0
  1. #==============================================================================
  2. #
  3. # • Dhoom Sprite Enhancement v.1.4
  4. #   drd-workshop.blogspot.com
  5. # -- Last Updated: 02.04.2015
  6. # -- Requires: None
  7. #
  8. #==============================================================================
  9. # This is for scripter only. Add a bunch of method for animating sprites.
  10. #==============================================================================
  11.  
  12. class Sprite
  13.  
  14.   alias dhoom_battlehud_spr_initialize initialize
  15.   def initialize(*args)
  16.     dhoom_battlehud_spr_initialize(*args)
  17.     @blink = false
  18.     @glow = false
  19.     @zoom = false
  20.     @shake = false
  21.   end
  22.  
  23. #------------------------------------------------------------------------------
  24. # Change the opacity to make it look like it is blinking
  25. #------------------------------------------------------------------------------
  26.   def blink(duration, force = false)
  27.     return if @blink && !force
  28.     @blink = true
  29.     @blink_duration = duration
  30.     @blink_current_duration = duration
  31.     @blink_phase = 0
  32.     @blink_opacity = self.opacity
  33.   end
  34.  
  35. #------------------------------------------------------------------------------
  36. # Make the sprite glow. Use a cloned sprite for the glowing effect.
  37. #------------------------------------------------------------------------------
  38.   def glow(duration, zoom, force = false)
  39.     return if @glow && !force
  40.     @glow = true
  41.     @glow_duration = duration
  42.     @glow_current_duration = duration
  43.     @glow_zoom = (zoom-1.0)/@glow_duration
  44.     @glow_opacity = 255.0/@glow_duration
  45.     @glow_sprite = make_clone
  46.     @glow_sprite.x += @glow_sprite.ox
  47.     @glow_sprite.y += @glow_sprite.oy
  48.   end
  49.  
  50. #------------------------------------------------------------------------------
  51. # Zoom in and zoom out.
  52. #------------------------------------------------------------------------------
  53.   def zoom(duration, value, force = false)
  54.     return if @zoom && !force
  55.     @zoom = true
  56.     @zoom_duration = duration
  57.     @zoom_total_duration = duration
  58.     @zoom_valuex = (value-self.zoom_x)/(duration/2)
  59.     @zoom_valuey = (value-self.zoom_y)/(duration/2)
  60.     @zoom_ori_zvaluex = self.zoom_x
  61.     @zoom_ori_zvaluey = self.zoom_y
  62.     @zoom_x = self.x
  63.     @zoom_y = self.y
  64.     @zoom_phase = 0
  65.     self.ox = self.width/2
  66.     self.oy = self.height/2
  67.     self.x += self.width/2
  68.     self.y += self.height/2
  69.   end
  70.  
  71. #------------------------------------------------------------------------------
  72. # Shake the sprite in its position. Mode: 0 = x and y, 1 = x only, 2 = y only.
  73. #------------------------------------------------------------------------------
  74.   def shake(duration, value, mode = 0, force = false)
  75.     return if @shake && !force
  76.     @shake = true
  77.     @shake_duration = duration
  78.     @shake_total_duration = duration
  79.     @shake_value = value
  80.     @shake_orix = self.x
  81.     @shake_oriy = self.y
  82.     @shake_mode = mode
  83.   end
  84.  
  85. #------------------------------------------------------------------------------
  86. # Make a clone of this sprite then return it. Useful for manipulating the
  87. # sprite without modifying the real sprite. In this script, I used it for glow
  88. # method.
  89. #------------------------------------------------------------------------------
  90.   def make_clone
  91.     sprite = Sprite.new(self.viewport)
  92.     sprite.bitmap = self.bitmap.dup
  93.     sprite.x = self.x
  94.     sprite.y = self.y
  95.     sprite.z = self.z
  96.     sprite.ox = self.ox
  97.     sprite.oy = self.oy
  98.     sprite.opacity = self.opacity
  99.     sprite.visible = self.visible
  100.     sprite
  101.   end
  102.  
  103.   alias dhoom_battlehud_spr_update update
  104.   def update
  105.     dhoom_battlehud_spr_update
  106.     update_blink if @blink
  107.     update_glow if @glow
  108.     update_zoom if @zoom
  109.     update_shake if @shake
  110.   end
  111.  
  112.   def update_blink
  113.     if @blink_phase == 0
  114.       self.opacity -= @blink_opacity/(@blink_duration/2.0)
  115.     else
  116.       self.opacity += @blink_opacity/(@blink_duration/2.0)
  117.     end
  118.     @blink_current_duration -= 1
  119.     if @blink_current_duration == @blink_duration/2
  120.       @blink_phase = 1
  121.     end
  122.     if @blink_current_duration == 0
  123.       self.opacity = @blink_opacity
  124.       @blink = false
  125.     end
  126.   end
  127.  
  128.   def blink?
  129.     @blink
  130.   end
  131.  
  132.   def update_glow
  133.     @glow_sprite.zoom_x += @glow_zoom
  134.     @glow_sprite.zoom_y += @glow_zoom
  135.     @glow_sprite.opacity -= @glow_opacity
  136.     @glow_current_duration -= 1
  137.     if @glow_current_duration == 0
  138.       @glow_sprite.dispose
  139.       @glow = false
  140.     end
  141.   end
  142.  
  143.   def glow?
  144.     @glow
  145.   end
  146.  
  147.   def update_zoom
  148.     if @zoom_phase == 0
  149.       self.zoom_x += @zoom_valuex
  150.       self.zoom_y += @zoom_valuey
  151.     else
  152.       self.zoom_x -= @zoom_valuex
  153.       self.zoom_y -= @zoom_valuey
  154.     end
  155.     @zoom_duration -= 1
  156.     if @zoom_duration == 0
  157.       self.zoom_x = @zoom_ori_zvaluex
  158.       self.zoom_y = @zoom_ori_zvaluey
  159.       self.x = @zoom_x
  160.       self.y = @zoom_y
  161.       self.ox = 0
  162.       self.oy = 0
  163.       @zoom = false
  164.     elsif @zoom_duration == @zoom_total_duration/2
  165.       @zoom_phase = 1
  166.     end
  167.   end
  168.  
  169.   def update_shake    
  170.     self.x = @shake_orix - rand(@shake_value) + rand(@shake_value) if @shake_mode == 0 || @shake_mode == 1
  171.     self.y = @shake_oriy - rand(@shake_value) + rand(@shake_value) if @shake_mode == 0 || @shake_mode == 2
  172.     @shake_duration -= 1
  173.     if @shake_duration == 0
  174.       self.x = @shake_orix
  175.       self.y = @shake_oriy
  176.       @shake = false
  177.     end
  178.   end
  179.  
  180.   def shake?
  181.     @shake
  182.   end
  183.  
  184.   alias dhoom_battlehud_spr_dispose dispose
  185.   def dispose
  186.     dhoom_battlehud_spr_dispose
  187.     @glow_sprite.dispose if @glow_sprite
  188.   end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement