Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # * [ACE] Smooth Animation System
- #===============================================================================
- # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
- # * Version: 1.5
- # * Updated: 27/06/2019
- # * Requires: Sixth's Smooth Animation Module
- #-------------------------------------------------------------------------------
- # * < Change Log >
- #-------------------------------------------------------------------------------
- # * Version 1.0 (07/10/2016)
- # - Initial release.
- # * Version 1.1 (13/11/2016)
- # - Added a method to skip to a specified frame in the animation.
- # - Added a method to forcibly update an animated object without proceeding
- # the animation. Might come in handy for initializing resumed animations.
- # - Added a class which can be used to run the animation and store the
- # current values, instead of directly modifying the animated object itself.
- # This makes it possible to save these instanced classes as a reference
- # later on, in case you need to resume an animation from the same time and
- # with the same values. The class is named SmoothNum, and it's only purpose
- # is to animate it's @value property.
- # * Version 1.2 (23/12/2016)
- # - Added a method to completely clear all the animations at once.
- # You can choose to finish them or not before clearing them.
- # - All animations will get cleared when starting the title scene from now on.
- # This is to prevent undesired left-over animations for some global objects.
- # * Version 1.3 (17/02/2017)
- # - Fixed a typo that crashes the game if the 'rem_time' method was used.
- # - Fixed the 'animating?' check method. It returned wrong results in some
- # very specific cases.
- # - Added a method to check the current time in the animation as well as the
- # remaining time of it.
- # * Version 1.4 (20/04/2018)
- # - Minor performance update.
- # * Version 1.5 (27/06/2019)
- # - Moved the update method for the animations to the update_basic method of
- # Scene_Base instead of the update method. More compatible this way.
- #-------------------------------------------------------------------------------
- # * < Description >
- #-------------------------------------------------------------------------------
- # * This script will let you animate whatever you want for any object, as long
- # as the thing you want to animate is represented with a numeric value.
- # This means that it can be:
- # - Position change (X and Y).
- # - Size change.
- # - Zoom change.
- # - Opacity change.
- # - Angle change.
- # - And so on, and so on...
- # So, basically anything can be animated!
- # * When I say "animate" here, I mean the ability to smoothly change these
- # properties over time, instead of just setting the value instantly.
- # * Easing animations are possible to make too!
- # * Stop, pause, speed up, slow down, or skip the animations!
- # * Checking method to see if an object is being animated or not.
- # * NOTE: This is a tool meant to be used by experienced scripters!
- # This script will NOT do anything on it's own just by being installed!
- # The scripter must set the animations up in other scripts to see the
- # effects of this script!
- #-------------------------------------------------------------------------------
- # * < Script Calls >
- #-------------------------------------------------------------------------------
- # * To add a new animation for an object, use this script call:
- #
- # SmoothAnim.add_d(object,property,end_value,time,type,mcall)
- #
- # This will let you set the destination value for the animated property
- # (the end_value) directly, hence the name 'add_d' ("add direct").
- #
- # Replace the object with the object you want to animate.
- # This can be anything from Sprites to Windows, Colors, Tones, Rects, etc.
- # NOTE:
- # If the object is disposed or returns nil, nothing will happen!
- # Also, if the object would get disposed while an animation on it still runs,
- # the remaining animation(s) will be discarded automatically!
- #
- # The property is a valid variable of the object you want to animate.
- # This property must have a getter and setter method defined! If there is no
- # such methods, nothing will happen!
- # Must be a string, and the object must have a getter and setter method
- # defined the same way (with the setter method looking like: property= ).
- #
- # The end_value is the value of the property after the end of the animation.
- # In other words, you want to smoothly change the value of the property from
- # whatever it's value currently is to this end value you set here.
- # Must be a numeric value!
- #
- # The time is the time in frames for the animation to complete.
- # This much time the animation will last, and this much time is needed to
- # reach the end value of the property you have set up in the script call.
- # Must be a numeric value!
- #
- # And the type will decide the animation type you want to use.
- # There are many types available to use, elastic, bouncing, exponential, and
- # so on. The linear animation is the default value of this argument.
- # This must be a symbol or a string, and there must be a method named the same
- # way in the SmoothAnim module!
- #
- # And the mcall argument can be used to call a method on any object you want
- # (not just on the animated object) every time the value of the animated
- # property changes. This must be an array with the following structure:
- # [object, method_name, arg1, arg2, arg3, ...]
- # The object here is the object the method will be called on.
- # The method_name is... well, it's the name of the method, obviously.
- # And the following elements in the array will all be arguments for the
- # method you called. These are optional, you don't have to use them if the
- # method got no arguments (you should NOT use them than, actually).
- # This argument is optional. If you omit it, no other methods will be called
- # on any objects.
- #
- # Examples:
- #
- # SmoothAnim.add_d(@help_window,"x",120,90,:bounce_in)
- # Will smoothly move the @help_window object to the X position of 120.
- # The animation will last 90 frames (1 and a half second), and it will use the
- # "bounce_in" easing animation for the movement.
- #
- # SmoothAnim.add_d(@img,"angle",360,180)
- # Will rotate the @img object by 360 angles (a full cycle). The animation
- # lasts 180 frames (3 seconds). It will use the "linear" animation mode
- # (because the type argument is omitted), which is what it's name suggests,
- # a static change in the angle without any special animation effects.
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To add a new animation for an object, use this script call:
- #
- # SmoothAnim.add_c(object,property,change,time,type,mcall)
- #
- # Deja Vu, right? Well, this script call is the same as the above one with a
- # small difference (aside from the obviously different method name).
- # Instead of specifying the end value, here you will specify the value of
- # change (and this is why it's named 'add_c' - "add change").
- #
- # Everything is the same here like in the above script call, except the change
- # argument. That one replaces the end_value argument, and specifies the value
- # of change in the animated property.
- #
- # Examples:
- #
- # SmoothAnim.add_c(@img.src_rect,"width",-200,300,:cubic_out)
- # This will modify the Rect of the @img object (which is a sprite), it's
- # width, to be more specific.
- # Will slowly shrink it by 200 pixels within 300 frames using the "cubic_out"
- # easing animation method.
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To delete an animation on an object, use this script call:
- #
- # SmoothAnim.del(object,finish?,property)
- #
- # With this, you can either stop the animation permanently (so, you won't be
- # able to resume it later!), or instantly skip to the end of the animation,
- # which means that the value of the animated properties will be instantly set
- # to the values they would get after finishing the animation.
- #
- # The object is the same as in the other script calls.
- #
- # The finish? argument can be true or false.
- # If true, the animation will be finished instantly (in other words, it will
- # be skipped to the end).
- # If false, the animation will be permanently deleted and it will NOT be
- # finished, so wherever it stopped, it will stay that way!
- # If you omit this argument, true will be used by default!
- #
- # And the property must be a variable of the object.
- # Additionally, if you want to stop/skip all of the currently playing
- # animations on the object, you can use the symbol :all for this argument.
- # If you want to directly stop/skip a specific variable, this argument must
- # be a string!
- # If you omit this, :all will be used by default!
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To clear all animations at once, use this script call:
- #
- # SmoothAnim.clear(finish?)
- #
- # This will remove all animations on all of your animated objects.
- #
- # Set the finish? to true if you want to finish the animations before
- # clearing them, or set it to false if you want to end them without finishing
- # them. The default value is false.
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * The following script calls are all used to control the speed of the already
- # existing animations on the objects:
- #
- # SmoothAnim.pause(object,property)
- # SmoothAnim.speed_set(object,speed,property)
- # SmoothAnim.speed_up(object,speed,property)
- # SmoothAnim.slow_down(object,speed,property)
- #
- # The name of the methods should tell what they do.
- # Again, if the object is disposed or if they would return nil, these will
- # not do anything!
- # Similarly, if the entered property is not a valid one, nothing happens!
- #
- # The object is the animated object you want to manipulate (speed up, etc.
- #
- # The speed is a numeric value, and that will decide the change in the speed
- # of the animations.
- # The default values are:
- # Set: 1 (1 is the default speed of every animation!)
- # Speed up: 2 (2 will double the speed of the animation - multiplier!)
- # Slow down: 2 (2 will halve the speed of the animation - divider!)
- # Pause: 0 (0 will stop the animation - this can't be changed!
- # This is not really an argument because you can't cahnge it in the
- # script call, just noting here that this will set it to 0.)
- # So, in case you omit this argument, the default will be used.
- #
- # And the property must be a variable of the object.
- # Additionally, if you want to manipulate all of the currently playing
- # animations on the object, you can use the symbol :all for this argument.
- # If you want to directly manipulate a specific variable, this argument must
- # be a string!
- # If you omit this, :all will be used by default!
- #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # * To check if an object is currently being animated or not, use this script
- # call:
- #
- # SmoothAnim.animating?(object,property)
- #
- # Obviously, if the object is disposed or returns nil, this will return false!
- #
- # The property can be the symbol :any, which means that the check will return
- # true if there is any kind of animation on the object, or a string, in which
- # case you will only check for the animation of that specific variable.
- #-------------------------------------------------------------------------------
- # * < Installation >
- #-------------------------------------------------------------------------------
- # * Place this script between Materials and Main!
- #-------------------------------------------------------------------------------
- # * < Compatibility Info >
- #-------------------------------------------------------------------------------
- # * No known incompatibilities.
- #-------------------------------------------------------------------------------
- # * < Known Issues >
- #-------------------------------------------------------------------------------
- # * No known issues.
- #-------------------------------------------------------------------------------
- # * < Terms of Use >
- #-------------------------------------------------------------------------------
- # * Free to use for whatever purposes you want.
- # * Credit me (Sixth) in your game, pretty please! :P
- # * Posting modified versions of this script is allowed as long as you notice me
- # about it with a link to it!
- #===============================================================================
- $imported = {} if $imported.nil?
- $imported["SixthABSSmoothAnim"] = true
- #===============================================================================
- # No Settings! o.O
- #===============================================================================
- module SmoothAnim
- attr_accessor :anim
- @gspd = 1
- # Animation container
- def self.anim
- @anim = {} if @anim.nil?
- return @anim
- end
- # Clears all animations. You can finish them before the purge if needed.
- def self.clear(finish=false)
- self.anim.each do |obj,props|
- if finish
- props.each {|var,info|
- obj.send(var+'=',info[:end])
- info[:mobj].send(info[:mcall],*info[:margs]) if info[:mcall]
- }
- end
- self.anim.delete(obj)
- end
- end
- # Adding an animation for the object - type 1
- def self.add_d(obj,var,dest,time,type=:linear,mcall=nil,*args)
- return if obj.nil? || obj.disposed?
- return unless obj.respond_to?(var+"=")
- self.anim[obj] = {} if self.anim[obj].nil?
- start = obj.send(var,*args)
- mcall = mcall.clone if mcall
- self.anim[obj][var] = {
- :start => start.to_f,
- :end => dest.to_f,
- :change => dest - start,
- :time => time.to_f,
- :type => type.is_a?(Array) ? SmoothAnim.rand_anim(*type) : type,
- :c_time => 0.0,
- :spd => 1.0,
- :mobj => mcall.nil? ? nil : mcall.shift,
- :mcall => mcall.nil? ? nil : mcall.shift,
- :margs => mcall,
- :args => args,
- }
- end
- # Adding an animation for the object - type 2
- def self.add_c(obj,var,change,time,type=:linear,mcall=nil,*args)
- return if obj.nil? || obj.disposed?
- return unless obj.respond_to?(var+"=")
- self.anim[obj] = {} if self.anim[obj].nil?
- start = obj.send(var,*args)
- mcall = mcall.clone if mcall
- self.anim[obj][var] = {
- :start => start.to_f,
- :end => start + change.to_f,
- :change => change.to_f,
- :time => time.to_f,
- :type => type.is_a?(Array) ? SmoothAnim.rand_anim(*type) : type,
- :c_time => 0.0,
- :spd => 1.0,
- :mobj => mcall.nil? ? nil : mcall.shift,
- :mcall => mcall.nil? ? nil : mcall.shift,
- :margs => mcall,
- :args => args,
- }
- end
- # Deletes the animation for the object.
- # Can set the finishing values for the object if needed (making a "skip animation" effect)
- # Omit the last argument to remove all animations for the object
- def self.del(obj,finish=true,var=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if var == :all
- if finish
- self.anim[obj].each {|var,info|
- obj.send(var+'=',info[:end])
- info[:mobj].send(info[:mcall],*info[:margs]) if info[:mcall]
- }
- end
- self.anim.delete(obj)
- else
- return unless self.anim[obj][var]
- if finish
- an = self.anim[obj][var]
- obj.send(var+'=',an[:end])
- an[:mobj].send(an[:mcall],*an[:margs]) if an[:mcall]
- end
- self.anim[obj].delete(var)
- end
- end
- # Gets the remaining time of the animation
- def self.rem_time(obj,prop=:any)
- return 0 if obj.nil? || obj.disposed?
- return 0 unless self.anim[obj]
- if prop == :any
- high = self.anim[obj].max_by {|var,info| info[:c_time] }[1]
- return high[:time] - high[:c_time]
- else
- return 0 unless self.anim[obj][prop]
- andt = self.anim[obj][prop]
- return andt[:time] - andt[:c_time]
- end
- end
- # Gets the current time of the animation
- def self.get_frame(obj,prop=:all)
- return 0 if obj.nil? || obj.disposed?
- return 0 unless self.anim[obj]
- if prop == :all
- high = self.anim[obj].max_by {|var,info| info[:c_time] }[1]
- return high[:c_time]
- else
- return 0 unless self.anim[obj][prop]
- return self.anim[obj][prop][:c_time]
- end
- end
- # Skips to the specified frame of the animation
- def self.set_frame(obj,frame,prop=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if prop == :all
- self.anim[obj].each {|var,info| info[:c_time] = frame }
- else
- return unless self.anim[obj][prop]
- self.anim[obj][prop][:c_time] = frame
- end
- end
- # Pause the animation
- def self.pause(obj,var=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if var == :all
- self.anim[obj].each {|var,info| info[:spd] = 0 }
- else
- return unless self.anim[obj][var]
- self.anim[obj][var][:spd] = 0
- end
- end
- # Sets a speed modifier for all animations
- def self.set_speed_mod(spd=1)
- @gspd = spd
- end
- # Normalize the speed of the animation
- def self.speed_set(obj,spd=1,var=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if var == :all
- self.anim[obj].each {|var,info| info[:spd] = spd }
- else
- return unless self.anim[obj][var]
- self.anim[obj][var][:spd] = spd
- end
- end
- # Speed up the animation by 'spd'
- def self.speed_up(obj,spd=2,var=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if var == :all
- self.anim[obj].each {|var,info| info[:spd] *= spd }
- else
- return unless self.anim[obj][var]
- self.anim[obj][var][:spd] *= spd
- end
- end
- # Slow down the animation by 'spd'
- def self.slow_down(obj,spd=2,var=:all)
- return if obj.nil? || obj.disposed?
- return unless self.anim[obj]
- if var == :all
- self.anim[obj].each {|var,info| info[:spd] /= spd }
- else
- return unless self.anim[obj][var]
- self.anim[obj][var][:spd] /= spd
- end
- end
- # Checks if an object got an animation or not
- def self.animating?(obj,var=:any)
- return false if obj.nil? || obj.disposed?
- return false unless self.anim[obj]
- if var == :any
- return !self.anim[obj].nil? && !self.anim[obj].empty?
- else
- return self.anim[obj][var]
- end
- end
- # Forcibly updates the animated object without proceeding it
- def self.force_update(obj)
- return unless self.anim[obj]
- self.anim[obj].each do |var,an|
- break if obj.nil? || obj.disposed?
- update_force_anim(obj,var,an)
- end
- end
- # Part of the force update method above
- def self.update_force_anim(obj,var,an)
- if an[:c_time] >= an[:time]
- obj.send(var+'=',an[:end])
- an[:mobj].send(an[:mcall],*an[:margs]) if an[:mcall]
- self.anim[obj].delete(var)
- self.anim.delete(obj) if self.anim[obj].empty?
- else
- an[:c_time] = an[:time] if an[:c_time] > an[:time]
- new_val = send(an[:type],an[:start],an[:change],an[:c_time],an[:time])
- obj.send(var+'=',new_val)
- an[:mobj].send(an[:mcall],*an[:margs]) if an[:mcall]
- end
- end
- # Animation update for all objects currently animated
- def self.update
- #p "updating smooth anim!"
- self.anim.each do |obj,data|
- if obj.nil? || obj.disposed?
- self.anim.delete(obj)
- else
- data.each do |var,an|
- break if obj.nil? || obj.disposed?
- update_anim_ex(obj,var,an)
- end
- end
- end
- end
- # Animation process
- def self.update_anim_ex(obj,var,an)
- if an[:c_time] >= an[:time]
- obj.send(var+'=',an[:end])
- an[:mobj].send(an[:mcall],*an[:margs]) if an[:mcall]
- self.anim[obj].delete(var)
- self.anim.delete(obj) if self.anim[obj].empty?
- else
- an[:c_time] += an[:spd] * @gspd
- an[:c_time] = an[:time] if an[:c_time] > an[:time]
- new_val = send(an[:type],an[:start],an[:change],an[:c_time],an[:time])
- obj.send(var+'=',new_val)
- an[:mobj].send(an[:mcall],*an[:margs]) if an[:mcall]
- end
- end
- end
- class SmoothNum
- attr_accessor :value, :anim
- def initialize(value=0,anim=nil)
- @value = value
- @anim = anim
- end
- def value(round=false)
- return round ? @value.to_i : @value
- end
- def value=(value)
- @value = value
- end
- end
- class Scene_Base
- # Added the update method here...
- # Might move it to the Graphics module instead...
- alias add_smooth_anims8827 update_basic
- def update_basic
- add_smooth_anims8827
- SmoothAnim.update
- end
- end
- class Object
- def disposed?
- return false
- end
- end
- class Scene_Title < Scene_Base
- alias clear_anims9917 start
- def start
- SmoothAnim.clear
- clear_anims9917
- end
- end
- class Scene_Gameover < Scene_Base
- alias clear_anims1117 start
- def start
- SmoothAnim.clear
- clear_anims1117
- end
- end
- #==============================================================================
- # !!END OF SCRIPT - OHH, NOES!!
- #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement