diff --git a/Assets/Scripts/Behaviours/Ped/Ped_Spawning.cs b/Assets/Scripts/Behaviours/Ped/Ped_Spawning.cs index a64b94e9..dcac3440 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped_Spawning.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped_Spawning.cs @@ -21,6 +21,8 @@ namespace SanAndreasUnity.Behaviours public static Ped SpawnPed (PedestrianDef def, Vector3 pos, Quaternion rot) { + Net.NetStatus.ThrowIfNotOnServer(); + CheckPedPrefab (); var go = Instantiate (PedManager.Instance.pedPrefab, pos, rot); @@ -34,7 +36,7 @@ namespace SanAndreasUnity.Behaviours destroyer.timeUntilDestroyed = PedManager.Instance.AIOutOfRangeTimeout; destroyer.range = PedManager.Instance.AIOutOfRangeDistance; - Mirror.NetworkServer.Spawn(go); + Net.NetManager.Spawn(go); return ped; } diff --git a/Assets/Scripts/Behaviours/SpawnManager.cs b/Assets/Scripts/Behaviours/SpawnManager.cs new file mode 100644 index 00000000..3bc0f558 --- /dev/null +++ b/Assets/Scripts/Behaviours/SpawnManager.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace SanAndreasUnity.Behaviours +{ + + public class SpawnManager : MonoBehaviour + { + Transform[] m_spawnPositions = null; + GameObject m_container; + + + void Start() + { + this.InvokeRepeating(nameof(UpdateSpawnPositions), 1f, 1f); + } + + void UpdateSpawnPositions() + { + if (Net.NetStatus.IsServer && Ped.Instance) + { + if (null == m_spawnPositions) + { + // create parent game object for spawn positions + if (null == m_container) + m_container = new GameObject("Spawn positions"); + + // create spawn positions + m_spawnPositions = new Transform[5]; + for (int i = 0; i < m_spawnPositions.Length; i++) + { + m_spawnPositions[i] = new GameObject("Spawn position " + i).transform; + m_spawnPositions[i].SetParent(m_container.transform); + Net.NetManager.AddSpawnPosition(m_spawnPositions[i]); + } + } + + // update spawn positions + for (int i = 0; i < m_spawnPositions.Length; i++) + { + m_spawnPositions[i].position = Ped.Instance.transform.position + Random.insideUnitCircle.ToVector3XZ() * 15f; + } + + } + } + + } + +} diff --git a/Assets/Scripts/Behaviours/SpawnManager.cs.meta b/Assets/Scripts/Behaviours/SpawnManager.cs.meta new file mode 100644 index 00000000..6a80db80 --- /dev/null +++ b/Assets/Scripts/Behaviours/SpawnManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 20df1bf3631feafd8984718a6894cbb6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/NetManager.cs b/Assets/Scripts/Networking/NetManager.cs index 17a10b88..c34ec983 100644 --- a/Assets/Scripts/Networking/NetManager.cs +++ b/Assets/Scripts/Networking/NetManager.cs @@ -88,7 +88,7 @@ namespace SanAndreasUnity.Net public static void CheckIfServerIsStarted() { - if (NetStatus.IsServerStarted ()) + if (NetStatus.IsServerStarted) throw new System.Exception ("Server already started"); } @@ -139,6 +139,17 @@ namespace SanAndreasUnity.Net } + + public static void AddSpawnPosition(Transform tr) + { + NetworkManager.startPositions.Add(tr); + } + + public static void Spawn(GameObject go) + { + NetworkServer.Spawn(go); + } + } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/NetStatus.cs b/Assets/Scripts/Networking/NetStatus.cs index 83360207..b568008a 100644 --- a/Assets/Scripts/Networking/NetStatus.cs +++ b/Assets/Scripts/Networking/NetStatus.cs @@ -50,23 +50,19 @@ namespace SanAndreasUnity.Net } } - public static bool IsServerStarted() { - return serverStatus == NetworkServerStatus.Started; - } - + public static bool IsServerStarted => NetStatus.serverStatus == NetworkServerStatus.Started; + /// /// Is server active ? /// - public static bool IsServer() { - return NetStatus.IsServerStarted (); - } + public static bool IsServer => NetStatus.IsServerStarted; /// /// Is host active ? /// public static bool IsHost() { - if (!NetStatus.IsServer ()) + if (!NetStatus.IsServer) return false; return NetworkServer.localClientActive; @@ -100,6 +96,15 @@ namespace SanAndreasUnity.Net } + /// + /// Throws exception if server is not active. + /// + public static void ThrowIfNotOnServer() + { + if (!NetStatus.IsServer) + throw new System.Exception("Not on a server"); + } + } } \ No newline at end of file