2020-07-05 16:37:56 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Mirror;
|
|
|
|
|
using SanAndreasUnity.Behaviours;
|
2020-07-01 21:36:01 +00:00
|
|
|
|
using SanAndreasUnity.Behaviours.Vehicles;
|
2020-07-05 16:37:56 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Items;
|
|
|
|
|
using SanAndreasUnity.Importing.Items.Definitions;
|
2020-07-05 20:33:34 +00:00
|
|
|
|
using SanAndreasUnity.Importing.Vehicles;
|
2020-07-01 21:36:01 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Net
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class NetworkedVehicleDetachedPart : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
public NetworkRigidBody NetworkRigidBody { get; private set; }
|
|
|
|
|
|
2020-07-05 16:37:56 +00:00
|
|
|
|
[SyncVar] uint m_net_vehicleNetId;
|
|
|
|
|
[SyncVar] int m_net_vehicleModelId;
|
2020-07-05 19:39:59 +00:00
|
|
|
|
[SyncVar] string m_net_vehicleColors;
|
2020-07-01 21:36:01 +00:00
|
|
|
|
[SyncVar] string m_net_frameName;
|
|
|
|
|
[SyncVar] float m_net_mass;
|
|
|
|
|
|
2020-07-05 16:37:56 +00:00
|
|
|
|
class VehicleInfo
|
|
|
|
|
{
|
|
|
|
|
public int numReferences;
|
|
|
|
|
public FrameContainer frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static readonly Dictionary<uint, VehicleInfo> s_dummyObjectsPerVehicle = new Dictionary<uint, VehicleInfo>();
|
|
|
|
|
|
|
|
|
|
bool m_isReferencingFrame = false;
|
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
|
2020-07-04 18:34:17 +00:00
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
this.NetworkRigidBody = this.GetComponentOrThrow<NetworkRigidBody>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 16:37:56 +00:00
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
if (m_isReferencingFrame)
|
|
|
|
|
{
|
|
|
|
|
m_isReferencingFrame = false;
|
|
|
|
|
if (s_dummyObjectsPerVehicle.TryGetValue(m_net_vehicleNetId, out VehicleInfo vehicleInfo))
|
|
|
|
|
{
|
|
|
|
|
vehicleInfo.numReferences--;
|
|
|
|
|
if (vehicleInfo.numReferences <= 0)
|
|
|
|
|
{
|
2020-07-05 19:39:59 +00:00
|
|
|
|
// TODO: check if it is null
|
2020-07-05 16:37:56 +00:00
|
|
|
|
Object.Destroy(vehicleInfo.frames.gameObject);
|
|
|
|
|
s_dummyObjectsPerVehicle.Remove(m_net_vehicleNetId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 19:39:59 +00:00
|
|
|
|
public void InitializeOnServer(uint vehicleNetId, int vehicleModelId, int[] vehicleColors, string frameName, float mass, Rigidbody rigidbody)
|
2020-07-01 21:36:01 +00:00
|
|
|
|
{
|
2020-07-04 18:34:17 +00:00
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
|
|
|
|
|
2020-07-05 16:37:56 +00:00
|
|
|
|
m_net_vehicleNetId = vehicleNetId;
|
|
|
|
|
m_net_vehicleModelId = vehicleModelId;
|
2020-07-05 19:39:59 +00:00
|
|
|
|
m_net_vehicleColors = VehicleController.SerializeColors(vehicleColors);
|
2020-07-01 21:36:01 +00:00
|
|
|
|
m_net_frameName = frameName;
|
|
|
|
|
m_net_mass = mass;
|
|
|
|
|
|
|
|
|
|
this.NetworkRigidBody.Rigidbody = rigidbody;
|
2020-07-01 22:59:17 +00:00
|
|
|
|
this.NetworkRigidBody.UpdateServer();
|
2020-07-01 21:36:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe(() => this.OnStartClientInternal());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnStartClientInternal()
|
|
|
|
|
{
|
2020-07-05 16:37:56 +00:00
|
|
|
|
GameObject vehicleGo = NetManager.GetNetworkObjectById(m_net_vehicleNetId);
|
2020-07-01 21:36:01 +00:00
|
|
|
|
if (null == vehicleGo)
|
|
|
|
|
{
|
2020-07-05 16:37:56 +00:00
|
|
|
|
// this should only happen when vehicle has been destroyed before client connected, but it's detached parts are still alive
|
|
|
|
|
// load vehicle's model, and attach it's frames to dummy object, and detach a frame from that dummy object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VehicleDef def = Item.GetDefinitionOrThrow<VehicleDef>(m_net_vehicleModelId);
|
|
|
|
|
|
2020-07-05 19:39:59 +00:00
|
|
|
|
int[] colors = VehicleController.DeserializeColors(m_net_vehicleColors);
|
|
|
|
|
|
2020-07-05 16:37:56 +00:00
|
|
|
|
var geometryParts = Vehicle.LoadGeometryParts(def);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VehicleInfo vehicleInfo;
|
|
|
|
|
if (s_dummyObjectsPerVehicle.TryGetValue(m_net_vehicleNetId, out vehicleInfo))
|
|
|
|
|
{
|
|
|
|
|
// we already created dummy object for this vehicle
|
|
|
|
|
vehicleInfo.numReferences++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// need to create dummy object for this vehicle, and attach all vehicle's frames to it
|
|
|
|
|
Transform transformDummy = new GameObject($"{def.GameName}_detached_part_{m_net_frameName}").transform;
|
|
|
|
|
vehicleInfo = new VehicleInfo();
|
|
|
|
|
vehicleInfo.frames = geometryParts.AttachFrames(transformDummy, Importing.Conversion.MaterialFlags.Vehicle);
|
2020-07-05 20:33:34 +00:00
|
|
|
|
SetColors(vehicleInfo.frames, colors);
|
2020-07-05 16:37:56 +00:00
|
|
|
|
vehicleInfo.numReferences = 1;
|
|
|
|
|
s_dummyObjectsPerVehicle.Add(m_net_vehicleNetId, vehicleInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_isReferencingFrame = true;
|
|
|
|
|
|
|
|
|
|
Frame frame = vehicleInfo.frames.FirstOrDefault(f => f.gameObject.name == m_net_frameName);
|
|
|
|
|
if (null == frame)
|
|
|
|
|
throw new System.Exception($"Failed to find frame by name {m_net_frameName}");
|
|
|
|
|
|
|
|
|
|
// detach frame from dummy object
|
2020-07-05 20:33:34 +00:00
|
|
|
|
Vehicle.DetachFrameFromTransformDuringExplosion(vehicleInfo.frames.transform, frame, m_net_mass, this.gameObject, m_net_vehicleNetId, m_net_vehicleModelId, colors);
|
2020-07-05 16:37:56 +00:00
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Vehicle vehicle = vehicleGo.GetComponentOrThrow<Vehicle>();
|
|
|
|
|
vehicle.DetachFrameDuringExplosion(m_net_frameName, m_net_mass, this.gameObject);
|
|
|
|
|
}
|
2020-07-05 16:37:56 +00:00
|
|
|
|
|
2021-02-14 14:13:39 +00:00
|
|
|
|
var rb = this.GetComponentInChildren<Rigidbody>();
|
|
|
|
|
if (rb != null)
|
|
|
|
|
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
|
|
|
|
this.NetworkRigidBody.Rigidbody = rb;
|
2020-07-05 16:37:56 +00:00
|
|
|
|
|
2020-07-01 21:36:01 +00:00
|
|
|
|
}
|
2020-07-05 20:33:34 +00:00
|
|
|
|
|
2021-02-13 22:04:47 +00:00
|
|
|
|
private static void SetColors(FrameContainer frames, int[] colors)
|
2020-07-05 20:33:34 +00:00
|
|
|
|
{
|
2021-02-13 22:04:47 +00:00
|
|
|
|
Vehicle.UpdateMaterials(frames, colors, new []{ 0f, 0f, 0f, 0f }, new MaterialPropertyBlock());
|
2020-07-05 20:33:34 +00:00
|
|
|
|
}
|
2020-07-01 21:36:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|