diff options
| -rw-r--r-- | adapt_triangle.rb | 127 | ||||
| -rw-r--r-- | camera.rb | 105 | ||||
| -rw-r--r-- | run.rb | 42 |
3 files changed, 28 insertions, 246 deletions
diff --git a/adapt_triangle.rb b/adapt_triangle.rb deleted file mode 100644 index dd73991..0000000 --- a/adapt_triangle.rb +++ /dev/null @@ -1,127 +0,0 @@ -# frozen_string_literal: true - -module AdaptTriangle - -=begin - def _translate_x(x) - @x1 += x - @x2 += x - @x3 += x - end - - def _translate_y(y) - @y1 += y - @y2 += y - @y3 += y - end - - def _rotate(angle) - @x1 = ((@x1) * Math.cos(angle)) - ((@y1) * Math.sin(angle)) - @y1 = ((@x1) * Math.sin(angle)) + ((@y1) * Math.cos(angle)) - @x2 = ((@x2) * Math.cos(angle)) - ((@y2) * Math.sin(angle)) - @y2 = ((@x2) * Math.sin(angle)) + ((@y2) * Math.cos(angle)) - @x3 = ((@x3) * Math.cos(angle)) - ((@y3) * Math.sin(angle)) - @y3 = ((@x3) * Math.sin(angle)) + ((@y3) * Math.cos(angle)) - end -=end - def _update - angle = Camera.angle * (Math::PI / 180) - @x1 = (((x + x1 - Camera.x) * Math.cos(angle)) - ((y + y1 - Camera.y) * Math.sin(angle))) * Camera.zoom - @y1 = (((x + x1 - Camera.x) * Math.sin(angle)) + ((y + y1 - Camera.y) * Math.cos(angle))) * Camera.zoom - @x2 = (((x + x2 - Camera.x) * Math.cos(angle)) - ((y + y2 - Camera.y) * Math.sin(angle))) * Camera.zoom - @y2 = (((x + x2 - Camera.x) * Math.sin(angle)) + ((y + y2 - Camera.y) * Math.cos(angle))) * Camera.zoom - @x3 = (((x + x3 - Camera.x) * Math.cos(angle)) - ((y + y3 - Camera.y) * Math.sin(angle))) * Camera.zoom - @y3 = (((x + x3 - Camera.x) * Math.sin(angle)) + ((y + y3 - Camera.y) * Math.cos(angle))) * Camera.zoom - width = Window.width * 0.5 - height = Window.height * 0.5 - @x1 += width - @x2 += width - @x3 += width - @y1 += height - @y2 += height - @y3 += height - - end - - def _x1 - @x1 - end - - def _y1 - @y1 - end - - def x - @x ||= x1 - end - - def x=(x) - @x = x - _update - end - - def y - @y ||= y1 - end - - def y=(y) - @y = y - end - - #undo rotation - def x1 - @virtual_x1 ||= @x1 - end - - #difference between 'x1' and undone rotation > then rotated - def x1=(x1) - @virtual_x1 = x1 - _update - end - - def y1 - @virtual_y1 ||= @y1 - end - - def y1=(y1) - @virtual_y1 = y1 - _update - end - - def x2 - @virtual_x2 ||= @x2 - end - - def x2=(x2) - @virtual_x2 = x2 - _update - end - - def y2 - @virtual_y2 ||= @y2 - end - - def y2=(y2) - @virtual_y2 = y2 - _update - end - - def x3 - @virtual_x3 ||= @x3 - end - - def x3=(x3) - @virtual_x3 = x3 - _update - end - - def y3 - @virtual_y3 ||= @y3 - end - - def y3=(y3) - @virtual_y3 = y3 - _update - end -end - diff --git a/camera.rb b/camera.rb deleted file mode 100644 index cd4dc38..0000000 --- a/camera.rb +++ /dev/null @@ -1,105 +0,0 @@ -# frozen_string_literal: true - -require_relative 'adapt_triangle' - -module Camera - - class <<self - private - def objects - @objects ||= [] - end - end - - def self.<<(item) - item.extend AdaptTriangle if item.is_a? Triangle - objects.push(item) unless objects.include?(item) - end -=begin - def self.center_x - x + ((Window.width / 2) * (1/zoom)) - end - - def self.center_x=(center_x) - @x = center_x - ((Window.width / 2) )#* (1/zoom)) - objects.each(&:_update) - end - - def self.center_y - y + ((Window.height / 2) )#* (1/zoom)) - end - - def self.center_y=(center_y) - @y = center_y - ((Window.height / 2) )#* (1/zoom)) - objects.each(&:_update) - end -=begin - def self.center(center_x, center_y) - self.center_y = center_y - self.center_x = center_y - #@y = center_y - #@x = center_x - angle = Math::PI / 180 * (Camera.angle + (1 * 45)) - offset_angle = Math::PI / 180 * 45 - x = (Math.cos(angle) - Math.cos(offset_angle)) * (Window.width/2) * Math.sqrt(2) * (1 / zoom) - y = (Math.sin(angle) - Math.sin(offset_angle)) * (Window.height/2) * Math.sqrt(2) * (1 / zoom) - @x += x - @y += y - #@x = x + ((pivot_x - x) * Math.cos(angle)) - ((pivot_y - y) * Math.sin(angle)) - #@y = y + ((pivot_x - x) * Math.sin(angle)) + ((pivot_y - y) * Math.cos(angle)) - objects.each(&:_update) - end -=end - - def self._x(x) - @x += x - objects.each(&:_update) - end - - def self._y(y) - @y += y - objects.each(&:_update) - end - - def self.x - @x ||= 0 - end - - def self.x=(x) - #angle = Camera.angle * (Math::PI / 180) - #@y += (x - self.x) * Math.sin(-angle) - @x = x#+= (x - self.x) * Math.cos(-angle) - objects.each(&:_update) - end - - def self.y - @y ||= 0 - end - - def self.y=(y) - #angle = Camera.angle * (Math::PI / 180) - #@x += -(y - self.y) * Math.sin(-angle) - @y = y#+= (y - self.y) * Math.cos(-angle) - objects.each(&:_update) - end - - def self.zoom - @zoom ||= 1.0 - end - - def self.zoom=(zoom) - if zoom != self.zoom - @zoom = zoom - objects.each(&:_update) - end - end - - def self.angle - @angle ||= 0 - end - - def self.angle=(angle) - @angle = angle - objects.each(&:_update) - end -end @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'ruby2d' -require_relative 'camera' +require_relative 'lib/camera/camera' #set width: 700, height: 300 @@ -17,6 +17,8 @@ module SizableImage end end +Window.set(icon: './assets/player.png') + # There is 2 ways you can add objects to be known and controlled by the camera, both do the same thing 25.times do tempx = (0..1920).to_a.sample @@ -31,6 +33,13 @@ end size: (10..50).to_a.sample, color: 'random') end +@background = Image.new( + 'assets/background.png', + x: 0, y: 0, + width: (1920 * 0.25), height: (1080 * 0.25), + z: -1 +) +Camera << @background @player = Triangle.new(x1: 0, y1: 0, x2: 0, @@ -38,7 +47,7 @@ end x3: 0 + 25, y3: 0 + 25, size: (10..50).to_a.sample, - color: 'random') + color: 'blue') Camera << @player Rectangle.new( width: 350, @@ -100,12 +109,6 @@ Rectangle.new( @debug3 = 0 on :key do |event| - if event.key == 't' - @debug3 += 2 - end - if event.key == 'g' - @debug3 -= 2 - end if event.key == 'a' @y_move += 0 @x_move += -@speed @@ -138,12 +141,19 @@ on :key do |event| @cam_y_move += @speed @is_follow = false end +end +on :key_down do |event| if event.key == 'f' - @debug1 += 2 - #@is_follow = true + Camera.debug_x += 2 end if event.key == 'h' - @debug1 -= 2 + Camera.debug_x -= 2 + end + if event.key == 't' + Camera.debug_y += 2 + end + if event.key == 'g' + Camera.debug_y -= 2 end end @@ -162,10 +172,10 @@ on :key do |event| end if event.key == 'q' - Camera.angle += 2 + Camera.angle -= 2 end if event.key == 'e' - Camera.angle -= 2 + Camera.angle += 2 end if event.key == 'r' Camera.angle = 0.0 @@ -195,6 +205,10 @@ update do @ui_zoom.text = "Zoom: #{Camera.zoom.round(3)}" @ui_fps.text = "FPS: #{Window.fps.round(2)}" @ui_rotation.text = "Angle: #{Camera.angle}" + puts "Debug X" + puts Camera.debug_x + puts "Debug Y" + puts Camera.debug_y + Camera.update end - show |
