Create SpawnManager script

This commit is contained in:
in0finite 2019-04-24 16:58:13 +02:00
parent 7106892550
commit 272cc63d02
5 changed files with 88 additions and 10 deletions

View file

@ -21,6 +21,8 @@ namespace SanAndreasUnity.Behaviours
public static Ped SpawnPed (PedestrianDef def, Vector3 pos, Quaternion rot) public static Ped SpawnPed (PedestrianDef def, Vector3 pos, Quaternion rot)
{ {
Net.NetStatus.ThrowIfNotOnServer();
CheckPedPrefab (); CheckPedPrefab ();
var go = Instantiate (PedManager.Instance.pedPrefab, pos, rot); var go = Instantiate (PedManager.Instance.pedPrefab, pos, rot);
@ -34,7 +36,7 @@ namespace SanAndreasUnity.Behaviours
destroyer.timeUntilDestroyed = PedManager.Instance.AIOutOfRangeTimeout; destroyer.timeUntilDestroyed = PedManager.Instance.AIOutOfRangeTimeout;
destroyer.range = PedManager.Instance.AIOutOfRangeDistance; destroyer.range = PedManager.Instance.AIOutOfRangeDistance;
Mirror.NetworkServer.Spawn(go); Net.NetManager.Spawn(go);
return ped; return ped;
} }

View file

@ -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;
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 20df1bf3631feafd8984718a6894cbb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -88,7 +88,7 @@ namespace SanAndreasUnity.Net
public static void CheckIfServerIsStarted() { public static void CheckIfServerIsStarted() {
if (NetStatus.IsServerStarted ()) if (NetStatus.IsServerStarted)
throw new System.Exception ("Server already started"); 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);
}
} }
} }

View file

@ -50,23 +50,19 @@ namespace SanAndreasUnity.Net
} }
} }
public static bool IsServerStarted() { public static bool IsServerStarted => NetStatus.serverStatus == NetworkServerStatus.Started;
return serverStatus == NetworkServerStatus.Started;
}
/// <summary> /// <summary>
/// Is server active ? /// Is server active ?
/// </summary> /// </summary>
public static bool IsServer() { public static bool IsServer => NetStatus.IsServerStarted;
return NetStatus.IsServerStarted ();
}
/// <summary> /// <summary>
/// Is host active ? /// Is host active ?
/// </summary> /// </summary>
public static bool IsHost() { public static bool IsHost() {
if (!NetStatus.IsServer ()) if (!NetStatus.IsServer)
return false; return false;
return NetworkServer.localClientActive; return NetworkServer.localClientActive;
@ -100,6 +96,15 @@ namespace SanAndreasUnity.Net
} }
/// <summary>
/// Throws exception if server is not active.
/// </summary>
public static void ThrowIfNotOnServer()
{
if (!NetStatus.IsServer)
throw new System.Exception("Not on a server");
}
} }
} }