summaryrefslogtreecommitdiffhomepage
path: root/lib/ruby2d/camera
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ruby2d/camera')
-rw-r--r--lib/ruby2d/camera/circle.rb10
-rw-r--r--lib/ruby2d/camera/image.rb14
-rw-r--r--lib/ruby2d/camera/line.rb10
-rw-r--r--lib/ruby2d/camera/quad.rb16
-rw-r--r--lib/ruby2d/camera/rectangle.rb46
-rw-r--r--lib/ruby2d/camera/sprite.rb43
-rw-r--r--lib/ruby2d/camera/square.rb36
-rw-r--r--lib/ruby2d/camera/text.rb27
-rw-r--r--lib/ruby2d/camera/triangle.rb11
9 files changed, 157 insertions, 56 deletions
diff --git a/lib/ruby2d/camera/circle.rb b/lib/ruby2d/camera/circle.rb
index ea841f5..c6e1eb0 100644
--- a/lib/ruby2d/camera/circle.rb
+++ b/lib/ruby2d/camera/circle.rb
@@ -8,6 +8,7 @@ module Ruby2D
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -23,7 +24,14 @@ module Ruby2D
def initialize(opts= {})
super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
end
end
diff --git a/lib/ruby2d/camera/image.rb b/lib/ruby2d/camera/image.rb
index 19f9084..fe777b3 100644
--- a/lib/ruby2d/camera/image.rb
+++ b/lib/ruby2d/camera/image.rb
@@ -7,6 +7,7 @@ module Camera
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
temp_angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -26,10 +27,17 @@ module Camera
color: [self.color.r, self.color.g, self.color.b, self.color.a])
end
- def initialize(opts= {})
- super(opts)
+ def initialize(path, opts= {})
+ super(path, opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
+ end
end
end
diff --git a/lib/ruby2d/camera/line.rb b/lib/ruby2d/camera/line.rb
index e7e7c0f..9113b92 100644
--- a/lib/ruby2d/camera/line.rb
+++ b/lib/ruby2d/camera/line.rb
@@ -8,6 +8,7 @@ module Ruby2D
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -31,7 +32,14 @@ module Ruby2D
def initialize(opts= {})
super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
#Methods for moving the shape
diff --git a/lib/ruby2d/camera/quad.rb b/lib/ruby2d/camera/quad.rb
index 5dc52e1..92d858a 100644
--- a/lib/ruby2d/camera/quad.rb
+++ b/lib/ruby2d/camera/quad.rb
@@ -7,7 +7,8 @@ module Ruby2D
class Quad < Ruby2D::Quad
# Recalculates real coordiantes
# Use after changing variables
- def redraw
+ def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -21,8 +22,8 @@ module Ruby2D
temp_y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
Ruby2D::Quad.draw(x1: temp_x1, y1: temp_y1,
x2: temp_x2, y2: temp_y2,
- x3: temp_x3, x3: temp_x3,
- x4: temp_x4, x4: temp_x4,
+ x3: temp_x3, y3: temp_y3,
+ x4: temp_x4, y4: temp_y4,
color: [
[self.c1.r, self.c1.g, self.c1.b, self.c1.a],
[self.c2.r, self.c2.g, self.c2.b, self.c2.a],
@@ -36,7 +37,14 @@ module Ruby2D
def initialize(opts= {})
super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
diff --git a/lib/ruby2d/camera/rectangle.rb b/lib/ruby2d/camera/rectangle.rb
index cbedefc..2a3667d 100644
--- a/lib/ruby2d/camera/rectangle.rb
+++ b/lib/ruby2d/camera/rectangle.rb
@@ -4,19 +4,11 @@ module Ruby2D
module Camera
# Wraps existing variables as well as adding new methods
# so that it can be handled by the Camera Module
- module Rectange < Ruby2D::Rectangle
- # Rectangles are part of the exception where
- # their x and y variables need to be reset
- # when wrapped
- #def self.extended(obj)
- # obj.instance_exec do
- # @x = 0
- # @y = 0
- # end
- #end
+ class Rectangle < Ruby2D::Rectangle
# Recalculates real coordiantes
# Use after changing variables
- def redraw
+ def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -28,12 +20,39 @@ module Ruby2D
temp_y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
temp_x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
temp_y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
+ Ruby2D::Quad.draw(x1: temp_x1, y1: temp_y1,
+ x2: temp_x2, y2: temp_y2,
+ x3: temp_x3, y3: temp_y3,
+ x4: temp_x4, y4: temp_y4,
+ color: [
+ [self.c1.r, self.c1.g, self.c1.b, self.c1.a],
+ [self.c2.r, self.c2.g, self.c2.b, self.c2.a],
+ [self.c3.r, self.c3.g, self.c3.b, self.c3.a],
+ [self.c4.r, self.c4.g, self.c4.b, self.c4.a]
+ ],
+ z: self.z
+ )
end
def initialize(opts= {})
- super(otps)
+ super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ @x1 -= @x
+ @x2 -= @x
+ @x3 -= @x
+ @x4 -= @x
+ @y1 -= @y
+ @y2 -= @y
+ @y3 -= @y
+ @y4 -= @y
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
#Methods for moving the shape
def x
@@ -51,7 +70,6 @@ module Ruby2D
def y=(y)
@y = y
end
-
end
end
end
diff --git a/lib/ruby2d/camera/sprite.rb b/lib/ruby2d/camera/sprite.rb
index 4ad5bca..0d239a1 100644
--- a/lib/ruby2d/camera/sprite.rb
+++ b/lib/ruby2d/camera/sprite.rb
@@ -7,41 +7,52 @@ module Ruby2D
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
- offset_x = x + (width / 2)
- offset_y = y + (height / 2)
- temp_x = @flip_x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \
+ offset_x = @x + (width / 2)
+ offset_y = @y + (height / 2)
+ temp_x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \
* Camera.zoom + half_width - (width * Camera.zoom / 2)
- temp_y = @flip_y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \
+ temp_y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \
* Camera.zoom + half_height - (height * Camera.zoom / 2)
temp_rotate = rotate + Camera.angle
- temp_width = @flip_width = width * Camera.zoom
- temp_height = @flip_height = height * Camera.zoom
+ temp_width = width * Camera.zoom
+ temp_height = height * Camera.zoom
case @flip
when :both
- @flip_x = @x + @height
- @flip_width = -@width
- @flip_y = @y + @width
- @flip_height = -@height
+ temp_x = temp_x + temp_height
+ temp_y = temp_y + temp_width
+ temp_width = -temp_width
+ temp_height = -temp_height
+ puts 'both'
when :horizontal
- @flip_y = @y + @width
- @flip_height = -@height
+ temp_y = temp_y + temp_width
+ temp_height = -temp_height
when :vertical
- @flip_x = @x + @height
- @flip_width = -@width
+ temp_width = -temp_width
+ temp_x = temp_x + temp_height
end
- self.draw(x: temp_x, y: temp_x,
+ self.draw(x: temp_x, y: temp_y,
width: temp_width,
height: temp_height,
rotate: temp_rotate)
+ self.update
end
def initialize(path, opts= {})
super(path, opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ end
+
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
end
end
diff --git a/lib/ruby2d/camera/square.rb b/lib/ruby2d/camera/square.rb
index d013e56..584af03 100644
--- a/lib/ruby2d/camera/square.rb
+++ b/lib/ruby2d/camera/square.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-class Ruby2D
+module Ruby2D
module Camera
# Wraps existing variables as well as adding new methods
# so that it can be handled by the Camera Module
@@ -8,17 +8,18 @@ class Ruby2D
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
- @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
- @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
- @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
- @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
- @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
- @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
- @x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
- @y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
+ temp_x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
+ temp_y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
+ temp_x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
+ temp_y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
+ temp_x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
+ temp_y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
+ temp_x4 = (((x + x4 - Camera.x) * Math.cos(angle)) - ((y + y4 - Camera.y) * Math.sin(angle))) * Camera.zoom + half_width
+ temp_y4 = (((x + x4 - Camera.x) * Math.sin(angle)) + ((y + y4 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
Ruby2D::Quad.draw(x1: temp_x1, y1: temp_y1,
x2: temp_x2, y2: temp_y2,
x3: temp_x3, y3: temp_y3,
@@ -36,9 +37,24 @@ class Ruby2D
def initialize(opts= {})
super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ @x1 -= @x
+ @x2 -= @x
+ @x3 -= @x
+ @x4 -= @x
+ @y1 -= @y
+ @y2 -= @y
+ @y3 -= @y
+ @y4 -= @y
end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
+ end
#Methods for moving the shape
def x
@x ||= 0
diff --git a/lib/ruby2d/camera/text.rb b/lib/ruby2d/camera/text.rb
index 21c9e1b..6966a86 100644
--- a/lib/ruby2d/camera/text.rb
+++ b/lib/ruby2d/camera/text.rb
@@ -10,11 +10,10 @@ module Ruby2D
# to be corrected again(see image_wrapper.rb for reference, that has
# math that allows for resizing)
class Text < Ruby2D::Text
- @center = false
-
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -31,18 +30,34 @@ module Ruby2D
* Camera.zoom + half_height - (height / 2)
temp_rotate = rotate + Camera.angle
# Workaround for resizing text
+ # TODO: resizing doesnt work at all even with workaround
temp_size = size# * Camera.zoom
self.size *= Camera.zoom
self.draw(x: temp_x, y: temp_y,
rotate: temp_rotate,
- color: self.color)
+ color: [self.color.r, self.color.g, self.color.b, self.color.a])
self.size = temp_size
end
- def initialize(opts = {})
- super(opts)
+ def initialize(text, opts= {})
+ super(text, opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
+ end
+
+ def center
+ @center
+ end
+
+ def center=(center)
+ @center = center
end
end
end
diff --git a/lib/ruby2d/camera/triangle.rb b/lib/ruby2d/camera/triangle.rb
index 356222b..4065b62 100644
--- a/lib/ruby2d/camera/triangle.rb
+++ b/lib/ruby2d/camera/triangle.rb
@@ -6,6 +6,7 @@ module Ruby2D
# Recalculates real coordiantes
# Use after changing variables
def _draw
+ return if @hide
angle = Ruby2D::Camera.angle * (Math::PI / 180)
half_width = Window.width * 0.5
half_height = Window.height * 0.5
@@ -29,7 +30,15 @@ module Ruby2D
def initialize(opts= {})
super(opts)
Ruby2D::Camera << self
- self.remove
+ Window.remove(self)
+ self.add
+ end
+ def remove
+ @hide = true
+ end
+
+ def add
+ @hide = false
end
#Methods for moving the shape