diff options
| author | lstrzebinczyk <[email protected]> | 2017-04-26 18:32:25 +0200 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2017-05-05 16:57:24 -0400 |
| commit | 2c164654e5ebec39a3ae2c47d352349332f50ee3 (patch) | |
| tree | 189a16ca51b5fdd579b23fac639aac15665896d9 /lib | |
| parent | 3cd7a36b88ae7cc4441417cf44eb1c44ea131f81 (diff) | |
| download | ruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.tar.gz ruby2d-2c164654e5ebec39a3ae2c47d352349332f50ee3.zip | |
another take on z-index
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d/image.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/quad.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/rectangle.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/renderable.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/sprite.rb | 10 | ||||
| -rw-r--r-- | lib/ruby2d/square.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/triangle.rb | 3 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 9 |
9 files changed, 34 insertions, 11 deletions
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 587a3c0..85ee076 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -7,7 +7,7 @@ module Ruby2D attr_accessor :x, :y, :width, :height, :data attr_reader :path, :color - def initialize(x, y, path) + def initialize(x, y, path, z=0) unless RUBY_ENGINE == 'opal' unless File.exists? path @@ -17,6 +17,7 @@ module Ruby2D @type_id = 3 @x, @y, @path = x, y, path + @z = z @color = Color.new([1, 1, 1, 1]) init(path) add diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb index 5175513..60994a5 100644 --- a/lib/ruby2d/quad.rb +++ b/lib/ruby2d/quad.rb @@ -15,9 +15,10 @@ module Ruby2D attr_reader :color - def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white') + def initialize(x1=0, y1=0, x2=100, y2=0, x3=100, y3=100, x4=100, y4=100, c='white', z=0) @type_id = 2 @x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4 + @z = z self.color = c add diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb index 14fef3f..37ff3c7 100644 --- a/lib/ruby2d/rectangle.rb +++ b/lib/ruby2d/rectangle.rb @@ -5,9 +5,10 @@ module Ruby2D attr_reader :x, :y, :width, :height - def initialize(x=0, y=0, w=200, h=100, c='white') + def initialize(x=0, y=0, w=200, h=100, c='white', z=0) @type_id = 2 @x, @y, @width, @height = x, y, w, h + @z = z update_coords(x, y, w, h) self.color = c diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb index c8fac82..888c29f 100644 --- a/lib/ruby2d/renderable.rb +++ b/lib/ruby2d/renderable.rb @@ -1,5 +1,13 @@ module Ruby2D module Renderable + attr_reader :z + + def z=(z) + remove + @z = z + add + end + def add if Module.const_defined? :DSL Application.add(self) diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index d5695a1..ad8dc17 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -4,9 +4,10 @@ module Ruby2D class Sprite attr_accessor :x, :y, :clip_x, :clip_y, :clip_w, :clip_h, :data - - def initialize(x, y, path) - + attr_reader :z + + def initialize(x, y, path, z=0) + # unless File.exists? path # raise Error, "Cannot find image file `#{path}`" # end @@ -19,7 +20,8 @@ module Ruby2D @current_animation = nil @current_frame = 0 @current_frame_time = 0 - + @z = z + init(path) if Module.const_defined? :DSL Application.add(self) diff --git a/lib/ruby2d/square.rb b/lib/ruby2d/square.rb index 43d6c14..66347ec 100644 --- a/lib/ruby2d/square.rb +++ b/lib/ruby2d/square.rb @@ -5,10 +5,11 @@ module Ruby2D attr_reader :size - def initialize(x=0, y=0, s=100, c='white') + def initialize(x=0, y=0, s=100, c='white', z=0) @type_id = 2 @x, @y = x, y @width = @height = @size = s + @z = z update_coords(x, y, s, s) self.color = c diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 736c8a1..b3b4529 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -7,7 +7,7 @@ module Ruby2D attr_accessor :x, :y, :data attr_reader :text, :size, :width, :height, :font, :color - def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c="white") + def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c='white', z=0) # if File.exists? font @font = font @@ -17,6 +17,7 @@ module Ruby2D @type_id = 5 @x, @y, @size = x, y, size + @z = z @text = text.to_s self.color = c init diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb index c6bac41..cf6e24a 100644 --- a/lib/ruby2d/triangle.rb +++ b/lib/ruby2d/triangle.rb @@ -9,11 +9,12 @@ module Ruby2D :x3, :y3, :c3 attr_reader :color, :type_id - def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white') + def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white', z=0) @type_id = 1 @x1, @y1 = x1, y1 @x2, @y2 = x2, y2 @x3, @y3 = x3, y3 + @z = z self.color = c add diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 4ae1d6e..2b22efa 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -230,7 +230,14 @@ module Ruby2D def add_object(o) if [email protected]?(o) - @objects.push(o) + index = @objects.index do |object| + object.z > o.z + end + if index + @objects.insert(index, o) + else + @objects.push(o) + end true else false |
