SanAndreasUnity/Assets/Scripts/Behaviours/SpawnManager.cs

191 lines
5.6 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 23:14:06 +00:00
public bool spawnPlayerWhenConnected = true;
public bool IsSpawningPaused { get; set; } = false;
public float spawnInterval = 4f;
float m_lastSpawnTime = 0f;
2021-02-21 01:35:26 +00:00
public bool addWeaponsToSpawnedPlayers = true;
public static SpawnHandler DefaultSpawnHandler { get; } = new SpawnHandler();
private SpawnHandler m_spawnHandler = DefaultSpawnHandler;
public SpawnHandler SpawnHandler
{
get => m_spawnHandler;
set
{
if (value == null)
value = DefaultSpawnHandler;
m_spawnHandler = value;
}
}
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
SpawnPlayers();
}
public static Transform GetSpawnFocusPos()
2019-04-24 17:58:40 +00:00
{
if (Ped.Instance)
return Ped.Instance.transform;
if (Camera.main)
return Camera.main.transform;
return null;
}
void RepeatedMethod()
{
if (!NetStatus.IsServer)
return;
if (this.IsSpawningPaused)
return;
if (Time.time - m_lastSpawnTime >= this.spawnInterval)
{
// enough time passed
m_lastSpawnTime = Time.time;
2020-06-05 22:45:53 +00:00
F.RunExceptionSafe(() => this.SpawnPlayers());
}
}
public static bool GetSpawnPositionFromFocus(Transform focusPos, out TransformDataStruct transformData)
{
transformData = new TransformDataStruct(
focusPos.position + Random.insideUnitCircle.ToVector3XZ() * 15f,
Quaternion.Euler(0f, Random.Range(0f, 360f), 0f));
return true;
}
public static bool GetSpawnPositionFromFocus(out TransformDataStruct transformData)
2019-04-24 14:58:13 +00:00
{
2019-04-24 17:58:40 +00:00
Transform focusPos = GetSpawnFocusPos();
if (null == focusPos)
{
transformData = new TransformDataStruct();
return false;
2019-04-24 14:58:13 +00:00
}
return GetSpawnPositionFromFocus(focusPos, out transformData);
2019-04-24 14:58:13 +00:00
}
public static bool GetSpawnPositionFromInteriors(out TransformDataStruct transformData)
{
transformData = World.Cell.GetEnexExitTransform(
World.Cell.Instance.GetEnexesFromLoadedInteriors()
.ToList()
.RandomElement());
return true;
}
bool GetSpawnPositionFromHandler(Player player, out TransformDataStruct transformData)
{
bool success = false;
TransformDataStruct t = new TransformDataStruct();
F.RunExceptionSafe(() =>
{
success = this.SpawnHandler.GetSpawnPosition(player, out t);
});
transformData = t;
return success;
}
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;
TransformDataStruct transformData;
2019-04-24 17:18:18 +00:00
foreach (var player in Player.AllPlayers)
{
if (this.GetSpawnPositionFromHandler(player, out transformData))
SpawnPlayer(player, transformData);
else
break;
2019-04-24 23:14:06 +00:00
}
2019-04-24 17:18:18 +00:00
2019-04-24 23:14:06 +00:00
}
2019-04-24 17:18:18 +00:00
2021-02-21 01:35:26 +00:00
public Ped SpawnPlayer (Player player, TransformDataStruct spawnPlace)
2019-04-24 23:14:06 +00:00
{
if (player.OwnedPed != null)
return null;
2019-04-24 17:18:18 +00:00
var ped = Ped.SpawnPed(Ped.RandomPedId, spawnPlace.position, spawnPlace.rotation, false);
ped.NetPlayerOwnerGameObject = player.gameObject;
2021-02-21 01:35:26 +00:00
if (this.addWeaponsToSpawnedPlayers)
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
NetManager.Spawn(ped.gameObject);
player.OwnedPed = ped;
2019-07-11 16:26:54 +00:00
Debug.LogFormat("Spawned ped {0} for player {1}, time: {2}", ped.DescriptionForLogging, player.DescriptionForLogging,
F.CurrentDateForLogging);
2019-04-24 23:14:06 +00:00
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
if (this.GetSpawnPositionFromHandler(player, out TransformDataStruct transformData))
SpawnPlayer(player, transformData);
2019-04-24 17:18:18 +00:00
}
2019-04-24 14:58:13 +00:00
}
}