forked from bionickatana/number_factory
Finished camera and rendering of objects on screen.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user