Finished allowing placement and collision detection. However, the placement is very inconsistent. Thinking about redoing coordinates again...

This commit is contained in:
2026-06-17 22:14:22 -06:00
parent 6f5ad9803f
commit e7825478f1
7 changed files with 161 additions and 36 deletions
+76 -26
View File
@@ -5,6 +5,8 @@
require 'gosu'
require 'yaml'
require './gamegrid.rb'
require './extractor.rb'
require './producer.rb'
require './consumer.rb'
require './dialouge_box.rb'
@@ -19,8 +21,8 @@ BOX_SIZE = 64
# Default game settings
GAME_SAVE_DIRECTORY = 'game_save_data'
GAME_WIDTH = 101 # Odd numbers look best, in squares
GAME_HEIGHT = 101
GAME_WIDTH = 201 # Odd numbers look best, in squares
GAME_HEIGHT = 201
GAME_PRODUCER_CHANCE = 0.01
GAME_PRODUCER_VALUE_MULT = 0.1
GAME_PRODUCER_BASE_SPEED = 1
@@ -42,6 +44,7 @@ class NumFactory < Gosu::Window
@background_color = Gosu::Color::BLUE
@cursor_image = Gosu::Image.new('assets/images/cursor.png')
@cursor = @cursor_image
@cursor_offset = 16
@camera_x = 0
@camera_y = 0
@@ -57,9 +60,11 @@ class NumFactory < Gosu::Window
@adder_image = Gosu::Image.new('assets/images/adder.png')
@delete_image = Gosu::Image.new('assets/images/delete.png')
@dialouges = [
#DialougeBox.new(0, 5, BOX_SIZE, BOX_SIZE, WIDTH, HEIGHT, 'this is some really good test text!', BOX_SIZE, 500)
]
@dialouges = []
@extractors = []
# Setup the game grid
@game_grid = GameGrid.new(GAME_WIDTH, GAME_HEIGHT)
# Load from file or initialize a new game
begin
@@ -87,17 +92,18 @@ class NumFactory < Gosu::Window
# Loop through and place producers
center_x = ((@game_width - 1) / 2).to_i
center_y = ((@game_height - 1) / 2).to_i
@consumers.append(
Consumer.new(self, center_x, center_y, BOX_SIZE, WIDTH, HEIGHT),
)
consumer = Consumer.new(self, center_x, center_y, BOX_SIZE, WIDTH, HEIGHT)
if @game_grid.add_item(center_x, center_y, consumer)
@consumers.append(consumer)
end
# The value of the producer is proportional to the distance from the center
((@game_width).to_i).times do |xx|
(@game_height).to_i.times do |yy|
if rand < GAME_PRODUCER_CHANCE
# Place a producer:
@producers.append(
producer =
Producer.new(
self,
xx,
@@ -108,7 +114,10 @@ class NumFactory < Gosu::Window
WIDTH,
HEIGHT,
)
)
# Add an item to the game with checking that the internal game state is good
if @game_grid.add_item(xx, yy, producer)
@producers.append(producer)
end
end
end
end
@@ -141,6 +150,38 @@ class NumFactory < Gosu::Window
end
def button_down(id)
### Process mouse ###
mouse_used = @ui.update(self.mouse_x, self.mouse_y)
# They clicked in the game screen, attempt to place an item
if !mouse_used && id == Gosu::MS_LEFT
case @current_tool
when 'delete'
@cursor = @delete_image
@cursor_offset = @delete_image.height
#break
when 'belt'
@cursor = @belt_image
@cursor_offset = @belt_image.height
#break
when 'adder'
@cursor = @adder_image
@cursor_offset = @adder_image.height
#break
when 'extractor'
#xx = @camera_x + ((((self.mouse_x) - (WIDTH/2)) / BOX_SIZE - 1) * @camera_zoom).to_i
#yy = @camera_y + ((((self.mouse_y) - (HEIGHT/2)) / BOX_SIZE - 1) * @camera_zoom).to_i
xx = @camera_x + ((self.mouse_x - @cursor_offset + (BOX_SIZE * @camera_zoom)) / (BOX_SIZE * @camera_zoom)).to_i
yy = @camera_y + ((self.mouse_y - @cursor_offset + (BOX_SIZE * @camera_zoom)) / (BOX_SIZE * @camera_zoom))
extractor = Extractor.new(xx, yy, BOX_SIZE)
if @game_grid.add_item(xx, yy, extractor)
@extractors.append(extractor)
end
#break
end
end
### Process keyboard ###
if id == Gosu::KB_ESCAPE
close
elsif [Gosu::MS_WHEEL_UP, Gosu::KB_Z].include? id
@@ -174,6 +215,7 @@ class NumFactory < Gosu::Window
@camera_y_zoom_offset = HEIGHT.to_f / (@camera_boxes * BOX_SIZE * 2).to_f
elsif id == Gosu::KB_G
@cursor = @cursor_image
@cursor_offset = 16
@current_tool = nil
else
super
@@ -221,30 +263,27 @@ class NumFactory < Gosu::Window
end
# Prevent camera from leaving the screen
@camera_x = [@camera_x, 0].max
@camera_x = [@camera_x, @game_width].min
@camera_y = [@camera_y, 0].max
@camera_y = [@camera_y, @game_width].min
@camera_x = [@camera_x.to_i, 0].max
@camera_x = [@camera_x.to_i, @game_width].min
@camera_y = [@camera_y.to_i, 0].max
@camera_y = [@camera_y.to_i, @game_width].min
### ITEMS ###
mouse_used = @ui.update(self.mouse_x, self.mouse_y)
@producers.each do |producer|
producer.update(delta_mult, @camera_x, @camera_y, @camera_zoom)
end
@belts.each do |belt|
belt.update(delta_mult)
@extractors.each do |extractor|
extractor.update(delta_mult, @camera_x, @camera_y, @camera_zoom)
end
@consumers.each do |consumer|
consumer.update(delta_mult, @camera_x, @camera_y, @camera_zoom, self.mouse_x, self.mouse_y, mouse_used)
consumer.update(delta_mult, @camera_x, @camera_y, @camera_zoom, self.mouse_x, self.mouse_y)
end
if !mouse_used
@dialouges.each do |dialouge|
dialouge.update(delta_mult, @camera_zoom, self.mouse_x, self.mouse_y)
end
# Finally process the dialouges
@dialouges.each do |dialouge|
dialouge.update(delta_mult, @camera_zoom, self.mouse_x, self.mouse_y)
end
case @current_tool
@@ -290,6 +329,15 @@ class NumFactory < Gosu::Window
end
end
end
@extractors.each do |extractor|
if extractor.draw_x <= WIDTH && extractor.draw_x >= 0
if extractor.draw_y <= HEIGHT && extractor.draw_y >= 0
extractor.draw()
onscreen_items += 1
end
end
end
@consumers.each do |consumer|
if consumer.draw_x <= WIDTH && consumer.draw_x >= 0
@@ -309,6 +357,7 @@ class NumFactory < Gosu::Window
Gosu::Image.from_text("#{width}, #{height}", 20, font: FONT_PATH).draw(1600, 0, 1)
Gosu::Image.from_text("#{mouse_x}, #{mouse_y}", 20, font: FONT_PATH).draw(1600, 100, 1)
Gosu::Image.from_text("#{@camera_x + ((self.mouse_x - @cursor_offset + (BOX_SIZE * @camera_zoom)) / (BOX_SIZE * @camera_zoom)).to_i}, #{@camera_y + ((self.mouse_y - (HEIGHT/2)) / BOX_SIZE * @camera_zoom).to_i}", 20, font: FONT_PATH).draw(1600, 125, 1)
Gosu::Image.from_text("Tick time: #{((Time.now() - @tick_time).to_f * 1000).to_i}", 20, font: FONT_PATH).draw(1600, 25, 1)
Gosu::Image.from_text("FPS: #{Gosu.fps}", 20, font: FONT_PATH).draw(1600, 50, 1)
Gosu::Image.from_text("items on screen: #{onscreen_items}", 20, font: FONT_PATH).draw(1600, 75, 1)
@@ -318,10 +367,11 @@ class NumFactory < Gosu::Window
Gosu::Image.from_text("Camera viewport: (#{@camera_min_x}, #{@camera_min_y}) to (#{@camera_max_x}, #{@camera_max_y})", 20, font: FONT_PATH).draw(1600, 250, 1)
if @cursor == @cursor_image
@cursor.draw(self.mouse_x - 16, self.mouse_y - 16, 10)
@cursor.draw(self.mouse_x - @cursor_offset, self.mouse_y - @cursor_offset, 10)
else
@cursor.draw(self.mouse_x - @cursor_offset, self.mouse_y - @cursor_offset, 10, BOX_SIZE / 32.0 * @camera_zoom, BOX_SIZE / 32.0 * @camera_zoom)
@cursor.draw(self.mouse_x - @cursor_offset, self.mouse_y - @cursor_offset, 10, BOX_SIZE / 32.0 * @camera_zoom, BOX_SIZE / 32.0 * @camera_zoom, 0x88_888888)
end
Gosu.draw_rect(self.mouse_x - 2, self.mouse_y - 2, 4, 4, Gosu::Color::RED)
end
end