summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-10-11 22:48:47 -0700
committerTom Black <[email protected]>2018-10-11 22:48:47 -0700
commita4a7e23038e26ecc63d94b5e815c36dff2ffb433 (patch)
tree45f937c897466f25be07203032026953f0acea25
parentee4a4815b65f4d785cb8167717a1bc436e8b7674 (diff)
downloadruby2d-a4a7e23038e26ecc63d94b5e815c36dff2ffb433.tar.gz
ruby2d-a4a7e23038e26ecc63d94b5e815c36dff2ffb433.zip
Sprite `play` method now takes opts hash
-rw-r--r--lib/ruby2d/sprite.rb33
-rw-r--r--test/sprite.rb26
2 files changed, 34 insertions, 25 deletions
diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb
index 35c3c90..41c48e2 100644
--- a/lib/ruby2d/sprite.rb
+++ b/lib/ruby2d/sprite.rb
@@ -78,7 +78,7 @@ module Ruby2D
# Set the x coordinate
def x=(x)
@x = @flip_x = x
- if @flip == :flip_h || @flip == :flip_hv
+ if @flip == :horizontal || @flip == :both
@flip_x = x + @width
end
end
@@ -86,7 +86,7 @@ module Ruby2D
# Set the y coordinate
def y=(y)
@y = @flip_y = y
- if @flip == :flip_v || @flip == :flip_hv
+ if @flip == :vertical || @flip == :both
@flip_y = y + @height
end
end
@@ -94,7 +94,7 @@ module Ruby2D
# Set the width
def width=(width)
@width = @flip_width = width
- if @flip == :flip_h || @flip == :flip_hv
+ if @flip == :horizontal || @flip == :both
@flip_width = -width
end
end
@@ -102,13 +102,18 @@ module Ruby2D
# Set the height
def height=(height)
@height = @flip_height = height
- if @flip == :flip_v || @flip == :flip_hv
+ if @flip == :vertical || @flip == :both
@flip_height = -height
end
end
# Play an animation
- def play(animation = nil, loop = nil, flip = nil, &done_proc)
+ def play(opts = {}, &done_proc)
+
+ animation = opts[:animation]
+ loop = opts[:loop]
+ flip = opts[:flip]
+
if !@playing || (animation != @playing_animation && animation != nil) || flip != @flip
@playing = true
@@ -127,11 +132,11 @@ module Ruby2D
when Array
@first_frame = 0
@current_frame = 0
- @last_frame = frames.length - 1
+ @last_frame = frames.length - 1
end
# Set looping
- @loop = loop == :loop || @defaults[:loop] ? true : false
+ @loop = loop == true || @defaults[:loop] ? true : false
set_frame
restart_time
@@ -151,8 +156,12 @@ module Ruby2D
# Flip the sprite
def flip_sprite(flip)
- # A width and height must be set for the sprite for this to work
- unless @width && @height then return end
+ # The sprite width and height must be set for it to be flipped correctly
+ if (!@width || !@height) && flip
+ raise Error,
+ "Sprite width and height must be set in order to flip; " +
+ "occured playing animation `:#{@playing_animation}` with image `#{@path}`"
+ end
@flip = flip
@@ -163,13 +172,13 @@ module Ruby2D
@flip_height = @height
case flip
- when :flip_h # horizontal
+ when :horizontal
@flip_x = @x + @width
@flip_width = -@width
- when :flip_v # vertical
+ when :vertical
@flip_y = @y + @height
@flip_height = -@height
- when :flip_hv # horizontal and vertical
+ when :both # horizontal and vertical
@flip_x = @x + @width
@flip_width = -@width
@flip_y = @y + @height
diff --git a/test/sprite.rb b/test/sprite.rb
index e62488b..e0042b7 100644
--- a/test/sprite.rb
+++ b/test/sprite.rb
@@ -84,7 +84,7 @@ atlas = Sprite.new(
}
)
-atlas.play :count, :loop
+atlas.play animation: :count, loop: true
on :key_down do |e|
@@ -95,9 +95,9 @@ on :key_down do |e|
coin1.play
coin2.play
boom.play
- atlas.play :count
+ atlas.play animation: :count
when 'b'
- boom.play nil, nil, nil do
+ boom.play do
puts "Boom animation finished!"
end
when 's'
@@ -106,33 +106,33 @@ on :key_down do |e|
hero.stop
atlas.stop
when 'left'
- hero.play :walk, :loop, :flip_h
+ hero.play animation: :walk, loop: true, flip: :horizontal
when 'right'
- hero.play :walk, :loop
+ hero.play animation: :walk, loop: true
when 'up'
- hero.play :climb, :loop
+ hero.play animation: :climb, loop: true
when 'down'
- hero.play :climb, :loop, :flip_v
+ hero.play animation: :climb, loop: true, flip: :vertical
when 'h'
- hero.play :climb, :loop, :flip_hv
+ hero.play animation: :climb, loop: true, flip: :both
when 'c'
- hero.play :cheer
+ hero.play animation: :cheer
end
end
on :key_held do |e|
case e.key
when 'a'
- hero.play :walk, :loop, :flip_h
+ hero.play animation: :walk, loop: true, flip: :horizontal
hero.x -= 1
when 'd'
- hero.play :walk, :loop
+ hero.play animation: :walk, loop: true
hero.x += 1
when 'w'
- hero.play :climb, :loop
+ hero.play animation: :climb, loop: true
hero.y -= 1
when 's'
- hero.play :climb, :loop, :flip_v
+ hero.play animation: :climb, loop: true, flip: :vertical
hero.y += 1
when 'z'
hero.width = get(:mouse_x)