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