Add table with finite state machine

This commit is contained in:
JonShard
2026-07-20 13:39:25 +02:00
parent b6372c532a
commit ab6cce3d9a
7 changed files with 239 additions and 9 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ albedo_color = Color(1.1759251, 0.741115, 0.5692133, 1)
shape = SubResource("BoxShape3D_24d3s")
[node name="XRToolsSnapZone" type="Area3D" parent="." unique_id=1647380272 groups=["station"]]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5270121, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5608841, 0)
collision_layer = 65536
collision_mask = 65536
script = ExtResource("1_dlkho")
+13 -7
View File
@@ -31,37 +31,43 @@ shape = SubResource("SphereShape3D_vlqg6")
[node name="CSGBox3D" type="CSGBox3D" parent="CSGCombiner3D" unique_id=1111505172]
[node name="CSGCylinder3D" type="CSGCylinder3D" parent="CSGCombiner3D/CSGBox3D" unique_id=1338463939]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5128712, 0)
transform = Transform3D(0.8943019, 0, 0, 0, 0.8943019, 0, 0, 0, 0.8943019, 0, 0.5103538, -7.827557e-05)
operation = 2
radius = 0.25976563
height = 0.08898926
sides = 32
[node name="CSGTorus3D3" type="CSGTorus3D" parent="CSGCombiner3D" unique_id=666891341]
transform = Transform3D(0.8943019, 0, 0, 0, 0.8943019, 0, 0, 0, 0.8943019, 0, 0.5003869, -7.827557e-05)
inner_radius = 0.09153879
outer_radius = 0.120084204
sides = 32
[node name="CSGTorus3D" type="CSGTorus3D" parent="CSGCombiner3D" unique_id=1901978664]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.50172627, 0)
transform = Transform3D(0.8943019, 0, 0, 0, 0.8943019, 0, 0, 0, 0.8943019, 0, 0.5003869, -7.827557e-05)
inner_radius = 0.20426142
outer_radius = 0.2321898
sides = 32
[node name="CSGTorus3D2" type="CSGTorus3D" parent="CSGCombiner3D" unique_id=24541894]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.50172627, 0)
transform = Transform3D(0.8943019, 0, 0, 0, 0.8943019, 0, 0, 0, 0.8943019, 0, 0.5003869, -7.827557e-05)
inner_radius = 0.15036294
outer_radius = 0.17592031
sides = 32
[node name="CSGCylinder3D" type="CSGCylinder3D" parent="CSGCombiner3D" unique_id=1107239311]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.44518578, 0)
radius = 0.1352539
transform = Transform3D(0.8943019, 0, 0, 0, 0.8943019, 0, 0, 0, 0.8943019, 0, 0.4498226, -7.827557e-05)
radius = 0.071777344
height = 0.09637451
sides = 32
[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="CSGCombiner3D" unique_id=435540815]
transform = Transform3D(1, 0, 0, 0, -4.371139e-08, 1, 0, -1, -4.371139e-08, 0, 0.48640895, -0.0022216737)
transform = Transform3D(0.8943019, 0, 0, 0, -3.9091177e-08, 0.8943019, 0, -0.8943019, -3.9091177e-08, 0, 0.48668858, -0.0020651226)
radius = 0.008
height = 0.44487303
[node name="CSGCylinder3D3" type="CSGCylinder3D" parent="CSGCombiner3D" unique_id=878171801]
transform = Transform3D(-4.371139e-08, -1, -4.371139e-08, 0, -4.371139e-08, 1, -1, 4.371139e-08, 1.9106855e-15, 0, 0.48640895, -0.0022216737)
transform = Transform3D(-3.9091177e-08, -0.8943019, -3.9091177e-08, 0, -3.9091177e-08, 0.8943019, -0.8943019, 3.9091177e-08, 1.7087296e-15, 0, 0.48668858, -0.0020651226)
radius = 0.008
height = 0.44487303
+100
View File
@@ -0,0 +1,100 @@
extends StaticBody3D
@export var initial_thinking_time: float = 8
@export var initial_primary_time: float = 3
@export var initial_friend_time: float = 3
@export var initial_eating_time: float = 3
@onready var label_3d: Label3D = $Label3D
@onready var label_3d_time: Label3D = $Label3DTime
@onready var snap_zone: XRToolsSnapZone = $XRToolsSnapZone
const lbl_thinking: String = "Thinking..."
const lbl_waiting_primary: String = "Waiting for food"
const lbl_gameover: String = "Game Over!"
const lbl_eating: String = "Eating..."
### Finite State Machine ####
enum TableState {
EMPTY, # Just finished eating or havent eaten yet
THINKING,
WAITING_PRIMARY, # Inital wait to be served
WAITING_FRIEND, # Waiting for friends food to arrive
EATING
}
var _state = TableState.EMPTY # use _setState(), never set directly
var _state_time: float = 0.0
func _ready() -> void:
if not label_3d:
push_error("Table is missing reference to Label3D")
if not label_3d_time:
push_error("Table is missing reference to Label3DTime")
if not snap_zone:
push_error("Table is missing reference to XRToolsSnapZone")
snap_zone.has_picked_up.connect(_on_object_picked_up)
snap_zone.has_dropped.connect(_on_object_dropped)
_setState(TableState.EMPTY)
func _on_object_picked_up(_item) -> void:
print("Table: object picked up: ", _item)
func _on_object_dropped() -> void:
print("Table: object dropped ")
func place_order() -> void:
print("Table: place_order()")
label_3d.text = "order!"
func _setState(newState: TableState) -> void:
print("Table set _state: ", TableState.keys()[newState])
if newState == TableState.EMPTY:
label_3d.text = "empty"
if newState == TableState.THINKING:
_state_time = initial_eating_time
label_3d.text = lbl_thinking
if newState == TableState.WAITING_PRIMARY:
_state_time = initial_primary_time
label_3d.text = lbl_waiting_primary
if newState == TableState.WAITING_FRIEND:
_state_time = initial_friend_time
if newState == TableState.EATING:
_state_time = initial_eating_time
label_3d.text = lbl_eating
_state = newState
func _process(delta: float) -> void:
# Wait for state to finish
if _state_time > 0:
_state_time -= delta
label_3d_time.text = "%.1f" % _state_time
return
match _state:
TableState.EMPTY:
_setState(TableState.THINKING)
TableState.THINKING:
place_order()
_setState(TableState.WAITING_PRIMARY)
TableState.WAITING_PRIMARY:
label_3d.text = lbl_gameover
TableState.EATING:
_setState(TableState.EMPTY)
+1
View File
@@ -0,0 +1 @@
uid://caeikc7e3igkd
+119
View File
@@ -0,0 +1,119 @@
[gd_scene format=3 uid="uid://caf0xanmxbshy"]
[ext_resource type="Script" uid="uid://caeikc7e3igkd" path="res://Stations/table.gd" id="1_2vcpj"]
[ext_resource type="Script" uid="uid://cquqe4f1m1sw6" path="res://addons/godot-xr-tools/objects/snap_zone.gd" id="1_8j1nt"]
[sub_resource type="BoxShape3D" id="BoxShape3D_24d3s"]
[sub_resource type="SphereShape3D" id="SphereShape3D_dlkho"]
radius = 0.3
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_24d3s"]
albedo_color = Color(0.31, 0.21576, 0.1333, 1)
[node name="Table" type="StaticBody3D" unique_id=1863572470 groups=["station"]]
script = ExtResource("1_2vcpj")
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=228053941]
shape = SubResource("BoxShape3D_24d3s")
[node name="XRToolsSnapZone" type="Area3D" parent="." unique_id=1947858647 groups=["station"]]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5270121, 0)
collision_layer = 65536
collision_mask = 65536
script = ExtResource("1_8j1nt")
snap_mode = 1
metadata/_custom_type_script = "uid://cquqe4f1m1sw6"
[node name="CollisionShape3D2" type="CollisionShape3D" parent="XRToolsSnapZone" unique_id=1176887393]
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, -0.000975132, 0)
shape = SubResource("SphereShape3D_dlkho")
[node name="CSGCombiner3D" type="CSGCombiner3D" parent="." unique_id=2085635675]
[node name="CSGBox3D" type="CSGBox3D" parent="CSGCombiner3D" unique_id=411048765]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.46137518, 0)
size = Vector3(1, 0.07678223, 1)
material = SubResource("StandardMaterial3D_24d3s")
[node name="CSGBox3D2" type="CSGBox3D" parent="CSGCombiner3D" unique_id=2093582402]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0023368, 0)
size = Vector3(0.1, 0.8768555, 0.1)
material = SubResource("StandardMaterial3D_24d3s")
[node name="CSGBox3D3" type="CSGBox3D" parent="CSGCombiner3D" unique_id=839228683]
transform = Transform3D(0.86602545, 0.50000006, 0, -0.50000006, 0.86602545, 0, 0, 0, 1, -0.21657634, -0.5113707, 0)
size = Vector3(0.1, 0.8768555, 0.1)
material = SubResource("StandardMaterial3D_24d3s")
[node name="CSGBox3D4" type="CSGBox3D" parent="CSGCombiner3D" unique_id=1028598922]
transform = Transform3D(-0.45971727, -0.2654179, 0.8474747, -0.50000006, 0.86602545, 0, -0.73393464, -0.4237374, -0.53083575, 0.10796261, -0.5113707, 0.16580808)
size = Vector3(0.1, 0.8768555, 0.1)
material = SubResource("StandardMaterial3D_24d3s")
[node name="CSGBox3D5" type="CSGBox3D" parent="CSGCombiner3D" unique_id=892392906]
transform = Transform3D(-0.38498235, -0.12489955, -0.9144338, -0.5286442, 0.8420017, 0.107556336, 0.756521, 0.5248176, -0.39018327, 0.059352398, -0.5113707, -0.2290039)
size = Vector3(0.1, 0.8768555, 0.1)
material = SubResource("StandardMaterial3D_24d3s")
[node name="Customers" type="Node3D" parent="." unique_id=2147179512]
[node name="Customer" type="Node3D" parent="Customers" unique_id=1265943831]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.016780734, -0.08218986, 0)
[node name="CSGCombiner3D" type="CSGCombiner3D" parent="Customers/Customer" unique_id=1211819222]
transform = Transform3D(1.095, 0, 0, 0, 1.095, 0, 0, 0, 1.095, 0.08442788, -0.19989291, 0)
[node name="CSGSphere3D" type="CSGSphere3D" parent="Customers/Customer/CSGCombiner3D" unique_id=2104132980]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.88519466, 0.9358529, 0)
radius = 0.17520222
[node name="CSGSphere3D2" type="CSGSphere3D" parent="Customers/Customer/CSGCombiner3D" unique_id=808477724]
transform = Transform3D(1, 0, 0, 0, 1.747647, 0, 0, 0, 1, -0.9068739, 0.51278543, 0)
radius = 0.17520222
[node name="Seats" type="Node3D" parent="." unique_id=1101423789]
[node name="Seat" type="Node3D" parent="Seats" unique_id=1951252898]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.9079602, -0.37259835, 0)
[node name="CSGCombiner3D" type="CSGCombiner3D" parent="Seats/Seat" unique_id=384912485]
[node name="CSGCylinder3D" type="CSGCylinder3D" parent="Seats/Seat/CSGCombiner3D" unique_id=170426769]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.14400849, 0)
radius = 0.25634766
height = 0.051940918
sides = 16
[node name="CSGCylinder3D2" type="CSGCylinder3D" parent="Seats/Seat/CSGCombiner3D" unique_id=1294605073]
transform = Transform3D(0.24355066, 0.98373884, 0, -1.3339849, 0.1796049, 0, 0, 0, 0.8365013, -0.23884833, 0.48196742, 0)
radius = 0.25634766
height = 0.051940918
sides = 16
[node name="CSGBox3D" type="CSGBox3D" parent="Seats/Seat/CSGCombiner3D" unique_id=1761839440]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.15600169, -0.18527734, 0.1341562)
size = Vector3(0.05, 0.6713196, 0.05)
[node name="CSGBox3D2" type="CSGBox3D" parent="Seats/Seat/CSGCombiner3D" unique_id=1395371703]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.15600169, -0.18527734, -0.15065354)
size = Vector3(0.05, 0.6713196, 0.05)
[node name="CSGBox3D3" type="CSGBox3D" parent="Seats/Seat/CSGCombiner3D" unique_id=96608061]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.1444692, -0.18527734, 0.1341562)
size = Vector3(0.05, 0.6713196, 0.05)
[node name="CSGBox3D4" type="CSGBox3D" parent="Seats/Seat/CSGCombiner3D" unique_id=1684738537]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.1444692, -0.18527734, -0.15065354)
size = Vector3(0.05, 0.6713196, 0.05)
[node name="Label3D" type="Label3D" parent="." unique_id=662960977]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1781815, 0)
billboard = 2
text = "table state"
[node name="Label3DTime" type="Label3D" parent="." unique_id=1534849507]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.0412213, 0)
pixel_size = 0.003
billboard = 2
text = "20.1s"