diff options
| -rw-r--r-- | adapt_triangle.rb | 12 | ||||
| -rw-r--r-- | camera.rb | 49 | ||||
| -rw-r--r-- | run.rb | 41 |
3 files changed, 69 insertions, 33 deletions
diff --git a/adapt_triangle.rb b/adapt_triangle.rb index e091ec1..de1ae0d 100644 --- a/adapt_triangle.rb +++ b/adapt_triangle.rb @@ -25,12 +25,12 @@ module AdaptTriangle def _update angle = Camera.angle * (Math::PI / 180) - @x1 = ((x + x1 + Camera.x) * Math.cos(angle)) - ((y + y1 + Camera.y) * Math.sin(angle)) - @y1 = ((x + x1 + Camera.x) * Math.sin(angle)) + ((y + y1 + Camera.y) * Math.cos(angle)) - @x2 = ((x + x2 + Camera.x) * Math.cos(angle)) - ((y + y2 + Camera.y) * Math.sin(angle)) - @y2 = ((x + x2 + Camera.x) * Math.sin(angle)) + ((y + y2 + Camera.y) * Math.cos(angle)) - @x3 = ((x + x3 + Camera.x) * Math.cos(angle)) - ((y + y3 + Camera.y) * Math.sin(angle)) - @y3 = ((x + x3 + Camera.x) * Math.sin(angle)) + ((y + y3 + Camera.y) * Math.cos(angle)) + @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 end def _x1 @@ -16,14 +16,46 @@ module Camera objects.push(item) unless objects.include?(item) end + 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 + 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 def self.x @x ||= 0 end def self.x=(x) angle = Camera.angle * (Math::PI / 180) - @y -= (-self.x + x) * Math.sin(-angle) - @x -= (-self.x + x) * Math.cos(-angle) + @y += (x - self.x) * Math.sin(-angle) + @x += (x - self.x) * Math.cos(-angle) objects.each(&:_update) end @@ -33,8 +65,8 @@ module Camera def self.y=(y) angle = Camera.angle * (Math::PI / 180) - @x -= -(-self.y + y) * Math.sin(-angle) - @y -= (-self.y + y) * Math.cos(-angle) + @x += -(y - self.y) * Math.sin(-angle) + @y += (y - self.y) * Math.cos(-angle) objects.each(&:_update) end @@ -43,8 +75,13 @@ module Camera end def self.zoom=(zoom) - #TODO - @zoom = zoom + if zoom != self.zoom + temp = [center_x, center_y] + @zoom = zoom + self.center_x = temp[0] + self.center_y = temp[1] + objects.each(&:_update) + end end def self.angle @@ -40,25 +40,31 @@ end Camera << @player Rectangle.new( width: 350, - height: 105, + height: 135, color: 'navy' ) -@ui_pos = Text.new( +@ui_pos_cam = Text.new( 'pos: 0,0', x: 10, y: 10, color: 'teal' ) +@ui_pos_ply = Text.new( + 'pos: 0,0', + x: 10, + y: 40, + color: 'teal' +) @ui_zoom = Text.new( 'zoom: 0', x: 10, - y: 40, + y: 70, color: 'lime' ) @ui_rotation = Text.new( 'rotation: 0', x: 10, - y: 70, + y: 100, color: 'lime' ) Rectangle.new( @@ -146,12 +152,8 @@ on :key do |event| Camera.angle -= 1 end if event.key == 'r' - Camera.angle = 0 - #unless Camera.rotation_degrees.zero? - #Camera.rotate_by(-Camera.rotation_degrees) - #Camera.move_to(@player.lazy_center[0] - (Window.width / 2), - # @player.lazy_center[1] - (Window.height / 2)) - #end + Camera.angle = 0.0 + Camera.zoom = 1.0 end end update do @@ -159,29 +161,26 @@ update do @player.y += @y_move @x_move = 0 @y_move = 0 + Camera.zoom *= @zoom_by if @is_follow - Camera.x = @player.x - Camera.y = @player.y + #Camera.center(@player.x, @player.y) + Camera.center_x = @player.x + Camera.center_y = @player.y else Camera.x += @cam_x_move Camera.y += @cam_y_move end @cam_x_move = 0 @cam_y_move = 0 - @ui_pos.text = "Camera Position: #{Camera.x.round(1)}, #{Camera.y.round(1)}" - @ui_zoom.text = "Zoom: 'N/A'" #{Camera.zoom_level.round(3)}" + @zoom_by = 1 + @ui_pos_cam.text = "Camera Position: #{Camera.center_x.round(1)}, #{Camera.center_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}" -=begin - puts 'player position' puts @player.x puts @player.y - puts 'camera position' - puts "y: #{Camera.y}" - puts "x: #{Camera.x}" -=end - end show |
