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

296 lines
11 KiB
C#
Raw Normal View History

2021-01-07 17:44:47 +01:00
using System.Collections.Generic;
2021-01-05 21:09:25 +01:00
using System.Linq;
using Mirror;
using SanAndreasUnity.Importing.Items;
using SanAndreasUnity.Importing.Items.Definitions;
using SanAndreasUnity.Net;
2021-01-05 18:21:57 +01:00
using SanAndreasUnity.Utilities;
2021-01-05 03:09:45 +01:00
using UnityEngine;
2021-01-05 21:09:25 +01:00
using Object = UnityEngine.Object;
using Random = UnityEngine.Random;
2021-01-05 03:09:45 +01:00
namespace SanAndreasUnity.Behaviours.Peds
{
2021-01-05 21:09:25 +01:00
public class DeadBody : NetworkBehaviour
2021-01-05 03:09:45 +01:00
{
private static List<DeadBody> _deadBodies = new List<DeadBody>();
public static IEnumerable<DeadBody> DeadBodies => _deadBodies;
public static int NumDeadBodies => _deadBodies.Count;
2021-01-05 18:21:57 +01:00
public PushableByDamage PushableByDamage { get; private set; }
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-05 21:09:25 +01:00
2021-01-05 22:58:47 +01:00
private Dictionary<int, Rigidbody> m_rigidBodiesDict = new Dictionary<int, Rigidbody>();
2021-01-05 21:09:25 +01:00
[SyncVar] private int m_net_modelId;
private class SyncDictionaryIntVector3 : SyncDictionary<int, Vector3>
{
}
private SyncDictionaryIntVector3 m_syncDictionaryBonePositions = new SyncDictionaryIntVector3();
private SyncDictionaryIntVector3 m_syncDictionaryBoneRotations = new SyncDictionaryIntVector3();
2021-01-05 22:58:47 +01:00
private SyncDictionaryIntVector3 m_syncDictionaryBoneVelocities = new SyncDictionaryIntVector3();
2021-01-05 21:09:25 +01:00
2021-01-05 18:21:57 +01:00
private void Awake()
{
this.PushableByDamage = this.GetComponentOrThrow<PushableByDamage>();
this.PushableByDamage.forceMultiplier = PedManager.Instance.ragdollDamageForceWhenDetached;
2021-01-05 21:32:20 +01:00
this.RefreshSyncRate();
2021-01-05 18:21:57 +01:00
}
2021-01-05 03:09:45 +01:00
private void OnEnable()
{
_deadBodies.Add(this);
}
private void OnDisable()
{
_deadBodies.Remove(this);
}
2021-01-05 21:09:25 +01: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 15:26:11 +01:00
var model = this.gameObject.GetOrAddComponent<PedModel>();
model.Load(m_net_modelId);
// build character joints - we need them to constraint the bones
model.RagdollBuilder.BuildBodies();
foreach (var rb in this.transform.GetComponentsInChildren<Rigidbody>())
{
rb.isKinematic = true;
rb.interpolation = RigidbodyInterpolation.Extrapolate;
}
model.RagdollBuilder.BuildJoints();
m_framesDict = model.Frames.ToDictionary(f => f.BoneId, f => new BoneInfo(f.transform));
2021-01-05 22:58:47 +01:00
2021-01-07 15:26:11 +01:00
Object.Destroy(model.AnimComponent);
Object.Destroy(model);
2021-01-05 21:09:25 +01:00
2021-01-07 17:50:58 +01:00
// apply initial sync data
2021-01-05 21:09:25 +01:00
foreach (var pair in m_framesDict)
{
2021-01-07 17:50:58 +01:00
int boneId = pair.Key;
BoneInfo boneInfo = pair.Value;
2021-01-07 17:50:58 +01:00
if (m_syncDictionaryBonePositions.TryGetValue(boneId, out Vector3 pos))
SetPosition(boneInfo, pos);
2021-01-07 17:50:58 +01:00
if (m_syncDictionaryBoneRotations.TryGetValue(boneId, out Vector3 rotation))
SetRotation(boneInfo, rotation);
2021-01-07 17:50:58 +01:00
if (m_syncDictionaryBoneVelocities.TryGetValue(boneId, out Vector3 receivedVelocity))
SetVelocity(boneInfo, receivedVelocity);
2021-01-05 21:09:25 +01:00
}
// register to dictionary callbacks
2021-01-07 15:26:11 +01:00
// RegisterDictionaryCallback(
// m_syncDictionaryBonePositions,
// (tr, pos) => tr.localPosition = pos);
// RegisterDictionaryCallback(
// m_syncDictionaryBoneRotations,
// (tr, rotation) => tr.localRotation = Quaternion.Euler(rotation));
2021-01-05 21:09:25 +01:00
RegisterDictionaryCallback(
2021-01-07 15:26:11 +01:00
m_syncDictionaryBoneVelocities,
2021-01-07 17:47:45 +01:00
SetVelocity);
2021-01-05 21:09:25 +01:00
}
private void RegisterDictionaryCallback<T>(SyncDictionary<int, T> dict, System.Action<BoneInfo, T> action)
2021-01-05 21:09:25 +01:00
{
dict.Callback += (op, key, item) => F.RunExceptionSafe(() =>
{
if (m_framesDict.TryGetValue(key, out BoneInfo boneInfo))
action(boneInfo, item);
2021-01-05 21:09:25 +01: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 21:09:25 +01:00
deadBody.m_framesDict = ragdollTransform.GetComponentsInChildren<Frame>()
.ToDictionary(f => f.BoneId, f => new BoneInfo(f.transform));
2021-01-05 21:09:25 +01:00
2021-01-05 22:58:47 +01:00
foreach (var pair in deadBody.m_framesDict)
{
var rb = pair.Value.Rigidbody;
2021-01-05 22:58:47 +01:00
if (rb != null)
deadBody.m_rigidBodiesDict.Add(pair.Key, rb);
}
2021-01-05 21:09:25 +01:00
deadBody.InitSyncVarsOnServer(ped);
NetManager.Spawn(ragdollGameObject);
return deadBody;
}
2021-01-05 21:09:25 +01:00
private void InitSyncVarsOnServer(Ped ped)
{
m_net_modelId = ped.PedDef.Id;
// assign initial bones transformations
foreach (var pair in m_framesDict)
{
m_syncDictionaryBonePositions.Add(pair.Key, pair.Value.Transform.localPosition);
m_syncDictionaryBoneRotations.Add(pair.Key, pair.Value.Transform.localRotation.eulerAngles);
2021-01-05 21:09:25 +01:00
}
2021-01-05 22:58:47 +01:00
// assign initial velocities
foreach (var pair in m_rigidBodiesDict)
{
2021-01-07 16:51:30 +01:00
m_syncDictionaryBoneVelocities.Add(pair.Key, GetVelocityForSending(pair.Value));
2021-01-05 22:58:47 +01:00
}
2021-01-05 21:09:25 +01:00
}
private void Update()
{
if (NetStatus.IsServer)
{
foreach (var pair in m_framesDict)
{
Vector3 pos = pair.Value.Transform.localPosition;
Vector3 rotation = pair.Value.Transform.localRotation.eulerAngles;
2021-01-05 21:09:25 +01:00
if (m_syncDictionaryBonePositions[pair.Key] != pos)
m_syncDictionaryBonePositions[pair.Key] = pos;
if (m_syncDictionaryBoneRotations[pair.Key] != rotation)
m_syncDictionaryBoneRotations[pair.Key] = rotation;
}
2021-01-05 22:58:47 +01:00
foreach (var pair in m_rigidBodiesDict)
{
2021-01-07 16:51:30 +01:00
Vector3 velocity = GetVelocityForSending(pair.Value);
2021-01-05 22:58:47 +01:00
if (m_syncDictionaryBoneVelocities[pair.Key] != velocity)
m_syncDictionaryBoneVelocities[pair.Key] = velocity;
}
2021-01-05 21:09:25 +01:00
}
else
{
2021-01-07 15:26:11 +01:00
foreach (var pair in m_framesDict)
2021-01-05 22:58:47 +01:00
{
int boneId = pair.Key;
BoneInfo boneInfo = pair.Value;
Transform tr = boneInfo.Transform;
2021-01-07 15:26:11 +01:00
2021-01-07 16:51:30 +01:00
// rotation
if (m_syncDictionaryBoneRotations.TryGetValue(boneId, out Vector3 rotation))
SetRotation(boneInfo, rotation);
2021-01-07 16:51:30 +01:00
// after rotation is applied, transform velocity to local space and predict position based on it
2021-01-07 15:26:11 +01:00
if (m_syncDictionaryBonePositions.TryGetValue(boneId, out Vector3 pos))
{
Vector3 targetPos = pos;
2021-01-07 15:43:56 +01:00
// predict position based on velocity and sync interval
2021-01-07 16:51:30 +01:00
// if (boneId == 0) // only for root bone
2021-01-07 15:26:11 +01:00
// {
2021-01-07 16:51:30 +01:00
// if (m_syncDictionaryBoneVelocities.TryGetValue(boneId, out Vector3 receivedVelocity))
// {
// Vector3 localVelocity = GetReceivedVelocityAsLocal(tr, receivedVelocity);
// targetPos += localVelocity * this.syncInterval;
// }
2021-01-07 15:26:11 +01:00
// }
SetPosition(boneInfo, targetPos);
2021-01-07 15:26:11 +01:00
}
2021-01-05 22:58:47 +01:00
}
2021-01-07 15:26:11 +01:00
2021-01-07 15:43:56 +01:00
// apply velocity on clients - this is done by rigid bodies ?
2021-01-07 15:26:11 +01:00
// foreach (var pair in m_syncDictionaryBoneVelocities)
// {
// int boneId = pair.Key;
2021-01-07 16:51:30 +01:00
// Vector3 receivedVelocity = pair.Value;
2021-01-07 20:59:36 +01:00
// if (m_framesDict.TryGetValue(boneId, out BoneInfo boneInfo))
2021-01-07 16:51:30 +01:00
// {
2021-01-07 20:59:36 +01:00
// if (null == boneInfo.Rigidbody) // only for bones which don't have rigid body
// {
// Vector3 localVelocity = GetReceivedVelocityAsLocal(boneInfo.Transform, receivedVelocity);
// boneInfo.Transform.localPosition += localVelocity * Time.deltaTime;
// }
2021-01-07 16:51:30 +01:00
// }
2021-01-07 15:26:11 +01:00
// }
2021-01-05 21:09:25 +01:00
}
}
2021-01-05 21:32:20 +01:00
public void RefreshSyncRate()
{
this.syncInterval = 1.0f / PedManager.Instance.ragdollSyncRate;
}
2021-01-07 16:51:30 +01: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 17:47:45 +01:00
private static void SetPosition(BoneInfo boneInfo, Vector3 receivedPosition)
{
boneInfo.Transform.localPosition = receivedPosition;
}
private static void SetRotation(BoneInfo boneInfo, Vector3 receivedRotation)
{
boneInfo.Transform.localRotation = Quaternion.Euler(receivedRotation);
}
private static void SetVelocity(BoneInfo boneInfo, Vector3 receivedVelocity)
2021-01-07 17:47:45 +01:00
{
boneInfo.Rigidbody.velocity = GetReceivedVelocityAsWorld(boneInfo.Transform, receivedVelocity);
2021-01-07 17:47:45 +01:00
}
2021-01-05 03:09:45 +01:00
}
}