diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/circle.rb | 15 | ||||
| -rw-r--r-- | lib/ruby2d/color.rb | 22 | ||||
| -rw-r--r-- | lib/ruby2d/dsl.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/exceptions.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/image.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/line.rb | 13 | ||||
| -rw-r--r-- | lib/ruby2d/music.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/quad.rb | 7 | ||||
| -rw-r--r-- | lib/ruby2d/rectangle.rb | 14 | ||||
| -rw-r--r-- | lib/ruby2d/renderable.rb | 6 | ||||
| -rw-r--r-- | lib/ruby2d/sound.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/sprite.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/square.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 6 | ||||
| -rw-r--r-- | lib/ruby2d/triangle.rb | 6 | ||||
| -rw-r--r-- | lib/ruby2d/version.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 12 |
18 files changed, 74 insertions, 64 deletions
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 6dc72dc..1d48ab8 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -1,4 +1,4 @@ -# ruby2d.rb +# Ruby2D module and native extension loader, adds DSL require 'ruby2d/renderable' require 'ruby2d/exceptions' diff --git a/lib/ruby2d/circle.rb b/lib/ruby2d/circle.rb index 4f87828..90b86df 100644 --- a/lib/ruby2d/circle.rb +++ b/lib/ruby2d/circle.rb @@ -1,4 +1,4 @@ -# circle.rb +# Ruby2D::Circle module Ruby2D class Circle @@ -8,12 +8,12 @@ module Ruby2D attr_accessor :x, :y, :radius, :sectors def initialize(opts = {}) - @x = opts[:x] || 25 - @y = opts[:y] || 25 - @radius = opts[:radius] || 25 - @sectors = opts[:sectors] || 20 - @z = opts[:z] || 0 - self.color = opts[:color] || 'white' + @x = opts[:x] || 25 + @y = opts[:y] || 25 + @z = opts[:z] || 0 + @radius = opts[:radius] || 25 + @sectors = opts[:sectors] || 20 + self.color = opts[:color] || 'white' add end @@ -24,5 +24,6 @@ module Ruby2D def contains?(x, y) Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius end + end end diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb index a41e6c7..26d176f 100644 --- a/lib/ruby2d/color.rb +++ b/lib/ruby2d/color.rb @@ -1,11 +1,12 @@ -# color.rb +# Ruby2D::Color module Ruby2D class Color + # Color::Set represents an array of colors class Set def initialize(colors) - @colors = colors.map{|c| Color.new(c)} + @colors = colors.map { |c| Color.new(c) } end def [](i) @@ -83,31 +84,28 @@ module Ruby2D # Array of Floats from 0.0..1.0 c.class == Array && c.length == 4 && - c.all? { |el| - el.is_a?(Numeric) - } + c.all? { |el| el.is_a?(Numeric) } end + # Create a color from whatever is provided def self.from(input) - # If a valid array of colors, return a Color::Set with those colors - # Else return single color + # If a valid array of colors, return a `Color::Set` with those colors if input.is_a? Array and input.all? { |el| Color.is_valid? el } Color::Set.new(input) + # Otherwise, return single color else Color.new(input) end end + # Convenience methods to alias `opacity` to `@a` def opacity; @a end - - def opacity=(opacity) - @a = opacity - end + def opacity=(opacity); @a = opacity end private - # TODO: Only `Number` supported in JS # Convert from Fixnum (0..255) to Float (0.0..1.0) + # TODO: Only `Number` is supported in JS def to_f(a) b = [] a.each do |n| diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index 993b5ca..a6818fa 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -1,4 +1,4 @@ -# dsl.rb +# Ruby2D::DSL module Ruby2D::DSL diff --git a/lib/ruby2d/exceptions.rb b/lib/ruby2d/exceptions.rb index 687ab11..595c328 100644 --- a/lib/ruby2d/exceptions.rb +++ b/lib/ruby2d/exceptions.rb @@ -1,4 +1,4 @@ -# exceptions.rb +# Ruby2D::Error module Ruby2D class Error < StandardError diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 5490d53..9386f6b 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -1,4 +1,4 @@ -# image.rb +# Ruby2D::Image module Ruby2D class Image @@ -19,10 +19,9 @@ module Ruby2D @x = opts[:x] || 0 @y = opts[:y] || 0 @z = opts[:z] || 0 - @width = opts[:width] || nil + @width = opts[:width] || nil @height = opts[:height] || nil - @rotate = 0 - + @rotate = opts[:rotate] || 0 self.color = opts[:color] || 'white' ext_init(@path) @@ -36,5 +35,6 @@ module Ruby2D def contains?(x, y) @x < x and @x + @width > x and @y < y and @y + @height > y end + end end diff --git a/lib/ruby2d/line.rb b/lib/ruby2d/line.rb index 23fb03d..e817acb 100644 --- a/lib/ruby2d/line.rb +++ b/lib/ruby2d/line.rb @@ -1,4 +1,4 @@ -# line.rb +# Ruby2D::Line module Ruby2D class Line @@ -14,7 +14,6 @@ module Ruby2D @width = opts[:width] || 2 @z = opts[:z] || 0 self.color = opts[:color] || 'white' - add end @@ -23,13 +22,15 @@ module Ruby2D update_color(@color) end + # Return the length of the line def length points_distance(@x1, @y1, @x2, @y2) end - # Line contains a point if the point is closer than the length of line from both ends - # and if the distance from point to line is smaller than half of the width. - # Check https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line for reference + # Line contains a point if the point is closer than the length of line from + # both ends and if the distance from point to line is smaller than half of + # the width. For reference: + # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line def contains?(x, y) points_distance(x1, y1, x, y) < length and points_distance(x2, y2, x, y) < length and @@ -38,6 +39,7 @@ module Ruby2D private + # Calculate the distance between two points def points_distance(x1, y1, x2, y2) Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) end @@ -59,5 +61,6 @@ module Ruby2D @c4 = c end end + end end diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index 0ace141..41b8901 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -1,4 +1,4 @@ -# music.rb +# Ruby2D::Music module Ruby2D class Music @@ -19,24 +19,30 @@ module Ruby2D ext_init(path) end + # Play the music def play ext_play end + # Pause the music def pause ext_pause end + # Resume paused music def resume ext_resume end + # Stop playing the music, start at beginning def stop ext_stop end + # Fade out music over provided milliseconds def fadeout(ms) ext_fadeout(ms) end + end end diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb index 8ffc194..e514a39 100644 --- a/lib/ruby2d/quad.rb +++ b/lib/ruby2d/quad.rb @@ -1,4 +1,4 @@ -# quad.rb +# Ruby2D::Quad module Ruby2D class Quad @@ -25,9 +25,7 @@ module Ruby2D @y3 = opts[:y3] || 100 @x4 = opts[:x4] || 0 @y4 = opts[:y4] || 100 - - @z = opts[:z] || 0 - + @z = opts[:z] || 0 self.color = opts[:color] || 'white' add end @@ -74,5 +72,6 @@ module Ruby2D @c4 = c end end + end end diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb index 4b9db5e..43b0db5 100644 --- a/lib/ruby2d/rectangle.rb +++ b/lib/ruby2d/rectangle.rb @@ -1,4 +1,4 @@ -# rectangle.rb +# Ruby2D::Rectangle module Ruby2D class Rectangle < Quad @@ -6,15 +6,13 @@ module Ruby2D attr_reader :x, :y, :width, :height def initialize(opts = {}) - @x = opts[:x] || 0 - @y = opts[:y] || 0 - @z = opts[:z] || 0 - @width = opts[:width] || 200 + @x = opts[:x] || 0 + @y = opts[:y] || 0 + @z = opts[:z] || 0 + @width = opts[:width] || 200 @height = opts[:height] || 100 - - update_coords(@x, @y, @width, @height) - self.color = opts[:color] || 'white' + update_coords(@x, @y, @width, @height) add end diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb index d704c17..6a6761d 100644 --- a/lib/ruby2d/renderable.rb +++ b/lib/ruby2d/renderable.rb @@ -1,5 +1,8 @@ +# Ruby2D::Renderable + module Ruby2D module Renderable + attr_reader :z def z=(z) @@ -29,7 +32,8 @@ module Ruby2D end def contains?(x, y) - raise "Not implemented yet" + raise Error, "\`#contains?\` not implemented for this class yet" end + end end diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index 3ead3fa..c1d4060 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -1,4 +1,4 @@ -# sound.rb +# Ruby2D::Sound module Ruby2D class Sound @@ -18,6 +18,7 @@ module Ruby2D ext_init(path) end + # Play the sound def play ext_play end diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index 68b6ace..37753fc 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -1,4 +1,4 @@ -# sprite.rb +# Ruby2D::Sprite module Ruby2D class Sprite diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb index 1a862b9..83cefb0 100644 --- a/lib/ruby2d/square.rb +++ b/lib/ruby2d/square.rb @@ -1,4 +1,4 @@ -# square.rb +# Ruby2D::Square module Ruby2D class Square < Rectangle @@ -10,18 +10,18 @@ module Ruby2D @y = opts[:y] || 0 @z = opts[:z] || 0 @width = @height = @size = opts[:size] || 100 - self.color = opts[:color] || 'white' - update_coords(@x, @y, @size, @size) - add end + # Set the size of the square def size=(s) self.width = self.height = @size = s end + # Make the inherited width and height attribute accessors private private :width=, :height= + end end diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 9a66b6d..1b2207d 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -1,4 +1,4 @@ -# text.rb +# Ruby2D::Text module Ruby2D class Text @@ -11,9 +11,9 @@ module Ruby2D @x = opts[:x] || 0 @y = opts[:y] || 0 @z = opts[:z] || 0 - @text = (opts[:text] || "Hello World!").to_s + @text = (opts[:text] || "Hello Ruby!").to_s @size = opts[:size] || 20 - @rotate = 0 + @rotate = opts[:rotate] || 0 @font = opts[:font] unless RUBY_ENGINE == 'opal' diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb index 75615cf..0888994 100644 --- a/lib/ruby2d/triangle.rb +++ b/lib/ruby2d/triangle.rb @@ -1,4 +1,4 @@ -# triangle.rb +# Ruby2D::Triangle module Ruby2D class Triangle @@ -16,8 +16,7 @@ module Ruby2D @y2 = opts[:y2] || 100 @x3 = opts[:x3] || 0 @y3 = opts[:y3] || 100 - @z = opts[:z] || 0 - + @z = opts[:z] || 0 self.color = opts[:color] || 'white' add end @@ -61,5 +60,6 @@ module Ruby2D @c3 = c end end + end end diff --git a/lib/ruby2d/version.rb b/lib/ruby2d/version.rb index 46d7714..19ae871 100644 --- a/lib/ruby2d/version.rb +++ b/lib/ruby2d/version.rb @@ -1,4 +1,4 @@ -# version.rb +# Ruby2D::VERSION module Ruby2D VERSION = '0.5.1' diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 6ed6677..385070b 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -1,4 +1,4 @@ -# Ruby 2D Window class +# Ruby2D::Window # Represents a window on screen, responsible for storing renderable graphics, # event handlers, the update loop, showing and closing the window. @@ -28,12 +28,12 @@ module Ruby2D @icon = nil # Window size and characteristics - @width = args[:width] || 640 - @height = args[:height] || 480 - @resizable = false + @width = args[:width] || 640 + @height = args[:height] || 480 + @resizable = false @borderless = false @fullscreen = false - @highdpi = false + @highdpi = false # Size of the window's viewport (the drawable area) @viewport_width, @viewport_height = nil, nil @@ -46,7 +46,7 @@ module Ruby2D # Frames per second upper limit, and the actual FPS @fps_cap = args[:fps_cap] || 60 - @fps = @fps_cap + @fps = @fps_cap # Vertical synchronization, set to prevent screen tearing (recommended) @vsync = args[:vsync] || true |
