23 lines
603 B
GDScript
23 lines
603 B
GDScript
extends State
|
|
|
|
var threat: Node2D
|
|
var threshold: float = 100
|
|
|
|
func enter(previous_state_path: String, data := {}) -> void:
|
|
if data.has("threat"):
|
|
threat = data["threat"]
|
|
else:
|
|
# default behaviour; do nothing
|
|
threat = owner
|
|
|
|
func physics_update(_delta: float) -> void:
|
|
if owner.position.distance_to(threat.position) > threshold or threat == owner:
|
|
finished.emit(owner.fsm.States.IDLE, {})
|
|
return
|
|
owner.move(flee_from(threat.position))
|
|
|
|
func flee_from(pos: Vector2) -> Vector3:
|
|
var diff = threat.position - owner.position
|
|
diff = diff.normalized() * -1
|
|
return Vector3(diff.x, diff.y ,0)
|