Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # =============================================================================
  2. # TheoAllen - Character Animation Loop
  3. # Version : 1.1b
  4. # =============================================================================
  5. ($imported ||= {})[:Theo_CharAnimloop] = true
  6. # =============================================================================
  7. # Change Logs:
  8. # -----------------------------------------------------------------------------
  9. # 2018.08.07 - Added bottom flag
  10. # 2018.07.15 - Fixes script efficiency for less fps drop
  11. # 2014.02.12 - Finished script
  12. # =============================================================================
  13. =begin
  14.   -----------------------------------------------------------------------------
  15.   Intro :
  16.   This script allow you to play animation on map sprite, loop it, and follow
  17.   the character
  18.  
  19.   -----------------------------------------------------------------------------
  20.   How to use :
  21.   Put this script below material and above main
  22.   Use these script call in SET MOVE ROUTE (pick one u need)
  23.  
  24.   animloop(id)
  25.   animloop(id, mirror)
  26.   animloop(id, mirror, rate)
  27.   animloop(id, mirror, rate, bottom)
  28.  
  29.   id      > animation id in database
  30.   mirror  > will animation will be mirrored? (true/false)
  31.   rate    > animation speed. Put in from range 1 ~ 4
  32.   bottom  > true/false. If set to true, will play behind the sprite. Default
  33.             is false
  34.  
  35.   If you just want to play animation on the back, just have to fill ALL the
  36.   others even though u don't need it. For example
  37.  
  38.  animloop(66, false, 3, true)
  39.            
  40.  To stop animation, write a script call
  41.  end_animloop
  42.  
  43.  -----------------------------------------------------------------------------
  44.  Terms of use :
  45.  Credit me, TheoAllen. You are free to edit this script by your own. As long
  46.  as you don't claim it yours. For commercial purpose, don't forget to give me
  47.  a free copy of the game.
  48. =end
  49. #==============================================================================
  50. # No config whatsoever
  51. #==============================================================================
  52. class Game_Character
  53.  attr_accessor :animloop_id
  54.  attr_accessor :animloop_mirror
  55.  attr_accessor :animloop_rate
  56.  attr_accessor :animloop_bottom
  57.  
  58.  alias theo_animloop_id_init initialize
  59.  def initialize
  60.    theo_animloop_id_init
  61.    init_animloop_members
  62.  end
  63.  
  64.  def init_animloop_members
  65.    @animloop_id = 0
  66.    @animloop_mirror = false
  67.    @animloop_rate = 3
  68.    @animloop_bottom = false
  69.  end
  70.  
  71.  def animloop(id, mirror = false, rate = 3, bottom = false)
  72.    @animloop_id = id
  73.    @animloop_mirror = mirror
  74.    @animloop_rate = rate
  75.    @animloop_bottom = bottom
  76.    sprset = get_spriteset
  77.    return unless sprset
  78.    spr = get_spriteset.get_sprite(self)
  79.    get_spriteset.get_sprite(self).end_animation if spr
  80.  end
  81.  
  82.  def end_animloop
  83.    init_animloop_members
  84.  end
  85.  
  86.  def animloop_id
  87.    return @animloop_id ||= 0
  88.  end
  89.  
  90. end
  91. #------------------------------------------------------------------------------
  92. # Pseudo Sprite for animation
  93. #------------------------------------------------------------------------------
  94. class Char_Animloop < Sprite_Base  
  95.  attr_reader :char_sprite
  96.  
  97.  def initialize(char_sprite)
  98.    super(char_sprite.viewport)
  99.    @char_sprite = char_sprite
  100.    update_all
  101.  end
  102.  
  103.  def update_all
  104.    src_rect.set(char_sprite.src_rect)
  105.    self.ox = char_sprite.ox
  106.    self.oy = char_sprite.oy
  107.    last_x = char_sprite.x - self.x
  108.    last_y = char_sprite.y - self.y
  109.    move_animation(last_x, last_y)
  110.    self.x = char_sprite.x
  111.    self.y = char_sprite.y
  112.    self.z = char_sprite.z
  113.  end
  114.  
  115.  def update
  116.    super
  117.    update_all
  118.    setup_animation
  119.  end
  120.  
  121.  def setup_animation
  122.    if !animation? && character.animloop_id != 0
  123.      @anim_id = character.animloop_id
  124.      start_animation($data_animations[@anim_id], character.animloop_mirror)
  125.    end
  126.  end
  127.  
  128.  def character
  129.    char_sprite.character
  130.  end
  131.  
  132.  def end_animation
  133.    if character.animloop_id == @anim_id
  134.      @ani_duration = @animation.frame_max * @ani_rate + 1
  135.    # Revert back
  136.    elsif character.animloop_id != @anim_id && character.animloop_id != 0
  137.      @anim_id = character.animloop_id
  138.      start_animation($data_animations[@anim_id], character.animloop_mirror)
  139.    # Change animation  
  140.    else
  141.      @anim_id = 0
  142.      super
  143.    # End animation
  144.    end
  145.  end
  146.  
  147.  def move_animation(dx, dy)
  148.    if @animation && @animation.position != 3
  149.      @ani_ox += dx
  150.      @ani_oy += dy
  151.      @ani_sprites.each do |sprite|
  152.        sprite.x += dx
  153.        sprite.y += dy
  154.      end
  155.    end
  156.  end
  157.  
  158.  def set_animation_rate
  159.    @ani_rate = character.animloop_rate
  160.  end
  161.  
  162.    def animation_set_sprites(frame)
  163.    cell_data = frame.cell_data
  164.    @ani_sprites.each_with_index do |sprite, i|
  165.      next unless sprite
  166.      pattern = cell_data[i, 0]
  167.      if !pattern || pattern < 0
  168.        sprite.visible = false
  169.        next
  170.      end
  171.      sprite.bitmap = pattern < 100 ? @ani_bitmap1 : @ani_bitmap2
  172.      sprite.visible = true
  173.      sprite.src_rect.set(pattern % 5 * 192,
  174.        pattern % 100 / 5 * 192, 192, 192)
  175.      if @ani_mirror
  176.        sprite.x = @ani_ox - cell_data[i, 1]
  177.        sprite.y = @ani_oy + cell_data[i, 2]
  178.        sprite.angle = (360 - cell_data[i, 4])
  179.        sprite.mirror = (cell_data[i, 5] == 0)
  180.      else
  181.        sprite.x = @ani_ox + cell_data[i, 1]
  182.        sprite.y = @ani_oy + cell_data[i, 2]
  183.        sprite.angle = cell_data[i, 4]
  184.        sprite.mirror = (cell_data[i, 5] == 1)
  185.      end
  186.      zpos = character.animloop_bottom ? 50 - 17 : 300
  187.      sprite.z = self.z + zpos + i
  188.      sprite.ox = 96
  189.      sprite.oy = 96
  190.      sprite.zoom_x = cell_data[i, 3] / 100.0
  191.      sprite.zoom_y = cell_data[i, 3] / 100.0
  192.      sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
  193.      sprite.blend_type = cell_data[i, 7]
  194.    end
  195.  end
  196.  
  197. end
  198.  
  199. class Sprite_Character
  200.  
  201.  alias theo_animloop_id_update update
  202.  def update
  203.    theo_animloop_id_update
  204.    if character.animloop_id > 0 && !@sprite_animloop
  205.      @sprite_animloop = Char_Animloop.new(self)
  206.    end
  207.    @sprite_animloop.update if @sprite_animloop
  208.  end
  209.  
  210.  alias theo_animloop_id_dispose dispose
  211.  def dispose
  212.    theo_animloop_id_dispose
  213.    @sprite_animloop.dispose if @sprite_animloop
  214.  end
  215.  
  216. end
  217.  
  218. class Spriteset_Map
  219.  
  220.  def get_sprite(char)
  221.    @character_sprites.find {|c| c.character == char}
  222.  end
  223.  
  224. end
  225. def get_spriteset
  226.  get_scene.instance_variable_get("@spriteset")
  227. end
  228. def get_scene
  229.  SceneManager.scene
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement