mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2026-11-04 08:23:57 +00:00
Added .idea to gitignore, first pass at adding intregrated pathfindiing support using redblobgames.com tutorials as reference
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
/.idea
|
||||||
/.bundle/
|
/.bundle/
|
||||||
/.yardoc
|
/.yardoc
|
||||||
/_yardoc/
|
/_yardoc/
|
||||||
|
|||||||
15
lib/cyberarm_engine/pathfinding/graph.rb
Normal file
15
lib/cyberarm_engine/pathfinding/graph.rb
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class Graph
|
||||||
|
Location = Data.define(:x, :y, :z, :cost)
|
||||||
|
|
||||||
|
def initialize(edges: {})
|
||||||
|
@edges = edges
|
||||||
|
end
|
||||||
|
|
||||||
|
def neighbors(location)
|
||||||
|
@edges[location]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
6
lib/cyberarm_engine/pathfinding/graph_search.rb
Normal file
6
lib/cyberarm_engine/pathfinding/graph_search.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class GraphSearch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class AStarSearch < GraphSearch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class BreadthFirstSearch < GraphSearch
|
||||||
|
def search(graph:, start:, goal: nil)
|
||||||
|
frontier = []
|
||||||
|
came_from = {}
|
||||||
|
|
||||||
|
frontier.put(start)
|
||||||
|
came_from[start] = start
|
||||||
|
|
||||||
|
while(current = frontier.shift)
|
||||||
|
# early exit if we reach our goal
|
||||||
|
break if current == goal
|
||||||
|
|
||||||
|
graph.neighbors(current).each do |neighbor|
|
||||||
|
next if came_from[neighbor]
|
||||||
|
|
||||||
|
frontier.put(neighbor)
|
||||||
|
came_from[neighbor] = current
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
came_from
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class DijkstraSearch < GraphSearch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
33
lib/cyberarm_engine/pathfinding/graphs/grid.rb
Normal file
33
lib/cyberarm_engine/pathfinding/graphs/grid.rb
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
module Pathfinding
|
||||||
|
class Grid < Graph
|
||||||
|
Cell = Data.define(:location, :blocker, :cost)
|
||||||
|
|
||||||
|
# nodes: a 2d array of nodes to convert to list of edges
|
||||||
|
def self.edges_from_world_grid(nodes:)
|
||||||
|
edges = {}
|
||||||
|
|
||||||
|
nodes.each_index do |y, y_index|
|
||||||
|
y.each_with_index do |x_index|
|
||||||
|
node = nodes[y_index][x_index]
|
||||||
|
next unless node
|
||||||
|
|
||||||
|
left = nodes[y_index]&.[x_index - 1]
|
||||||
|
right = nodes[y_index]&.[x_index + 1]
|
||||||
|
top = nodes[y_index - 1]&.[x_index]
|
||||||
|
bottom = nodes[y_index + 1]&.[x_index]
|
||||||
|
|
||||||
|
location = Location.new(x_index, y_index, 0, 0)
|
||||||
|
edges[location] = []
|
||||||
|
edges[location].push(Location.new(x_index - 1, y_index, 0, 0)) if left
|
||||||
|
edges[location].push(Location.new(x_index + 1, y_index, 0, 0)) if right
|
||||||
|
edges[location].push(Location.new(x_index, y_index - 1, 0, 0)) if top
|
||||||
|
edges[location].push(Location.new(x_index, y_index + 1, 0, 0)) if bottom
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
new(edges: edges)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user