Working basic camera
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user