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-06-25 23:51:09 +00:00
|
|
|
|
public static SpawnManager Instance { get; private set; }
|
|
|
|
|
|
2019-04-24 23:14:06 +00:00
|
|
|
|
public bool spawnPlayerWhenConnected = true;
|
2019-06-25 23:51:09 +00:00
|
|
|
|
public bool IsSpawningPaused { get; set; } = false;
|
|
|
|
|
public float spawnInterval = 4f;
|
|
|
|
|
float m_lastSpawnTime = 0f;
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
|
|
|
|
|
|
2019-06-25 23:51:09 +00:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
2019-06-25 23:51:09 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 23:59:01 +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();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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-06-25 23:51:09 +00:00
|
|
|
|
void RepeatedMethod()
|
|
|
|
|
{
|
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (this.IsSpawningPaused)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Time.time - m_lastSpawnTime >= this.spawnInterval)
|
|
|
|
|
{
|
|
|
|
|
// enough time passed
|
|
|
|
|
m_lastSpawnTime = Time.time;
|
|
|
|
|
this.SpawnPlayers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 02:00:16 +00:00
|
|
|
|
List<TransformDataStruct> GetSpawnPositionsFromFocus()
|
2019-04-24 14:58:13 +00:00
|
|
|
|
{
|
2019-10-22 22:07:45 +00:00
|
|
|
|
var spawns = new List<TransformDataStruct>();
|
2019-04-24 17:58:40 +00:00
|
|
|
|
|
|
|
|
|
Transform focusPos = GetSpawnFocusPos();
|
|
|
|
|
if (null == focusPos)
|
2019-10-22 22:07:45 +00:00
|
|
|
|
return spawns;
|
2019-04-24 17:58:40 +00:00
|
|
|
|
|
2019-10-22 22:07:45 +00:00
|
|
|
|
for (int i=0; i < 5; i++)
|
2019-04-24 17:58:40 +00:00
|
|
|
|
{
|
2019-10-22 19:03:44 +00:00
|
|
|
|
var transformData = new TransformDataStruct(focusPos.position + Random.insideUnitCircle.ToVector3XZ() * 15f);
|
2019-10-22 22:07:45 +00:00
|
|
|
|
spawns.Add(transformData);
|
2019-04-24 14:58:13 +00:00
|
|
|
|
}
|
2019-10-22 22:07:45 +00:00
|
|
|
|
|
|
|
|
|
return spawns;
|
2019-04-24 14:58:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 02:00:16 +00:00
|
|
|
|
List<TransformDataStruct> GetSpawnPositionsFromInteriors()
|
|
|
|
|
{
|
|
|
|
|
return World.Cell.Instance.GetEnexesFromLoadedInteriors().Select(enex => World.Cell.GetEnexExitTransform(enex)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<TransformDataStruct> GetSpawnPositions()
|
|
|
|
|
{
|
|
|
|
|
if (null == World.Cell.Instance || World.Cell.Instance.HasExterior)
|
|
|
|
|
return GetSpawnPositionsFromFocus();
|
|
|
|
|
else
|
|
|
|
|
return GetSpawnPositionsFromInteriors();
|
|
|
|
|
}
|
|
|
|
|
|
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-10-22 22:07:45 +00:00
|
|
|
|
var list = GetSpawnPositions();
|
|
|
|
|
|
2019-04-24 17:18:18 +00:00
|
|
|
|
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-10-22 19:03:44 +00:00
|
|
|
|
public static Ped SpawnPlayer (Player player, List<TransformDataStruct> spawns)
|
2019-04-24 23:14:06 +00:00
|
|
|
|
{
|
|
|
|
|
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();
|
2019-07-05 00:00:24 +00:00
|
|
|
|
var ped = Ped.SpawnPed(Ped.RandomPedId, spawn.position, spawn.rotation, false);
|
|
|
|
|
ped.NetPlayerOwnerGameObject = player.gameObject;
|
2019-04-25 00:01:40 +00:00
|
|
|
|
ped.WeaponHolder.autoAddWeapon = true;
|
2019-05-27 19:37:07 +00:00
|
|
|
|
// this ped should not be destroyed when he gets out of range
|
|
|
|
|
ped.gameObject.DestroyComponent<OutOfRangeDestroyer>();
|
2019-04-24 23:14:06 +00:00
|
|
|
|
|
2019-07-05 00:00:24 +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)
|
|
|
|
|
{
|
2019-04-24 23:59:01 +00:00
|
|
|
|
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
|
|
|
|
|
2019-10-22 22:07:45 +00:00
|
|
|
|
SpawnPlayer(player, GetSpawnPositions());
|
2019-04-24 23:59:01 +00:00
|
|
|
|
|
2019-04-24 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|