navmesh.gd 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. extends Navigation
  2. const SPEED = 10.0
  3. var camrot = 0.0
  4. var m = SpatialMaterial.new()
  5. var path = []
  6. var show_path = true
  7. onready var robot = get_node("RobotBase")
  8. onready var camera = get_node("CameraBase/Camera")
  9. func _ready():
  10. set_process_input(true)
  11. m.flags_unshaded = true
  12. m.flags_use_point_size = true
  13. m.albedo_color = Color.white
  14. func _physics_process(delta):
  15. var direction = Vector3()
  16. # We need to scale the movement speed by how much delta has passed,
  17. # otherwise the motion won't be smooth.
  18. var step_size = delta * SPEED
  19. if path.size() > 0:
  20. # Direction is the difference between where we are now
  21. # and where we want to go.
  22. var destination = path[0]
  23. direction = destination - robot.translation
  24. # If the next node is closer than we intend to 'step', then
  25. # take a smaller step. Otherwise we would go past it and
  26. # potentially go through a wall or over a cliff edge!
  27. if step_size > direction.length():
  28. step_size = direction.length()
  29. # We should also remove this node since we're about to reach it.
  30. path.remove(0)
  31. # Move the robot towards the path node, by how far we want to travel.
  32. # Note: For a KinematicBody, we would instead use move_and_slide
  33. # so collisions work properly.
  34. robot.translation += direction.normalized() * step_size
  35. # Lastly let's make sure we're looking in the direction we're traveling.
  36. # Clamp y to 0 so the robot only looks left and right, not up/down.
  37. direction.y = 0
  38. if direction:
  39. # Direction is relative, so apply it to the robot's location to
  40. # get a point we can actually look at.
  41. var look_at_point = robot.translation + direction.normalized()
  42. # Make the robot look at the point.
  43. robot.look_at(look_at_point, Vector3.UP)
  44. func _unhandled_input(event):
  45. if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
  46. var from = camera.project_ray_origin(event.position)
  47. var to = from + camera.project_ray_normal(event.position) * 1000
  48. var target_point = get_closest_point_to_segment(from, to)
  49. # Set the path between the robots current location and our target.
  50. path = get_simple_path(robot.translation, target_point, true)
  51. if show_path:
  52. draw_path(path)
  53. if event is InputEventMouseMotion:
  54. if event.button_mask & (BUTTON_MASK_MIDDLE + BUTTON_MASK_RIGHT):
  55. camrot += event.relative.x * 0.005
  56. get_node("CameraBase").set_rotation(Vector3(0, camrot, 0))
  57. print("Camera Rotation: ", camrot)
  58. func draw_path(path_array):
  59. var im = get_node("Draw")
  60. im.set_material_override(m)
  61. im.clear()
  62. im.begin(Mesh.PRIMITIVE_POINTS, null)
  63. im.add_vertex(path_array[0])
  64. im.add_vertex(path_array[path_array.size() - 1])
  65. im.end()
  66. im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
  67. for x in path:
  68. im.add_vertex(x)
  69. im.end()