Working on player spawning

This commit is contained in:
in0finite 2019-04-24 19:18:18 +02:00
parent 06a2dc4c5f
commit 01701be12d
4 changed files with 57 additions and 12 deletions

View file

@ -1,49 +1,78 @@
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities;
using System.Linq;
namespace SanAndreasUnity.Behaviours
{
public class SpawnManager : MonoBehaviour
{
Transform[] m_spawnPositions = null;
List<Transform> m_spawnPositions = new List<Transform>();
GameObject m_container;
void Start()
{
this.InvokeRepeating(nameof(UpdateSpawnPositions), 1f, 1f);
this.InvokeRepeating(nameof(SpawnPlayers), 1f, 3f);
}
void UpdateSpawnPositions()
{
if (Net.NetStatus.IsServer && Ped.Instance)
if (NetStatus.IsServer && Ped.Instance)
{
if (null == m_spawnPositions)
if (null == m_container)
{
// 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.Clear();
for (int i = 0; i < 5; i++)
{
m_spawnPositions[i] = new GameObject("Spawn position " + i).transform;
m_spawnPositions[i].SetParent(m_container.transform);
Net.NetManager.AddSpawnPosition(m_spawnPositions[i]);
Transform spawnPos = new GameObject("Spawn position " + i).transform;
spawnPos.SetParent(m_container.transform);
m_spawnPositions.Add(spawnPos);
NetManager.AddSpawnPosition(spawnPos);
}
}
// update spawn positions
for (int i = 0; i < m_spawnPositions.Length; i++)
m_spawnPositions.RemoveDeadObjects();
foreach (Transform spawnPos in m_spawnPositions)
{
m_spawnPositions[i].position = Ped.Instance.transform.position + Random.insideUnitCircle.ToVector3XZ() * 15f;
spawnPos.position = Ped.Instance.transform.position + Random.insideUnitCircle.ToVector3XZ() * 15f;
}
}
}
void SpawnPlayers()
{
if (!NetStatus.IsServer)
return;
var list = NetManager.SpawnPositions.ToList();
list.RemoveDeadObjects();
if (list.Count < 1)
return;
foreach (var player in Player.AllPlayers)
{
// if player is not spawned, spawn him
var spawn = list.RandomElement();
}
}
}
}

View file

@ -145,6 +145,8 @@ namespace SanAndreasUnity.Net
NetworkManager.startPositions.Add(tr);
}
public static Transform[] SpawnPositions { get { return NetworkManager.startPositions.ToArray(); } }
public static void Spawn(GameObject go)
{
NetworkServer.Spawn(go);

View file

@ -440,6 +440,18 @@ namespace SanAndreasUnity.Utilities
return false;
}
public static T RandomElement<T> (this IList<T> list)
{
if (list.Count < 1)
throw new System.InvalidOperationException("List has no elements");
return list [UnityEngine.Random.Range(0, list.Count)];
}
public static int RemoveDeadObjects<T> (this List<T> list) where T : UnityEngine.Object
{
return list.RemoveAll(item => null == item);
}
private static Dictionary<string, Texture2D> Texturemap = new Dictionary<string, Texture2D>();
private static Texture2D Font;

View file

@ -14,6 +14,8 @@
- player's ped must be created by server (when host starts) - it should be removed from scenes
- PedsWindow throws NRE when Ped.Instance is null
# Potential problems