From a6c2b080b91e3b0d8a35a3dee0e8a4fe6353e6d4 Mon Sep 17 00:00:00 2001 From: cyberarm Date: Tue, 5 Jan 2021 17:30:23 -0600 Subject: [PATCH] Updated lobby to show map preview, misc. --- lib/components/sidebar_actions.rb | 4 ++-- lib/map.rb | 24 +++++++++++++++++++- lib/states/menus/main_menu.rb | 3 +-- lib/states/menus/solo_lobby_menu.rb | 9 ++++++-- lib/tiled_map.rb | 35 +++++++++++++++++------------ 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/lib/components/sidebar_actions.rb b/lib/components/sidebar_actions.rb index 67de355..79c8b15 100644 --- a/lib/components/sidebar_actions.rb +++ b/lib/components/sidebar_actions.rb @@ -16,7 +16,7 @@ class IMICRTS raise "Failed to find entity: #{data[:entity].inspect}" unless ent action.label = ent.name.to_s.split("_").map{ |s| s.capitalize }.join(" ") - action.description = "Cost: #{ent.cost}\n#{ent.description}" + action.description = "#{action.label}\nCost: #{ent.cost}\n#{ent.description}" action.block = proc do @parent.director.schedule_order(IMICRTS::Order::BUILD_UNIT, @parent.player.id, @parent.id, data[:entity]) end @@ -26,7 +26,7 @@ class IMICRTS raise "Failed to find entity: #{data[:entity].inspect}" unless ent action.label = ent.name.to_s.split("_").map { |s| s.capitalize }.join(" ") - action.description = "Cost: #{ent.cost}\n#{ent.description}" + action.description = "#{action.label}\nCost: #{ent.cost}\n#{ent.description}" action.block = proc { @parent.director.game.set_tool(data[:tool], data) } else diff --git a/lib/map.rb b/lib/map.rb index 27b00e5..6c1a0e4 100644 --- a/lib/map.rb +++ b/lib/map.rb @@ -4,7 +4,8 @@ class IMICRTS def initialize(map_file:) @tiled_map = TiledMap.new(map_file) - @width, @height = @tiled_map.width, @tiled_map.height + @width = @tiled_map.width + @height = @tiled_map.height @tile_size = @tiled_map.tile_size @tiles = {} @@ -62,6 +63,27 @@ class IMICRTS end end + def render_preview + Gosu.render(1024, 1024, retro: true) do + Gosu.scale(1024 / (@width.to_f * @tile_size), 1024 / (@height.to_f * @tile_size)) do + @height.times do |y| + @width.times do |x| + tile = tile_at(x, y) + ore = ore_at(x, y) + + tile.image.draw(tile.position.x, tile.position.y, tile.position.z) if tile + ore.image.draw(ore.position.x, ore.position.y, ore.position.z) if ore + end + end + + @spawnpoints.each do |spawnpoint| + Gosu.draw_circle(spawnpoint.x, spawnpoint.y, @tile_size.to_f / 4 * 3, 36, Gosu::Color::BLACK, Float::INFINITY) + Gosu.draw_circle(spawnpoint.x, spawnpoint.y, @tile_size.to_f / 2, 36, Gosu::Color::GRAY, Float::INFINITY) + end + end + end + end + def visible_tiles(camera) _tiles = [] diff --git a/lib/states/menus/main_menu.rb b/lib/states/menus/main_menu.rb index ddd3a9b..5c844ea 100644 --- a/lib/states/menus/main_menu.rb +++ b/lib/states/menus/main_menu.rb @@ -8,12 +8,11 @@ class IMICRTS stack(width: IMICRTS::MENU_WIDTH, height: 1.0, padding: IMICRTS::MENU_PADDING) do background [0xff555555, Gosu::Color::GRAY] label IMICRTS::NAME, text_size: 78, margin: 20 - button("Campaign", width: 1.0, enabled: false) do + button("Campaign", width: 1.0, enabled: false, tip: "No campaign available, yet...") do push_state(CampaignMenu) end button("Skirmish", width: 1.0) do - # push_state(SoloPlayMenu) push_state(SoloLobbyMenu) end diff --git a/lib/states/menus/solo_lobby_menu.rb b/lib/states/menus/solo_lobby_menu.rb index 47e7d10..5bdb9bd 100644 --- a/lib/states/menus/solo_lobby_menu.rb +++ b/lib/states/menus/solo_lobby_menu.rb @@ -3,7 +3,7 @@ class IMICRTS def setup background [0xff7b6ead, 0xff7a0d71, 0xff7a0d71, 0xff7b6ead] - stack(width: 0.5, min_width: 720, height: 1.0, padding: IMICRTS::MENU_PADDING) do + stack(width: 1.0, height: 1.0, padding: IMICRTS::MENU_PADDING) do background [0xff555555, Gosu::Color::GRAY] label "Lobby", text_size: 78, margin: 20 @@ -51,7 +51,12 @@ class IMICRTS # TODO: Show preview image label "Map" @map_name = list_box items: [:test_map], choose: :test_map, width: 1.0 - image "#{GAME_ROOT_PATH}/assets/logo.png", width: 1.0 + @map_name.subscribe(:changed) do |sender, value| + map = Map.new(map_file: "maps/#{value}.tmx") + @map_preview.instance_variable_set(:"@image", map.render_preview) + @map_preview.recalculate + end + @map_preview = image "#{GAME_ROOT_PATH}/assets/logo.png", width: 1.0, border_thickness: 2, border_color: Gosu::Color::BLACK, background: Gosu::Color::GRAY end end diff --git a/lib/tiled_map.rb b/lib/tiled_map.rb index affdf14..5f02d48 100644 --- a/lib/tiled_map.rb +++ b/lib/tiled_map.rb @@ -1,11 +1,12 @@ class IMICRTS class TiledMap - attr_reader :width, :height, :tile_size - attr_reader :layers, :tilesets, :spawnpoints + attr_reader :width, :height, :tile_size, :layers, :tilesets, :spawnpoints + def initialize(map_file) @xml = Nokogiri::XML(File.read("#{IMICRTS::ASSETS_PATH}/#{map_file}")) - @width, @height = 0, 0 + @width = 0 + @height = 0 @tile_size = 32 @layers = [] @@ -17,7 +18,8 @@ class IMICRTS parse terrain = @layers.find { |layer| layer.name.downcase == "terrain" } - @width, @height = terrain.width, terrain.height + @width = terrain.width + @height = terrain.height @tilesets.each do |tileset| @tiles.push(*tileset.tiles) @@ -36,11 +38,12 @@ class IMICRTS end @xml.search("//objectgroup").each do |objectgroup| - if objectgroup.attr("name") == "spawns" - objectgroup.children.each do |object| - next unless object.attr("name") && object.attr("name").downcase.strip == "spawn" - @spawnpoints << SpawnPoint.new(object) - end + next unless objectgroup.attr("name") == "spawns" + + objectgroup.children.each do |object| + next unless object.attr("name") && object.attr("name").downcase.strip == "spawn" + + @spawnpoints << SpawnPoint.new(object) end end end @@ -55,9 +58,11 @@ class IMICRTS class Layer attr_reader :name, :width, :height + def initialize(xml_layer) @name = xml_layer["name"] - @width, @height = Integer(xml_layer["width"]), Integer(xml_layer["height"]) + @width = Integer(xml_layer["width"]) + @height = Integer(xml_layer["height"]) @data = [] xml_layer.css("data").inner_html.each_line do |line| @@ -74,8 +79,8 @@ class IMICRTS class TileSet - attr_reader :first_gid, :name, :tile_width, :tile_height, :tile_count, :columns, :rows - attr_reader :tiles + attr_reader :first_gid, :name, :tile_width, :tile_height, :tile_count, :columns, :rows, :tiles + Tile = Struct.new(:image, :data) def initialize(xml_tileset) @first_gid = nil @@ -116,7 +121,7 @@ class IMICRTS end path = tileset.search("//image").first.attributes.detect { |a, v| a == "source" }.last.value - images = Gosu::Image.load_tiles("#{IMICRTS::ASSETS_PATH}/tilesets/#{path}", @tile_width, @tile_height, retro: true) + images = Gosu::Image.load_tiles("#{IMICRTS::ASSETS_PATH}/tilesets/#{path}", @tile_width, @tile_height, retro: true, tileable: true) tileset.search("//tile").each_with_index do |xml, index| tile = Tile.new @@ -136,8 +141,10 @@ class IMICRTS class SpawnPoint attr_reader :x, :y + def initialize(xml_object) - @x, @y = Integer(xml_object.attr("x")), Integer(xml_object.attr("y")) + @x = Integer(xml_object.attr("x")) + @y = Integer(xml_object.attr("y")) end end end