SanAndreasUnity/Assets/Scripts/Behaviours/Ped/Ped_Spawning.cs

113 lines
2.9 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Importing.Items;
using SanAndreasUnity.Importing.Items.Definitions;
using System.Linq;
namespace SanAndreasUnity.Behaviours
{
public partial class Ped
2020-05-31 17:07:22 +00:00
{
public static IEnumerable<PedestrianDef> SpawnablePedDefs {
get {
return Item.GetDefinitions<PedestrianDef> ().Where (def => def.Id != 0 && def.ModelName != "WMYST"
&& def.TextureDictionaryName != "generic");
}
}
public static Ped SpawnPed (PedestrianDef def, Vector3 pos, Quaternion rot, bool spawnOnNetwork)
2020-05-31 17:07:22 +00:00
{
2019-04-24 14:58:13 +00:00
Net.NetStatus.ThrowIfNotOnServer();
2020-05-31 17:07:22 +00:00
CheckPedPrefab ();
var go = Instantiate (PedManager.Instance.pedPrefab, pos, rot);
go.name = "Ped " + def.ModelName + " " + def.Id;
var ped = go.GetComponentOrThrow<Ped> ();
ped.PlayerModel.StartingPedId = def.Id;
ped.EnterVehicleRadius = PedManager.Instance.AIVehicleEnterDistance;
if (spawnOnNetwork)
Net.NetManager.Spawn(go);
2019-04-24 13:54:17 +00:00
2020-05-31 17:07:22 +00:00
return ped;
}
public static Ped SpawnPed (int pedId, Vector3 pos, Quaternion rot, bool spawnOnNetwork)
2020-05-31 17:07:22 +00:00
{
var def = Item.GetDefinition<PedestrianDef> (pedId);
if (null == def)
throw new System.ArgumentException ("Failed to spawn ped: definition not found by id: " + pedId);
return SpawnPed (def, pos, rot, spawnOnNetwork);
2020-05-31 17:07:22 +00:00
}
public static PedAI SpawnPedAI(int pedId, Vector3 pos, Quaternion rot)
2020-05-31 17:07:22 +00:00
{
var ped = SpawnPed(pedId, pos, rot, true);
return ped.gameObject.GetOrAddComponent<PedAI>();
2020-05-31 17:07:22 +00:00
}
2021-09-12 22:08:06 +00:00
public static PedAI SpawnPedAI(int pedId, Transform nearbyTransform)
{
if (GetPositionForPedSpawn (out var pos, out var rot, nearbyTransform))
return SpawnPedAI(pedId, pos, rot);
return null;
}
2021-09-11 16:04:50 +00:00
public static PedAI SpawnPedStalker (int pedId, Vector3 pos, Quaternion rot, Ped targetPed)
2020-05-31 17:07:22 +00:00
{
var ped = SpawnPed (pedId, pos, rot, true);
2020-05-31 17:07:22 +00:00
2021-09-11 16:04:50 +00:00
var pedAI = ped.gameObject.GetOrAddComponent<PedAI> ();
pedAI.StartFollowing(targetPed);
2020-05-31 17:07:22 +00:00
2021-09-11 16:04:50 +00:00
return pedAI;
2020-05-31 17:07:22 +00:00
}
2021-09-11 16:04:50 +00:00
public static PedAI SpawnPedStalker (int pedId, Transform nearbyTransform, Ped targetPed)
2020-05-31 17:07:22 +00:00
{
Vector3 pos;
Quaternion rot;
if (GetPositionForPedSpawn (out pos, out rot, nearbyTransform))
2019-07-08 20:11:26 +00:00
return SpawnPedStalker (pedId, pos, rot, targetPed);
2020-05-31 17:07:22 +00:00
return null;
}
public static bool GetPositionForPedSpawn (out Vector3 pos, out Quaternion rot, Transform nearbyTransform)
2020-05-31 17:07:22 +00:00
{
pos = Vector3.zero;
rot = Quaternion.identity;
if (nearbyTransform != null) {
2020-05-31 17:07:22 +00:00
Vector3 offset = Random.onUnitSphere;
offset.y = 0f;
offset.Normalize ();
offset *= Random.Range (5f, 15f);
pos = nearbyTransform.TransformPoint (offset);
2020-05-31 17:07:22 +00:00
rot = Random.rotation;
return true;
}
return false;
}
private static void CheckPedPrefab ()
{
if(null == PedManager.Instance.pedPrefab)
throw new System.Exception ("Ped prefab is null");
}
}
}