Meldewesen runs towards player when seeing it

The NPC got some functionality which now allows it to tell the
PathNavigator where it wants to go, in addition to the default
behavior.
This commit is contained in:
karl 2019-11-11 10:44:20 +01:00
parent 93f59ec131
commit 9fd3087fdc
3 changed files with 19 additions and 8 deletions

View File

@ -15,18 +15,17 @@ func _ready():
_visibility.connect("body_exited", self, "_on_body_exited_visibility") _visibility.connect("body_exited", self, "_on_body_exited_visibility")
func _on_body_entered_visibility(body: Node): func _on_body_entered_visibility(body: PhysicsBody):
Logger.trace("Meldewesen seeing %s" % [body]) Logger.trace("Meldewesen seeing %s" % [body])
if body.is_in_group("Player"): if body.is_in_group("Player"):
Logger.info("Seeing player!") Logger.info("Seeing player!")
# TODO: Check if the Player is in an area where they shouldn't be # TODO: Check if the Player is in an area where they shouldn't be
# TODO: Follow the Player current_target = body.transform.origin
func _on_body_exited_visibility(body: Node): func _on_body_exited_visibility(body: PhysicsBody):
if body.is_in_group("Player"): if body.is_in_group("Player"):
print("Stopped seeing player!")
Logger.info("Stopped seeing player!") Logger.info("Stopped seeing player!")
# TODO: Stop following the Player current_target = null

View File

@ -1,2 +1,13 @@
extends KinematicBody extends KinematicBody
class_name NPC class_name NPC
var current_target = null
func set_position(position: Vector3):
transform.origin = position
func move_towards(direction_times_speed: Vector3):
move_and_slide(direction_times_speed)

View File

@ -34,20 +34,21 @@ func _restart_navigation():
body = get_node(body_nodepath) as KinematicBody body = get_node(body_nodepath) as KinematicBody
# Initialize the position # Initialize the position
body.transform.origin = curve.get_point_position(0) body.set_position(curve.get_point_position(0))
# Get the first goal # Get the first goal
_get_new_navigation() _get_new_navigation()
func _process(delta): func _process(delta):
var current_goal = _get_current_goal() # Use either the NPC's current target or the paths next goal
var current_goal = body.current_target if body.current_target else _get_current_goal()
# Move towards the current goal # Move towards the current goal
var direction: Vector3 = (current_goal - _get_body_position()) var direction: Vector3 = (current_goal - _get_body_position())
var direction_normalized: Vector3 = direction.normalized() var direction_normalized: Vector3 = direction.normalized()
body.move_and_slide(direction_normalized * speed) body.move_towards(direction_normalized * speed)
# Look towards that goal as well # Look towards that goal as well
# Avoid look_at if we're already (almost) there, since that'd be an invalid look direction # Avoid look_at if we're already (almost) there, since that'd be an invalid look direction