blob: 1a03c1bbf59e25913b2f6a90a375f71a81c43b66 (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
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)
|