CrowMakerVX

Window ShakeNFlash

Jul 26th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.07 KB | None | 0 0
  1. ################################################################################
  2. #                 WINDOW SHAKE N FLASH
  3. #                       By: Ixfuru
  4. ################################################################################
  5. # This short little script is actually a tool for scripters, though I imagine it
  6. # can be called from anywhere, as long as there is a window in the given scene.
  7. # Basically, what it does is call a flash or shake of a particular
  8. # window.  The flash color and flash duration are customizable, or you
  9. # can create your own color and duration before calling the methods and send
  10. # them as arguments.  Just keep in mind that you must send a three-value array
  11. # as the color argument.
  12. #
  13. ################################################################################
  14. # CREDITS:
  15. #
  16. # You can use as you like, commercially or non-commercially.  Just give me,
  17. # "Ixfuru" credit when you do.
  18. #
  19. # ALSO:
  20. #
  21. # You MUST CREDIT 'Mithran' for his creation of the basic method used to
  22. # call the flash.  I merely took out a few lines and adjusted the creation
  23. # of the viewport to match the size of the window provided when calling it.
  24. ################################################################################
  25. #
  26. # Here's a few ways you can call the methods:
  27. #
  28. #-------------------------------------------------------------------------------
  29. # FLASHING
  30. #-------------------------------------------------------------------------------
  31. # To flash the window of a scene, call:
  32. #
  33. # flash_window(window, color, duration)
  34. #
  35. # For the window argument pass the array like this:
  36. #                 [red, green, blue]
  37. #
  38. # So if you call:
  39. #
  40. #   flash_window(@gold_window, [0, 255, 0], 40)
  41. #
  42. # You'd be telling the window to flash the gold window green for 40 frames.
  43. #
  44. # To use the default color and duration of flash, just call:
  45. #
  46. #    flash_window(@gold_window)
  47. #-------------------------------------------------------------------------------
  48. # SHAKING
  49. #-------------------------------------------------------------------------------
  50. # To shake a window, just call this method:
  51. #
  52. # shake_window(window, strength, speed, axis)
  53. #
  54. # Again, pass the window variable as the 'window' argument.  Then the strength
  55. # should be set to the amount of pixels you wish for the window to shake.
  56. # Speed will control how many pixels per frame the window shakes.  And axis
  57. # is used to determine if you want the window to shake from side to side
  58. # or up and down.  Use 0 for side to side, and 1 for up and down.
  59. #
  60. #  So, if you called:
  61. #        
  62. # shake_window(@gold_window, 50, 7, 0)
  63. #
  64. # The gold window would be shook by 50 pixels, seven pixels per frame from side
  65. # to side.
  66. #
  67. # The default of strength is 25, speed is 5 and axis is 0, so by default you
  68. # can call:
  69. #
  70. # shake_window(@gold_window)
  71. #-------------------------------------------------------------------------------
  72. #
  73. # FURTHERMORE, for scripters:
  74. #
  75. # You can call these methods from anywhere by using:
  76. #
  77. # $scene.flash_window($scene.window)
  78. # or
  79. # $scene.shake_window($scene.window)
  80. #
  81. # Provided of course that you have the window set as a public instance variable
  82. # in the scene you're calling it from.
  83. #
  84. ################################################################################
  85. #SETTINGS:
  86. ################################################################################
  87. module Ixfuru
  88.  
  89.   module ShakeNFlash
  90.    
  91.     DEFAULT_FLASH_COLOR = [255, 0, 0]
  92.     DEFAULT_FLASH_DURATION = 40
  93.    
  94.   end
  95.  
  96. end
  97.  
  98. # Don't edit below unless you know what you're doing!
  99. ################################################################################
  100. #*******************************************************************************
  101. #                       SCENE BASE
  102. ################################################################################
  103. class Scene_Base
  104.  
  105.    #-----------------------------------------------------------------------------
  106.   # Shake Window
  107.   # New method used to shake a specific window.
  108.   #
  109.   # window: the window you want to shake
  110.   # strength: max pixels to move
  111.   # speed: pixels to move per every 2 frames
  112.   # axis: 0 for x axis, 1 for y
  113.   #-----------------------------------------------------------------------------
  114.   def shake_window(window, strength = 25, speed = 5, axis = 0)
  115.     case axis
  116.     when 0
  117.       point = window.x
  118.       until window.x >= point + strength
  119.         window.x += speed
  120.         Graphics.wait(1)
  121.       end
  122.       until window.x <= point - strength
  123.         window.x -= speed
  124.         Graphics.wait(1)
  125.       end
  126.       until window.x >= point
  127.         window.x += speed
  128.         Graphics.wait(1)
  129.       end
  130.       window.x = point
  131.     when 1
  132.       point = window.y
  133.       until window.y >= point + strength
  134.         window.y += speed
  135.         Graphics.wait(1)
  136.       end
  137.       until window.y <= point - strength
  138.         window.y -= speed
  139.         Graphics.wait(1)
  140.       end
  141.       until window.y >= point
  142.         window.y += speed
  143.         Graphics.wait(1)
  144.       end
  145.       window.y = point
  146.     end
  147.   end
  148.  
  149.   #-----------------------------------------------------------------------------
  150.   # Flash Window (basic process was written by Mithran, I just linked the process
  151.   #                to link the viewport to a specific window!)
  152.   # window: the window you want to flash
  153.   # color: pass an array of 3 values [red, green, blue]
  154.   # duration: how long before the flash is over
  155.   #-----------------------------------------------------------------------------
  156.   def flash_window(window, color = Ixfuru::ShakeNFlash::DEFAULT_FLASH_COLOR, duration = Ixfuru::ShakeNFlash::DEFAULT_FLASH_DURATION)
  157.     viewport = Viewport.new(window.x, window.y, window.width, window.height)
  158.     flash_color = Color.new(color[0], color[1], color[2])
  159.     flash_duration = duration
  160.     loop do
  161.       viewport.color = flash_color
  162.       Graphics.update
  163.       break if flash_duration == 0
  164.       flash_color.alpha *= (flash_duration - 1) / flash_duration.to_f
  165.       flash_duration -= 1 # decrease flash duration
  166.     end
  167.     viewport.dispose
  168.   end
  169.  
  170. end
Advertisement
Add Comment
Please, Sign In to add comment