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 0000000..7f70173 Binary files /dev/null and b/assets/images/background_square.png differ diff --git a/assets/images/background_square.xcf b/assets/images/background_square.xcf new file mode 100644 index 0000000..f5ee3ff Binary files /dev/null and b/assets/images/background_square.xcf differ diff --git a/consumer.rb b/consumer.rb new file mode 100644 index 0000000..c1922ae --- /dev/null +++ b/consumer.rb @@ -0,0 +1,36 @@ +## Nathan Hinton +## A number consumer that will create numbers when an extractor is placed on it. + +CONSUMER_DEBUG = false + +class Consumer + attr_reader :x, :y + # Create consumer at a position with the number and rate specified + # @param x [Integer] X position to start at + # @param y [Integer] Y position to start at + # @param number [Integer] The number to produce + # @param rate [Integer] The speed in numbers per second to produce + def initialize(x, y) + @x = x + @y = y + @image = Gosu::Image.new('assets/images/background_square.png', {:tileable => 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