TheoAllen

Sprite_Character

Dec 5th, 2014
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.41 KB | None | 0 0
  1. #==============================================================================
  2. # ** Sprite_Character
  3. #------------------------------------------------------------------------------
  4. #  This sprite is used to display characters. It observes an instance of the
  5. # Game_Character class and automatically changes sprite state.
  6. #==============================================================================
  7.  
  8. class Sprite_Character < Sprite_Base
  9.   #--------------------------------------------------------------------------
  10.   # * Public Instance Variables
  11.   #--------------------------------------------------------------------------
  12.   attr_accessor :character
  13.   #--------------------------------------------------------------------------
  14.   # * Object Initialization
  15.   #     character : Game_Character
  16.   #--------------------------------------------------------------------------
  17.   def initialize(viewport, character = nil)
  18.     super(viewport)
  19.     @character = character
  20.     @balloon_duration = 0
  21.     update
  22.   end
  23.   #--------------------------------------------------------------------------
  24.   # * Free
  25.   #--------------------------------------------------------------------------
  26.   def dispose
  27.     end_animation
  28.     end_balloon
  29.     super
  30.   end
  31.   #--------------------------------------------------------------------------
  32.   # * Frame Update
  33.   #--------------------------------------------------------------------------
  34.   def update
  35.     super
  36.     update_bitmap
  37.     update_src_rect
  38.     update_position
  39.     update_other
  40.     update_balloon
  41.     setup_new_effect
  42.   end
  43.   #--------------------------------------------------------------------------
  44.   # * Get Tileset Image That Includes the Designated Tile
  45.   #--------------------------------------------------------------------------
  46.   def tileset_bitmap(tile_id)
  47.     Cache.tileset($game_map.tileset.tileset_names[5 + tile_id / 256])
  48.   end
  49.   #--------------------------------------------------------------------------
  50.   # * Update Transfer Origin Bitmap
  51.   #--------------------------------------------------------------------------
  52.   def update_bitmap
  53.     if graphic_changed?
  54.       @tile_id = @character.tile_id
  55.       @character_name = @character.character_name
  56.       @character_index = @character.character_index
  57.       if @tile_id > 0
  58.         set_tile_bitmap
  59.       else
  60.         set_character_bitmap
  61.       end
  62.     end
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Determine if Graphic Changed
  66.   #--------------------------------------------------------------------------
  67.   def graphic_changed?
  68.     @tile_id != @character.tile_id ||
  69.     @character_name != @character.character_name ||
  70.     @character_index != @character.character_index
  71.   end
  72.   #--------------------------------------------------------------------------
  73.   # * Set Tile Bitmap
  74.   #--------------------------------------------------------------------------
  75.   def set_tile_bitmap
  76.     sx = (@tile_id / 128 % 2 * 8 + @tile_id % 8) * 32;
  77.     sy = @tile_id % 256 / 8 % 16 * 32;
  78.     self.bitmap = tileset_bitmap(@tile_id)
  79.     self.src_rect.set(sx, sy, 32, 32)
  80.     self.ox = 16
  81.     self.oy = 32
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # * Set Character Bitmap
  85.   #--------------------------------------------------------------------------
  86.   def set_character_bitmap
  87.     self.bitmap = Cache.character(@character_name)
  88.     sign = @character_name[/^[\!\$]./]
  89.     if sign && sign.include?('$')
  90.       @cw = bitmap.width / 3
  91.       @ch = bitmap.height / 4
  92.     else
  93.       @cw = bitmap.width / 12
  94.       @ch = bitmap.height / 8
  95.     end
  96.     self.ox = @cw / 2
  97.     self.oy = @ch
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # * Update Transfer Origin Rectangle
  101.   #--------------------------------------------------------------------------
  102.   def update_src_rect
  103.     if @tile_id == 0
  104.       index = @character.character_index
  105.       pattern = @character.pattern < 3 ? @character.pattern : 1
  106.       sx = (index % 4 * 3 + pattern) * @cw
  107.       sy = (index / 4 * 4 + (@character.direction - 2) / 2) * @ch
  108.       self.src_rect.set(sx, sy, @cw, @ch)
  109.     end
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # * Update Position
  113.   #--------------------------------------------------------------------------
  114.   def update_position
  115.     move_animation(@character.screen_x - x, @character.screen_y - y)
  116.     self.x = @character.screen_x
  117.     self.y = @character.screen_y
  118.     self.z = @character.screen_z
  119.   end
  120.   #--------------------------------------------------------------------------
  121.   # * Update Other
  122.   #--------------------------------------------------------------------------
  123.   def update_other
  124.     self.opacity = @character.opacity
  125.     self.blend_type = @character.blend_type
  126.     self.bush_depth = @character.bush_depth
  127.     self.visible = !@character.transparent
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # * Set New Effect
  131.   #--------------------------------------------------------------------------
  132.   def setup_new_effect
  133.     if !animation? && @character.animation_id > 0
  134.       animation = $data_animations[@character.animation_id]
  135.       start_animation(animation)
  136.     end
  137.     if !@balloon_sprite && @character.balloon_id > 0
  138.       @balloon_id = @character.balloon_id
  139.       start_balloon
  140.     end
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # * Move Animation
  144.   #--------------------------------------------------------------------------
  145.   def move_animation(dx, dy)
  146.     if @animation && @animation.position != 3
  147.       @ani_ox += dx
  148.       @ani_oy += dy
  149.       @ani_sprites.each do |sprite|
  150.         sprite.x += dx
  151.         sprite.y += dy
  152.       end
  153.     end
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # * End Animation
  157.   #--------------------------------------------------------------------------
  158.   def end_animation
  159.     super
  160.     @character.animation_id = 0
  161.   end
  162.   #--------------------------------------------------------------------------
  163.   # * Start Balloon Icon Display
  164.   #--------------------------------------------------------------------------
  165.   def start_balloon
  166.     dispose_balloon
  167.     @balloon_duration = 8 * balloon_speed + balloon_wait
  168.     @balloon_sprite = ::Sprite.new(viewport)
  169.     @balloon_sprite.bitmap = Cache.system("Balloon")
  170.     @balloon_sprite.ox = 16
  171.     @balloon_sprite.oy = 32
  172.     update_balloon
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # * Free Balloon Icon
  176.   #--------------------------------------------------------------------------
  177.   def dispose_balloon
  178.     if @balloon_sprite
  179.       @balloon_sprite.dispose
  180.       @balloon_sprite = nil
  181.     end
  182.   end
  183.   #--------------------------------------------------------------------------
  184.   # * Update Balloon Icon
  185.   #--------------------------------------------------------------------------
  186.   def update_balloon
  187.     if @balloon_duration > 0
  188.       @balloon_duration -= 1
  189.       if @balloon_duration > 0
  190.         @balloon_sprite.x = x
  191.         @balloon_sprite.y = y - height
  192.         @balloon_sprite.z = z + 200
  193.         sx = balloon_frame_index * 32
  194.         sy = (@balloon_id - 1) * 32
  195.         @balloon_sprite.src_rect.set(sx, sy, 32, 32)
  196.       else
  197.         end_balloon
  198.       end
  199.     end
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   # * End Balloon Icon
  203.   #--------------------------------------------------------------------------
  204.   def end_balloon
  205.     dispose_balloon
  206.     @character.balloon_id = 0
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   # * Balloon Icon Display Speed
  210.   #--------------------------------------------------------------------------
  211.   def balloon_speed
  212.     return 8
  213.   end
  214.   #--------------------------------------------------------------------------
  215.   # * Wait Time for Last Frame of Balloon
  216.   #--------------------------------------------------------------------------
  217.   def balloon_wait
  218.     return 12
  219.   end
  220.   #--------------------------------------------------------------------------
  221.   # * Frame Number of Balloon Icon
  222.   #--------------------------------------------------------------------------
  223.   def balloon_frame_index
  224.     return 7 - [(@balloon_duration - balloon_wait) / balloon_speed, 0].max
  225.   end
  226. end
Advertisement
Add Comment
Please, Sign In to add comment