More work on pathfinding support

This commit is contained in:
2026-09-28 15:22:42 -05:00
parent 70d270688f
commit 904ac78d89
3 changed files with 75 additions and 29 deletions

View File

@@ -1,14 +1,47 @@
module CyberarmEngine
module Pathfinding
class Graph
Location = Data.define(:x, :y, :z, :cost)
class Node
KEY_FORMAT = "%s:%s:%s".freeze
def initialize(edges: {})
@edges = edges
attr_reader :x, :y, :z, :cost, :key, :edge_nodes
def initialize(x:, y:, z: 0, cost: 0)
@x = x
@y = y
@z = z
@cost = cost
@key = format(KEY_FORMAT, @x, @y, @z)
@edge_nodes = []
end
def add_edge_node(node)
index = @edge_nodes.find_index { |n| n.key == node.key }
if index
@edge_nodes[index] = node
else
@edge_nodes.push(node)
end
end
def remove_edge_node(node)
index = @edge_nodes.find_index { |n| n.key == node.key }
return unless index
@edge_nodes.delete(index)
end
end
def neighbors(location)
@edges[location]
def initialize(nodes: {})
@nodes = nodes
end
def neighbors(node)
node.edge_nodes
end
end
end