2021-01-07 16:44:47 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-05 20:09:25 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Mirror;
|
|
|
|
|
using SanAndreasUnity.Importing.Items;
|
|
|
|
|
using SanAndreasUnity.Importing.Items.Definitions;
|
|
|
|
|
using SanAndreasUnity.Net;
|
2021-01-05 17:21:57 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2021-01-05 02:09:45 +00:00
|
|
|
|
using UnityEngine;
|
2021-01-05 20:09:25 +00:00
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
using Random = UnityEngine.Random;
|
2021-01-05 02:09:45 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.Peds
|
|
|
|
|
{
|
2021-01-05 20:09:25 +00:00
|
|
|
|
public class DeadBody : NetworkBehaviour
|
2021-01-05 02:09:45 +00:00
|
|
|
|
{
|
|
|
|
|
private static List<DeadBody> _deadBodies = new List<DeadBody>();
|
|
|
|
|
public static IEnumerable<DeadBody> DeadBodies => _deadBodies;
|
|
|
|
|
public static int NumDeadBodies => _deadBodies.Count;
|
|
|
|
|
|
2021-01-05 17:21:57 +00:00
|
|
|
|
public PushableByDamage PushableByDamage { get; private set; }
|
|
|
|
|
|
2021-01-07 17:32:59 +00:00
|
|
|
|
private struct BoneInfo
|
|
|
|
|
{
|
|
|
|
|
public BoneInfo(Transform transform)
|
|
|
|
|
{
|
|
|
|
|
this.Transform = transform;
|
|
|
|
|
this.Rigidbody = transform.GetComponent<Rigidbody>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Transform Transform { get; set; }
|
|
|
|
|
public Rigidbody Rigidbody { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, BoneInfo> m_framesDict = new Dictionary<int, BoneInfo>();
|
2021-01-08 01:57:39 +00:00
|
|
|
|
public int NumBones => m_framesDict.Count;
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
2021-01-05 21:58:47 +00:00
|
|
|
|
private Dictionary<int, Rigidbody> m_rigidBodiesDict = new Dictionary<int, Rigidbody>();
|
|
|
|
|
|
2021-01-08 01:40:45 +00:00
|
|
|
|
private int m_net_modelId;
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
2021-01-08 01:40:45 +00:00
|
|
|
|
private struct BoneSyncData
|
2021-01-05 20:09:25 +00:00
|
|
|
|
{
|
2021-01-08 01:40:45 +00:00
|
|
|
|
public byte boneId;
|
|
|
|
|
public Vector3 position;
|
|
|
|
|
public Vector3 rotation;
|
|
|
|
|
public Vector3 velocity;
|
2021-01-05 20:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-08 01:40:45 +00:00
|
|
|
|
private List<BoneSyncData> m_bonesSyncData = new List<BoneSyncData>();
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
2021-01-05 17:21:57 +00:00
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
this.PushableByDamage = this.GetComponentOrThrow<PushableByDamage>();
|
|
|
|
|
this.PushableByDamage.forceMultiplier = PedManager.Instance.ragdollDamageForceWhenDetached;
|
2021-01-05 20:32:20 +00:00
|
|
|
|
|
|
|
|
|
this.RefreshSyncRate();
|
2021-01-05 17:21:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 02:09:45 +00:00
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
_deadBodies.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
_deadBodies.Remove(this);
|
|
|
|
|
}
|
2021-01-05 18:30:14 +00:00
|
|
|
|
|
2021-01-05 20:09:25 +00:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe(this.InitialClientOnlySetup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitialClientOnlySetup()
|
|
|
|
|
{
|
|
|
|
|
var def = Item.GetDefinition<PedestrianDef>(m_net_modelId);
|
|
|
|
|
if (null == def)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"Failed to initialize dead body: ped definition not found by id {m_net_modelId}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.gameObject.name = $"dead body {m_net_modelId} {def.ModelName}";
|
|
|
|
|
|
2021-01-07 14:26:11 +00:00
|
|
|
|
var model = this.gameObject.GetOrAddComponent<PedModel>();
|
|
|
|
|
model.Load(m_net_modelId);
|
|
|
|
|
|
2021-01-07 23:18:46 +00:00
|
|
|
|
// add rigid bodies - syncing looks smoother with them
|
2021-01-07 14:26:11 +00:00
|
|
|
|
model.RagdollBuilder.BuildBodies();
|
|
|
|
|
foreach (var rb in this.transform.GetComponentsInChildren<Rigidbody>())
|
|
|
|
|
{
|
2021-01-07 20:35:14 +00:00
|
|
|
|
rb.useGravity = false;
|
|
|
|
|
rb.detectCollisions = false;
|
2021-01-07 22:21:28 +00:00
|
|
|
|
rb.maxAngularVelocity = 0;
|
2021-01-07 20:02:12 +00:00
|
|
|
|
rb.interpolation = PedManager.Instance.ragdollInterpolationMode;
|
2021-01-07 14:26:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 17:32:59 +00:00
|
|
|
|
m_framesDict = model.Frames.ToDictionary(f => f.BoneId, f => new BoneInfo(f.transform));
|
2021-01-05 21:58:47 +00:00
|
|
|
|
|
2021-01-07 14:26:11 +00:00
|
|
|
|
Object.Destroy(model.AnimComponent);
|
|
|
|
|
Object.Destroy(model);
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
2021-01-07 16:50:58 +00:00
|
|
|
|
// apply initial sync data
|
2021-01-08 01:40:45 +00:00
|
|
|
|
// first sync should've been done before calling this function
|
|
|
|
|
this.UpdateBonesAfterDeserialization((byte)m_bonesSyncData.Count);
|
|
|
|
|
|
2021-01-05 20:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-08 01:40:45 +00:00
|
|
|
|
public override bool OnSerialize(NetworkWriter writer, bool initialState)
|
|
|
|
|
{
|
|
|
|
|
if (initialState)
|
|
|
|
|
writer.Write(m_net_modelId);
|
|
|
|
|
|
|
|
|
|
writer.Write((byte)m_framesDict.Count);
|
|
|
|
|
|
|
|
|
|
foreach (var pair in m_framesDict)
|
|
|
|
|
{
|
|
|
|
|
int boneId = pair.Key;
|
|
|
|
|
Transform tr = pair.Value.Transform;
|
|
|
|
|
|
|
|
|
|
var boneSyncData = new BoneSyncData();
|
|
|
|
|
boneSyncData.boneId = (byte)boneId;
|
|
|
|
|
boneSyncData.position = tr.localPosition;
|
|
|
|
|
boneSyncData.rotation = tr.localRotation.eulerAngles;
|
|
|
|
|
boneSyncData.velocity = m_rigidBodiesDict.TryGetValue(boneId, out Rigidbody rb) ? GetVelocityForSending(rb) : Vector3.zero;
|
|
|
|
|
|
|
|
|
|
writer.Write(boneSyncData.boneId);
|
|
|
|
|
writer.Write(boneSyncData.position);
|
|
|
|
|
writer.Write(boneSyncData.rotation);
|
|
|
|
|
writer.Write(boneSyncData.velocity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnDeserialize(NetworkReader reader, bool initialState)
|
|
|
|
|
{
|
|
|
|
|
if (initialState)
|
|
|
|
|
m_net_modelId = reader.ReadInt32();
|
|
|
|
|
|
|
|
|
|
byte count = reader.ReadByte();
|
|
|
|
|
|
|
|
|
|
m_bonesSyncData.EnsureCount(count);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var boneSyncData = new BoneSyncData();
|
|
|
|
|
boneSyncData.boneId = reader.ReadByte();
|
|
|
|
|
boneSyncData.position = reader.ReadVector3();
|
|
|
|
|
boneSyncData.rotation = reader.ReadVector3();
|
|
|
|
|
boneSyncData.velocity = reader.ReadVector3();
|
|
|
|
|
m_bonesSyncData[i] = boneSyncData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe(() => UpdateBonesAfterDeserialization(count));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateBonesAfterDeserialization(byte count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var boneSyncData = m_bonesSyncData[i];
|
|
|
|
|
if (m_framesDict.TryGetValue(boneSyncData.boneId, out BoneInfo boneInfo))
|
|
|
|
|
{
|
|
|
|
|
SetPosition(boneInfo, boneSyncData.position);
|
|
|
|
|
SetRotation(boneInfo, boneSyncData.rotation);
|
|
|
|
|
if (boneInfo.Rigidbody != null)
|
|
|
|
|
SetVelocity(boneInfo, boneSyncData.velocity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 18:30:14 +00:00
|
|
|
|
public static DeadBody Create(Transform ragdollTransform, Ped ped)
|
|
|
|
|
{
|
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
|
|
|
|
|
|
|
|
|
GameObject ragdollGameObject = Object.Instantiate(PedManager.Instance.ragdollPrefab);
|
|
|
|
|
DeadBody deadBody = ragdollGameObject.GetComponentOrThrow<DeadBody>();
|
|
|
|
|
|
|
|
|
|
Object.Destroy(ragdollGameObject, PedManager.Instance.ragdollLifetime * Random.Range(0.85f, 1.15f));
|
|
|
|
|
|
|
|
|
|
ragdollGameObject.name = "dead body " + ped.name;
|
|
|
|
|
|
|
|
|
|
ragdollTransform.SetParent(ragdollGameObject.transform);
|
|
|
|
|
|
2021-01-05 20:09:25 +00:00
|
|
|
|
deadBody.m_framesDict = ragdollTransform.GetComponentsInChildren<Frame>()
|
2021-01-07 17:32:59 +00:00
|
|
|
|
.ToDictionary(f => f.BoneId, f => new BoneInfo(f.transform));
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
2021-01-05 21:58:47 +00:00
|
|
|
|
foreach (var pair in deadBody.m_framesDict)
|
|
|
|
|
{
|
2021-01-07 17:32:59 +00:00
|
|
|
|
var rb = pair.Value.Rigidbody;
|
2021-01-05 21:58:47 +00:00
|
|
|
|
if (rb != null)
|
|
|
|
|
deadBody.m_rigidBodiesDict.Add(pair.Key, rb);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 20:09:25 +00:00
|
|
|
|
deadBody.InitSyncVarsOnServer(ped);
|
|
|
|
|
|
2021-01-05 18:30:14 +00:00
|
|
|
|
NetManager.Spawn(ragdollGameObject);
|
|
|
|
|
|
|
|
|
|
return deadBody;
|
|
|
|
|
}
|
2021-01-05 20:09:25 +00:00
|
|
|
|
|
|
|
|
|
private void InitSyncVarsOnServer(Ped ped)
|
|
|
|
|
{
|
|
|
|
|
m_net_modelId = ped.PedDef.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
2021-01-08 01:40:45 +00:00
|
|
|
|
this.SetDirtyBit(1);
|
2021-01-05 20:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-05 20:32:20 +00:00
|
|
|
|
|
|
|
|
|
public void RefreshSyncRate()
|
|
|
|
|
{
|
|
|
|
|
this.syncInterval = 1.0f / PedManager.Instance.ragdollSyncRate;
|
|
|
|
|
}
|
2021-01-07 15:51:30 +00:00
|
|
|
|
|
|
|
|
|
private static Vector3 GetVelocityForSending(Rigidbody rb)
|
|
|
|
|
{
|
|
|
|
|
// it's better to send local velocity, because rotation of ragdoll can change very fast, and so
|
|
|
|
|
// will the world velocity
|
|
|
|
|
return rb.transform.InverseTransformVector(rb.velocity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Vector3 GetReceivedVelocityAsLocal(Transform tr, Vector3 receivedVelocity)
|
|
|
|
|
{
|
|
|
|
|
return receivedVelocity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Vector3 GetReceivedVelocityAsWorld(Transform tr, Vector3 receivedVelocity)
|
|
|
|
|
{
|
|
|
|
|
return tr.TransformVector(receivedVelocity);
|
|
|
|
|
}
|
2021-01-07 16:47:45 +00:00
|
|
|
|
|
2021-01-07 17:48:02 +00:00
|
|
|
|
private static void SetPosition(BoneInfo boneInfo, Vector3 receivedPosition)
|
|
|
|
|
{
|
2021-01-07 20:43:03 +00:00
|
|
|
|
// if (boneInfo.Rigidbody != null)
|
|
|
|
|
// boneInfo.Rigidbody.MovePosition(boneInfo.Transform.TransformVector(receivedPosition));
|
|
|
|
|
// else
|
|
|
|
|
// boneInfo.Transform.localPosition = receivedPosition;
|
|
|
|
|
|
2021-01-07 17:48:02 +00:00
|
|
|
|
boneInfo.Transform.localPosition = receivedPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetRotation(BoneInfo boneInfo, Vector3 receivedRotation)
|
|
|
|
|
{
|
2021-01-07 20:43:03 +00:00
|
|
|
|
// Quaternion localRotation = Quaternion.Euler(receivedRotation);
|
|
|
|
|
// if (boneInfo.Rigidbody != null)
|
|
|
|
|
// boneInfo.Rigidbody.MoveRotation(boneInfo.Transform.TransformRotation(localRotation));
|
|
|
|
|
// else
|
|
|
|
|
// boneInfo.Transform.localRotation = localRotation;
|
|
|
|
|
|
2021-01-07 17:48:02 +00:00
|
|
|
|
boneInfo.Transform.localRotation = Quaternion.Euler(receivedRotation);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 17:32:59 +00:00
|
|
|
|
private static void SetVelocity(BoneInfo boneInfo, Vector3 receivedVelocity)
|
2021-01-07 16:47:45 +00:00
|
|
|
|
{
|
2021-01-07 17:32:59 +00:00
|
|
|
|
boneInfo.Rigidbody.velocity = GetReceivedVelocityAsWorld(boneInfo.Transform, receivedVelocity);
|
2021-01-07 16:47:45 +00:00
|
|
|
|
}
|
2021-01-05 02:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|