From 202331474b2478bd070d70d0f0935d7eb56a7d2b Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Fri, 25 Oct 2019 13:56:54 -0500 Subject: [PATCH] Improved pathing debug draw --- lib/entity.rb | 19 ++++++++++++------- lib/pathfinding/base_pathfinder.rb | 8 ++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/entity.rb b/lib/entity.rb index f6fbfa1..3018d14 100644 --- a/lib/entity.rb +++ b/lib/entity.rb @@ -96,7 +96,10 @@ class IMICRTS end def update - rotate_towards(@target) if @target && @movement + if @movement + # rotate_towards(@target) if @target + rotate_towards(@pathfinder.path_current_node.tile.position) if @pathfinder && @pathfinder.path_current_node + end if @movement follow_path @@ -113,7 +116,7 @@ class IMICRTS def follow_path if @pathfinder && node = @pathfinder.path_current_node - @pathfinder.path_next_node if @pathfinder.at_current_path_node?(@position) + @pathfinder.path_next_node if @pathfinder.at_current_path_node?(self) @position -= (@position.xy - node.tile.position.xy).normalized * @speed end end @@ -130,21 +133,23 @@ 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 @pathfinder && Setting.enabled?(:debug_pathfinding) && @pathfinder.path.first + if @pathfinder && @pathfinder.path_current_node && 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, + @pathfinder.path_current_node.tile.position.x, @pathfinder.path_current_node.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 + node = @pathfinder.path_current_node + @pathfinder.path[@pathfinder.path_current_node_index..@pathfinder.path.size - 1].each do |next_node| + if 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 ) + + node = next_node end end end diff --git a/lib/pathfinding/base_pathfinder.rb b/lib/pathfinding/base_pathfinder.rb index 12aa27f..acca276 100644 --- a/lib/pathfinding/base_pathfinder.rb +++ b/lib/pathfinding/base_pathfinder.rb @@ -74,13 +74,17 @@ class IMICRTS @path[@path_current_node] end + def path_current_node_index + @path_current_node + end + def path_next_node @path_current_node += 1 end - def at_current_path_node?(position) + def at_current_path_node?(entity) if node = path_current_node - position.distance(node.tile.position) < @map.tile_size / 2 + entity.position.distance(node.tile.position) <= @entity.radius else true end