remove bug and add spider - add advanced flow field debug vis
This commit is contained in:
@@ -1,80 +1,117 @@
|
||||
extends Node3D
|
||||
class_name FlowFieldNav
|
||||
|
||||
@onready var player: Player = %player
|
||||
@export_category("Debug Visualization")
|
||||
@export_group("Grid Center")
|
||||
@export var show_grid_center: bool = false
|
||||
@export_color_no_alpha var grid_center_color: Color = Color(1, 1, 1, 1)
|
||||
@export var grid_center_size: float = 0.5
|
||||
@export_group("Border")
|
||||
@export var show_border: bool = false
|
||||
@export_color_no_alpha var border_color: Color = Color(1, 1, 1, 1)
|
||||
@export_group("Cells")
|
||||
@export_subgroup("X Lines")
|
||||
@export var show_x_lines: bool = false
|
||||
@export_color_no_alpha var x_lines_color: Color = Color(1, 1, 1)
|
||||
@export_subgroup("Z Lines")
|
||||
@export var show_z_lines: bool = false
|
||||
@export_color_no_alpha var z_lines_color: Color = Color(1, 1, 1)
|
||||
@export_group("Arrows")
|
||||
@export var show_arrows: bool = false
|
||||
@export_color_no_alpha var cell_center_color: Color = Color(1, 1, 1)
|
||||
@export var cell_center_size: float = 0.2
|
||||
|
||||
|
||||
@export_group("Debug")
|
||||
@export var debugVisu: bool = true
|
||||
@export var line_color: Color = Color(1, 1, 1, 1)
|
||||
|
||||
@export_group("Grid")
|
||||
@export var grid_size: int = 10
|
||||
@export_category("Grid")
|
||||
@export var cell_size: float = 1.0
|
||||
@export_group("Grid Size", "grid_size_")
|
||||
@export var grid_size_x: int = 2
|
||||
@export var grid_size_z: int = 5
|
||||
|
||||
var _cells: Dictionary
|
||||
var grid_offset: Vector3
|
||||
|
||||
var target_position: Vector3
|
||||
var target_cell: FlowFieldCell
|
||||
|
||||
var lines: PackedVector3Array
|
||||
var flow_direction_list: Array[Vector2] = []
|
||||
|
||||
func _ready() -> void:
|
||||
var half_size = (grid_size * cell_size) * 0.5
|
||||
grid_offset = Vector3(-half_size + (cell_size * 0.5), 0, -half_size + (cell_size * 0.5))
|
||||
create_grid()
|
||||
|
||||
# Vertikale Linien
|
||||
for x in range(grid_size + 1):
|
||||
var pos_x = x * cell_size + grid_offset.x - (cell_size * 0.5)
|
||||
lines.append(Vector3(pos_x, 0, grid_offset.z - (cell_size * 0.5)))
|
||||
lines.append(Vector3(pos_x, 0, grid_offset.z + grid_size * cell_size - (cell_size * 0.5)))
|
||||
|
||||
# Horizontale Linien
|
||||
for z in range(grid_size + 1):
|
||||
var pos_z = z * cell_size + grid_offset.z - (cell_size * 0.5)
|
||||
lines.append(Vector3(grid_offset.x - (cell_size * 0.5), 0, pos_z))
|
||||
lines.append(Vector3(grid_offset.x + grid_size * cell_size - (cell_size * 0.5), 0, pos_z))
|
||||
|
||||
func create_grid() -> void:
|
||||
_cells.clear()
|
||||
for x in range(grid_size):
|
||||
for z in range(grid_size):
|
||||
var cell = FlowFieldCell.new()
|
||||
var world_x = x * cell_size + grid_offset.x
|
||||
var world_z = z * cell_size + grid_offset.z
|
||||
cell.position = Vector3(world_x, 0, world_z)
|
||||
cell.cell_size = cell_size
|
||||
_cells[Vector3(x, 0, z)] = cell # Dictionary mit Grid-Koordinaten als Key
|
||||
|
||||
for x in range(grid_size_x):
|
||||
for z in range(grid_size_z):
|
||||
flow_direction_list.append(Vector2(0.0, 0.0))
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
target_position = Vector3(player.position.x, 0, player.position.z)
|
||||
var new_target_cell = get_cell_from_pos(target_position)
|
||||
if target_cell != null and target_cell != new_target_cell:
|
||||
target_cell.is_target = false
|
||||
new_target_cell.is_target = true
|
||||
target_cell = new_target_cell
|
||||
else:
|
||||
target_cell = new_target_cell
|
||||
target_cell.is_target = true
|
||||
_draw_debug()
|
||||
|
||||
|
||||
|
||||
func _draw_debug():
|
||||
if show_grid_center:
|
||||
DebugDraw3D.draw_sphere(position, grid_center_size, grid_center_color)
|
||||
if show_border:
|
||||
_draw_border()
|
||||
|
||||
if debugVisu:
|
||||
_debug_draw_grid()
|
||||
for cell in _cells.values():
|
||||
cell.debug_process()
|
||||
|
||||
for cell in _cells.values():
|
||||
cell.update(target_position)
|
||||
if show_x_lines:
|
||||
_draw_x_lines()
|
||||
|
||||
if show_z_lines:
|
||||
_draw_z_lines()
|
||||
|
||||
func _debug_draw_grid() -> void:
|
||||
DebugDraw3D.draw_lines(lines, line_color)
|
||||
if show_arrows:
|
||||
_draw_arrows()
|
||||
|
||||
func _draw_border():
|
||||
var half_size_x = (grid_size_x * cell_size) * 0.5
|
||||
var half_size_z = (grid_size_z * cell_size) * 0.5
|
||||
|
||||
func get_cell_from_pos(world_pos: Vector3) -> FlowFieldCell:
|
||||
var grid_x = int((world_pos.x - grid_offset.x) / cell_size)
|
||||
var grid_z = int((world_pos.z - grid_offset.z) / cell_size)
|
||||
var cell_pos = Vector3(grid_x, 0, grid_z)
|
||||
var top_left = position - Vector3(half_size_x, 0 , half_size_z)
|
||||
var top_right = position + Vector3(half_size_x, 0, -half_size_z)
|
||||
var bottom_left = position - Vector3(half_size_x, 0, -half_size_z)
|
||||
var bottom_right = position + Vector3(half_size_x, 0, half_size_z)
|
||||
|
||||
var lines: PackedVector3Array
|
||||
lines.append_array(PackedVector3Array([top_left, top_right]))
|
||||
lines.append_array(PackedVector3Array([top_right, bottom_right]))
|
||||
lines.append_array(PackedVector3Array([bottom_right, bottom_left]))
|
||||
lines.append_array(PackedVector3Array([bottom_left, top_left]))
|
||||
DebugDraw3D.draw_lines(lines, border_color)
|
||||
|
||||
return _cells.get(cell_pos, null)
|
||||
func _draw_x_lines():
|
||||
var half_size_x = (grid_size_x * cell_size) * 0.5
|
||||
var half_size_z = (grid_size_z * cell_size) * 0.5
|
||||
var lines: PackedVector3Array
|
||||
for x in range(grid_size_x - 1):
|
||||
var start = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, half_size_z)
|
||||
var end = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, -half_size_z)
|
||||
lines.append_array(PackedVector3Array([start, end]))
|
||||
DebugDraw3D.draw_lines(lines, x_lines_color)
|
||||
|
||||
func _draw_z_lines():
|
||||
var half_size_x = (grid_size_x * cell_size) * 0.5
|
||||
var half_size_z = (grid_size_z * cell_size) * 0.5
|
||||
var lines: PackedVector3Array
|
||||
for z in range(grid_size_z - 1):
|
||||
var start = position - Vector3(half_size_x, 0, half_size_z - ((z + 1) * cell_size))
|
||||
var end = position - Vector3(-half_size_x, 0, half_size_z - ((z + 1) * cell_size))
|
||||
lines.append_array(PackedVector3Array([start, end]))
|
||||
DebugDraw3D.draw_lines(lines, z_lines_color)
|
||||
|
||||
func _draw_arrows():
|
||||
var half_size_x = (grid_size_x * cell_size) * 0.5
|
||||
var half_size_z = (grid_size_z * cell_size) * 0.5
|
||||
var points: PackedVector3Array
|
||||
for x in range(grid_size_x):
|
||||
for z in range(grid_size_z):
|
||||
var cell_center = position + Vector3(
|
||||
(-half_size_x + (x * cell_size) + (cell_size * 0.5)),
|
||||
0,
|
||||
(-half_size_z + (z * cell_size) + (cell_size * 0.5))
|
||||
)
|
||||
points.append(cell_center)
|
||||
#var index = z * grid_size_x + x
|
||||
#if flow_direction_list[index].length() > 0.0:
|
||||
#pass #zeichne pfeil
|
||||
#else:
|
||||
#DebugDraw3D.draw_sphere(cell_center, cell_center_size, cell_center_color)
|
||||
DebugDraw3D.draw_points(points, DebugDraw3D.POINT_TYPE_SQUARE, cell_center_size, cell_center_color)
|
||||
|
||||
func _get_index_from_position(position: Vector3) -> int:
|
||||
return 0
|
||||
|
||||
func get_direction_from_position(position: Vector3) -> Vector3:
|
||||
var index = _get_index_from_position(position)
|
||||
return Vector3(flow_direction_list[index].x, 0, flow_direction_list[index].y)
|
||||
|
||||
Reference in New Issue
Block a user