mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-22 20:13:02 +00:00
Create SpawnManager script
This commit is contained in:
parent
7106892550
commit
272cc63d02
5 changed files with 88 additions and 10 deletions
|
@ -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;
|
||||
}
|
||||
|
|
49
Assets/Scripts/Behaviours/SpawnManager.cs
Normal file
49
Assets/Scripts/Behaviours/SpawnManager.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/Behaviours/SpawnManager.cs.meta
Normal file
11
Assets/Scripts/Behaviours/SpawnManager.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 20df1bf3631feafd8984718a6894cbb6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -50,23 +50,19 @@ namespace SanAndreasUnity.Net
|
|||
}
|
||||
}
|
||||
|
||||
public static bool IsServerStarted() {
|
||||
return serverStatus == NetworkServerStatus.Started;
|
||||
}
|
||||
|
||||
public static bool IsServerStarted => NetStatus.serverStatus == NetworkServerStatus.Started;
|
||||
|
||||
/// <summary>
|
||||
/// Is server active ?
|
||||
/// </summary>
|
||||
public static bool IsServer() {
|
||||
return NetStatus.IsServerStarted ();
|
||||
}
|
||||
public static bool IsServer => NetStatus.IsServerStarted;
|
||||
|
||||
/// <summary>
|
||||
/// Is host active ?
|
||||
/// </summary>
|
||||
public static bool IsHost() {
|
||||
|
||||
if (!NetStatus.IsServer ())
|
||||
if (!NetStatus.IsServer)
|
||||
return false;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue