lib/lotu/actor.rb in lotu-0.1.11 vs lib/lotu/actor.rb in lotu-0.1.12
- old
+ new
@@ -25,16 +25,20 @@
opts = default_opts.merge!(opts)
@parent = opts[:parent]
@parent.manage_me(self)
set_image(opts[:image], opts) if opts[:image]
parse_options(opts)
+ @color = rand_color if opts[:rand_color]
@systems = {}
+ set_keys(opts[:keys]) unless opts[:keys].nil?
+
# Add extra functionality
self.extend Eventful
self.extend Collidable
use(AnimationSystem)
+ use(TransformationSystem)
end
# Easy access to delta-time
def dt
$lotu.dt
@@ -48,40 +52,64 @@
@center_x = opts[:center_x] || @center_x
@center_y = opts[:center_y] || @center_y
@factor_x = opts[:factor_x] || @factor_x
@factor_y = opts[:factor_y] || @factor_y
@color = opts[:color] || @color
+ if @color.kind_of?(Integer)
+ @color = Gosu::Color.new(opts[:color])
+ end
@mode = opts[:mode] || @mode
end
+ def rand_color
+ Gosu::Color.from_hsv(rand(360), 1, 1)
+ end
+
def set_image(image, opts={})
@image = @parent.image(image)
- puts "Image \"#{image}\" not found".red if @image.nil?
+ if @image.nil?
+ puts "Image \"#{image}\" not found".red
+ return
+ end
parse_options(opts)
- @width = opts[:width] || @image.width
- @height = opts[:height] || @image.height
+ adjust_width_and_height(opts)
calc_zoom
end
def set_gosu_image(image, opts={})
@image = image
parse_options(opts)
- @width = opts[:width] || @image.width
- @height = opts[:height] || @image.height
+ adjust_width_and_height(opts)
calc_zoom
end
def width=(width)
- @width = width
+ @width = Float(width)
calc_zoom
end
def height=(height)
- @height = height
+ @height = Float(height)
calc_zoom
end
+ def adjust_width_and_height(opts)
+ if(opts[:width] && opts[:height])
+ @width = Float(opts[:width])
+ @height = Float(opts[:height])
+ elsif(opts[:width])
+ @width = Float(opts[:width])
+ @height = @width * @image.height / @image.width
+ elsif(opts[:height])
+ @height = Float(opts[:height])
+ @width = @height * @image.width / @image.height
+ else
+ @width = Float(@image.width)
+ @height = Float(@image.height)
+ end
+ end
+
def calc_zoom
@zoom_x = Float(@width)/@image.width
@zoom_y = Float(@height)/@image.height
end
@@ -95,11 +123,13 @@
system.update
end
end
def draw
- @image.draw_rot(@x, @y, @z, @angle, @center_x, @center_y, @factor_x*@zoom_x, @factor_y*@zoom_y, @color, @mode) unless @image.nil?
- draw_debug if $lotu.debug? unless @image.nil?
+ unless @image.nil?
+ @image.draw_rot(@x, @y, @z, @angle, @center_x, @center_y, @factor_x*@zoom_x, @factor_y*@zoom_y, @color, @mode)
+ draw_debug if $lotu.debug?
+ end
end
def draw_debug
draw_box(@x-@image.width/2, @y-@image.height/2, @image.width, @image.height)
draw_box(@x-@width/2, @y-@height/2, @width, @height, 0xff00ff00)