From bcbe70d8d869bdc53fe00437bb2df8a73984e83b Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Thu, 24 Oct 2019 18:03:56 -0500 Subject: [PATCH] Added Settings --- README.md | 3 +-- i-mic-rts.rb | 5 +++++ lib/director.rb | 4 ++-- lib/entity.rb | 20 +++++++++++++----- lib/pathfinding/base_pathfinder.rb | 12 +++++------ lib/states/menus/settings_menu.rb | 32 ++++++++++++++++++++++++++++- lib/states/menus/solo_lobby_menu.rb | 2 +- lib/window.rb | 6 +++--- 8 files changed, 64 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cf8ba78..2325460 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,4 @@ Building a Command & Conquer style RTS in Ruby * `ruby i-mic-rts.rb` ### Options -* `--debug` Jump straight to Game -* `--fast` Skip intro \ No newline at end of file +* `--debug-game` Jump straight to Game \ No newline at end of file diff --git a/i-mic-rts.rb b/i-mic-rts.rb index 68af85d..71278db 100755 --- a/i-mic-rts.rb +++ b/i-mic-rts.rb @@ -8,10 +8,13 @@ end require "nokogiri" +require "json" + require_relative "lib/version" require_relative "lib/errors" require_relative "lib/window" require_relative "lib/camera" +require_relative "lib/setting" require_relative "lib/states/boot" require_relative "lib/states/game" @@ -36,4 +39,6 @@ require_relative "lib/director" require_relative "lib/player" require_relative "lib/connection" +IMICRTS::Setting.setup + IMICRTS::Window.new(width: Gosu.screen_width / 4 * 3, height: Gosu.screen_height / 4 * 3, fullscreen: false, resizable: true).show \ No newline at end of file diff --git a/lib/director.rb b/lib/director.rb index d4d1772..c9afeff 100644 --- a/lib/director.rb +++ b/lib/director.rb @@ -57,9 +57,9 @@ class IMICRTS @players.find { |player| player.id == id } end - def find_path(player:, entity:, goal:, travels_along: :ground, allow_diagonal: true, klass: IMICRTS::Pathfinder::BasePathfinder) + def find_path(player:, entity:, goal:, travels_along: :ground, allow_diagonal: Setting.enabled?(:debug_pathfinding_allow_diagonal), klass: IMICRTS::Pathfinder::BasePathfinder) if klass.cached_path(entity, goal, travels_along) - puts "using a cached path!" if true#Setting.enabled?(:debug_mode) + puts "using a cached path!" if Setting.enabled?(:debug_mode) return klass.cached_path(entity, goal, travels_along) end diff --git a/lib/entity.rb b/lib/entity.rb index 9040529..7f8af7b 100644 --- a/lib/entity.rb +++ b/lib/entity.rb @@ -67,7 +67,7 @@ class IMICRTS def target=(entity) @target = entity - @path = @director.find_path(player: @player, entity: self, goal: @target) + @pathfinder = @director.find_path(player: @player, entity: self, goal: @target) end def hit?(x_or_vector, y = nil) @@ -119,11 +119,21 @@ class IMICRTS def draw_gizmos Gosu.draw_rect(@position.x - @radius, @position.y - (@radius + 2), @radius * 2, 2, Gosu::Color::GREEN, ZOrder::ENTITY_GIZMOS) - if @path - @path.path.each_with_index do |node, i| - next_node = @path.path.dig(i + 1) + if @pathfinder && Setting.enabled?(:debug_pathfinding) && @pathfinder.path.first + Gosu.draw_line( + @position.x, @position.y, Gosu::Color::RED, + @pathfinder.path.first.tile.position.x, @pathfinder.path.first.tile.position.y, Gosu::Color::RED, + ZOrder::ENTITY_GIZMOS + ) + + @pathfinder.path.each_with_index do |node, i| + next_node = @pathfinder.path.dig(i + 1) if next_node - Gosu.draw_line(node.tile.position.x, node.tile.position.y, Gosu::Color::RED, next_node.tile.position.x, next_node.tile.position.y, Gosu::Color::RED, ZOrder::ENTITY_GIZMOS) + Gosu.draw_line( + node.tile.position.x, node.tile.position.y, Gosu::Color::RED, + next_node.tile.position.x, next_node.tile.position.y, Gosu::Color::RED, + ZOrder::ENTITY_GIZMOS + ) end end end diff --git a/lib/pathfinding/base_pathfinder.rb b/lib/pathfinding/base_pathfinder.rb index 7afe835..edf003b 100644 --- a/lib/pathfinding/base_pathfinder.rb +++ b/lib/pathfinding/base_pathfinder.rb @@ -4,8 +4,8 @@ class IMICRTS Node = Struct.new(:tile, :parent, :distance, :cost) CACHE = {} - def self.cached_path(source, goal, travels_along) - found_path = CACHE.dig(travels_along, source, goal) + def self.cached_path(entity, goal, travels_along) + found_path = CACHE.dig(travels_along, entity, goal) if found_path found_path = nil unless found_path.valid? end @@ -55,7 +55,7 @@ class IMICRTS @current_node = create_node(position.x, position.y) unless @current_node - puts "Failed to find path!" if true + puts "Failed to find path!" if Setting.enabled?(:debug_mode) return end @@ -65,7 +65,7 @@ class IMICRTS find - Pathfinder.cache_path(self) if @path.size > 0 && false#Setting.enabled?(:cache_paths) + self.class.cache_path(self) if @path.size > 0 && true#Setting.enabled?(:debug_cache_paths) end # Checks if Map still has all of paths required tiles @@ -87,7 +87,7 @@ class IMICRTS end if @depth >= @max_depth - puts "Failed to find path from: #{@source.x}:#{@source.y} (#{@map.grid.dig(@source.x,@source.y).element.class}) to: #{@goal.position.x}:#{@goal.position.y} (#{@goal.element.class}) [#{@depth}/#{@max_depth} depth]" if true#Setting.enabled?(:debug_mode) + puts "Failed to find path from: #{@source.x}:#{@source.y} (#{@map.grid.dig(@source.x,@source.y).element.class}) to: #{@goal.position.x}:#{@goal.position.y} (#{@goal.element.class}) [#{@depth}/#{@max_depth} depth]" if Setting.enabled?(:debug_mode) end end @@ -112,7 +112,7 @@ class IMICRTS @path.reverse! @seeking = false - puts "Generated path with #{@path.size} steps, #{@created_nodes} nodes created. [#{@depth}/#{@max_depth} depth]" if true#Setting.enabled?(:debug_mode) + puts "Generated path with #{@path.size} steps, #{@created_nodes} nodes created. [#{@depth}/#{@max_depth} depth]" if Setting.enabled?(:debug_mode) return end diff --git a/lib/states/menus/settings_menu.rb b/lib/states/menus/settings_menu.rb index 15c3f43..f8c0566 100644 --- a/lib/states/menus/settings_menu.rb +++ b/lib/states/menus/settings_menu.rb @@ -7,12 +7,42 @@ class IMICRTS background [0xff555555, Gosu::Color::GRAY] label "Settings", text_size: 78, margin: 20 - label "Nothing to see here, move along." + + stack(width: 1.0) do + background 0xff030303 + + label "Debug Settings" + @debug_mode = check_box "Debug Mode", checked: Setting.enabled?(:debug_mode) + @debug_info_bar = check_box "Show Debug Info Bar", checked: Setting.enabled?(:debug_info_bar) + @debug_pathfinding = check_box "Debug Pathfinding", checked: Setting.enabled?(:debug_pathfinding) + @debug_pathfinding_allow_diagonal = check_box "Allow Diagonal Paths", checked: Setting.enabled?(:debug_pathfinding_allow_diagonal) + end + + button("Save and Close", width: 1.0, margin_top: 20) do + if valid_options? + save_settings + + push_state(MainMenu) + end + end button("Back", width: 1.0, margin_top: 20) do push_state(MainMenu) end end end + + def valid_options? + true + end + + def save_settings + Setting.set(:debug_mode, @debug_mode.value) + Setting.set(:debug_info_bar, @debug_info_bar.value) + Setting.set(:debug_pathfinding, @debug_pathfinding.value) + Setting.set(:debug_pathfinding_allow_diagonal, @debug_pathfinding_allow_diagonal.value) + + Setting.save! + end end end \ No newline at end of file diff --git a/lib/states/menus/solo_lobby_menu.rb b/lib/states/menus/solo_lobby_menu.rb index c9787fc..2b18b07 100644 --- a/lib/states/menus/solo_lobby_menu.rb +++ b/lib/states/menus/solo_lobby_menu.rb @@ -18,7 +18,7 @@ class IMICRTS stack do case elements[index] when :edit_line - edit_line "Novice" + edit_line Setting.get(:player_name) when :button button item when :toggle_button diff --git a/lib/window.rb b/lib/window.rb index 56b25af..95b4dc7 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -6,10 +6,10 @@ class IMICRTS @mouse = CyberarmEngine::Vector.new self.caption = "#{IMICRTS::NAME} (#{IMICRTS::VERSION} #{IMICRTS::VERSION_NAME})" - if ARGV.join.include?("--fast") - push_state(MainMenu) - elsif ARGV.join.include?("--debug") + if ARGV.join.include?("--debug-game") push_state(Game) + elsif Setting.enabled?(:skip_intro) + push_state(MainMenu) else push_state(Boot) end