From 5e47e02118e5ed57e1c1ac43f372cfb76526a3c5 Mon Sep 17 00:00:00 2001 From: bionickatana Date: Sat, 13 Jun 2026 15:14:36 -0600 Subject: [PATCH] Finished camera and rendering of objects on screen. --- .gitignore | 1 - assets/images/background_square.png | Bin 0 -> 653 bytes assets/images/background_square.xcf | Bin 0 -> 3079 bytes consumer.rb | 36 +++++++ main.rb | 148 +++++++++++++++++++++++----- producer.rb | 21 +++- 6 files changed, 178 insertions(+), 28 deletions(-) create mode 100644 assets/images/background_square.png create mode 100644 assets/images/background_square.xcf create mode 100644 consumer.rb diff --git a/.gitignore b/.gitignore index d6aa672..dafd772 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,3 @@ build-iPhoneSimulator/ # Used by RuboCop. Remote config files pulled in from inherit_from directive. # .rubocop-https?--* - diff --git a/assets/images/background_square.png b/assets/images/background_square.png new file mode 100644 index 0000000000000000000000000000000000000000..7f7017325be2f40ca016065f144c3ef91761c6bb GIT binary patch literal 653 zcmV;80&@L{P)z@;j|==^1poj5AY({UO#lFTCIA3{ga82g0001h=l}q9FaQARU;qF* zm;eA5aGbhPJOBUy32;bRa{vG?BLDy{BLR4&KXw2B00(qQO+^Rl1`QMs0qv2P%m4rZ z<4Ht8R7l6Qmdm=sAP9s(@v*z;YgdhDF)5jXO;=|Y`7G5RM7)Unn^Z&|k4Nvl_ddoL zW6U}6J>C+LwU&seD!#SWQc5YMzGY_kLeJ;ZMCA2)fy;F0IF7XzxKJrYRp*@G9wam4 z&L$$AbdOe5RRtZ~Ij5PS+m#B{JM>Ze9_o)xt3AIJk~82I`=^R`euQf z49zk5v`j>5t=OF`td*ZZnR~Ab4TWmMZsKwdT#P~^*iW=~%4jZOKZf|;@~ew=MU87B zTt8v6g{Uze*WlK{2YP!h^O?XWCx^@qzM{k3iW6!ZHRuktqzfH(8V1if!_|k`@JoTw ztu+t-M7P#B-crgK0~Pr#QF$d@tu+8Y(5gDdKwO}Idxv<^{g3(sl0sr=6REB*00000NkvXXu0mjfGzbvV literal 0 HcmV?d00001 diff --git a/assets/images/background_square.xcf b/assets/images/background_square.xcf new file mode 100644 index 0000000000000000000000000000000000000000..f5ee3ff845104d94bf3994b26a6910f40172d816 GIT binary patch literal 3079 zcmYe#%q>u;NKR8IGcYt{00KEE2C=6Bu^XhyXDQ5QF5zpln7U zEy}>aRtBVb(}70oCg zZhBE>3P=p3dW}YLNo7u|LUDdhW{M`4Mp}AueolUof(Dqc{CTHfQD;OF8d6g(UC`Y$6FSA4;GcP$KwHTx$KP{~|wL}4|9*GTMqACWt zSp>-+xv3?IDTyVCAaS5ySd(q+D{^xb%2JDpGxPIo)eZFw)D=?mlJir5PO?=G4RO=8 zP`9_^vVm!|<5EztDM?IM$V<#kwN-Mh$V}7o1O<_vQ)WqVKx$ENVs1fBs*;_B0ub8h z1C`>|;sOlxkjz}5Y9j+9Gb;l#D??)iLlY}QLn{+QBK7(gWu|B5CFYQ02H4>~sd?!o z8A^7>1_W#bs}9dhAxd#jDlmDJmH-1a6c~9x#}P0*0-*uq6a#%i{*NSHLqJA;Nxq&l zD3JyO6B{tNaT^X%3yT;RVAz&qR;8wp7P_d0kQYM`GyK6x9g;?Lf$75lcO;=}Kyo{Y zIuQ;=*9c1VpiG&nP*9YbmRVt|Q~(Ow;*9(PC56(W99yM~l9B=|ef_**y~LFKq*Og% zNuv)}qYunp`bu^-`fzj5@l58@7xi->XxqJ#i>31omTwB(`02wH0 zQcTUDueMJ*CwdyB+@F_Eh`Zl}cL?DV4i(mZgD1FFbCfDMR272AIx zRs^x_8-OIR@&jU!IIuKlP=d076oU*Bn+u7pfW+29Vsj(0d63w=NNj!}8)P30umc$& zyFqrTLfJryK?TJA4+KD38HtTtIRk}Z0NF^G7`(=3hKO*0?0^BJ;NmcW30BP6IxRXl z%21^BuluwxuZ0d%!!km|eCs=fCz zO#1rz{QUjTpFgh{6CZ#7?p<43+s-r3pFT}peRa|Kg9jO%zkU0rkdvREpO+`reKbBU zVDr|kp;}X4IG9^0FTR;0Cok{ZaQXGuhVC=ZZ{Cy)y4WCL$e5Rtvu6GJ=S4f`I|eLk z`{lIofXE}`C1I--?>+Dky}2PH;UrI@!h{^N-Me=EI()QTTqGES{ zL;*oe%pW0^)T|Za@sC)i>uRL@oS|T}+rf5i5O-N+rR7wwMlThij}?D5ckk#E<)DwvIC~OElKb^l|&PDP<+=*L$uSB0&cqd`Zmwl0M~5+QTJ` zDgR_Ap7dPW5~+Cb(4iXfmLEDTVLcPdItYdfG`p&kc#spFi)_xoOn$ zucxVJX8xp$Hr~e~EDmY)=$y6t>3g=wxzliIHHf(mXmy} zjx(ihJF3omyP#-s(8`FsGikCfyK-_Dee(6Ot37zi>aR{l^4nj$y7PZ-@!j$z;8CPk zcV|@Aj4O4{zl^g^rTk_;>7cdzU*5s5+3rmnln7AHeJYdvR#|jl=!r{o5#-uG|c!cyK9HeYU^cS0cEzsX5p%F2tY9oF+5 z+2A|lLGs(Z2R}O$M16g7;j_beiDep<5BH~}#Y-i~%ge{d#kt7wuzmmjea2(^$v0Ae zc&yr?v3tRS<${~LJ0*-BzIt^k&OISMKHgQX*A19q_8iR1%ZuRKeE7Hwn-ObEoA{Lt jdTHXqh90R3w*o9=_>MpRm||4 true}) + end + + # Creates numbers depending on the rate. This is done with knowing that the + # game is 30 fps so we use that for a counter + + # @param delta_mult [Float] The delta multiplier for any actions + def update(delta_mult) + end + + # Draw the consumer. Uses the camera x and y to shift where it should draw + # @param camera_x [Integer] The camera X position + # @param camera_y [Integer] The camera Y position + # @param camera_zoom [Float] The camera zoom + def draw(camera_x, camera_y, camera_zoom) + if CONSUMER_DEBUG + puts "Drawing at #{@x - camera_x}, #{@y - camera_y}" + end + @image.draw(camera_zoom * (@x - camera_x), camera_zoom * (@y - camera_y), 0, camera_zoom, camera_zoom) + end +end diff --git a/main.rb b/main.rb index 9702630..f37e95e 100644 --- a/main.rb +++ b/main.rb @@ -3,18 +3,33 @@ require 'gosu' +require 'yaml' require './producer.rb' +require './consumer.rb' +# Viewport resolution WIDTH = 640*2 HEIGHT = 480*2 + +# Default game settings +GAME_SAVE_DIRECTORY = 'game_save_data' +GAME_WIDTH = 10000 +GAME_HEIGHT = 10000 +GAME_PRODUCER_CHANCE = 0.005 +GAME_PRODUCER_VALUE_MULT = 0.1 +GAME_PRODUCER_BASE_SPEED = 1 + +# Camera settings CAMERA_MOVE_RANGE = 10 -CAMERA_MOVE_SPEED = 1 +CAMERA_MOVE_SPEED = 30 CAMERA_MOVE_MAX_SPEED = (CAMERA_MOVE_RANGE * CAMERA_MOVE_SPEED / 2).to_i +CAMERA_MIN_ZOOM = 0.3 +CAMERA_MAX_ZOOM = 20 class NumFactory < Gosu::Window def initialize - super WIDTH, HEIGHT, :fullscreen => true, :update_interval => 16.666 + super WIDTH, HEIGHT, :fullscreen => true self.caption = "Number factory" @background_color = Gosu::Color::BLUE @@ -27,21 +42,72 @@ class NumFactory < Gosu::Window @camera_y = 0 @camera_zoom = 1.0 - @producers = [ - Producer.new(0, 0, 1, 1), - Producer.new(1000, 1000, 1, 1), - Producer.new(2000, 2000, 1, 1), - ] - @consumers = [] - @belts = [] + begin + game_save_data = YAML.load_file("#{GAME_SAVE_DIRECTORY}/positions.yaml") + puts game_save_data + # Load from file OR generate new save file + @producers = [ + Producer.new(0, 0, 1, 1), + Producer.new(1000, 1000, 1, 1), + Producer.new(2000, 2000, 1, 1), + ] + @consumers = [] + @belts = [] + + rescue Errno::ENOENT # File does not exist + puts "Generating new game..." + @game_width = GAME_WIDTH + @game_height = GAME_HEIGHT - @tick_time = 0 + @producers = [] + @consumers = [] # Consumer in center + @belts = [] + + + # Loop through and place producers + center_x = (@game_width / 60).to_i + center_y = (@game_height / 60).to_i + + # Move the camera to the center of the map: + @camera_x = center_x * 30 - WIDTH / 2 + @camera_y = center_y * 30 - HEIGHT / 2 + + @consumers.append( + Consumer.new(center_x * 30, center_y * 30), + ) + + # The value of the producer is proportional to the distance from the center + ((@game_width / 30).to_i).times do |xx| + (@game_height / 30).to_i.times do |yy| + if rand < GAME_PRODUCER_CHANCE + # Place a producer: + @producers.append( + Producer.new( + xx * 30, + yy * 30, + (Math.sqrt((xx - center_x)**2 + (yy - center_y) ** 2) * GAME_PRODUCER_VALUE_MULT).to_i, + GAME_PRODUCER_BASE_SPEED) + ) + end + end + end + end # End file loading + @tick_time = Time.now() + @delta_time = 0 + end + def button_down(id) if id == Gosu::KB_ESCAPE close + elsif id == Gosu::MS_WHEEL_UP + @camera_zoom *= 1.1 + elsif id == Gosu::MS_WHEEL_DOWN + @camera_zoom *= 0.9 + elsif id == Gosu::KB_SPACE + @camera_zoom = 1.0 else super end @@ -50,52 +116,88 @@ class NumFactory < Gosu::Window ## Called every frame def update + @delta_time = Time.now() - @tick_time + delta_mult = @delta_time * 30 + #puts "Time: #{@delta_time}, Mult: #{delta_mult}" # Timing debug @tick_time = Time.now() + ### CAMERA ### # Move the camera if needed if self.mouse_x < CAMERA_MOVE_RANGE - @camera_x -= [(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - self.mouse_x ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min + @camera_x -= ([(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - self.mouse_x ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min * delta_mult * @camera_zoom) elsif self.mouse_x > WIDTH - CAMERA_MOVE_RANGE - @camera_x += [(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - (WIDTH - self.mouse_x) ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min + @camera_x += ([(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - (WIDTH - self.mouse_x) ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min * delta_mult * @camera_zoom) end - + if self.mouse_y < CAMERA_MOVE_RANGE - @camera_y -= [(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - self.mouse_y ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min + @camera_y -= ([(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - self.mouse_y ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min * delta_mult) elsif self.mouse_y > HEIGHT - CAMERA_MOVE_RANGE - @camera_y += [(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - (HEIGHT - (self.mouse_y + 1)) ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min + @camera_y += ([(CAMERA_MOVE_SPEED * ((CAMERA_MOVE_RANGE - (HEIGHT - (self.mouse_y + 1)) ) / 2)).to_i, CAMERA_MOVE_MAX_SPEED].min * delta_mult) end + # Prevent camera from leaving the screen + @camera_x = [@camera_x, -10].max + @camera_x = [@camera_x, @game_width - (WIDTH / @camera_zoom) + 10].min + @camera_y = [@camera_y, -10].max + @camera_y = [@camera_y, @game_height - (HEIGHT / @camera_zoom) + 10].min + + # Setup variables used to know what portions of the screen need to be rendered + @camera_max_x = [@game_width, @camera_x + (WIDTH / @camera_zoom)].min + @camera_min_x = [0, @camera_x].max + @camera_max_y = [@game_height, @camera_y + (WIDTH / @camera_zoom)].min + @camera_min_y = [0, @camera_y].max + ### ITEMS ### @producers.each do |producer| - producer.update + producer.update(delta_mult) end @belts.each do |belt| - belt.update + belt.update(delta_mult) end @consumers.each do |consumer| - consumer.update + consumer.update(delta_mult) end - @tick_time = Time.now() - @tick_time end ## Called when OS wants to update window. def draw # Draw mouse coords: - Gosu.draw_rect(0, 0, self.width, self.height, @background_color, 0) + onscreen_items = 0 + Gosu.draw_rect( + [(@camera_min_x - @camera_x) * @camera_zoom, self.width].min, + [(@camera_min_y - @camera_y) * @camera_zoom, self.width].min, + [self.width, (@camera_max_x - @camera_x) * @camera_zoom].min, + [self.height, (@camera_max_y - @camera_y) * @camera_zoom].min, + @background_color, 0) @producers.each do |producer| - if producer.x <= (@camera_x + WIDTH) && producer.x >= (@camera_x) - if producer.y <= (@camera_y + WIDTH) && producer.y >= (@camera_y) + if producer.x <= @camera_max_x && producer.x >= @camera_min_x + if producer.y <= @camera_max_y && producer.y >= @camera_min_y producer.draw(@camera_x, @camera_y, @camera_zoom) + onscreen_items += 1 + end + end + end + @consumers.each do |consumer| + if consumer.x <= (@camera_x + WIDTH) && consumer.x >= (@camera_x) + if consumer.y <= (@camera_y + WIDTH) && consumer.y >= (@camera_y) + consumer.draw(@camera_x, @camera_y, @camera_zoom) + onscreen_items += 1 end end end Gosu::Image.from_text("#{mouse_x}, #{mouse_y}", 20).draw(0, 100, 1) - Gosu::Image.from_text("Tick time: #{@tick_time}", 20).draw(0, 25, 1) + Gosu::Image.from_text("Tick time: #{((Time.now() - @tick_time).to_f * 1000).to_i}", 20).draw(0, 25, 1) + Gosu::Image.from_text("FPS: #{Gosu.fps}", 20).draw(0, 50, 1) + Gosu::Image.from_text("items on screen: #{onscreen_items}", 20).draw(0, 75, 1) Gosu::Image.from_text("#{width}, #{height}", 20).draw(0, 0, 1) Gosu::Image.from_text("Camera location: #{@camera_x}, #{@camera_y}", 20).draw(0, 200, 1) + Gosu::Image.from_text("Camera zoom: #{@camera_zoom}", 20).draw(0, 225, 1) + Gosu::Image.from_text("Camera viewport: (#{@camera_min_x}, #{@camera_min_y}) to (#{@camera_max_x}, #{@camera_max_y})", 20).draw(0, 250, 1) end end NumFactory.new.show + + diff --git a/producer.rb b/producer.rb index dc985a8..8458846 100644 --- a/producer.rb +++ b/producer.rb @@ -1,6 +1,8 @@ ## Nathan Hinton ## A number producer that will create numbers when an extractor is placed on it. +PRODUCER_DEBUG = false + class Producer attr_reader :x, :y # Create producer at a position with the number and rate specified @@ -13,10 +15,19 @@ class Producer @y = y @number = number @rate = (30/rate).to_i + @image = Gosu.render(30, 30) { + # Render the item + Gosu.draw_rect(0, 0, 30, 30, Gosu::Color::RED, 0) + Gosu::Image.from_text("#{@number}", 20).draw(5, 5, 1) + } + end - # Creates numbers depending on the rate. This is done with knowing that the game is 30 fps so we use that for a counter - def update + # Creates numbers depending on the rate. This is done with knowing that the + # game is 30 fps so we use that for a counter + + # @param delta_mult [Float] The delta multiplier for any actions + def update(delta_mult) end @@ -25,7 +36,9 @@ class Producer # @param camera_y [Integer] The camera Y position # @param camera_zoom [Float] The camera zoom def draw(camera_x, camera_y, camera_zoom) - puts "Drawing at #{@x - camera_x}, #{@y - camera_y}" - Gosu.draw_rect(@x - camera_x, @y - camera_y, camera_zoom * 30, camera_zoom * 30, Gosu::Color::RED, 0) + if PRODUCER_DEBUG + puts "Drawing at #{@x - camera_x}, #{@y - camera_y}" + end + @image.draw(camera_zoom * (@x - camera_x), camera_zoom * (@y - camera_y), 0, camera_zoom, camera_zoom) end end