Finished camera and rendering of objects on screen.

This commit is contained in:
2026-06-13 15:14:36 -06:00
parent 9cf37b5173
commit 5e47e02118
6 changed files with 178 additions and 28 deletions
-1
View File
@@ -55,4 +55,3 @@ build-iPhoneSimulator/
# Used by RuboCop. Remote config files pulled in from inherit_from directive. # Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--* # .rubocop-https?--*
Binary file not shown.

After

Width:  |  Height:  |  Size: 653 B

Binary file not shown.
+36
View File
@@ -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
+117 -15
View File
@@ -3,18 +3,33 @@
require 'gosu' require 'gosu'
require 'yaml'
require './producer.rb' require './producer.rb'
require './consumer.rb'
# Viewport resolution
WIDTH = 640*2 WIDTH = 640*2
HEIGHT = 480*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_RANGE = 10
CAMERA_MOVE_SPEED = 1 CAMERA_MOVE_SPEED = 30
CAMERA_MOVE_MAX_SPEED = (CAMERA_MOVE_RANGE * CAMERA_MOVE_SPEED / 2).to_i 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 class NumFactory < Gosu::Window
def initialize def initialize
super WIDTH, HEIGHT, :fullscreen => true, :update_interval => 16.666 super WIDTH, HEIGHT, :fullscreen => true
self.caption = "Number factory" self.caption = "Number factory"
@background_color = Gosu::Color::BLUE @background_color = Gosu::Color::BLUE
@@ -27,6 +42,10 @@ class NumFactory < Gosu::Window
@camera_y = 0 @camera_y = 0
@camera_zoom = 1.0 @camera_zoom = 1.0
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 = [ @producers = [
Producer.new(0, 0, 1, 1), Producer.new(0, 0, 1, 1),
Producer.new(1000, 1000, 1, 1), Producer.new(1000, 1000, 1, 1),
@@ -35,13 +54,60 @@ class NumFactory < Gosu::Window
@consumers = [] @consumers = []
@belts = [] @belts = []
@tick_time = 0 rescue Errno::ENOENT # File does not exist
puts "Generating new game..."
@game_width = GAME_WIDTH
@game_height = GAME_HEIGHT
@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 end
def button_down(id) def button_down(id)
if id == Gosu::KB_ESCAPE if id == Gosu::KB_ESCAPE
close 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 else
super super
end end
@@ -50,52 +116,88 @@ class NumFactory < Gosu::Window
## Called every frame ## Called every frame
def update 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() @tick_time = Time.now()
### CAMERA ### ### CAMERA ###
# Move the camera if needed # Move the camera if needed
if self.mouse_x < CAMERA_MOVE_RANGE 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 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 end
if self.mouse_y < CAMERA_MOVE_RANGE 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 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 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 ### ### ITEMS ###
@producers.each do |producer| @producers.each do |producer|
producer.update producer.update(delta_mult)
end end
@belts.each do |belt| @belts.each do |belt|
belt.update belt.update(delta_mult)
end end
@consumers.each do |consumer| @consumers.each do |consumer|
consumer.update consumer.update(delta_mult)
end end
@tick_time = Time.now() - @tick_time
end end
## Called when OS wants to update window. ## Called when OS wants to update window.
def draw def draw
# Draw mouse coords: # 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| @producers.each do |producer|
if producer.x <= (@camera_x + WIDTH) && producer.x >= (@camera_x) if producer.x <= @camera_max_x && producer.x >= @camera_min_x
if producer.y <= (@camera_y + WIDTH) && producer.y >= (@camera_y) if producer.y <= @camera_max_y && producer.y >= @camera_min_y
producer.draw(@camera_x, @camera_y, @camera_zoom) 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 end
end end
Gosu::Image.from_text("#{mouse_x}, #{mouse_y}", 20).draw(0, 100, 1) 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("#{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 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
end end
NumFactory.new.show NumFactory.new.show
+16 -3
View File
@@ -1,6 +1,8 @@
## Nathan Hinton ## Nathan Hinton
## A number producer that will create numbers when an extractor is placed on it. ## A number producer that will create numbers when an extractor is placed on it.
PRODUCER_DEBUG = false
class Producer class Producer
attr_reader :x, :y attr_reader :x, :y
# Create producer at a position with the number and rate specified # Create producer at a position with the number and rate specified
@@ -13,10 +15,19 @@ class Producer
@y = y @y = y
@number = number @number = number
@rate = (30/rate).to_i @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 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 # Creates numbers depending on the rate. This is done with knowing that the
def update # 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 end
@@ -25,7 +36,9 @@ class Producer
# @param camera_y [Integer] The camera Y position # @param camera_y [Integer] The camera Y position
# @param camera_zoom [Float] The camera zoom # @param camera_zoom [Float] The camera zoom
def draw(camera_x, camera_y, camera_zoom) def draw(camera_x, camera_y, camera_zoom)
if PRODUCER_DEBUG
puts "Drawing at #{@x - camera_x}, #{@y - camera_y}" 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) end
@image.draw(camera_zoom * (@x - camera_x), camera_zoom * (@y - camera_y), 0, camera_zoom, camera_zoom)
end end
end end