summaryrefslogtreecommitdiffstats
path: root/Chicken.gd
blob: 996c2ebd49032888635db522b585fcef2c94610d (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
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 squak_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() * rot_range
		rotate(Vector3.UP, ang)
	if roll_die(mov_chance):
		anim.state.travel("MOVING")
		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):
		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
func state_moving(_delta):
	if mov_lerp_pos >= 1.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)