-
Notifications
You must be signed in to change notification settings - Fork 1
/
chicken.tscn
169 lines (123 loc) · 4.04 KB
/
chicken.tscn
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
[gd_scene load_steps=6 format=1]
[ext_resource path="res://chicken.png" type="Texture" id=1]
[sub_resource type="CircleShape2D" id=1]
custom_solver_bias = 0.0
radius = 10.0
[sub_resource type="GDScript" id=2]
script/source = "extends Object
const SFSM = preload('res://sfsm.gd')
onready var fsm = SFSM.new(self)
var current_target = null
var speed = 10
var can_be_eaten = false
var random_location = Vector2(0, 0)
func _ready():
set_process(true)
fsm.push_state('state_thinking')
func _process(delta):
fsm.update(delta)
get_node('Sprite').set_scale(Vector2(1,1))
func state_thinking(delta):
if has_target():
fsm.push_state('state_food_found')
else :
random_location = Vector2(get_pos().x + rand_range(-100, 100), get_pos().y + rand_range(-100, 100))
fsm.push_state('state_wandering', 5)
func state_wandering(delta):
recheck_food_nearby()
move_to_target(random_location, delta)
if has_target():
fsm.pop_state()
func state_food_found(delta):
if has_target():
if can_be_eaten:
get_target().consume(10*delta)
else:
move_to_target(get_target().get_pos(), delta) # Move The object
else:
can_be_eaten = false
fsm.pop_state();
func move_to_target(pos, delta):
var target_angle = get_pos().angle_to_point(pos) + deg2rad(0)
var direction = Vector2(0,-1).rotated(target_angle)
if(direction.length_squared() > 1):
direction = direction.normalized()
move(direction * speed * delta)
func _on_Area2D_body_enter( body ):
if body.has_method('edible') and body.edible():
if has_target():
current_target = weakref(get_nearest_target(get_target(), body))
else :
current_target = weakref(body)
func recheck_food_nearby():
var bodies = get_node('Site').get_overlapping_bodies();
for body in bodies:
if body.has_method('edible') and body.edible():
if has_target():
current_target = weakref(get_nearest_target(get_target(), body))
else:
current_target = weakref(body)
func get_target():
return current_target.get_ref()
func has_target():
return current_target and current_target.get_ref()
func get_nearest_target(target1, target2):
if get_pos().distance_to(target1.get_pos()) > get_pos().distance_to(target2.get_pos()):
return target2
return target1
func _on_Reach_body_enter( body ):
if has_target() and get_target() == body:
can_be_eaten = true
"
[sub_resource type="CircleShape2D" id=3]
custom_solver_bias = 0.0
radius = 100.0
[sub_resource type="CircleShape2D" id=4]
custom_solver_bias = 0.0
radius = 15.0
[node name="Chicken" type="KinematicBody2D"]
editor/display_folded = true
transform/pos = Vector2( 232.95, 163.065 )
input/pickable = false
shapes/0/shape = SubResource( 1 )
shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 )
shapes/0/trigger = false
collision/layers = 1
collision/mask = 1
collision/margin = 0.08
script/script = SubResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )
trigger = false
_update_shape_index = 0
[node name="Site" type="Area2D" parent="."]
input/pickable = true
shapes/0/shape = SubResource( 3 )
shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 )
shapes/0/trigger = false
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
[node name="CollisionShape2D" type="CollisionShape2D" parent="Site"]
shape = SubResource( 3 )
trigger = false
_update_shape_index = 0
[node name="Reach" type="Area2D" parent="."]
input/pickable = true
shapes/0/shape = SubResource( 4 )
shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 )
shapes/0/trigger = false
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
[node name="CollisionShape2D" type="CollisionShape2D" parent="Reach"]
shape = SubResource( 4 )
trigger = false
_update_shape_index = 0
[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 )
[connection signal="body_enter" from="Site" to="." method="_on_Area2D_body_enter"]
[connection signal="body_exit" from="Site" to="." method="_on_Area2D_body_exit"]
[connection signal="body_enter" from="Reach" to="." method="_on_Reach_body_enter"]