Animated Sprite
Author: RPG
Updated: November 07, 2007
Description
This class inherits RPG::Sprite (which in turn inherits Sprite) and allows you to have animated sprites and also sprites that move around the screen. You can probably make Sprite_Battle inherit this to allow battle sprite animation and movement. To use...
Uh, I'm too lazy to explain it right now.#==============================================================================
# * Animated_Sprite Scripted by: RPG
#------------------------------------------------------------------------------
# A class for animated sprites.
#==============================================================================
class Animated_Sprite < RPG::Sprite
#--------------------------------------------------------------------------
# - Accessible instance variables.
#--------------------------------------------------------------------------
attr_accessor :frames # Number of animation frames
attr_accessor :delay # Delay time between frames (speed)
attr_accessor :frame_width # Width of each frame
attr_accessor :frame_height # Height of each frame
attr_accessor :offset_x # X coordinate of the 1st frame
attr_accessor :offset_y # Y coordinate of all frames
attr_accessor :current_frame # Current animation frame
attr_accessor :moving # Is the sprite moving?
#--------------------------------------------------------------------------
# - Initialize an animated sprite
# bitmap : The sprite's bitmap
# frames : Number of animation frames
# delay : Frame delay, controls animation speed
# startf : Starting frame for animation
# fwidth : Width of each frame
# fheight : Height of each frame
# viewport : Sprite viewport
#--------------------------------------------------------------------------
def initialize(bitmap = nil, frames = 0, delay = 0, startf = 0,
fwidth = 0, fheight = 0, viewport = nil)
super(viewport)
self.bitmap = bitmap
@frames = frames
@delay = delay
@offset_x, @offset_y = 0, 0
@frame_width, @frame_height = fwidth, fheight
@current_frame = startf
self.src_rect =
Rect.new(x + startf * fwidth, y, fwidth, fheight)
end
#--------------------------------------------------------------------------
# - Update animation and movement
#--------------------------------------------------------------------------
def update
super
unless self.bitmap == nil
x = @current_frame * @frame_width + @offset_x
self.src_rect =
Rect.new(x, @offset_y, @frame_width, @frame_height)
@current_frame = (@current_frame + 1) % @frames unless @frames == 0
delay(@delay)
end
end
#--------------------------------------------------------------------------
# - Move the sprite
# x : X coordinate of the destination point
# y : Y coordinate of the destination point
# speed : Speed of movement (0 = delayed, 1+ = faster)
# delay : Movement delay if speed is at 0
#--------------------------------------------------------------------------
def move(x, y, speed = 1, delay = 0)
@destx = x
@desty = y
@move_speed = speed
@move_delay = delay
@move_old = Graphics.frame_count
@moving = true
end
#--------------------------------------------------------------------------
# - Move sprite to destx and desty
#--------------------------------------------------------------------------
def update_move
return unless @moving
movinc = @move_speed == 0 ? 1 : @move_speed
if Graphics.frame_count - @move_old > @move_delay or @move_speed != 0
self.x += movinc if self.x < @destx
self.x -= movinc if self.x > @destx
self.y += movinc if self.y < @desty
self.y -= movinc if self.y > @desty
@move_old = Graphics.frame_count
end
if @move_speed > 1
self.x += 1 if (@destx - self.x).abs % @move_speed != 0
self.y += 1 if (@desty - self.y).abs % @move_speed != 0
end
if self.x == @destx and self.y == @desty
@moving = false
end
end
#--------------------------------------------------------------------------
# - Pause animation, but still updates movement
# frames : Number of frames
#--------------------------------------------------------------------------
def delay(frames)
old = Graphics.frame_count
loop do
Graphics.update
update_move
break if (Graphics.frame_count - old >= frames)
end
end
end