Advertisement
Vlue

Animated Battlers

Jan 26th, 2015
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.23 KB | None | 0 0
  1. #Animated Battlers v1.0
  2. #----------#
  3. #Features: Allows you to mostly uneasily incorporate animated battlers into
  4. #           your game for that cooler and more polished look! You'll be able
  5. #           to decide individual sprite animations and more complicated skill
  6. #           animations.
  7. #
  8. #Usage:    Plug and play, Customize (heavily) as needed.
  9. #        
  10. %Q(        (Great Schlee, there's too much to explain here without taking up
  11.          18 million lines. So here's a quick key guide while you can find
  12.           the full depth guide here:
  13.           http://daimonioustails.weebly.com/animated-battlers.html )
  14. )
  15. #
  16. #           Notetags (Actors, Enemies, Classes)
  17. #            <???Anim ["filename", animation_sym]>
  18. #
  19. #           Notetags (Skills)
  20. #            <ANIMATION animation_name>
  21. #
  22. #        Adding Animation Files:
  23. #         Images are taken from Graphics/Battler.
  24. #          "imagename" => [columns,rows],
  25. #
  26. #       Adding Animation Frames:
  27. #         ANIMATIONS is where you create frame data.
  28. #          :animation => [ [id,timer] , [id,timer] , ... ],
  29. #
  30. #       Adding Skill Animations:
  31. #         SKILL_ANIMATIONS are the meat and potatoes.
  32. #          :animation => [ [step1] , [step2] , ... ],
  33. #
  34. #        Valid steps:
  35. #       [:moveto,:target,:align,speed]         -move self to target
  36. #       [:jumpto,:target,:align,speed]         -jump self to target (Exp)
  37. #       [:move,:target,x,y,speed]              -move self based on target
  38. #       [:jump,:target,x,y,speed]              -jump self based on target (Exp)
  39. #       [:play,:target,"filename",:step,timer] -play sprite animation for target
  40. #       [:play,:target,:base,nil,timer]        -play sprite animation for target
  41. #       [:anim,:target,anim_id,timer]          -play animation on target
  42. #       [:mirror,:target,boolean]              -mirror sprite
  43. #       [:wait,timer]                          -wait frames
  44. #       [:flash,:target,color,duration]        -flash target or screen
  45. #       [:tint,tone,duration]                  -tint screen
  46. #       [:se,"name",volume,pitch]              -play se
  47. #       [:damage,:target,amount]               -display damage (Basic Damage Popup)
  48. #       [:field,id]                            -change background (Field Effects)
  49. #       [:missile,:target,icon_id,:effect]     -throw something at target
  50. #
  51. #         :target = :self, :target, :target#, :screen(flash)
  52. #         :align = :align, :align_o (opposite side of sprite)
  53. #         speed = # of frames to move from point a to point b
  54. #         :step = ANIMATIONS name
  55. #         timer = wait in frames (nil for completion)
  56. #         :base = Base anim (:Dead,:Idle,:Run,etc.)
  57. #         anim_id = animation id (database)
  58. #         color = Color.new(r,g,b,a) (alpha optional)
  59. #         tone = Tone.new(r,g,b,g)
  60. #         amount = 0-100 (%), or :rest for remaining
  61. #         :effect = nil, :rotate (spins)
  62. #        
  63. #  
  64. #----------#
  65. #-- Script by: V.M of D.T
  66. #
  67. #- Questions or comments can be:
  68. #    given by email: sumptuaryspade@live.ca
  69. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  70. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  71. #
  72. #--- Free to use in any project, commercial or non-commercial, with credit given
  73. # - - Though a donation's always a nice way to say thank you~ (I also accept actual thank you's)
  74.  
  75. $imported = {} if $imported.nil?
  76. $imported[:VlueAnimatedBattlers] = true
  77.  
  78. module Animation
  79.   ANIMATION_FILES = { "elf-f-1" => [3,8], "elf-f-2" => [3,8],
  80.                       "mage-f-1" => [3,8], "mage-f-2" => [3,8],
  81.                       "man5-1" => [3,8], "man5-2" => [3,8],
  82.                       "ninja-m-1" => [3,8], "ninja-m-2" => [3,8], }
  83.                      
  84.   ANIMATIONS = { :baseidle => [ [6,10] , [7,10] , [8,10] ],
  85.                  :baseattack => [ [3,10] , [4,10] , [5,10] ],
  86.                  :basehurt => [ [6,10] , [7,10] , [8,10] ],
  87.                  :basedead => [ [9,1] ],
  88.                  :baserun  => [ [18,10], [19,10], [20,10] ],
  89.                  :basecheer=> [ [0,10], [1,10], [2,10] ],}
  90.  
  91.   SKILL_ANIMATIONS = {
  92.       :baseattack => [ [:moveto,:target,:align],
  93.                      [:play,:self,:Attack,nil,20],
  94.                      [:damage,:target,:rest],
  95.                      [:play,:target,:Hurt,nil,20],
  96.                      [:moveto,:origin], ],
  97.                    
  98.       :fireball => [ [:move,:self,-10,0],
  99.                      [:play,:self,:Attack],
  100.                      [:missile,:target,96,:rotate],
  101.                      [:flash,:target,Color.new(255,0,0),60],
  102.                      [:anim,:target,57,nil],
  103.                      [:damage,:target,:rest],
  104.                      [:play,:target,:Hurt,nil,20],
  105.                      [:moveto,:origin], ],
  106.                      
  107.       :heal =>  [ [:move,:self,-10,0],
  108.                   [:play,:self,:Cheer,nil,10],
  109.                   [:flash,:target,Color.new(255,255,255),60],
  110.                   [:anim,:target,37,nil],
  111.                   [:damage,:target,:rest],
  112.                   [:play,:target,:Cheer,nil,20],
  113.                   [:moveto,:origin], ],
  114.   }
  115.  
  116.   def self.get_anim(file,anim,loop)
  117.     Animation.new(file,anim,loop)
  118.   end
  119.  
  120.   def self.get_steps(sym)
  121.     SKILL_ANIMATIONS[sym]
  122.   end
  123.  
  124.   class Animation
  125.     def initialize(file,anim,loop)
  126.       @bitmap = Cache.battler(file,0)
  127.       @col, @row = ANIMATION_FILES[file][0], ANIMATION_FILES[file][1]
  128.       @width, @height = @bitmap.width/@col, @bitmap.height/@row
  129.       @animation = ANIMATIONS[anim]
  130.       @frames = []
  131.       @animation.each do |array|
  132.         @frames.push(Frame.new(array[0],array[1]))
  133.       end
  134.       @current_frame = 0
  135.       @current_timer = 0
  136.       @frame_timer = 0
  137.       @loop = loop
  138.     end
  139.     def update
  140.       @current_timer += 1
  141.       @frame_timer += 1
  142.       if @frame_timer == frame.length
  143.         @current_frame += 1
  144.         @frame_timer = 0
  145.       end
  146.       @current_frame = 0 if @loop && @current_frame == @frames.size
  147.     end
  148.     def frame
  149.       @frames[@current_frame]
  150.     end
  151.     def image
  152.       return nil unless frame
  153.       tmpbitmap = Bitmap.new(@width,@height)
  154.       xx, yy = frame.id % @col, frame.id / @col
  155.       xx *= @width;yy *= @height
  156.       tmpbitmap.blt(0,0,@bitmap,Rect.new(xx,yy,@width,@height))
  157.       tmpbitmap
  158.     end
  159.     def done
  160.       @current_frame == @frames.size - 1
  161.     end
  162.    
  163.     class Frame
  164.       attr_reader :id
  165.       attr_reader :length
  166.       def initialize(id,len)
  167.         @id = id
  168.         @length = len
  169.       end
  170.     end
  171.    
  172.   end
  173.  
  174. end
  175.  
  176. class Sprite_Base
  177.   attr_reader :sprite_animation
  178.   alias animation_update update
  179.   def update
  180.     animation_update
  181.     update_sprite_animation
  182.   end
  183.   def play_animation(file,anim_id,loop = false)
  184.     @sprite_animation = Animation.get_anim(file,anim_id,loop)
  185.   end
  186.   def loop_animation(file,anim_id)
  187.     play_animation(file,anim_id,true)
  188.   end
  189.   def update_sprite_animation
  190.     return if @sprite_animation.nil?
  191.     @sprite_animation.update
  192.     self.bitmap = @sprite_animation.image
  193.   end
  194.   def moveto(x,y,speed,jump = false,rotate = false)
  195.     speed = 20 unless speed
  196.     if @battler && @battler.get_anim("Run")
  197.       loop_animation(@battler.get_anim("Run")[0],@battler.get_anim("Run")[1])
  198.     end
  199.     x_speed = (self.x - x).abs / speed.to_f
  200.     y_speed = (self.y - y).abs / speed.to_f
  201.     iter = 0
  202.     height = Math.sqrt((self.x - x).abs ** 2 + (self.y - y).abs ** 2).to_f
  203.     while x != self.x || y != self.y
  204.       if jump
  205.         self.oy += jump_height(iter, height) - jump_height(iter-1, height)
  206.       end
  207.       self.angle += 20 if rotate
  208.       self.x += x > self.x ? x_speed : -x_speed if x != self.x
  209.       self.y += y > self.y ? y_speed : -y_speed if y != self.y
  210.       self.y = y if (self.y - y).abs <= y_speed
  211.       self.x = x if (self.x - x).abs <= x_speed
  212.       SceneManager.scene.update_basic
  213.       iter += 1;break if iter > speed
  214.     end
  215.     if @battler && @battler.get_anim("Idle")
  216.       loop_animation(@battler.get_anim("Idle")[0],@battler.get_anim("Idle")[1])
  217.     end
  218.   end
  219.   def jump_height(count, height)
  220.     (height * height - (count - height).abs ** 2) / 8
  221.   end
  222. end
  223.  
  224. class Game_Actor
  225.   def get_anim(anim)
  226.     self.class.note =~ /<#{anim}Anim (.+)>/
  227.     return eval($1) if $1
  228.     actor.note =~ /<#{anim}Anim (.+)>/
  229.     $1 ? eval($1) : nil
  230.   end
  231.   def use_sprite?
  232.     true
  233.   end
  234.   def screen_x
  235.     400 + index * 12
  236.   end
  237.   def screen_y
  238.     200 + index * 32
  239.   end
  240.   def screen_z
  241.     100
  242.   end
  243.   alias ani_index index
  244.   def index
  245.     @spindex ? @spindex : ani_index
  246.   end
  247.   def set_spindex(set)
  248.     @spindex = set
  249.   end
  250. end
  251.  
  252. class Game_Enemy
  253.   def get_anim(anim)
  254.     enemy.note =~ /<#{anim}Anim (.+)>/
  255.     $1 ? eval($1) : nil
  256.   end
  257.    def atk_animation_id1
  258.     1
  259.   end
  260. end
  261.  
  262. class Spriteset_Battle
  263.   def create_actors
  264.     @actor_sprites = []
  265.     $game_party.battle_members.each do |actor|
  266.       @actor_sprites.push(Sprite_Battler.new(@viewport1, actor))
  267.     end
  268.   end
  269.   def update_actors
  270.     @actor_sprites.each do |sprite|
  271.       sprite.update
  272.     end
  273.   end
  274.   def reset_positions
  275.     battler_sprites.each {|sprite| sprite.init_position}
  276.   end
  277. end
  278.  
  279. class Sprite_Battler
  280.   alias animbat_init initialize
  281.   def initialize(*args)
  282.     animbat_init(*args)
  283.     init_position
  284.   end
  285.   def update_bitmap
  286.     if bitmap.nil?
  287.       if @battler.get_anim("Idle") && @battler.alive?
  288.         loop_animation(@battler.get_anim("Idle")[0],@battler.get_anim("Idle")[1])
  289.       elsif @battler.get_anim("Dead") && !@battler.alive?
  290.         loop_animation(@battler.get_anim("Dead")[0],@battler.get_anim("Dead")[1])
  291.       else
  292.         new_bitmap = Cache.battler(@battler.battler_name, @battler.battler_hue)
  293.         self.bitmap = new_bitmap
  294.       end
  295.       self.mirror = @battler.is_a?(Game_Enemy) if @battler.get_anim("Idle")
  296.       init_visibility
  297.     end
  298.   end
  299.   def skill_animation(targets)
  300.     @animation_playing = true
  301.     @steps = Animation.get_steps(@battler.current_action.item.animation)
  302.     @step_timer = 0
  303.     @current_step = -1
  304.     @damage_shown = [0]*8
  305.     update_skill_animation(targets)
  306.   end
  307.   def update_skill_animation(targets)
  308.     while @animation_playing
  309.       @current_step += 1
  310.       break if @current_step == @steps.size
  311.       step = @steps[@current_step].clone
  312.       case step[0]
  313.       when :moveto
  314.         process_moveto(targets,step,false)
  315.        
  316.       when :jumpto
  317.         process_moveto(targets,step,true)
  318.        
  319.       when :move
  320.         process_move(targets,step,false)
  321.        
  322.       when :jump
  323.         process_move(targets,step,true)
  324.        
  325.       when :play
  326.         ntarget = self if step[1] == :self
  327.         ntarget = targets[0] if step[1] == :target
  328.         if step[1].to_s =~ /target(.)/
  329.           ntarget = targets[$1.to_i % targets.size]
  330.         end
  331.         play_anim(ntarget,step)
  332.        
  333.       when :anim
  334.         ntarget = @battler if step[1] == :self
  335.         ntarget = targets[0] if step[1] == :target
  336.         if step[1].to_s =~ /target(.)/
  337.           ntarget = targets[$1.to_i % targets.size]
  338.         end
  339.         if step[2]
  340.           animation_id = step[2]
  341.         else
  342.           animation_id = @battler.atk_animation_id1
  343.         end
  344.         if ntarget
  345.           ntarget.animation_id = animation_id
  346.         else
  347.           targets.each {|ntarget| ntarget.animation_id = animation_id }
  348.         end
  349.         if step[3]
  350.           step[3].times {|i| SceneManager.scene.update_basic }
  351.         else
  352.           sprite = SceneManager.scene.get_sprite(ntarget)
  353.           SceneManager.scene.update_basic while sprite.animation?
  354.         end
  355.      
  356.       when :mirror
  357.         target = self if step[1] == :self
  358.         target = SceneManager.scene.get_sprite(targets[0]) if step[1] == :target
  359.         if step[1].to_s =~ /target(.)/
  360.           ntarget = SceneManager.scene.get_sprite(targets[$1.to_i % targets.size])
  361.         end
  362.         step[2] = !step[2] if target.battler.is_a?(Game_Enemy)
  363.         target.mirror = step[2]
  364.      
  365.       when :wait
  366.         step[1].times {|i| SceneManager.scene.update_basic }
  367.        
  368.       when :flash
  369.         if step[1] == :screen
  370.           $game_troop.screen.start_flash(step[2],step[3])
  371.         else
  372.           target = self if step[1] == :self
  373.           target = SceneManager.scene.get_sprite(targets[0]) if step[1] == :target
  374.           if step[1].to_s =~ /target(.)/
  375.             ntarget = SceneManager.scene.get_sprite(targets[$1.to_i % targets.size])
  376.           end
  377.           target.flash(step[2],step[3])
  378.         end
  379.        
  380.       when :tint
  381.         $game_troop.screen.start_tone_change(step[1],step[2])
  382.        
  383.       when :se
  384.         Audio.se_play("Audio/Se/" + step[1],step[2],step[3])
  385.        
  386.       when :damage
  387.         if $imported[:BasicDamagePopup]
  388.           i = 0
  389.           if step[1] == :all
  390.             targets.each do |target|
  391.               target.result.ignore = 1
  392.               partial_damage(step[2],target,i)
  393.               i += 1
  394.             end
  395.           else
  396.             ntarget = @battler if step[1] == :self
  397.             ntarget = targets[0] if step[1] == :target
  398.             if step[1].to_s =~ /target(.)/
  399.               ntarget = targets[$1.to_i % targets.size]
  400.             end
  401.             ntarget.result.ignore = 1
  402.             partial_damage(step[2],ntarget,$1.to_i % targets.size)
  403.           end
  404.         end
  405.      
  406.       when :field
  407.         if $imported[:VlueFieldEffects]
  408.           SceneManager.scene.apply_field(step[1])
  409.         end
  410.        
  411.       when :missile
  412.         ntarget = self if step[1] == :self
  413.         ntarget = targets[0] if step[1] == :target
  414.         if step[1].to_s =~ /target(.)/
  415.           ntarget = targets[$1.to_i % targets.size]
  416.         end
  417.         ntarget = SceneManager.scene.get_sprite(ntarget) if ntarget.is_a?(Game_Battler)
  418.         rotate = step[3] == :rotate ? true : false
  419.         @sprite = Sprite_Base.new
  420.         @sprite.x = self.x
  421.         @sprite.y = self.y - self.bitmap.height / 2
  422.         if step[2].is_a?(Integer)
  423.           @sprite.oy += 12
  424.           @sprite.ox += 12
  425.           @sprite.bitmap = Bitmap.new(24,24)
  426.           tmpbmp = Cache.system("Iconset")
  427.           rect = Rect.new(step[2] % 16 * 24, step[2] / 16 * 24, 24, 24)
  428.           @sprite.bitmap.blt(0, 0, tmpbmp, rect)
  429.         end
  430.         yy = ntarget.y - ntarget.bitmap.height / 4
  431.         @sprite.moveto(ntarget.x,yy,30,false,rotate)
  432.         @sprite.bitmap.dispose
  433.         @sprite.dispose
  434.      
  435.       end
  436.     end
  437.    
  438.   end
  439.   def process_move(targets, step, jump)
  440.     ntarget = self if step[1] == :self
  441.     ntarget = targets[0] if step[1] == :target
  442.     if step[1].to_s =~ /target(.)/
  443.       ntarget = targets[$1.to_i % targets.size]
  444.     end
  445.     ntarget = SceneManager.scene.get_sprite(ntarget) if ntarget.is_a?(Game_Battler)
  446.     step[2] *= -1 if self.mirror
  447.     xx = ntarget.x + step[2]
  448.     if xx != 0
  449.       step[2] < 0 ? xx -= ntarget.bitmap.width / 2 : xx += ntarget.bitmap.width / 2
  450.     end
  451.     moveto(xx,ntarget.y + step[3],step[4],jump)
  452.   end
  453.   def process_moveto(targets,step,jump)
  454.     ntarget = @battler if step[1] == :origin
  455.     ntarget = targets[0] if step[1] == :target
  456.     if step[1].to_s =~ /target(.)/
  457.       ntarget = targets[$1.to_i % targets.size]
  458.     end
  459.     if step[2]
  460.       ntarget = SceneManager.scene.get_sprite(ntarget)
  461.       width, height = ntarget.bitmap.width, ntarget.bitmap.height
  462.       x, y = ntarget.x, ntarget.y
  463.       if step[2] == :align
  464.         step[2] = :right if ntarget.x < self.x
  465.         step[2] = :left if ntarget.x > self.x
  466.       elsif step[2] == :align_o
  467.         step[2] = :right if ntarget.x > self.x
  468.         step[2] = :left if ntarget.x < self.x
  469.       end
  470.       x = ntarget.x + width / 2 if step[2] == :right
  471.       x = ntarget.x - width / 2 if step[2] == :left
  472.       y = ntarget.y + height / 2 if step[2] == :below
  473.       y = ntarget.y - height / 2 if step[2] == :above
  474.     elsif ntarget
  475.       x, y = ntarget.screen_x, ntarget.screen_y
  476.     else
  477.       x, y = 0, 0
  478.     end
  479.     moveto(x,y,step[3],jump)
  480.   end
  481.   def partial_damage(per, target, i)
  482.     item = @battler.current_action.item
  483.     ntarget = Marshal.load(Marshal.dump(target))
  484.     ntarget.set_spindex(target.index) if target.is_a?(Game_Actor)
  485.     if per == :rest
  486.       ntarget.result.hp_damage -= @damage_shown[i]
  487.     else
  488.       ntarget.result.hp_damage *= (per.to_f / 100)
  489.       ntarget.result.hp_damage = ntarget.result.hp_damage.to_i
  490.     end
  491.     @damage_shown[i] += ntarget.result.hp_damage
  492.     SceneManager.scene.add_damage_popup(ntarget,item,@battler)
  493.   end
  494.   def play_anim(target,step)
  495.     target = SceneManager.scene.get_sprite(target) if target.is_a?(Game_Battler)
  496.     file = [step[2],step[3]]
  497.     if step[2].is_a?(Symbol)
  498.       file = target.battler.get_anim(step[2].to_s)
  499.     end
  500.     return unless file
  501.     target.play_animation(file[0],file[1],false)
  502.     if step[4].nil?
  503.       SceneManager.scene.update_basic while !target.sprite_animation.done
  504.     else
  505.       step[4].times { SceneManager.scene.update_basic }
  506.     end
  507.   end
  508.   def update_position
  509.    
  510.   end
  511.   def init_position
  512.     self.x = @battler.screen_x
  513.     self.y = @battler.screen_y
  514.     self.z = @battler.screen_z
  515.   end
  516.   alias animbat_start_effect start_effect
  517.   def start_effect(effect_type)
  518.     if effect_type == :collapse && @battler.get_anim("Dead")
  519.       loop_animation(@battler.get_anim("Dead")[0],@battler.get_anim("Dead")[1])
  520.     else
  521.       animbat_start_effect(effect_type)
  522.     end
  523.   end
  524. end
  525.  
  526. class Scene_Battle
  527.   alias animated_animation show_animation
  528.   def show_animation(targets, animation_id)
  529.     if !@subject.current_action.item.animation
  530.       return animated_animation(targets,animation_id)
  531.     else
  532.       get_sprite(@subject).skill_animation(targets)
  533.     end
  534.     @spriteset.reset_positions
  535.   end
  536.   def get_sprite(target)
  537.     @spriteset.battler_sprites.each do |battler|
  538.       return battler if battler.battler == target
  539.     end
  540.   end
  541.   def get_all_but(subj,targ)
  542.     @spriteset.battler_sprites - [subj] - targ
  543.   end
  544.   def update_for_animation
  545.     @spriteset.update
  546.     Graphics.update
  547.   end
  548.   def use_item
  549.     item = @subject.current_action.item
  550.     @log_window.display_use_item(@subject, item)
  551.     @subject.use_item(item)
  552.     refresh_status
  553.     targets = @subject.current_action.make_targets.compact
  554.     targets.each do |target|
  555.       item.repeats.times do
  556.         (apply_substitute(target, item)).item_prepare(@subject, item)
  557.       end
  558.     end
  559.     show_animation(targets, item.animation_id)
  560.     targets.each {|target| item.repeats.times { invoke_item(target, item) } }
  561.   end
  562. end
  563.  
  564. class RPG::UsableItem
  565.   def animation
  566.     @note =~ /<ANIMATION (.+)>/ ? $1.to_sym : false
  567.   end
  568. end
  569.  
  570. class Game_Battler
  571.   def item_prepare(user, item)
  572.     @result.clear
  573.     @result.used = item_test(user, item)
  574.     @result.missed = (@result.used && rand >= item_hit(user, item))
  575.     @result.evaded = (!@result.missed && rand < item_eva(user, item))
  576.     if @result.hit?
  577.       unless item.damage.none?
  578.         @result.critical = (rand < item_cri(user, item))
  579.         make_damage_value(user, item)
  580.       end
  581.     end
  582.   end
  583.   def item_apply(user, item)
  584.     if @result.hit?
  585.       execute_damage(user)
  586.       item.effects.each {|effect| item_effect_apply(user, item, effect) }
  587.       item_user_effect(user, item)
  588.     end
  589.   end
  590. end
  591.  
  592. class Game_ActionResult
  593.   attr_accessor :ignore
  594. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement