summaryrefslogtreecommitdiffstats
path: root/Chicken.gd
blob: 09456fb030152325bff2b94b0cf47a0338a2f2c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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

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 !$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())
			var mov = local_forward * dist
			mov_velocity = mov.normalized() * mov_speed
			mov_target = transform.origin + 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
			$SmoothRotation.rotate_y((-transform.basis.z.normalized()).signed_angle_to(collision.normal,Vector3.UP),rot_speed)
	mov_steps -= 1.0
	if mov_steps < 1.0:
		mov_velocity *= mov_steps