summaryrefslogblamecommitdiffstats
path: root/Chicken.gd
blob: 1a03c1bbf59e25913b2f6a90a375f71a81c43b66 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                               
                               




                               
                              
                            
                                 

                                 


                                                        




                                        
                          
                                                                   



                                             

                                                 
                                                          

                            
                                             









                                           




                                                                
                                                    

                                                                                

                                                                              

                                                              
                                                    
                                           
                                                  
 


                               
                          
                            
                                          
                      
                                                           



                                                          

                                                                                                                      


                                         

           
                                       




                                    
                                      




                                  
                           









                                                                                              
extends KinematicBody

export var rot_chance = 5.0
export var rot_range = PI / 4.0
export var rot_speed = 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
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):
	if !is_on_floor():
		var _vmp = move_and_slide(Vector3.DOWN, Vector3.UP)
	if chasing:
		state_chasing(delta)
		return
	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 !$SmoothRotation.is_active():
		if roll_die(rot_chance):
			var ang = (randf() - 0.5) * rot_range
			$SmoothRotation.rotate_y(ang, rot_speed)
		elif 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_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")
		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
			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)