From e79aa18fd2e808b1818552f22ce575799106a20c Mon Sep 17 00:00:00 2001 From: dusoleil Date: Sat, 23 Jul 2022 10:56:12 -0400 Subject: Clean up Chicken movement --- Chicken.gd | 39 +++++++++++++++++++++------------------ Main.tscn | 15 ++++++++++----- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Chicken.gd b/Chicken.gd index 996c2eb..b9fb9a4 100644 --- a/Chicken.gd +++ b/Chicken.gd @@ -7,7 +7,7 @@ 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 squak_chance = 5.0 +export var squack_chance = 5.0 export var peck_chance = 5.0 onready var anim = $AnimationTree @@ -34,31 +34,34 @@ func _physics_process(delta): func state_idle(delta): if roll_die(rot_chance): - var ang = randf() * rot_range + 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()) - mov_lerp_pos = 0.0 - mov_lerp_step = (mov_speed * delta) / dist - var rotation = Transform(self.rotation, Vector3.ZERO) - var forward = rotation * Vector3.FORWARD - mov_lerp_start = translation - mov_lerp_target = translation + (forward * dist) - elif roll_die(squak_chance): + 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_lerp_start = Vector3.ZERO -var mov_lerp_target = Vector3.ZERO -var mov_lerp_step = 0.0 -var mov_lerp_pos = 0.0 +var mov_velocity = Vector3.ZERO +var mov_target = Vector3.ZERO +var mov_steps = 0.0 func state_moving(_delta): - if mov_lerp_pos >= 1.0: + if mov_steps <= 0.0: anim.state.travel("IDLE") return - mov_lerp_pos += mov_lerp_step - mov_lerp_pos = min(mov_lerp_pos, 1.0) - var pos = lerp(mov_lerp_start, mov_lerp_target, mov_lerp_pos) - pos = move_and_slide(pos - translation, Vector3.UP) + 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 diff --git a/Main.tscn b/Main.tscn index b57a581..180f5e6 100644 --- a/Main.tscn +++ b/Main.tscn @@ -86,36 +86,41 @@ transform = Transform( 1, 0, 0, 0, 0.968718, 0.248166, 0, -0.248166, 0.968718, 0 [node name="Chicken" parent="." instance=ExtResource( 4 )] transform = Transform( 0.25, 0, 0, 0, 0.25, 0, 0, 0, 0.25, 0, 1.25, 0 ) rot_chance = 10.0 +mov_length_max = 3.0 flap_chance = 2.0 -squak_chance = 2.0 +squack_chance = 2.0 peck_chance = 3.0 [node name="Chicken2" parent="." instance=ExtResource( 4 )] transform = Transform( -0.157042, 0, 0.19452, 0, 0.25, 0, -0.19452, 0, -0.157042, 2.50059, 1.25, -2.25906 ) rot_chance = 10.0 +mov_length_max = 3.0 flap_chance = 2.0 -squak_chance = 2.0 +squack_chance = 2.0 peck_chance = 3.0 [node name="Chicken3" parent="." instance=ExtResource( 4 )] transform = Transform( 0.181922, 0, -0.171477, 0, 0.25, 0, 0.171477, 0, 0.181922, 0, 1.25, -2.79105 ) rot_chance = 10.0 +mov_length_max = 3.0 flap_chance = 2.0 -squak_chance = 2.0 +squack_chance = 2.0 peck_chance = 3.0 [node name="Chicken4" parent="." instance=ExtResource( 4 )] transform = Transform( 0.177798, 0, 0.175749, 0, 0.25, 0, -0.175749, 0, 0.177798, -3.31942, 1.25, 0 ) rot_chance = 10.0 +mov_length_max = 3.0 flap_chance = 2.0 -squak_chance = 2.0 +squack_chance = 2.0 peck_chance = 3.0 [node name="Chicken5" parent="." instance=ExtResource( 4 )] transform = Transform( -0.24986, 0, 0.00835422, 0, 0.25, 0, -0.00835422, 0, -0.24986, -1.33975, 1.25, -1.28165 ) rot_chance = 10.0 +mov_length_max = 3.0 flap_chance = 2.0 -squak_chance = 2.0 +squack_chance = 2.0 peck_chance = 3.0 [node name="UI" type="Control" parent="."] -- cgit v1.2.3