SanAndreasUnity/Assets/Scripts/Behaviours/SpawnManager.cs

171 lines
4.7 KiB
C#
Raw Normal View History

2019-04-24 14:58:13 +00:00
using System.Collections.Generic;
using UnityEngine;
2019-04-24 17:18:18 +00:00
using SanAndreasUnity.Net;
using SanAndreasUnity.Utilities;
using System.Linq;
2019-04-24 14:58:13 +00:00
namespace SanAndreasUnity.Behaviours
{
public class SpawnManager : MonoBehaviour
{
public static SpawnManager Instance { get; private set; }
2019-04-24 17:18:18 +00:00
List<Transform> m_spawnPositions = new List<Transform>();
2019-04-24 14:58:13 +00:00
GameObject m_container;
2019-04-24 23:14:06 +00:00
public bool spawnPlayerWhenConnected = true;
public bool IsSpawningPaused { get; set; } = false;
public float spawnInterval = 4f;
float m_lastSpawnTime = 0f;
2019-04-24 14:58:13 +00:00
void Awake()
{
Instance = this;
}
2019-04-24 14:58:13 +00:00
void Start()
{
this.InvokeRepeating(nameof(RepeatedMethod), 1f, 1f);
2019-04-24 23:14:06 +00:00
Player.onStart += OnPlayerConnected;
2019-04-24 14:58:13 +00:00
}
void OnLoaderFinished()
{
if (!NetStatus.IsServer)
return;
// spawn players that were connected during loading process - this will always be the case for
// local player
UpdateSpawnPositions();
SpawnPlayers();
}
2019-04-24 17:58:40 +00:00
Transform GetSpawnFocusPos()
{
if (Ped.Instance)
return Ped.Instance.transform;
if (Camera.main)
return Camera.main.transform;
return null;
}
void RepeatedMethod()
{
if (!NetStatus.IsServer)
return;
this.UpdateSpawnPositions();
if (this.IsSpawningPaused)
return;
if (Time.time - m_lastSpawnTime >= this.spawnInterval)
{
// enough time passed
m_lastSpawnTime = Time.time;
this.SpawnPlayers();
}
}
2019-04-24 14:58:13 +00:00
void UpdateSpawnPositions()
{
2019-04-24 17:39:38 +00:00
if (!Loader.HasLoaded)
return;
2019-04-24 17:58:40 +00:00
if (!NetStatus.IsServer)
return;
Transform focusPos = GetSpawnFocusPos();
if (null == focusPos)
return;
if (null == m_container)
2019-04-24 14:58:13 +00:00
{
2019-04-24 17:58:40 +00:00
// create parent game object for spawn positions
m_container = new GameObject("Spawn positions");
2019-04-24 14:58:13 +00:00
2019-04-24 17:58:40 +00:00
// create spawn positions
m_spawnPositions.Clear();
for (int i = 0; i < 5; i++)
2019-04-24 14:58:13 +00:00
{
2019-04-24 17:58:40 +00:00
Transform spawnPos = new GameObject("Spawn position " + i).transform;
spawnPos.SetParent(m_container.transform);
m_spawnPositions.Add(spawnPos);
//NetManager.AddSpawnPosition(spawnPos);
2019-04-24 14:58:13 +00:00
}
2019-04-24 17:58:40 +00:00
}
2019-04-24 14:58:13 +00:00
2019-04-24 17:58:40 +00:00
// update spawn positions
m_spawnPositions.RemoveDeadObjects();
foreach (Transform spawnPos in m_spawnPositions)
{
spawnPos.position = focusPos.position + Random.insideUnitCircle.ToVector3XZ() * 15f;
2019-04-24 14:58:13 +00:00
}
2019-04-24 17:58:40 +00:00
2019-04-24 14:58:13 +00:00
}
2019-04-24 17:18:18 +00:00
void SpawnPlayers()
{
if (!NetStatus.IsServer)
return;
2019-04-24 17:39:38 +00:00
if (!Loader.HasLoaded)
return;
2019-04-24 17:58:40 +00:00
//var list = NetManager.SpawnPositions.ToList();
var list = m_spawnPositions;
2019-04-24 17:18:18 +00:00
list.RemoveDeadObjects();
if (list.Count < 1)
return;
foreach (var player in Player.AllPlayers)
{
2019-04-24 23:14:06 +00:00
SpawnPlayer(player, list);
}
2019-04-24 17:18:18 +00:00
2019-04-24 23:14:06 +00:00
}
2019-04-24 17:18:18 +00:00
2019-04-24 23:14:06 +00:00
public static Ped SpawnPlayer (Player player, List<Transform> spawns)
{
if (player.OwnedPed != null)
return null;
2019-04-24 17:18:18 +00:00
2019-04-24 23:14:06 +00:00
var spawn = spawns.RandomElement();
var ped = Ped.SpawnPed(Ped.RandomPedId, spawn.position, spawn.rotation);
player.OwnedPed = ped;
2019-04-25 00:01:40 +00:00
ped.WeaponHolder.autoAddWeapon = true;
// this ped should not be destroyed when he gets out of range
ped.gameObject.DestroyComponent<OutOfRangeDestroyer>();
2019-04-24 23:14:06 +00:00
Debug.LogFormat("Spawned ped for player {0}, net id {1}", player.connectionToClient.address, ped.netId);
return ped;
}
void OnPlayerConnected(Player player)
{
if (!NetStatus.IsServer)
return;
if (!Loader.HasLoaded) // these players will be spawned when loading process finishes
return;
2019-04-24 23:14:06 +00:00
if (!this.spawnPlayerWhenConnected)
return;
2019-04-24 17:18:18 +00:00
m_spawnPositions.RemoveDeadObjects();
2019-04-24 23:14:06 +00:00
SpawnPlayer(player, m_spawnPositions);
2019-04-24 17:18:18 +00:00
}
2019-04-24 14:58:13 +00:00
}
}