summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-08 02:14:53 -0400
committerrealtradam <[email protected]>2021-08-08 02:14:53 -0400
commitcad7a2876013ae52f248d7d3fd35bb6d7a2d36cb (patch)
tree7888eb2df8401feaa1426d9ca411a7e7b76cb792
parente9a05cecd41b244977c794931b4706015097774f (diff)
downloadruby2d-camera-cad7a2876013ae52f248d7d3fd35bb6d7a2d36cb.tar.gz
ruby2d-camera-cad7a2876013ae52f248d7d3fd35bb6d7a2d36cb.zip
fixed formatting and file structure
-rw-r--r--.rubocop.yml6
-rw-r--r--example/assets/background.png (renamed from assets/background.png)bin60142 -> 60142 bytes
-rw-r--r--example/assets/blobcoolthink.png (renamed from assets/blobcoolthink.png)bin13066 -> 13066 bytes
-rw-r--r--example/assets/blobshadow.png (renamed from assets/blobshadow.png)bin5679 -> 5679 bytes
-rw-r--r--example/assets/bricktexture.png (renamed from assets/bricktexture.png)bin741798 -> 741798 bytes
-rw-r--r--example/assets/legacy_player.png (renamed from assets/legacy_player.png)bin1044 -> 1044 bytes
-rw-r--r--example/assets/sprites/alienpls-56.png (renamed from assets/sprites/alienpls-56.png)bin255710 -> 255710 bytes
-rw-r--r--example/assets/sprites/blobdance-128.png (renamed from assets/sprites/blobdance-128.png)bin35825 -> 35825 bytes
-rw-r--r--example/assets/sprites/dance2-112.png (renamed from assets/sprites/dance2-112.png)bin214857 -> 214857 bytes
-rw-r--r--example/assets/sprites/dancer-128.png (renamed from assets/sprites/dancer-128.png)bin39270 -> 39270 bytes
-rw-r--r--example/assets/sprites/mainblob-128.png (renamed from assets/sprites/mainblob-128.png)bin94731 -> 94731 bytes
-rw-r--r--example/house.rb100
-rw-r--r--example/room.rb129
-rw-r--r--example/run.rb223
-rw-r--r--house.rb103
-rw-r--r--lib/camera/camera.rb125
-rw-r--r--lib/camera/wrappers/circle_wrapper.rb47
-rw-r--r--lib/camera/wrappers/image_wrapper.rb73
-rw-r--r--lib/camera/wrappers/line_wrapper.rb91
-rw-r--r--lib/camera/wrappers/quad_wrapper.rb108
-rw-r--r--lib/camera/wrappers/rectangle_wrapper.rb138
-rw-r--r--lib/camera/wrappers/sprite_wrapper.rb86
-rw-r--r--lib/camera/wrappers/square_wrapper.rb146
-rw-r--r--lib/camera/wrappers/text_wrapper.rb84
-rw-r--r--lib/camera/wrappers/triangle_wrapper.rb90
-rw-r--r--lib/ruby2d/camera.rb10
-rw-r--r--lib/ruby2d/camera/circle.rb8
-rw-r--r--lib/ruby2d/camera/image.rb32
-rw-r--r--lib/ruby2d/camera/line.rb32
-rw-r--r--lib/ruby2d/camera/quad.rb26
-rw-r--r--lib/ruby2d/camera/rectangle.rb26
-rw-r--r--lib/ruby2d/camera/sprite.rb22
-rw-r--r--lib/ruby2d/camera/square.rb27
-rw-r--r--lib/ruby2d/camera/text.rb20
-rw-r--r--lib/ruby2d/camera/triangle.rb25
-rw-r--r--lib/ruby2d/camera/version.rb2
-rw-r--r--room.rb132
-rw-r--r--run.rb1
-rw-r--r--test.rb17
39 files changed, 558 insertions, 1371 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index bfef2d0..adb3cb9 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -1,13 +1,13 @@
AllCops:
- TargetRubyVersion: 2.4
+ TargetRubyVersion: 3.0
Style/StringLiterals:
Enabled: true
- EnforcedStyle: double_quotes
+ EnforcedStyle: single_quotes
Style/StringLiteralsInInterpolation:
Enabled: true
- EnforcedStyle: double_quotes
+ EnforcedStyle: single_quotes
Layout/LineLength:
Max: 120
diff --git a/assets/background.png b/example/assets/background.png
index f411e96..f411e96 100644
--- a/assets/background.png
+++ b/example/assets/background.png
Binary files differ
diff --git a/assets/blobcoolthink.png b/example/assets/blobcoolthink.png
index 7cdfcc8..7cdfcc8 100644
--- a/assets/blobcoolthink.png
+++ b/example/assets/blobcoolthink.png
Binary files differ
diff --git a/assets/blobshadow.png b/example/assets/blobshadow.png
index ff981cb..ff981cb 100644
--- a/assets/blobshadow.png
+++ b/example/assets/blobshadow.png
Binary files differ
diff --git a/assets/bricktexture.png b/example/assets/bricktexture.png
index 4ef86e0..4ef86e0 100644
--- a/assets/bricktexture.png
+++ b/example/assets/bricktexture.png
Binary files differ
diff --git a/assets/legacy_player.png b/example/assets/legacy_player.png
index 485e972..485e972 100644
--- a/assets/legacy_player.png
+++ b/example/assets/legacy_player.png
Binary files differ
diff --git a/assets/sprites/alienpls-56.png b/example/assets/sprites/alienpls-56.png
index af9452a..af9452a 100644
--- a/assets/sprites/alienpls-56.png
+++ b/example/assets/sprites/alienpls-56.png
Binary files differ
diff --git a/assets/sprites/blobdance-128.png b/example/assets/sprites/blobdance-128.png
index 6326ec0..6326ec0 100644
--- a/assets/sprites/blobdance-128.png
+++ b/example/assets/sprites/blobdance-128.png
Binary files differ
diff --git a/assets/sprites/dance2-112.png b/example/assets/sprites/dance2-112.png
index 2d69026..2d69026 100644
--- a/assets/sprites/dance2-112.png
+++ b/example/assets/sprites/dance2-112.png
Binary files differ
diff --git a/assets/sprites/dancer-128.png b/example/assets/sprites/dancer-128.png
index c3fdbc0..c3fdbc0 100644
--- a/assets/sprites/dancer-128.png
+++ b/example/assets/sprites/dancer-128.png
Binary files differ
diff --git a/assets/sprites/mainblob-128.png b/example/assets/sprites/mainblob-128.png
index ca78578..ca78578 100644
--- a/assets/sprites/mainblob-128.png
+++ b/example/assets/sprites/mainblob-128.png
Binary files differ
diff --git a/example/house.rb b/example/house.rb
new file mode 100644
index 0000000..c6257ee
--- /dev/null
+++ b/example/house.rb
@@ -0,0 +1,100 @@
+# frozen_string_literal: true
+
+# Is a house
+class House
+ def initialize(x, y)
+ @objects = []
+ @x = x
+ @y = y
+ @objects.push Camera::Image.new('assets/blobshadow.png',
+ width: 320,
+ height: 250,
+ x: x - 10,
+ y: y + 130,
+ z: 0)
+ @objects.push Camera::Image.new('assets/bricktexture.png',
+ x: x,
+ y: y,
+ width: 300,
+ height: 300)
+ @objects.push Camera::Square.new(x: 125 + x,
+ y: 230 + y,
+ size: 50,
+ color: 'black')
+ @objects.push Camera::Circle.new(x: 125 + x,
+ y: 205 + y,
+ radius: 25,
+ color: 'black')
+ @objects.push Camera::Triangle.new(x1: -5 + x,
+ y1: 16 + y,
+ x2: 310 + x,
+ y2: 14 + y,
+ x3: 150 + x,
+ y3: -75 + y,
+ color: 'red')
+ @objects.push Camera::Square.new(x: 160 + x,
+ y: 20 + y,
+ size: 100,
+ color: 'brown',
+ z: 1)
+ @objects.push Camera::Square.new(x: 170 + x,
+ y: 25 + y,
+ size: 80,
+ opacity: 0.5,
+ color: 'blue',
+ z: 2)
+ @objects.push Camera::Rectangle.new(x: 160 + x,
+ y: 105 + y,
+ width: 100,
+ height: 20,
+ color: 'brown',
+ z: 3)
+ @objects.push Camera::Line.new(x1: 210 + x,
+ y1: 105 + y,
+ x2: 210 + x,
+ y2: 25 + y,
+ width: 4,
+ color: 'brown',
+ z: 3)
+ @objects.push Camera::Line.new(x1: 250 + x,
+ y1: 65 + y,
+ x2: 170 + x,
+ y2: 65 + y,
+ width: 4,
+ color: 'brown',
+ z: 3)
+ @objects.push Camera::Sprite.new('./assets/sprites/alienpls-56.png',
+ x: 175 + x,
+ y: 65 + y,
+ width: 56,
+ height: 56,
+ clip_width: 56,
+ loop: true,
+ time: 35,
+ z: 1)
+ @objects.last.play
+ # @objects.each do |item|
+ # Camera << item
+ # end
+ end
+
+ def remove
+ @objects.each do |item|
+ # Camera.remove item
+ item.remove
+ end
+ end
+
+ def visted_by?(character)
+ x = @x + 80
+ y = @y + 160
+ if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2))
+ Camera::Text.new('Press Space To Enter House',
+ x: x + 70,
+ y: y + 30,
+ color: 'white',
+ z: 98,
+ size: 25.0)
+ end
+ end
+end
diff --git a/example/room.rb b/example/room.rb
new file mode 100644
index 0000000..27c002d
--- /dev/null
+++ b/example/room.rb
@@ -0,0 +1,129 @@
+# frozen_string_literal: true
+
+# Is a house
+class Room
+ def initialize(x, y)
+ @objects = []
+ @x = x
+ @y = y
+ roomy = 155
+ @objects.push Camera::Square.new(x: 125 + x,
+ y: 230 + y,
+ size: 50,
+ color: 'blue',
+ z: 3)
+ @objects.push Camera::Circle.new(x: 125 + x,
+ y: 205 + y,
+ radius: 25,
+ sectors: 64,
+ color: 'blue',
+ z: 3)
+ @objects.push Camera::Quad.new(x1: 50 + x,
+ y1: 130 + y + roomy,
+ x2: 125 + x,
+ y2: 180 + y + roomy,
+ x3: 325 + x,
+ y3: 180 + y + roomy,
+ x4: 250 + x,
+ y4: 130 + y + roomy,
+ color: 'aqua',
+ z: 1)
+ @objects.push Camera::Quad.new(x1: 50 + x,
+ y1: 45 + y + roomy,
+ x2: 50 + x,
+ y2: 125 + y + roomy,
+ x3: 250 + x,
+ y3: 125 + y + roomy,
+ x4: 250 + x,
+ y4: 45 + y + roomy,
+ color: 'orange',
+ z: 1)
+ @objects.push Camera::Quad.new(x1: 255 + x,
+ y1: 45 + y + roomy,
+ x2: 255 + x,
+ y2: 125 + y + roomy,
+ x3: 330 + x,
+ y3: 175 + y + roomy,
+ x4: 330 + x,
+ y4: 95 + y + roomy,
+ color: 'olive',
+ z: 1)
+ @objects.push Camera::Sprite.new('./assets/sprites/blobdance-128.png',
+ x: 250 + x,
+ y: 135 + y + roomy,
+ width: 40,
+ height: 40,
+ clip_width: 128,
+ loop: true,
+ time: 24,
+ z: 5)
+ @objects.last.play
+ @objects.push Camera::Image.new(
+ 'assets/blobshadow.png',
+ x: 250 + x - 2 - 5,
+ y: 135 + y + roomy + 42 - 15,
+ width: 52,
+ height: 10,
+ z: 4
+ )
+ @objects.push Camera::Sprite.new('./assets/sprites/dance2-112.png',
+ x: 95 + x,
+ y: 115 + y + roomy,
+ width: 40,
+ height: 40,
+ clip_width: 112,
+ loop: true,
+ time: 13,
+ z: 5)
+ @objects.last.play
+ @objects.push Camera::Image.new(
+ 'assets/blobshadow.png',
+ x: 95 + x - 2 - 5,
+ y: 115 + y + roomy + 42 - 10,
+ width: 52,
+ height: 10,
+ z: 4
+ )
+ @objects.push Camera::Sprite.new('./assets/sprites/dancer-128.png',
+ x: 175 + x,
+ y: 120 + y + roomy,
+ width: 45,
+ height: 45,
+ clip_width: 128,
+ loop: true,
+ time: 60,
+ z: 5)
+ @objects.last.play
+ @objects.push Camera::Image.new(
+ 'assets/blobshadow.png',
+ x: 175 + x - 2 - 5,
+ y: 120 + y + roomy + 42 - 5,
+ width: 52,
+ height: 10,
+ z: 4
+ )
+ # @objects.each do |item|
+ # Camera << item
+ # end
+ end
+
+ def remove
+ @objects.each do |item|
+ # Camera.remove item
+ item.remove
+ end
+ end
+
+ def visted_by?(character)
+ x = @x + 80
+ y = @y + 160
+ if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2))
+ Camera::Text.new('Press Space To Exit House',
+ x: x + 70,
+ y: y + 30,
+ color: 'white',
+ z: 98,
+ size: 25.0)
+ end
+ end
+end
diff --git a/example/run.rb b/example/run.rb
new file mode 100644
index 0000000..96ab9ab
--- /dev/null
+++ b/example/run.rb
@@ -0,0 +1,223 @@
+# frozen_string_literal: true
+
+require 'ruby2d'
+require 'ruby2d/camera'
+require_relative 'house'
+require_relative 'room'
+
+Window.set(icon: './assets/blobcoolthink.png',
+ width: 1280,
+ height: 720,
+ background: 'blue')
+
+@player = Camera::Sprite.new('./assets/sprites/mainblob-128.png',
+ x: 1920 / 1.1,
+ y: 1080 / 1.1,
+ width: 50,
+ height: 50,
+ clip_width: 128,
+ loop: true,
+ time: 1,
+ z: 99,
+ animations: {
+ walk: 0...60,
+ stand: 60...61
+ })
+@shadow = Camera::Image.new(
+ 'assets/blobshadow.png',
+ width: 52,
+ height: 10,
+ z: 4
+)
[email protected] animation: :walk, loop: true
+
+# UI
+Rectangle.new(
+ width: 350,
+ height: 135,
+ color: 'navy',
+ z: 100
+)
+@ui_pos_cam = Text.new(
+ 'pos: 0,0',
+ x: 10,
+ y: 10,
+ color: 'teal',
+ z: 101
+)
+@ui_pos_ply = Text.new(
+ 'pos: 0,0',
+ x: 10,
+ y: 40,
+ color: 'teal',
+ z: 101
+)
+@ui_zoom = Text.new(
+ 'zoom: 0',
+ x: 10,
+ y: 70,
+ color: 'lime',
+ z: 101
+)
+@ui_rotation = Text.new(
+ 'rotation: 0',
+ x: 10,
+ y: 100,
+ color: 'lime',
+ z: 101
+)
+Rectangle.new(
+ x: (Window.width - 120),
+ width: 120,
+ height: 45,
+ color: 'navy',
+ z: 100
+)
+@ui_fps = Text.new(
+ 'fps: 60.00',
+ x: (Window.width - 110),
+ y: 10,
+ color: 'teal',
+ z: 101
+)
+
+# How fast the player can move
+@speed = 5
+
+# Initializing
+@player_movement_x = 0
+@player_movement_y = 0
+@pressed_space = false
+@scene_transition_into = false
+@scene_transition_out = false
+@indoors = false
+@house = nil
+@room = nil
+
+on :key do |event|
+ @player_movement_y -= @speed if event.key == 'w' && !(@scene_transition_into || @scene_transition_out)
+ @player_movement_y += @speed if event.key == 's' && !(@scene_transition_into || @scene_transition_out)
+ @player_movement_x += @speed if event.key == 'd' && !(@scene_transition_into || @scene_transition_out)
+ @player_movement_x -= @speed if event.key == 'a' && !(@scene_transition_into || @scene_transition_out)
+ @pressed_space = true if event.key == 'space' && !(@scene_transition_into || @scene_transition_out)
+ Camera.angle += 1 if event.key == 'q' && !(@scene_transition_into || @scene_transition_out)
+ Camera.angle -= 1 if event.key == 'e' && !(@scene_transition_into || @scene_transition_out)
+ if event.key == 'r' && !(@scene_transition_into || @scene_transition_out || Camera.angle.zero?)
+ if Camera.angle <= 180
+ Camera.angle -= if Camera.angle > 10
+ 10
+ else
+ 1
+ end
+ elsif Camera.angle += if Camera.angle < 350
+ 10
+ else
+ 1
+ end
+ end
+ end
+end
+
+update do
+ if (@player.x > 2371 && @player_movement_x.positive?) || (@player.x.negative? && @player_movement_x.negative?)
+ @player_movement_x = 0
+ end
+ if (@player.y > 1608 && @player_movement_y.positive?) || (@player.y.negative? && @player_movement_y.negative?)
+ @player_movement_y = 0
+ end
+ if !@player_movement_y.zero? == !@player_movement_x.zero?
+ @player_movement_x /= 1.4141
+ @player_movement_y /= 1.4141
+ end
+ @player.x += @player_movement_x
+ @player.y += @player_movement_y
+ if @player_movement_x.negative?
+ @player.play animation: :walk, loop: true
+ elsif @player_movement_x.positive? || !@player_movement_y.zero?
+ @player.play animation: :walk, loop: true, flip: :vertical
+ else
+ @player.play animation: :stand
+ end
+
+ if !@scene_transition_into && !@scene_transition_out
+ Camera.zoom += ((-[
+ Math.sqrt(((@player.x + (@player.width / 2) - Camera.x)**2) + ((@player.y + (@player.width / 2) - Camera.y)**2)), 350
+ ].min * 0.004) + 2 - Camera.zoom) * 0.25
+ Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.025
+ Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.025
+ elsif @scene_transition_into
+ if Camera.zoom < 250
+ Camera.zoom *= 1.05
+ Camera.angle += 5
+ else
+ @scene_transition_into = false
+ @scene_transition_out = true
+ if @room.nil?
+ @house.remove
+ @house = nil
+ @indoors = true
+ else
+ @room.remove
+ @room = nil
+ @indoors = false
+ end
+ @background.remove
+ @background = nil
+ end
+ Camera.x += (@player.x + (@player.width / 2) - Camera.x) * 0.25
+ Camera.y += (@player.y + (@player.height / 2) - Camera.y) * 0.25
+ elsif @scene_transition_out
+ if !((Camera.zoom <= (@zoom_transition * 1.001)) && (Camera.angle == @angle_transition))
+ Camera.zoom /= 1.05
+ Camera.angle -= 5
+ else
+ @scene_transition_out = false
+ end
+ end
+
+ if @house.nil? && !@indoors
+ @house = House.new(750, 300)
+ @background = Camera::Image.new(
+ 'assets/background.png',
+ x: 100, y: 100,
+ z: -1
+ )
+ elsif @room.nil? && @indoors
+ @room = Room.new(750, 300)
+ @background = Camera::Rectangle.new(
+ color: 'black',
+ x: 0,
+ y: 0,
+ width: 1920,
+ height: 1080,
+ z: -1
+ )
+ end
+ @shadow.x = @player.x - 2
+ @shadow.y = @player.y + 42
+
+ @house_text&.remove
+ @house_text = if @indoors
+ @room.visted_by?(@player)
+ else
+ @house.visted_by?(@player)
+ end
+ unless @house_text.nil?
+ @house_text.center = true
+ end
+ if !@house_text.nil? && @pressed_space && !@scene_transition_into && !@scene_transition_out
+ @scene_transition_into = true
+ @angle_transition = Camera.angle
+ @zoom_transition = Camera.zoom
+ end
+ @ui_pos_cam.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}"
+ @ui_pos_ply.text = "Player Position: #{@player.x.round(1)}, #{@player.y.round(1)}"
+ @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}"
+ @ui_fps.text = "FPS: #{Window.fps.round(2)}"
+ @ui_rotation.text = "Angle: #{Camera.angle}"
+ @player_movement_x = 0
+ @player_movement_y = 0
+ @pressed_space = false
+
+end
+show
diff --git a/house.rb b/house.rb
deleted file mode 100644
index b4b495f..0000000
--- a/house.rb
+++ /dev/null
@@ -1,103 +0,0 @@
-# frozen_string_literal: true
-
-# Is a house
-class House
- def initialize(x, y)
- @objects = []
- @x = x
- @y = y
- @objects.push Camera::Image.new('assets/blobshadow.png',
- width: 320,
- height: 250,
- x: x - 10,
- y: y + 130,
- z: 0)
- @objects.push Camera::Image.new('assets/bricktexture.png',
- x: x,
- y: y,
- width: 300,
- height: 300)
- @objects.push Camera::Square.new(x: 125 + x,
- y: 230 + y,
- size: 50,
- color: 'black')
- @objects.push Camera::Circle.new(x: 125 + x,
- y: 205 + y,
- radius: 25,
- color: 'black')
- @objects.push Camera::Triangle.new(x1: -5 + x,
- y1: 16 + y,
- x2: 310 + x,
- y2: 14 + y,
- x3: 150 + x,
- y3: -75 + y,
- color: 'red')
- @objects.push Camera::Square.new(x: 160 + x,
- y: 20 + y,
- size: 100,
- color: 'brown',
- z: 1)
- @objects.push Camera::Square.new(x: 170 + x,
- y: 25 + y,
- size: 80,
- opacity: 0.5,
- color: 'blue',
- z: 2)
- @objects.push Camera::Rectangle.new(x: 160 + x,
- y: 105 + y,
- width: 100,
- height: 20,
- color: 'brown',
- z: 3)
- @objects.push Camera::Line.new(x1: 210 + x,
- y1: 105 + y,
- x2: 210 + x,
- y2: 25 + y,
- width: 4,
- color: 'brown',
- z: 3)
- @objects.push Camera::Line.new(x1: 250 + x,
- y1: 65 + y,
- x2: 170 + x,
- y2: 65 + y,
- width: 4,
- color: 'brown',
- z: 3)
- @objects.push Camera::Sprite.new('./assets/sprites/alienpls-56.png',
- x: 175 + x,
- y: 65 + y,
- width: 56,
- height: 56,
- clip_width: 56,
- loop: true,
- time: 35,
- z: 1)
- @objects.last.play
- #@objects.each do |item|
- # Camera << item
- #end
- end
-
- def remove
- @objects.each do |item|
- #Camera.remove item
- item.remove
- end
- end
-
- def visted_by?(character)
- x = @x + 80
- y = @y + 160
- if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2))
- Camera::Text.new('Press Space To Enter House',
- x: x + 70,
- y: y + 30,
- color: 'white',
- z: 98,
- size: 25.0)
- else
- nil
- end
- end
-end
-
diff --git a/lib/camera/camera.rb b/lib/camera/camera.rb
deleted file mode 100644
index 64e2095..0000000
--- a/lib/camera/camera.rb
+++ /dev/null
@@ -1,125 +0,0 @@
-# frozen_string_literal: true
-
-Dir[File.join(__dir__, 'wrappers', '*.rb')].sort.each { |file| require file }
-
-# Handles rendering objects relative
-# to a camera location
-module Camera
- class <<self
-
- private
- # Contains all objects that are tracked
- def objects
- @objects ||= []
- end
- end
-
- def self.debug_x
- @debug_x ||= 0
- end
-
- def self.debug_x=(debug_x)
- @debug_x = debug_x
- end
-
- def self.debug_y
- @debug_y ||= 0
- end
-
- def self.debug_y=(debug_y)
- @debug_y = debug_y
- end
-
- # Adding objects so they are
- # tracked by the Camera
- def self.<<(item)
- case item
- when Triangle
- item.extend Camera::TriangleWrapped
- when Square
- item.extend Camera::SquareWrapped
- when Rectangle
- item.extend Camera::RectangleWrapped
- when Quad
- item.extend Camera::QuadWrapped
- when Line
- item.extend Camera::LineWrapped
- when Circle
- item.extend Camera::CircleWrapped
- when Image
- item.extend Camera::ImageWrapped
- when Sprite
- item.extend Camera::SpriteWrapped
- when Text
- item.extend Camera::TextWrapped
- else
- puts 'Warning: Non-standard Object added to Camera'
- puts ' Object may not behave as expected'
- end
- objects.push(item) unless objects.include?(item)
- end
-
- def self.remove(item)
- objects.delete(item) if objects.include?(item)
- end
-
- # Redraw all objects that
- # are tracked by the Camera
- def self.redraw(auto_purge: false)
- if auto_purge
- objects.each do |item|
- if item.nil?
- puts "Warning: Nil Object detected in Camera"
- puts " Nil Object removed"
- objects.delete(obj)
- else
- item.redraw
- end
- end
- else
- objects.each(&:redraw)
- end
- end
-
- # Variables changing Camera properties
- def self._x(x)
- @x += x
- end
-
- def self._y(y)
- @y += y
- end
-
- def self.x
- @x ||= 0
- end
-
- def self.x=(x)
- @x = x
- end
-
- def self.y
- @y ||= 0
- end
-
- def self.y=(y)
- @y = y
- end
-
- def self.zoom
- @zoom ||= 1.0
- end
-
- def self.zoom=(zoom)
- @zoom = zoom
- end
-
- def self.angle
- @angle ||= 0
- end
-
- def self.angle=(angle)
- angle %= 360
- @angle = angle
- end
-end
diff --git a/lib/camera/wrappers/circle_wrapper.rb b/lib/camera/wrappers/circle_wrapper.rb
deleted file mode 100644
index a7db05a..0000000
--- a/lib/camera/wrappers/circle_wrapper.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
-# Wraps existing variables as well as adding new methods
-# so that it can be handled by the Camera Module
- module CircleWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- angle = Camera.angle * (Math::PI / 180)
- half_width = Window.width * 0.5
- half_height = Window.height * 0.5
- @radius = radius * Camera.zoom
- @x = (((x - Camera.x + radius) * Math.cos(angle)) - ((y - Camera.y + radius) * Math.sin(angle))) * Camera.zoom + half_width
- @y = (((x - Camera.x + radius) * Math.sin(angle)) + ((y - Camera.y + radius) * Math.cos(angle))) * Camera.zoom + half_height
- end
-
- # Methods for moving the shape as well as
- # wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x
- @virtual_x ||= @x
- end
-
- def x=(x)
- @virtual_x = x
- end
-
- def y
- @virtual_y ||= @y
- end
-
- def y=(y)
- @virtual_y = y
- end
-
- def radius
- @virtual_radius ||= @radius
- end
-
- def radius=(radius)
- @virtual_radius = radius
- end
- end
-end
-
diff --git a/lib/camera/wrappers/image_wrapper.rb b/lib/camera/wrappers/image_wrapper.rb
deleted file mode 100644
index 9e4b163..0000000
--- a/lib/camera/wrappers/image_wrapper.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
- # Wraps existing variables as well as adding new methods
- # so that it can be handled by the Camera Module
- module ImageWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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)
- @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \
- * Camera.zoom + half_width - (width * Camera.zoom / 2)
- @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \
- * Camera.zoom + half_height - (height * Camera.zoom / 2)
- @rotate = rotate + Camera.angle
- @width = width * Camera.zoom
- @height = height * Camera.zoom
- end
-
- #Methods for moving the shape
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x
- @virtual_x ||= @x
- end
-
- def x=(x)
- @virtual_x = x
- end
-
- def y
- @virtual_y ||= @y
- end
-
- def y=(y)
- @virtual_y = y
- end
-
- def x
- @virtual_x ||= @x
- end
-
- def rotate
- @virtual_rotate ||= @rotate
- end
-
- def rotate=(rotate)
- @virtual_rotate = rotate
- end
-
- def width
- @virtual_width ||= @width
- end
-
- def width=(width)
- @virtual_width = width
- end
-
- def height
- @virtual_height ||= @height
- end
-
- def height=(height)
- @virtual_height = height
- end
- end
-end
diff --git a/lib/camera/wrappers/line_wrapper.rb b/lib/camera/wrappers/line_wrapper.rb
deleted file mode 100644
index 7d4d944..0000000
--- a/lib/camera/wrappers/line_wrapper.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
-# Wraps existing variables as well as adding new methods
-# so that it can be handled by the Camera Module
- module LineWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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
- @width = width * Camera.zoom
- end
-
- #Methods for moving the shape
- def x
- @x ||= 0
- end
-
- def x=(x)
- @x = x
- end
-
- def y
- @y ||= 0
- end
-
- def y=(y)
- @y = y
- end
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x1
- @virtual_x1 ||= @x1
- end
-
- def x1=(x1)
- @virtual_x1 = x1
- end
-
- def y1
- @virtual_y1 ||= @y1
- end
-
- def y1=(y1)
- @virtual_y1 = y1
- end
-
- def x2
- @virtual_x2 ||= @x2
- end
-
- def x2=(x2)
- @virtual_x2 = x2
- end
-
- def y2
- @virtual_y2 ||= @y2
- end
-
- def y2=(y2)
- @virtual_y2 = y2
- end
-
- def width
- @virtual_width ||= @width
- end
-
- def width=(width)
- @virtual_width = width
- end
-
- def length
- points_distance(x1, y1, x2, y2)
- end
-
- def contains?(x, y)
- points_distance(x1, y1, x, y) <= length &&
- points_distance(x2, y2, x, y) <= length &&
- (((y2 - y1) * x - (x2 - x1) * y + x2 * y1 - y2 * x1).abs / length) <= 0.5 * width
- end
- end
-end
-
diff --git a/lib/camera/wrappers/quad_wrapper.rb b/lib/camera/wrappers/quad_wrapper.rb
deleted file mode 100644
index 477dbfd..0000000
--- a/lib/camera/wrappers/quad_wrapper.rb
+++ /dev/null
@@ -1,108 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
-# Wraps existing variables as well as adding new methods
-# so that it can be handled by the Camera Module
- module QuadWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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
- end
-
- #Methods for moving the shape
- def x
- @x ||= 0
- end
-
- def x=(x)
- @x = x
- end
-
- def y
- @y ||= 0
- end
-
- def y=(y)
- @y = y
- end
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x1
- @virtual_x1 ||= @x1
- end
-
- def x1=(x1)
- @virtual_x1 = x1
- end
-
- def y1
- @virtual_y1 ||= @y1
- end
-
- def y1=(y1)
- @virtual_y1 = y1
- end
-
- def x2
- @virtual_x2 ||= @x2
- end
-
- def x2=(x2)
- @virtual_x2 = x2
- end
-
- def y2
- @virtual_y2 ||= @y2
- end
-
- def y2=(y2)
- @virtual_y2 = y2
- end
-
- def x3
- @virtual_x3 ||= @x3
- end
-
- def x3=(x3)
- @virtual_x3 = x3
- end
-
- def y3
- @virtual_y3 ||= @y3
- end
-
- def y3=(y3)
- @virtual_y3 = y3
- end
-
- def x4
- @virtual_x4 ||= @x4
- end
-
- def x4=(x4)
- @virtual_x4 = x4
- end
-
- def y4
- @virtual_y4 ||= @y4
- end
-
- def y4=(y4)
- @virtual_y4 = y4
- end
- end
-end
-
diff --git a/lib/camera/wrappers/rectangle_wrapper.rb b/lib/camera/wrappers/rectangle_wrapper.rb
deleted file mode 100644
index 93e888a..0000000
--- a/lib/camera/wrappers/rectangle_wrapper.rb
+++ /dev/null
@@ -1,138 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
-# Wraps existing variables as well as adding new methods
-# so that it can be handled by the Camera Module
- module RectangleWrapped
- # 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
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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
- end
-
- #Methods for moving the shape
- def x
- @x ||= 0
- end
-
- def x=(x)
- @x = x
- end
-
- def y
- @y ||= 0
- end
-
- def y=(y)
- @y = y
- end
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x1
- @virtual_x1 ||= @x1
- end
-
- def x1=(x1)
- @virtual_x1 = x1
- end
-
- def y1
- @virtual_y1 ||= @y1
- end
-
- def y1=(y1)
- @virtual_y1 = y1
- end
-
- def x2
- @virtual_x2 ||= @x2
- end
-
- def x2=(x2)
- @virtual_x2 = x2
- end
-
- def y2
- @virtual_y2 ||= @y2
- end
-
- def y2=(y2)
- @virtual_y2 = y2
- end
-
- def x3
- @virtual_x3 ||= @x3
- end
-
- def x3=(x3)
- @virtual_x3 = x3
- end
-
- def y3
- @virtual_y3 ||= @y3
- end
-
- def y3=(y3)
- @virtual_y3 = y3
- end
-
- def x4
- @virtual_x4 ||= @x4
- end
-
- def x4=(x4)
- @virtual_x4 = x4
- end
-
- def y4
- @virtual_y4 ||= @y4
- end
-
- def y4=(y4)
- @virtual_y4 = y4
- end
-
-
- def width
- @width ||= 200
- end
-
- def width=(width)
- self.x2 = width
- self.x3 = width
- @width = width
- end
-
- def height
- @height ||= 100
- end
-
- def height=(height)
- self.y3 = height
- self.y4 = height
- @height = height
- end
- end
-end
-
diff --git a/lib/camera/wrappers/sprite_wrapper.rb b/lib/camera/wrappers/sprite_wrapper.rb
deleted file mode 100644
index d38c0bc..0000000
--- a/lib/camera/wrappers/sprite_wrapper.rb
+++ /dev/null
@@ -1,86 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
- # Wraps existing variables as well as adding new methods
- # so that it can be handled by the Camera Module
- module SpriteWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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)
- @x = @flip_x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \
- * Camera.zoom + half_width - (width * Camera.zoom / 2)
- @y = @flip_y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \
- * Camera.zoom + half_height - (height * Camera.zoom / 2)
- @rotate = rotate + Camera.angle
- @width = @flip_width = width * Camera.zoom
- @height = @flip_height = height * Camera.zoom
- case @flip
- when :both
- @flip_x = @x + @height
- @flip_width = -@width
- @flip_y = @y + @width
- @flip_height = -@height
- when :horizontal
- @flip_y = @y + @width
- @flip_height = -@height
- when :vertical
- @flip_x = @x + @height
- @flip_width = -@width
- end
- end
-
- #Methods for moving the shape
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x
- @virtual_x ||= @x
- end
-
- def x=(x)
- @virtual_x = x
- end
-
- def y
- @virtual_y ||= @y
- end
-
- def y=(y)
- @virtual_y = y
- end
-
- def x
- @virtual_x ||= @x
- end
-
- def rotate
- @virtual_rotate ||= @rotate
- end
-
- def rotate=(rotate)
- @virtual_rotate = rotate
- end
-
- def width
- @virtual_width ||= @width
- end
-
- def width=(width)
- @virtual_width = width
- end
-
- def height
- @virtual_height ||= @height
- end
-
- def height=(height)
- @virtual_height = height
- end
- end
-end
diff --git a/lib/camera/wrappers/square_wrapper.rb b/lib/camera/wrappers/square_wrapper.rb
deleted file mode 100644
index 2215a30..0000000
--- a/lib/camera/wrappers/square_wrapper.rb
+++ /dev/null
@@ -1,146 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
- # Wraps existing variables as well as adding new methods
- # so that it can be handled by the Camera Module
- module SquareWrapped
- # Squares 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
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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
- end
-
- #Methods for moving the shape
- def x
- @x ||= 0
- end
-
- def x=(x)
- @x = x
- end
-
- def y
- @y ||= 0
- end
-
- def y=(y)
- @y = y
- end
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x1
- @virtual_x1 ||= @x1
- end
-
- def x1=(x1)
- @virtual_x1 = x1
- end
-
- def y1
- @virtual_y1 ||= @y1
- end
-
- def y1=(y1)
- @virtual_y1 = y1
- end
-
- def x2
- @virtual_x2 ||= @x2
- end
-
- def x2=(x2)
- @virtual_x2 = x2
- end
-
- def y2
- @virtual_y2 ||= @y2
- end
-
- def y2=(y2)
- @virtual_y2 = y2
- end
-
- def x3
- @virtual_x3 ||= @x3
- end
-
- def x3=(x3)
- @virtual_x3 = x3
- end
-
- def y3
- @virtual_y3 ||= @y3
- end
-
- def y3=(y3)
- @virtual_y3 = y3
- end
-
- def x4
- @virtual_x4 ||= @x4
- end
-
- def x4=(x4)
- @virtual_x4 = x4
- end
-
- def y4
- @virtual_y4 ||= @y4
- end
-
- def y4=(y4)
- @virtual_y4 = y4
- end
-
-
- def width
- @width ||= 200
- end
-
- def width=(width)
- self.x2 = width
- self.x3 = width
- @width = width
- end
-
- def height
- @height ||= 100
- end
-
- def height=(height)
- self.y3 = height
- self.y4 = height
- @height = height
- end
-
- def size
- @size ||= 100
- end
-
- def size=(size)
- @size = self.width = self.height = size
- end
- end
-end
-
diff --git a/lib/camera/wrappers/text_wrapper.rb b/lib/camera/wrappers/text_wrapper.rb
deleted file mode 100644
index ad96881..0000000
--- a/lib/camera/wrappers/text_wrapper.rb
+++ /dev/null
@@ -1,84 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
- # Wraps existing variables as well as adding new methods
- # so that it can be handled by the Camera Module
- # TODO: note that text could not be resized at the current iteration
- # of Ruby2D so the math needs to be changed compensate for this.
- # When Ruby2D gets updated to allow text resizing the math will need
- # to be corrected again(see image_wrapper.rb for reference, that has
- # math that allows for resizing)
- module TextWrapped
- @center = false
-
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- angle = Camera.angle * (Math::PI / 180)
- half_width = Window.width * 0.5
- half_height = Window.height * 0.5
- if center
- offset_y = y + (Camera.zoom / 2)
- offset_x = x + (Camera.zoom / 2)
- else
- offset_x = x + (width / Camera.zoom / 2)
- offset_y = y + (height / Camera.zoom / 2)
- end
- @x = (((offset_x - Camera.x) * Math.cos(angle)) - ((offset_y - Camera.y) * Math.sin(angle))) \
- * Camera.zoom + half_width - (width / 2)
- @y = (((offset_x - Camera.x) * Math.sin(angle)) + ((offset_y - Camera.y) * Math.cos(angle))) \
- * Camera.zoom + half_height - (height / 2)
- @rotate = rotate + Camera.angle
- @size = size * Camera.zoom
- end
-
- #Methods for moving the shape
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x
- @virtual_x ||= @x
- end
-
- def x=(x)
- @virtual_x = x
- end
-
- def y
- @virtual_y ||= @y
- end
-
- def y=(y)
- @virtual_y = y
- end
-
- def x
- @virtual_x ||= @x
- end
-
- def rotate
- @virtual_rotate ||= @rotate
- end
-
- def rotate=(rotate)
- @virtual_rotate = rotate
- end
-
- def size
- @virtual_size ||= @size
- end
-
- def size=(size)
- @virtual_size = size
- end
-
- def center
- @center
- end
-
- def center=(center)
- @center = center
- end
- end
-end
diff --git a/lib/camera/wrappers/triangle_wrapper.rb b/lib/camera/wrappers/triangle_wrapper.rb
deleted file mode 100644
index 0aba2c0..0000000
--- a/lib/camera/wrappers/triangle_wrapper.rb
+++ /dev/null
@@ -1,90 +0,0 @@
-# frozen_string_literal: true
-
-module Camera
-# Wraps existing variables as well as adding new methods
-# so that it can be handled by the Camera Module
- module TriangleWrapped
- # Recalculates real coordiantes
- # Use after changing variables
- def redraw
- 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
- end
-
- #Methods for moving the shape
- def x
- @x ||= 0
- end
-
- def x=(x)
- @x = x
- end
-
- def y
- @y ||= 0
- end
-
- def y=(y)
- @y = y
- end
-
- # Wrappers for the coordinates so that
- # they are handled by the Camera instead
- # and for ease of programming
- def x1
- @virtual_x1 ||= @x1
- end
-
- def x1=(x1)
- @virtual_x1 = x1
- end
-
- def y1
- @virtual_y1 ||= @y1
- end
-
- def y1=(y1)
- @virtual_y1 = y1
- end
-
- def x2
- @virtual_x2 ||= @x2
- end
-
- def x2=(x2)
- @virtual_x2 = x2
- end
-
- def y2
- @virtual_y2 ||= @y2
- end
-
- def y2=(y2)
- @virtual_y2 = y2
- end
-
- def x3
- @virtual_x3 ||= @x3
- end
-
- def x3=(x3)
- @virtual_x3 = x3
- end
-
- def y3
- @virtual_y3 ||= @y3
- end
-
- def y3=(y3)
- @virtual_y3 = y3
- end
- end
-end
-
diff --git a/lib/ruby2d/camera.rb b/lib/ruby2d/camera.rb
index d29609d..ba0a280 100644
--- a/lib/ruby2d/camera.rb
+++ b/lib/ruby2d/camera.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
-#require_relative "camera/version"
-#require_relative "camera/triangle"
+# require_relative "camera/version"
+# require_relative "camera/triangle"
require 'ruby2d'
Dir[File.join(__dir__, 'camera', '*.rb')].sort.each { |file| require file }
@@ -9,9 +9,9 @@ Dir[File.join(__dir__, 'camera', '*.rb')].sort.each { |file| require file }
# to a camera location
module Ruby2D
module Camera
-
- class <<self
+ class << self
private
+
# Contains all objects that are tracked
def objects
@objects ||= []
@@ -38,7 +38,7 @@ module Ruby2D
# tracked by the Camera
def self.<<(item)
objects.push(item) unless objects.include?(item)
- objects.sort_by! do |n|
+ objects.sort_by! do |n|
n.z
end
end
diff --git a/lib/ruby2d/camera/circle.rb b/lib/ruby2d/camera/circle.rb
index c6e1eb0..60629df 100644
--- a/lib/ruby2d/camera/circle.rb
+++ b/lib/ruby2d/camera/circle.rb
@@ -9,6 +9,7 @@ module Ruby2D
# 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
@@ -17,15 +18,16 @@ module Ruby2D
temp_y = (((@x - Ruby2D::Camera.x + radius) * Math.sin(angle)) + ((@y - Ruby2D::Camera.y + radius) * Math.cos(angle))) * Ruby2D::Camera.zoom + half_height
Ruby2D::Circle.draw(x: temp_x, y: temp_y,
radius: temp_radius,
- sectors: self.sectors,
- color: [self.color.r, self.color.g, self.color.b, self.color.a])
+ sectors: sectors,
+ color: [color.r, color.g, color.b, color.a])
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
end
+
def remove
@hide = true
end
diff --git a/lib/ruby2d/camera/image.rb b/lib/ruby2d/camera/image.rb
index fe777b3..dccbec9 100644
--- a/lib/ruby2d/camera/image.rb
+++ b/lib/ruby2d/camera/image.rb
@@ -8,6 +8,7 @@ module Camera
# 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
@@ -17,27 +18,28 @@ module Camera
* Camera.zoom + half_width - (width * Camera.zoom / 2)
temp_y = (((offset_x - Camera.x) * Math.sin(temp_angle)) + ((offset_y - Camera.y) * Math.cos(temp_angle))) \
* Camera.zoom + half_height - (height * Camera.zoom / 2)
- temp_rotate = self.rotate + Camera.angle
- temp_width = self.width * Camera.zoom
- temp_height = self.height * Camera.zoom
- self.draw(x: temp_x, y: temp_y,
- width: temp_width,
- height: temp_height,
- rotate: temp_rotate,
- color: [self.color.r, self.color.g, self.color.b, self.color.a])
+ temp_rotate = rotate + Camera.angle
+ temp_width = width * Camera.zoom
+ temp_height = height * Camera.zoom
+ draw(x: temp_x, y: temp_y,
+ width: temp_width,
+ height: temp_height,
+ rotate: temp_rotate,
+ color: [color.r, color.g, color.b, color.a])
end
- def initialize(path, opts= {})
+ def initialize(path, opts = {})
super(path, opts)
Ruby2D::Camera << self
Window.remove(self)
end
- def remove
- @hide = true
- end
- def add
- @hide = false
- 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 9113b92..f6d8f03 100644
--- a/lib/ruby2d/camera/line.rb
+++ b/lib/ruby2d/camera/line.rb
@@ -9,6 +9,7 @@ module Ruby2D
# 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
@@ -18,22 +19,23 @@ module Ruby2D
temp_y2 = (((x + @x2 - Camera.x) * Math.sin(angle)) + ((y + @y2 - Camera.y) * Math.cos(angle))) * Camera.zoom + half_height
temp_width = width * Camera.zoom
Ruby2D::Line.draw(x1: temp_x1, y1: temp_y1,
- x2: temp_x2, y2: temp_y2,
- width: temp_width,
- color: [
- [self.color.r, self.color.g, self.color.b, self.color.a],
- [self.color.r, self.color.g, self.color.b, self.color.a],
- [self.color.r, self.color.g, self.color.b, self.color.a],
- [self.color.r, self.color.g, self.color.b, self.color.a]
- ],
- z: self.z)
+ x2: temp_x2, y2: temp_y2,
+ width: temp_width,
+ color: [
+ [color.r, color.g, color.b, color.a],
+ [color.r, color.g, color.b, color.a],
+ [color.r, color.g, color.b, color.a],
+ [color.r, color.g, color.b, color.a]
+ ],
+ z: z)
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
end
+
def remove
@hide = true
end
@@ -42,22 +44,16 @@ module Ruby2D
@hide = false
end
- #Methods for moving the shape
+ # Methods for moving the shape
def x
@x ||= 0
end
- def x=(x)
- @x = x
- end
+ attr_writer :x, :y
def y
@y ||= 0
end
-
- def y=(y)
- @y = y
- end
end
end
end
diff --git a/lib/ruby2d/camera/quad.rb b/lib/ruby2d/camera/quad.rb
index 92d858a..5b778b9 100644
--- a/lib/ruby2d/camera/quad.rb
+++ b/lib/ruby2d/camera/quad.rb
@@ -9,6 +9,7 @@ module Ruby2D
# 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
@@ -25,20 +26,20 @@ module Ruby2D
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]
+ [c1.r, c1.g, c1.b, c1.a],
+ [c2.r, c2.g, c2.b, c2.a],
+ [c3.r, c3.g, c3.b, c3.a],
+ [c4.r, c4.g, c4.b, c4.a]
],
- z: self.z
- )
+ z: z)
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
end
+
def remove
@hide = true
end
@@ -47,23 +48,16 @@ module Ruby2D
@hide = false
end
-
- #Methods for moving the shape
+ # Methods for moving the shape
def x
@x ||= 0
end
- def x=(x)
- @x = x
- end
+ attr_writer :x, :y
def y
@y ||= 0
end
-
- def y=(y)
- @y = y
- end
end
end
end
diff --git a/lib/ruby2d/camera/rectangle.rb b/lib/ruby2d/camera/rectangle.rb
index 2a3667d..4d06eec 100644
--- a/lib/ruby2d/camera/rectangle.rb
+++ b/lib/ruby2d/camera/rectangle.rb
@@ -9,6 +9,7 @@ module Ruby2D
# 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
@@ -25,16 +26,15 @@ module Ruby2D
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]
+ [c1.r, c1.g, c1.b, c1.a],
+ [c2.r, c2.g, c2.b, c2.a],
+ [c3.r, c3.g, c3.b, c3.a],
+ [c4.r, c4.g, c4.b, c4.a]
],
- z: self.z
- )
+ z: z)
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
@@ -47,6 +47,7 @@ module Ruby2D
@y3 -= @y
@y4 -= @y
end
+
def remove
@hide = true
end
@@ -54,22 +55,17 @@ module Ruby2D
def add
@hide = false
end
- #Methods for moving the shape
+
+ # Methods for moving the shape
def x
@x ||= 0
end
- def x=(x)
- @x = x
- end
+ attr_writer :x, :y
def y
@y ||= 0
end
-
- def y=(y)
- @y = y
- end
end
end
end
diff --git a/lib/ruby2d/camera/sprite.rb b/lib/ruby2d/camera/sprite.rb
index 0d239a1..a1fb7b8 100644
--- a/lib/ruby2d/camera/sprite.rb
+++ b/lib/ruby2d/camera/sprite.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true
+
module Ruby2D
module Camera
# Wraps existing variables as well as adding new methods
@@ -8,6 +9,7 @@ module Ruby2D
# 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
@@ -22,26 +24,26 @@ module Ruby2D
temp_height = height * Camera.zoom
case @flip
when :both
- temp_x = temp_x + temp_height
- temp_y = temp_y + temp_width
+ temp_x += temp_height
+ temp_y += temp_width
temp_width = -temp_width
temp_height = -temp_height
puts 'both'
when :horizontal
- temp_y = temp_y + temp_width
+ temp_y += temp_width
temp_height = -temp_height
when :vertical
temp_width = -temp_width
- temp_x = temp_x + temp_height
+ temp_x += temp_height
end
- self.draw(x: temp_x, y: temp_y,
- width: temp_width,
- height: temp_height,
- rotate: temp_rotate)
- self.update
+ draw(x: temp_x, y: temp_y,
+ width: temp_width,
+ height: temp_height,
+ rotate: temp_rotate)
+ update
end
- def initialize(path, opts= {})
+ def initialize(path, opts = {})
super(path, opts)
Ruby2D::Camera << self
Window.remove(self)
diff --git a/lib/ruby2d/camera/square.rb b/lib/ruby2d/camera/square.rb
index 584af03..75e1e83 100644
--- a/lib/ruby2d/camera/square.rb
+++ b/lib/ruby2d/camera/square.rb
@@ -9,6 +9,7 @@ module Ruby2D
# 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
@@ -25,16 +26,15 @@ module Ruby2D
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]
+ [c1.r, c1.g, c1.b, c1.a],
+ [c2.r, c2.g, c2.b, c2.a],
+ [c3.r, c3.g, c3.b, c3.a],
+ [c4.r, c4.g, c4.b, c4.a]
],
- z: self.z
- )
+ z: z)
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
@@ -55,24 +55,17 @@ module Ruby2D
def add
@hide = false
end
- #Methods for moving the shape
+
+ # Methods for moving the shape
def x
@x ||= 0
end
- def x=(x)
- @x = x
- end
+ attr_writer :x, :y
def y
@y ||= 0
end
-
- def y=(y)
- @y = y
- end
-
end
end
end
-
diff --git a/lib/ruby2d/camera/text.rb b/lib/ruby2d/camera/text.rb
index 6966a86..00f15b3 100644
--- a/lib/ruby2d/camera/text.rb
+++ b/lib/ruby2d/camera/text.rb
@@ -14,6 +14,7 @@ module Ruby2D
# 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,19 +32,20 @@ module Ruby2D
temp_rotate = rotate + Camera.angle
# Workaround for resizing text
# TODO: resizing doesnt work at all even with workaround
- temp_size = size# * Camera.zoom
+ temp_size = size # * Camera.zoom
self.size *= Camera.zoom
- self.draw(x: temp_x, y: temp_y,
- rotate: temp_rotate,
- color: [self.color.r, self.color.g, self.color.b, self.color.a])
+ draw(x: temp_x, y: temp_y,
+ rotate: temp_rotate,
+ color: [color.r, color.g, color.b, color.a])
self.size = temp_size
end
- def initialize(text, opts= {})
+ def initialize(text, opts = {})
super(text, opts)
Ruby2D::Camera << self
Window.remove(self)
end
+
def remove
@hide = true
end
@@ -52,13 +54,7 @@ module Ruby2D
@hide = false
end
- def center
- @center
- end
-
- def center=(center)
- @center = center
- end
+ attr_accessor :center
end
end
end
diff --git a/lib/ruby2d/camera/triangle.rb b/lib/ruby2d/camera/triangle.rb
index 4065b62..b506fb4 100644
--- a/lib/ruby2d/camera/triangle.rb
+++ b/lib/ruby2d/camera/triangle.rb
@@ -7,6 +7,7 @@ module Ruby2D
# 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
@@ -20,19 +21,20 @@ module Ruby2D
x2: temp_x2, y2: temp_y2,
x3: temp_x3, y3: temp_y3,
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]
+ [c1.r, c1.g, c1.b, c1.a],
+ [c2.r, c2.g, c2.b, c2.a],
+ [c3.r, c3.g, c3.b, c3.a]
],
- z: self.z)
+ z: z)
end
- def initialize(opts= {})
+ def initialize(opts = {})
super(opts)
Ruby2D::Camera << self
Window.remove(self)
- self.add
+ add
end
+
def remove
@hide = true
end
@@ -41,23 +43,16 @@ module Ruby2D
@hide = false
end
- #Methods for moving the shape
+ # Methods for moving the shape
def x
@x ||= 0
end
- def x=(x)
- @x = x
- end
+ attr_writer :x, :y
def y
@y ||= 0
end
-
- def y=(y)
- @y = y
- end
end
end
end
-
diff --git a/lib/ruby2d/camera/version.rb b/lib/ruby2d/camera/version.rb
index a78e2f2..29ac7a8 100644
--- a/lib/ruby2d/camera/version.rb
+++ b/lib/ruby2d/camera/version.rb
@@ -2,6 +2,6 @@
module Ruby2d
module Camera
- VERSION = "0.1.0"
+ VERSION = '1.0.0'
end
end
diff --git a/room.rb b/room.rb
deleted file mode 100644
index 4a4be4e..0000000
--- a/room.rb
+++ /dev/null
@@ -1,132 +0,0 @@
-# frozen_string_literal: true
-
-# Is a house
-class Room
- def initialize(x, y)
- @objects = []
- @x = x
- @y = y
- roomy = 155
- @objects.push Camera::Square.new(x: 125 + x,
- y: 230 + y,
- size: 50,
- color: 'blue',
- z: 3)
- @objects.push Camera::Circle.new(x: 125 + x,
- y: 205 + y,
- radius: 25,
- sectors: 64,
- color: 'blue',
- z: 3)
- @objects.push Camera::Quad.new(x1: 50 + x,
- y1: 130 + y + roomy,
- x2: 125 + x,
- y2: 180 + y + roomy,
- x3: 325 + x,
- y3: 180 + y + roomy,
- x4: 250 + x,
- y4: 130 + y + roomy,
- color: 'aqua',
- z: 1)
- @objects.push Camera::Quad.new(x1: 50 + x,
- y1: 45 + y + roomy,
- x2: 50 + x,
- y2: 125 + y + roomy,
- x3: 250 + x,
- y3: 125 + y + roomy,
- x4: 250 + x,
- y4: 45 + y + roomy,
- color: 'orange',
- z: 1)
- @objects.push Camera::Quad.new(x1: 255 + x,
- y1: 45 + y + roomy,
- x2: 255 + x,
- y2: 125 + y + roomy,
- x3: 330 + x,
- y3: 175 + y + roomy,
- x4: 330 + x,
- y4: 95 + y + roomy,
- color: 'olive',
- z: 1)
- @objects.push Camera::Sprite.new('./assets/sprites/blobdance-128.png',
- x: 250 + x,
- y: 135 + y + roomy,
- width: 40,
- height: 40,
- clip_width: 128,
- loop: true,
- time: 24,
- z: 5)
- @objects.last.play
- @objects.push Camera::Image.new(
- 'assets/blobshadow.png',
- x: 250 + x - 2 - 5,
- y: 135 + y + roomy + 42 - 15,
- width: 52,
- height: 10,
- z: 4
- )
- @objects.push Camera::Sprite.new('./assets/sprites/dance2-112.png',
- x: 95 + x,
- y: 115 + y + roomy,
- width: 40,
- height: 40,
- clip_width: 112,
- loop: true,
- time: 13,
- z: 5)
- @objects.last.play
- @objects.push Camera::Image.new(
- 'assets/blobshadow.png',
- x: 95 + x - 2 - 5,
- y: 115 + y + roomy + 42 - 10,
- width: 52,
- height: 10,
- z: 4
- )
- @objects.push Camera::Sprite.new('./assets/sprites/dancer-128.png',
- x: 175 + x,
- y: 120 + y + roomy,
- width: 45,
- height: 45,
- clip_width: 128,
- loop: true,
- time: 60,
- z: 5)
- @objects.last.play
- @objects.push Camera::Image.new(
- 'assets/blobshadow.png',
- x: 175 + x - 2 - 5,
- y: 120 + y + roomy + 42 - 5,
- width: 52,
- height: 10,
- z: 4
- )
- #@objects.each do |item|
- # Camera << item
- #end
- end
-
- def remove
- @objects.each do |item|
- #Camera.remove item
- item.remove
- end
- end
-
- def visted_by?(character)
- x = @x + 80
- y = @y + 160
- if character.x >= x && character.x <= (x + (character.width * 2)) && character.y > y && character.y <= (y + (character.height * 2))
- Camera::Text.new('Press Space To Exit House',
- x: x + 70,
- y: y + 30,
- color: 'white',
- z: 98,
- size: 25.0)
- else
- nil
- end
- end
-end
-
diff --git a/run.rb b/run.rb
index 69f22f8..6e5c69c 100644
--- a/run.rb
+++ b/run.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require 'ruby2d'
-#require_relative 'lib/camera/camera'
require 'ruby2d/camera'
require_relative 'house'
require_relative 'room'
diff --git a/test.rb b/test.rb
deleted file mode 100644
index dd2b8f1..0000000
--- a/test.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-require 'ruby2d'
-require 'ruby2d/camera'
-
-Camera::Quad.new
-#Camera::Image.new('./assets/bricktexture.png')
-#@test = Image.new('./assets/bricktexture.png')
-#@test.remove
-update do
-# @test.draw(x: get(:mouse_x),
-# y: get(:mouse_y))
-# @test.x = get(:mouse_x)
-# @test.y = get(:mouse_y)
- Camera.x = get(:mouse_x)
- Camera.angle = get(:mouse_y)
-end
-
-show