Working basic camera

This commit is contained in:
2026-06-13 12:01:58 -06:00
parent 9e56dc174a
commit 9cf37b5173
5 changed files with 150 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'gosu'
+14
View File
@@ -0,0 +1,14 @@
GEM
remote: https://rubygems.org/
specs:
gosu (1.4.6)
PLATFORMS
ruby
x86_64-linux-gnu
DEPENDENCIES
gosu
BUNDLED WITH
2.6.7
+1
View File
@@ -1,2 +1,3 @@
# number_factory
A game where you create combinations of numbers!
+101
View File
@@ -0,0 +1,101 @@
## Nathan Hinton
## Main entry point for the game. Sets up the world and holds the main loop
require 'gosu'
require './producer.rb'
WIDTH = 640*2
HEIGHT = 480*2
CAMERA_MOVE_RANGE = 10
CAMERA_MOVE_SPEED = 1
CAMERA_MOVE_MAX_SPEED = (CAMERA_MOVE_RANGE * CAMERA_MOVE_SPEED / 2).to_i
class NumFactory < Gosu::Window
def initialize
super WIDTH, HEIGHT, :fullscreen => true, :update_interval => 16.666
self.caption = "Number factory"
@background_color = Gosu::Color::BLUE
### CAMERA ###
# camera_x [Integer] Used to know the x position of the camera
# camera_y [Integer] Used to know the y position of the camera
# camera_zoom [Float] Used to know the zoom of the camera
@camera_x = 0
@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 = []
@tick_time = 0
end
def button_down(id)
if id == Gosu::KB_ESCAPE
close
else
super
end
end
## Called every frame
def update
@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
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
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
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
end
### ITEMS ###
@producers.each do |producer|
producer.update
end
@belts.each do |belt|
belt.update
end
@consumers.each do |consumer|
consumer.update
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)
@producers.each do |producer|
if producer.x <= (@camera_x + WIDTH) && producer.x >= (@camera_x)
if producer.y <= (@camera_y + WIDTH) && producer.y >= (@camera_y)
producer.draw(@camera_x, @camera_y, @camera_zoom)
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("#{width}, #{height}", 20).draw(0, 0, 1)
Gosu::Image.from_text("Camera location: #{@camera_x}, #{@camera_y}", 20).draw(0, 200, 1)
end
end
NumFactory.new.show
+31
View File
@@ -0,0 +1,31 @@
## Nathan Hinton
## A number producer that will create numbers when an extractor is placed on it.
class Producer
attr_reader :x, :y
# Create producer 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, number, rate)
@x = x
@y = y
@number = number
@rate = (30/rate).to_i
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
end
# Draw the producer. 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)
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
end