mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-22 12:03:04 +00:00
Working on player spawning
This commit is contained in:
parent
06a2dc4c5f
commit
01701be12d
4 changed files with 57 additions and 12 deletions
|
@ -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");
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue