multyplayer

This commit is contained in:
algodoogle
2026-07-16 18:36:49 +01:00
parent 5e0b58a984
commit 1869dbf054
28 changed files with 974 additions and 60 deletions
+32 -29
View File
@@ -1,59 +1,62 @@
extends StaticBody3D
## Cooking station. The cook timer and item conversion run only on the machine
## that owns world logic (server or offline host); `progress` is replicated to
## clients for UI via a MultiplayerSynchronizer. This is the reference
## implementation of the reusable "station work-progress" pattern: a server-owned
## progress value advanced by an input source (here, a timer), which converts the
## held item once it reaches the threshold and spawns the result over the network.
@export var cook_speed = 1
@export var cook_speed := 1.0
@onready var snap_zone: XRToolsSnapZone = $XRToolsSnapZone
var item = null
var progress = 0
var progress: float = 0.0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if snap_zone.initial_object:
item = snap_zone.initial_object
print("Hob has item: ", item)
else:
print("Hob is empty")
func convert_item(cookable: CookableItem) -> void:
print("Hob converting ", cookable)
var old_pickable = snap_zone.picked_up_object
if not old_pickable:
push_warning("Hob finished cooking item, but snap zone is missing its reference")
return
print("picked_up_object ", snap_zone.picked_up_object)
var original_transform = old_pickable.global_transform
var new_scene_instance = cookable.turns_into.instantiate()
# Add the new item to the hob's parent (so it lives in the main world space)
get_parent().add_child(new_scene_instance)
new_scene_instance.global_transform = original_transform
var original_transform: Transform3D = old_pickable.global_transform
snap_zone.drop_object()
print("old_pickable ", old_pickable)
# Spawn the result over the network (server) or locally (offline).
var result := NetworkManager.spawn_item(cookable.turns_into.resource_path, original_transform)
# Remove the cooked item (despawn replicates for spawner-managed items).
snap_zone.drop_object()
old_pickable.queue_free()
snap_zone.pick_up_object(new_scene_instance)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
# Find the CookableItem in held object
# Snap the result into the now-empty zone.
if result:
snap_zone.pick_up_object(result)
func _process(delta: float) -> void:
# Only the world owner runs cooking logic; clients receive `progress` + the
# converted item via replication.
if not NetworkManager.owns_world():
return
# Find the CookableItem in the held object.
var cookable = null
if snap_zone.picked_up_object:
var matches = snap_zone.picked_up_object.get_children().filter(func(c): return c is CookableItem)
if matches.size() > 0:
cookable = matches[0]
# Cook it if exists, then convert item after cooking
# Advance progress and convert once cooked.
if cookable:
progress += cook_speed * delta
print("Cooking ", progress)
if progress >= cookable.cooking_time:
progress = 0
progress = 0.0
convert_item(cookable)
else:
progress = 0
progress = 0.0