Swarm/MovementBehaviour/Prefabs/MovementBehaviourBee.gd

39 lines
1.6 KiB
GDScript3
Raw Normal View History

2024-03-04 16:04:40 +01:00
extends MovementBehaviour
class_name MovementBehaviourBee
func can_reach(start: Vector2i, target: Vector2i, map: HexGrid) -> bool:
# if we have 5 potential spaces it can never be blocked
var cubepos: HexGrid.CubeCoordinates = map.axial_to_cube(HexGrid.AxialCoordinates.new(start.x, start.y))
var cubecoord = map.axial_to_cube(HexGrid.AxialCoordinates.new(target.x, target.y))
var cubetest: HexGrid.CubeCoordinates = map.axial_to_cube(HexGrid.AxialCoordinates.new(0, 0))
cubetest.q = cubecoord.q - cubepos.q
cubetest.r = cubecoord.r - cubepos.r
cubetest.s = cubecoord.s - cubepos.s
var left = get_left_neighbour(Vector3i(cubetest.q, cubetest.r, cubetest.s))
var right = get_right_neighbour(Vector3i(cubetest.q, cubetest.r, cubetest.s))
var left_coord = map.cube_to_axial(HexGrid.CubeCoordinates.new(left.x + cubepos.q, left.y + cubepos.r, left.z + cubepos.s))
var right_coord = map.cube_to_axial(HexGrid.CubeCoordinates.new(right.x + cubepos.q, right.y + cubepos.r, right.z + cubepos.s))
return map.is_cell_empty(Vector2i(left_coord.q, left_coord.r)) or map.is_cell_empty(Vector2i(right_coord.q, right_coord.r))
func get_left_neighbour(pos: Vector3i) -> Vector3i:
return Vector3(-pos.z, -pos.x, -pos.y)
func get_right_neighbour(pos: Vector3i) -> Vector3i:
return Vector3(-pos.y, -pos.z, -pos.x)
func get_available_spaces(pos: Vector2i, map: HexGrid) -> Array[Vector2i]:
var potential_spaces = map.get_empty_neighbours(pos)
var target_spaces: Array[Vector2i] = []
for neighbour in potential_spaces:
if can_reach(pos, neighbour, map):
target_spaces.append(neighbour)
return target_spaces