Reworked the dialouge system. Now the main.rb function can register and delete the dialouges.
This commit is contained in:
+5
-14
@@ -18,7 +18,8 @@ class Consumer
|
||||
# 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
|
||||
def initialize(x, y, box_size, screen_width, screen_height)
|
||||
def initialize(parent, x, y, box_size, screen_width, screen_height)
|
||||
@parent = parent
|
||||
@x = x - SIZE / 2
|
||||
@y = y - SIZE / 2
|
||||
@box_size = box_size
|
||||
@@ -28,8 +29,7 @@ class Consumer
|
||||
Gosu.draw_rect(0, 0, (SIZE * @box_size) - (2 * BORDER), (SIZE * @box_size) - (2 * BORDER), Gosu::Color::GREEN, 0)
|
||||
}
|
||||
@task_index = 0
|
||||
@tooltip_timer = 0
|
||||
@tooltip = DialougeBox.new(@x, @y, WIDTH, HEIGHT, TASKS[@task_index])
|
||||
@tooltip = @parent.register_dialouge(@x, @y, @box_size * SIZE, @box_size * SIZE, TASKS[@task_index], 500)
|
||||
end
|
||||
|
||||
# Consumes numbers created
|
||||
@@ -41,16 +41,8 @@ class Consumer
|
||||
@draw_x = (((((@screen_width / @box_size.to_f) / 2.0) + (@x - camera_x) * camera_zoom) * @box_size) + BORDER * camera_zoom)
|
||||
@draw_y = (((((@screen_height / @box_size.to_f) / 2.0) + (@y - camera_y) * camera_zoom) * @box_size) + BORDER * camera_zoom)
|
||||
@camera_zoom = camera_zoom
|
||||
if !mouse_used && 0 < mouse_x - @draw_x && mouse_x - @draw_x < (SIZE * @box_size * @camera_zoom) && 0 < mouse_y - @draw_y && mouse_y - @draw_y < (SIZE * @box_size * @camera_zoom)
|
||||
@tooltip_timer += 60.0 * delta_mult
|
||||
if @tooltip_timer > HOVER_TIME
|
||||
@tooltip.show = true
|
||||
@tooltip.update(@draw_x, @draw_y, @camera_zoom)
|
||||
end
|
||||
else
|
||||
@tooltip_timer = 0
|
||||
@tooltip.show = false
|
||||
end
|
||||
|
||||
@tooltip.move(@draw_x, @draw_y)
|
||||
end
|
||||
|
||||
# Draw the consumer. Uses the camera x and y to shift where it should draw
|
||||
@@ -59,6 +51,5 @@ class Consumer
|
||||
puts "Drawing at #{@x - camera_x}, #{@y - camera_y}"
|
||||
end
|
||||
@image.draw(@draw_x, @draw_y, 0, @camera_zoom, @camera_zoom)
|
||||
@tooltip.draw()
|
||||
end
|
||||
end
|
||||
|
||||
+39
-25
@@ -3,18 +3,26 @@
|
||||
|
||||
TEXT_SIZE = 20
|
||||
TEXT_MARGIN = 3
|
||||
|
||||
class DialougeBox
|
||||
attr_accessor :show
|
||||
def initialize(parent_x, parent_y, screen_width, screen_height, text)
|
||||
# @param draw_x [Integer]
|
||||
# @param draw_y [Integer]
|
||||
# @param size_x [Integer] The width that this should open at (in pixels at zero zoom)
|
||||
# @param size_y [Integer] The height that this should open at (in pixels at zero zoom)
|
||||
def initialize(x, y, size_x, size_y, screen_width, screen_height, text, box_size, hover_time)
|
||||
@text = text
|
||||
@longest_line = ((@text.split("\n").map { |ss| ss.length}.max) / 2.25).to_i
|
||||
@line_count = @text.split("\n").length
|
||||
|
||||
@x = parent_x
|
||||
@y = parent_y
|
||||
@show = false
|
||||
@x = x
|
||||
@y = y
|
||||
@size_x = size_x
|
||||
@size_y = size_y
|
||||
@screen_width = screen_width
|
||||
@screen_height = screen_height
|
||||
@box_size = box_size
|
||||
@hover_time = hover_time
|
||||
@show = false
|
||||
@image = Gosu.render(((TEXT_MARGIN * 2) + TEXT_SIZE) * @longest_line, (TEXT_SIZE + TEXT_MARGIN) * @line_count) {
|
||||
Gosu.draw_rect(
|
||||
0,
|
||||
@@ -26,34 +34,40 @@ class DialougeBox
|
||||
)
|
||||
Gosu::Image.from_text(text, TEXT_SIZE, font: FONT_PATH).draw(TEXT_MARGIN, TEXT_MARGIN)
|
||||
}
|
||||
@timer = 0
|
||||
@draw_x = 0
|
||||
@draw_y = 0
|
||||
end
|
||||
|
||||
def update(x, y, camera_zoom, newtext: nil)
|
||||
if !newtext.nil?
|
||||
@text = newtext
|
||||
@longest_line = ((@text.split("\n").map { |ss| ss.length}.max) / 3.5).to_i
|
||||
@line_count = @text.split("\n").length
|
||||
|
||||
@image = Gosu.render(((TEXT_MARGIN * 2) + TEXT_SIZE) * @longest_line, (TEXT_SIZE + TEXT_MARGIN) * @line_count) {
|
||||
Gosu.draw_rect(
|
||||
0,
|
||||
0,
|
||||
(((TEXT_MARGIN * 2) + TEXT_SIZE) * @longest_line) + @x,
|
||||
((TEXT_SIZE + TEXT_MARGIN) * @line_count) + @y,
|
||||
Gosu::Color.new(0xC0, 0xFF, 0xFF, 0xFF),
|
||||
0,
|
||||
)
|
||||
Gosu::Image.from_text(newtext, TEXT_SIZE, font: FONT_PATH).draw(TEXT_MARGIN, TEXT_MARGIN)
|
||||
}
|
||||
# Used to move the dialouge box around
|
||||
# @param x [Integer] The x position to move to.
|
||||
# @param y [Integer] The y position to move to.
|
||||
def move(x, y)
|
||||
@draw_x = x
|
||||
@draw_y = y
|
||||
end
|
||||
|
||||
# Update called every delta time
|
||||
def update(delta_mult, camera_zoom, mouse_x, mouse_y)
|
||||
@camera_zoom = camera_zoom
|
||||
@x = x
|
||||
@y = y
|
||||
|
||||
if 0 < mouse_x - @draw_x && mouse_x - @draw_x < (@size_x * @camera_zoom) && 0 < mouse_y - @draw_y && mouse_y - @draw_y < (@size_y * @camera_zoom)
|
||||
@timer += 60.0 * delta_mult
|
||||
if @timer > @hover_time
|
||||
@show = true
|
||||
end
|
||||
else
|
||||
@timer = 0
|
||||
@show = false
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
## Draw the dialouge if it is supposed to be shown.
|
||||
def draw()
|
||||
if @show
|
||||
@image.draw(@x, @y, 0, @camera_zoom, @camera_zoom)
|
||||
@image.draw(@draw_x, @draw_y, 0, @camera_zoom, @camera_zoom)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ require 'yaml'
|
||||
|
||||
require './producer.rb'
|
||||
require './consumer.rb'
|
||||
require './dialouge_box.rb'
|
||||
require './ui.rb'
|
||||
|
||||
FONT_PATH = 'assets/font/Cousine-Regular.ttf'
|
||||
@@ -46,6 +47,9 @@ class NumFactory < Gosu::Window
|
||||
@camera_y_zoom_offset = HEIGHT.to_f / (@camera_boxes * BOX_SIZE * 2).to_f
|
||||
|
||||
@ui = UI.new(WIDTH, HEIGHT)
|
||||
@dialouges = [
|
||||
#DialougeBox.new(0, 5, BOX_SIZE, BOX_SIZE, WIDTH, HEIGHT, 'this is some really good test text!', BOX_SIZE, 500)
|
||||
]
|
||||
|
||||
# Load from file or initialize a new game
|
||||
begin
|
||||
@@ -75,7 +79,7 @@ class NumFactory < Gosu::Window
|
||||
center_y = ((@game_height - 1) / 2).to_i
|
||||
|
||||
@consumers.append(
|
||||
Consumer.new(center_x, center_y, BOX_SIZE, WIDTH, HEIGHT),
|
||||
Consumer.new(self, center_x, center_y, BOX_SIZE, WIDTH, HEIGHT),
|
||||
)
|
||||
|
||||
# The value of the producer is proportional to the distance from the center
|
||||
@@ -85,6 +89,7 @@ class NumFactory < Gosu::Window
|
||||
# Place a producer:
|
||||
@producers.append(
|
||||
Producer.new(
|
||||
self,
|
||||
xx,
|
||||
yy,
|
||||
(Math.sqrt((xx - center_x)**2 + (yy - center_y) ** 2) * GAME_PRODUCER_VALUE_MULT).to_i,
|
||||
@@ -159,6 +164,24 @@ class NumFactory < Gosu::Window
|
||||
end
|
||||
|
||||
|
||||
## Register a dialouge into the game
|
||||
# @param x [Integer] The x position that the dialouge should be drawn at.
|
||||
# @param y [Integer] The y position that the dialouge should be drawn at.
|
||||
# @param size_x [Integer] The pixel width
|
||||
# @param size_y [Integer] The pixel width
|
||||
# @param text [String] The text for the dialouge box
|
||||
def register_dialouge(x, y, size_x, size_y, text, popup_time)
|
||||
dialouge = DialougeBox.new(x, y, size_x, size_y, WIDTH, HEIGHT, text, BOX_SIZE, popup_time)
|
||||
@dialouges.append(dialouge)
|
||||
return dialouge
|
||||
end
|
||||
|
||||
## Remove a dialouge from the game
|
||||
# @param dialouge [DialougeBox] The dialouge to remove
|
||||
def deregister_dialouge(dialouge)
|
||||
@dialouges.delete(dialouge)
|
||||
end
|
||||
|
||||
## Called every frame
|
||||
def update
|
||||
@delta_time = Time.now() - @tick_time
|
||||
@@ -190,7 +213,7 @@ class NumFactory < Gosu::Window
|
||||
mouse_used = @ui.update(self.mouse_x, self.mouse_y)
|
||||
|
||||
@producers.each do |producer|
|
||||
producer.update(delta_mult, @camera_x, @camera_y, @camera_zoom, self.mouse_x, self.mouse_y, mouse_used)
|
||||
producer.update(delta_mult, @camera_x, @camera_y, @camera_zoom)
|
||||
end
|
||||
|
||||
@belts.each do |belt|
|
||||
@@ -201,6 +224,9 @@ class NumFactory < Gosu::Window
|
||||
consumer.update(delta_mult, @camera_x, @camera_y, @camera_zoom, self.mouse_x, self.mouse_y, mouse_used)
|
||||
end
|
||||
|
||||
@dialouges.each do |dialouge|
|
||||
dialouge.update(delta_mult, @camera_zoom, self.mouse_x, self.mouse_y)
|
||||
end
|
||||
end
|
||||
|
||||
## Called when OS wants to update window.
|
||||
@@ -236,6 +262,11 @@ class NumFactory < Gosu::Window
|
||||
end
|
||||
end
|
||||
|
||||
@dialouges.each do |dialouge|
|
||||
dialouge.draw()
|
||||
end
|
||||
|
||||
|
||||
@ui.draw()
|
||||
|
||||
Gosu::Image.from_text("#{width}, #{height}", 20, font: FONT_PATH).draw(1600, 0, 1)
|
||||
|
||||
+8
-23
@@ -1,11 +1,8 @@
|
||||
## Nathan Hinton
|
||||
## A number producer that will create numbers when an extractor is placed on it.
|
||||
|
||||
require './dialouge_box.rb'
|
||||
|
||||
PRODUCER_DEBUG = false
|
||||
BORDER = 2
|
||||
HOVER_TIME = 500 # ms
|
||||
|
||||
class Producer
|
||||
attr_reader :draw_x, :draw_y
|
||||
@@ -14,9 +11,10 @@ class Producer
|
||||
# @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, box_size, screen_width, screen_height)
|
||||
def initialize(parent, x, y, number, rate, box_size, screen_width, screen_height)
|
||||
@x = x
|
||||
@y = y
|
||||
@parent = parent
|
||||
@number = [number, 1].max
|
||||
@rate = rate
|
||||
@box_size = box_size
|
||||
@@ -28,9 +26,11 @@ class Producer
|
||||
Gosu::Image.from_text("#{@number}", 20, font: FONT_PATH).draw(10 / 32.0 * @box_size, (8 / 32.0 * @box_size), 1, 2, 2, 0xFFFA32C0)
|
||||
}
|
||||
|
||||
@tooltip = DialougeBox.new(@x, @y, WIDTH, HEIGHT, "Produces #{@rate} number per second")
|
||||
puts "X: #{@x}, Y: #{@y}"
|
||||
@tooltip = @parent.register_dialouge(@x, @y, @box_size, @box_size, "Produces #{@rate} number per second", 500)
|
||||
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
|
||||
|
||||
@@ -38,23 +38,12 @@ class Producer
|
||||
# @param camera_x [Integer] The camera X position
|
||||
# @param camera_y [Integer] The camera Y position
|
||||
# @param camera_zoom [Float] The camera zoom
|
||||
def update(delta_mult, camera_x, camera_y, camera_zoom, mouse_x, mouse_y, mouse_used)
|
||||
def update(delta_mult, camera_x, camera_y, camera_zoom)
|
||||
@draw_x = (((((@screen_width / @box_size.to_f) / 2.0) + (@x - camera_x) * camera_zoom) * @box_size) + BORDER * camera_zoom)
|
||||
@draw_y = (((((@screen_height / @box_size.to_f) / 2.0) + (@y - camera_y) * camera_zoom) * @box_size) + BORDER * camera_zoom)
|
||||
@camera_zoom = camera_zoom
|
||||
if !mouse_used && 0 < mouse_x - @draw_x && mouse_x - @draw_x < (@box_size * @camera_zoom) && 0 < mouse_y - @draw_y && mouse_y - @draw_y < (@box_size * @camera_zoom)
|
||||
@tooltip_timer += 60.0 * delta_mult
|
||||
if @tooltip_timer > HOVER_TIME
|
||||
@tooltip.show = true
|
||||
@tooltip.update(@draw_x, @draw_y, @camera_zoom)
|
||||
end
|
||||
else
|
||||
@tooltip_timer = 0
|
||||
@tooltip.show = false
|
||||
end
|
||||
if @extractor
|
||||
@extractor.update()
|
||||
end
|
||||
|
||||
@tooltip.move(@draw_x, @draw_y)
|
||||
end
|
||||
|
||||
# Draw the producer. Uses the camera x and y to shift where it should draw
|
||||
@@ -63,9 +52,5 @@ class Producer
|
||||
puts "Drawing at #{@x - camera_x}, #{@y - camera_y}"
|
||||
end
|
||||
@image.draw(@draw_x, @draw_y, 0, @camera_zoom, @camera_zoom)
|
||||
@tooltip.draw()
|
||||
if @extractor
|
||||
@extractor.update()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user