diff --git a/resources/backgrounds/chapter_01/forest_clearing_trees_log .png~ b/resources/backgrounds/chapter_01/forest_clearing_trees_log .png~ new file mode 100644 index 0000000..e9b52d5 Binary files /dev/null and b/resources/backgrounds/chapter_01/forest_clearing_trees_log .png~ differ diff --git a/resources/menus/old_paper_800x500.png b/resources/menus/old_paper_800x500.png new file mode 100644 index 0000000..c44d1d8 Binary files /dev/null and b/resources/menus/old_paper_800x500.png differ diff --git a/src/disconnected/event_loop.cr b/src/disconnected/event_loop.cr index bd27d0a..26f4b37 100644 --- a/src/disconnected/event_loop.cr +++ b/src/disconnected/event_loop.cr @@ -35,18 +35,19 @@ module Disconnected @block_render = true @block_movment = true sleep 0.15 - txt = Text.get_text(10) - inv = "~~Inventory~~\n\n" - @player.inventory.each do |i| - inv += "Name: " + i.name + "\n" - inv += "Id: " + i.id.to_s + "\n" - inv += "\n\n" - end - txt.string = inv - txt.position = @player.position.dup - txt.position.x = @player.position.x + 50.0_f32 - @window.draw(txt) - @window.display + + # Create a menu instance + inv_menu = InventoryMenu.new( + location: @player.sprite.position.dup, + data: @player.inventory, + name: "Inventory", + texture_path: "./resources/menus/old_paper_800x500.png", + size: {w: 500, h: 400}) + + # Draw menu + inv_menu.draw_menu(@window) + + # @window.display end def render diff --git a/src/disconnected/inventory_menu.cr b/src/disconnected/inventory_menu.cr new file mode 100644 index 0000000..e47d13f --- /dev/null +++ b/src/disconnected/inventory_menu.cr @@ -0,0 +1,79 @@ +module Disconnected + class InventoryMenu + getter :name, :sprite + + def initialize(location : SF::Vector2, + @data : Array(Item), + @name : String, + texture_path : String, + @size : NamedTuple(w: Int32, h: Int32)) + # + texture = SF::Texture.from_file(texture_path, SF.int_rect(0, 0, size[:w], size[:h])) + texture.smooth = true + @sprite = SF::Sprite.new(texture) + @sprite.origin = SF.vector2(size[:w]/2, size[:h]/2) + @sprite.position = location + end + + def draw_menu(window) + # Draw background + window.draw(@sprite) + + # Add the menu header + txt = Text.get_text + txt.origin = SF.vector2(60, 0) + txt.string = @name + txt.color = SF::Color::Black + txt.character_size = 24 + txt.style = SF::Text::Bold + txt.position = @sprite.position.dup + SF.vector2(0, -(@size[:h] / 2)) + # txt.move(SF.vector2(0, -(@size[:h] / 2))) + # txt.position.x = @player.position.x + 50.0_f32 + window.draw(txt) + + # Move drawing location to the table + x_correction = (@size[:w] / 2) - 20 + y_correction = (@size[:h] / 2) - 50 + draw_location = @sprite.position.dup - SF.vector2(x_correction, y_correction) + + # Add table headers + line_spacing = 30 + + txt = Text.get_text + txt.string = "Name\t\t\tID" + txt.color = SF::Color::Black + txt.character_size = 20 + txt.style = SF::Text::Underlined # Underline doesn't work, must be a bug + txt.position = draw_location + window.draw(txt) + + # Move to next line + draw_location += SF.vector2(0, line_spacing*1.2) + + # Add table data + @data.each do |i| + data_text = Text.get_text + data_text.string = "#{i.name}\t\t\t\t\t#{i.id.to_s}" + data_text.color = SF::Color::Black + data_text.character_size = 16 + data_text.position = draw_location + window.draw(data_text) + + # Move to next line + draw_location += SF.vector2(0, line_spacing) + end + @data.each do |i| + data_text = Text.get_text + data_text.string = "#{i.name}\t\t\t\t\t#{i.id.to_s}" + data_text.character_size = 16 + data_text.position = draw_location + window.draw(data_text) + + # Move sprite vertically + draw_location += SF.vector2(0, line_spacing) + end + + window.display + end + end +end diff --git a/src/disconnected/player.cr b/src/disconnected/player.cr index 3893f77..e3b6c6d 100644 --- a/src/disconnected/player.cr +++ b/src/disconnected/player.cr @@ -3,9 +3,13 @@ require "./basic_char.cr" module Disconnected class Player < BasicChar property :interactions - getter :inventory + getter :inventory, :sprite - def initialize(texture_path : Array(String), starting_position_x : Int32, starting_position_y : Int32, @name : String = "", @health : Int32 = 10) + def initialize(texture_path : Array(String), + starting_position_x : Int32, + starting_position_y : Int32, + @name : String = "", + @health : Int32 = 10) @speed = 1 @images = Array(SF::Image).new texture_path.each do |p|