diff options
Diffstat (limited to 'Chicken.gd')
-rw-r--r-- | Chicken.gd | 54 |
1 files changed, 43 insertions, 11 deletions
@@ -10,20 +10,27 @@ 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 +export var detection_range = 20.0 onready var anim = $AnimationTree +var wings = "parameters/wings/playback" +var state = "parameters/state/playback" +var flap_speed = "parameters/wings/FLAP/TimeScale/scale" func roll_die(chance): return (randf() * 100) <= chance func _physics_process(delta): - match anim.wings.get_current_node(): + if chasing: + state_chasing(delta) + return + match anim[wings].get_current_node(): "IDLE": if roll_die(flap_chance): - anim.wings.travel("FLAP") + anim[wings].travel("FLAP") "FLAP": pass - match anim.state.get_current_node(): + match anim[state].get_current_node(): "IDLE": state_idle(delta) "MOVING": @@ -39,31 +46,56 @@ func state_idle(delta): var ang = (randf() - 0.5) * rot_range $SmoothRotation.rotate_y(ang, rot_speed) elif roll_die(mov_chance): - anim.state.travel("MOVING") + 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 = transform.origin + mov + mov_velocity = local_forward * mov_speed + mov_target = transform.origin + (local_forward * dist) mov_steps = (dist / mov_speed) / delta elif roll_die(squack_chance): - anim.state.travel("SQUACK") + anim[state].travel("SQUACK") elif roll_die(peck_chance): - anim.state.travel("PECK") + 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") + 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 - $SmoothRotation.rotate_y((-transform.basis.z.normalized()).signed_angle_to(collision.normal,Vector3.UP),rot_speed) + var local_forward = -transform.basis.z.normalized() + $SmoothRotation.rotate_y(local_forward.signed_angle_to(collision.normal,Vector3.UP),rot_speed) mov_steps -= 1.0 if mov_steps < 1.0: mov_velocity *= mov_steps + +var chasing +func _on_Detection_body_entered(body): + chasing = true + anim[state].travel("MOVING") + anim[flap_speed] = 8.0 + $SmoothRotation.stop_all() + +func _on_Detection_body_exited(body): + chasing = false + anim[state].travel("IDLE") + anim[flap_speed] = 2.0 + mov_steps = 0.0 + +func state_chasing(delta): + anim[wings].travel("FLAP") + var local_forward = -transform.basis.z.normalized() + var chaser = $Detection.get_overlapping_bodies()[0].transform.origin + chaser.y = transform.origin.y + var chase_dir = (transform.origin-chaser).normalized() + var ang = local_forward.signed_angle_to(chase_dir,Vector3.UP) + ((randf()-0.5)*(PI/2)) + if !$SmoothRotation.is_active(): + $SmoothRotation.rotate_y(ang,rot_speed*2) + var velocity = local_forward * mov_speed + velocity = move_and_slide(velocity, Vector3.UP) |