SanAndreasUnity/Assets/Scripts/Behaviours/SpawnManager.cs

139 lines
3.9 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
{
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;
2019-04-24 14:58:13 +00:00
void Start()
{
this.InvokeRepeating(nameof(UpdateSpawnPositions), 1f, 1f);
2019-04-24 17:18:18 +00:00
this.InvokeRepeating(nameof(SpawnPlayers), 1f, 3f);
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;
}
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;
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
}
}