class MiniGL::Sprite
This class represents an (optionally animated) image inside the game screen.
def animate(indices, interval)
A frame will usually represent 1/60 second (roughly 17
[interval] The amount of frames between each change in the image index.
interval
0..(sprite_cols * sprite_rows).bottom, inside the spritesheet. All indices must be in the
indices are determined from left to right, and from top to
[indices] The sequence of image indices used in the animation. The
Parameters:
sequence of indices and the interval.
Performs time checking to update the image index according to the
def animate(indices, interval) @anim_counter += 1 if @anim_counter >= interval @index_index += 1 @index_index = 0 if @index_index == indices.length @img_index = indices[@index_index] @anim_counter = 0 end end
def draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0)
[z_index] The z-order to draw the object. Objects with larger z-orders
to draw it vertically flipped.
[flip] Specify +:horiz+ to draw the image horizontally flipped or +:vert+
its center.
[angle] A rotation, in degrees, to be applied to the image, relative to
will be darkened, for example.
reddish colors with slight or no change, whereas bluish colors
will turn all colors to black. A red (0xff0000) filter will keep
will keep all colors unchanged, while a black (0x000000) filter
[color] A color filter to apply to the image. A white (0xffffff) filter
vary from 0 (fully transparent) to 255 (fully opaque).
[alpha] The opacity with which the image will be drawn. Valid values
[scale_y] A scale factor to be applied vertically to the image.
[scale_x] A scale factor to be applied horizontally to the image.
position of the camera).
and y coordinates of the sprite will be changed according to the
[map] A Map object, relative to which the sprite will be drawn (the x
Parameters:
Draws the sprite in the screen
def draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0) color = (alpha << 24) | color if angle @img[@img_index].draw_rot @x.round - (map ? map.cam.x : 0) + (flip == :horiz ? scale_x * @img[0].width : 0), @y.round - (map ? map.cam.y : 0) + (flip == :vert ? scale_y * @img[0].height : 0), z_index, angle, 0.5, 0.5, (flip == :horiz ? -scale_x : scale_x), (flip == :vert ? -scale_y : scale_y), color else @img[@img_index].draw @x.round - (map ? map.cam.x : 0) + (flip == :horiz ? scale_x * @img[0].width : 0), @y.round - (map ? map.cam.y : 0) + (flip == :vert ? scale_y * @img[0].height : 0), z_index, (flip == :horiz ? -scale_x : scale_x), (flip == :vert ? -scale_y : scale_y), color end end
def initialize(x, y, img, sprite_cols = nil, sprite_rows = nil)
[sprite_rows] The number of rows in the spritesheet. Use +nil+ if the
image is not a spritesheet.
[sprite_cols] The number of columns in the spritesheet. Use +nil+ if the
or +:sprite_1+.
'data/img/sprite/1.png', you must provide
"sprite_1"must not contain underscores). For example, if your image is
name, followed by an underscore (so the file and directories names
'data/img', you must prefix the file name with each subdirectory
extension, in this case. If the image is inside a subdirectory of
to the code file, and you must only provide the file name, without
convention: images must be inside a 'data/img' directory, relative
[img] The path to a PNG image or spritesheet, following the MiniGL
drawn. This can be modified later via the +y+ attribute.
[y] The y-coordinate in the screen (or map) where the sprite will be
drawn. This can be modified later via the +x+ attribute.
[x] The x-coordinate in the screen (or map) where the sprite will be
Parameters:
Creates a new sprite.
def initialize(x, y, img, sprite_cols = nil, sprite_rows = nil) @x = x; @y = y @img = if sprite_cols.nil? [Res.img(img)] else Res.imgs img, sprite_cols, sprite_rows end @anim_counter = 0 @img_index = 0 @index_index = 0 end
def visible?(map = nil)
viewport determined by the camera of the given map). If no map is given,
Returns whether this sprite is visible in the given map (i.e., in the
def visible?(map = nil) r = Rectangle.new @x, @y, @img[0].width, @img[0].height return Rectangle.new(0, 0, G.window.width, G.window.height).intersect? r if map.nil? map.cam.intersect? r end