Advertisement
neonblack

Animated Character

Aug 26th, 2012
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.69 KB | None | 0 0
  1. class Animated_Character
  2.   attr_accessor :x
  3.   attr_accessor :y
  4.   attr_accessor :name
  5.   attr_accessor :index
  6.   attr_accessor :face
  7.   attr_accessor :bitmap
  8.  
  9.   def initialize(x, y, name, index, face = 2)
  10.     @x = x
  11.     @y = y
  12.     @name = name
  13.     @index = index
  14.     @face = face
  15.   end
  16.  
  17.   def identical?(x, y, name, index)
  18.     return (@x == x && @y == y && @name == name && @index == index)
  19.   end
  20.  
  21.   def create_bitmap(width, height, map)
  22.     @bitmap = Bitmap.new(width, height)
  23.     @bitmap.blt(0, 0, map, map.rect)
  24.   end
  25. end
  26.  
  27. class Window_Base < Window
  28.   alias cp_anim_char update unless $@
  29.   def update
  30.     cp_anim_char
  31.     @step = 0 if @step.nil?
  32.     @step += 1; @step %= 40
  33.     update_anim_chars
  34.   end
  35.  
  36.   def update_anim_chars
  37.     @step = 0 if @step.nil?; @old_step = -1 if @old_step.nil?
  38.     return if (@step / 10) == (@old_step / 10)
  39.     @old_step = @step
  40.     @char_list = [] if @char_list.nil?
  41.     return if @char_list.empty?
  42.     fr = (@step / 10) == 3 ? 1 : @step / 10
  43.     @char_list.each_with_index do |char, i|
  44.       draw_an_character(char.name, char.index, char.x, char.y, char.face, fr, i)
  45.     end
  46.   end
  47.  
  48.   def draw_an_character(name, index, x, y, face = 2, frame = 1, array = nil)
  49.     return unless name
  50.     bitmap = Cache.character(name)
  51.     sign = name[/^[\!\$]./]
  52.     if sign && sign.include?('$')
  53.       cw = bitmap.width / 3
  54.       ch = bitmap.height / 4
  55.     else
  56.       cw = bitmap.width / 12
  57.       ch = bitmap.height / 8
  58.     end
  59.     n = index
  60.     f = (face / 2) - 1
  61.     src_rect = Rect.new((n%4*3+frame)*cw, (n/4*4)*ch + (ch * f), cw, ch)
  62.     clear_rect = Rect.new(x - cw / 2, y - ch, cw, ch)
  63.     bit = create_back_rect(array, clear_rect) if !array.nil?
  64.     contents.clear_rect(clear_rect)
  65.     contents.blt(clear_rect.x, clear_rect.y, bit, bit.rect) unless bit.nil?
  66.     contents.blt(clear_rect.x, clear_rect.y, bitmap, src_rect)
  67.   end
  68.  
  69.   def create_back_rect(index, rect)
  70.     return nil if @char_list[index].nil?
  71.     return @char_list[index].bitmap if @char_list[index].bitmap
  72.     map = Bitmap.new(rect.width, rect.height)
  73.     map.blt(0, 0, contents, rect)
  74.     @char_list[index].create_bitmap(rect.width, rect.height, map)
  75.     return @char_list[index].bitmap
  76.   end
  77.  
  78.   def draw_animated_character(name, index, x, y, facing = 2)
  79.     add_an_character(name, index, x, y, facing)
  80.     update_anim_chars
  81.   end
  82.  
  83.   def add_an_character(name, index, x, y, face)
  84.     same = false
  85.     @char_list = [] if @char_list.nil?
  86.     @char_list.each do |char|
  87.       next unless char.identical?(name, index, x, y)
  88.       char.face = face
  89.       same = true
  90.     end
  91.     @char_list.push(Animated_Character.new(x, y, name, index, face)) unless same
  92.   end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement