Files
jelito/Assets/jelycho/Code/Beacons/BaseManager.cs
2025-07-01 18:57:49 +02:00

47 lines
1.7 KiB
C#

using System.Collections.Generic;
using RebootKit.Engine.Main;
using Unity.Netcode;
using UnityEngine;
namespace RebootReality.jelycho.Beacons {
public class BaseManager : NetworkBehaviour {
[SerializeField] Beacon m_BeaconPrefab;
[SerializeField] float m_ConnectionRopeLength = 10.0f;
[SerializeField] float m_BeaconSpawnRadius = 15.0f;
readonly List<Beacon> m_Beacons = new List<Beacon>();
public IReadOnlyList<Beacon> Beacons => m_Beacons;
[ServerRpc(RequireOwnership = false)]
public void SpawnBeaconServerRpc(Vector3 position) {
if (!IsServer) {
Debug.LogWarning("Only the server can spawn beacons.");
return;
}
Beacon beacon = Instantiate(m_BeaconPrefab, position, Quaternion.identity);
beacon.NetworkObject.Spawn();
beacon.GrowClientRpc();
foreach (Beacon otherBeacon in m_Beacons) {
if ((otherBeacon.transform.position - beacon.transform.position).sqrMagnitude <
m_BeaconSpawnRadius * m_BeaconSpawnRadius) {
Vector3 startPosition = otherBeacon.RopeConnectionPoint.position;
Vector3 endPosition = beacon.RopeConnectionPoint.position;
SpawnRopeRpc(startPosition, endPosition);
}
}
m_Beacons.Add(beacon);
}
[Rpc(SendTo.Everyone)]
void SpawnRopeRpc(Vector3 startPosition, Vector3 endPosition) {
if (RR.World.Context is WorldContext worldContext) {
worldContext.RopesManager.SpawnLockedRope(m_ConnectionRopeLength, startPosition, endPosition);
}
}
}
}