extends KinematicBody export var rot_chance = 5.0 export var rot_range = PI / 4.0 export var mov_chance = 5.0 export var mov_speed = 1.0 export var mov_length_min = 1.0 export var mov_length_max = 5.0 export var flap_chance = 5.0 export var squack_chance = 5.0 export var peck_chance = 5.0 onready var anim = $AnimationTree func roll_die(chance): return (randf() * 100) <= chance func _physics_process(delta): match anim.wings.get_current_node(): "IDLE": if roll_die(flap_chance): anim.wings.travel("FLAP") "FLAP": pass match anim.state.get_current_node(): "IDLE": state_idle(delta) "MOVING": state_moving(delta) "SQUACK": pass "PECK": pass func state_idle(delta): if roll_die(rot_chance): var ang = (randf() - 0.5) * rot_range rotate(Vector3.UP, ang) if roll_die(mov_chance): anim.state.travel("MOVING") var local_forward = -transform.basis.z.normalized() var dist = lerp(mov_length_min, mov_length_max, randf()) var mov = local_forward * dist mov_velocity = mov.normalized() * mov_speed mov_target = translation + mov mov_steps = (dist / mov_speed) / delta elif roll_die(squack_chance): anim.state.travel("SQUACK") elif roll_die(peck_chance): anim.state.travel("PECK") var mov_velocity = Vector3.ZERO var mov_target = Vector3.ZERO var mov_steps = 0.0 func state_moving(_delta): if mov_steps <= 0.0: anim.state.travel("IDLE") return var vmp = move_and_slide(mov_velocity, Vector3.UP) for index in range(get_slide_count()): var collision = get_slide_collision(index) if collision.collider.name == "Fence": mov_steps = 0.0 look_at(translation + collision.normal ,Vector3.UP) mov_steps -= 1.0 if mov_steps < 1.0: mov_velocity *= mov_steps