diff options
| author | Tom Black <[email protected]> | 2016-10-09 15:35:13 -0400 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2016-10-09 15:35:13 -0400 |
| commit | 922d47027ead7cec732e086880a3a8127c0b8cf6 (patch) | |
| tree | 0e6e9a251e69fdb4936bd9c18df7b66c9172a78c /lib | |
| parent | 31aebd27ec78e6b751d69d2fb360187270b1be00 (diff) | |
| download | ruby2d-922d47027ead7cec732e086880a3a8127c0b8cf6.tar.gz ruby2d-922d47027ead7cec732e086880a3a8127c0b8cf6.zip | |
Add sprites
Also namespace `type_id` definitions
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d.rb | 1 | ||||
| -rw-r--r-- | lib/ruby2d/sprite.rb | 83 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 2 |
3 files changed, 85 insertions, 1 deletions
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 342e03a..4e41d22 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -9,6 +9,7 @@ require 'ruby2d/rectangle' require 'ruby2d/square' require 'ruby2d/triangle' require 'ruby2d/image' +require 'ruby2d/sprite' require 'ruby2d/text' require 'ruby2d/exceptions' require 'ruby2d/ruby2d' # load native extension diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb new file mode 100644 index 0000000..6e3eae4 --- /dev/null +++ b/lib/ruby2d/sprite.rb @@ -0,0 +1,83 @@ +# sprite.rb + +module Ruby2D + class Sprite + + attr_accessor :x, :y + + def initialize(x, y, path) + + unless File.exists? path + raise Error, "Cannot find image file `#{path}`" + end + + @type_id = 4 + @x, @y, @path = x, y, path + @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0 + @default = nil + @animations = {} + @current_animation = nil + @current_frame = 0 + @current_frame_time = 0 + + if defined? DSL + Application.add(self) + end + end + + def start(x, y, w, h) + @default = [x, y, w, h] + clip(x, y, w, h) + end + + def add(animations) + @animations.merge!(animations) + end + + def animate(animation) + if @current_animation != animation + @current_frame = 0 + @current_frame_time = 0 + @current_animation = animation + end + animate_frames(@animations[animation]) + end + + def reset + clip(@default[0], @default[1], @default[2], @default[3]) + @current_animation = nil + end + + def remove + if defined? DSL + Application.remove(self) + end + end + + private + + def clip(x, y, w, h) + @clip_x, @clip_y, @clip_w, @clip_h = x, y, w, h + end + + def animate_frames(frames) + if @current_frame_time < frames[@current_frame][4] + clip_with_current_frame(frames) + @current_frame_time += 1 + else + @current_frame += 1 + if @current_frame == frames.length + @current_frame = 0 + end + clip_with_current_frame(frames) + @current_frame_time = 0 + end + end + + def clip_with_current_frame(frames) + clip(frames[@current_frame][0], frames[@current_frame][1], + frames[@current_frame][2], frames[@current_frame][3]) + end + + end +end diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 9ba4e41..dedfa39 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -13,7 +13,7 @@ module Ruby2D @font = resolve_path(font) end - @type_id = 4 + @type_id = 5 @x, @y, @size = x, y, size @text, @color = msg, c update_color(c) |
