sync color of detached parts

This commit is contained in:
in0finite 2020-07-05 22:33:34 +02:00
parent f926bb7c24
commit 5816570fd2
3 changed files with 24 additions and 5 deletions

View file

@ -126,7 +126,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
private static int[] _sCarColorIds;
protected static int[] CarColorIds
public static int[] CarColorIds
{
get
{

View file

@ -224,10 +224,10 @@ namespace SanAndreasUnity.Behaviours.Vehicles
void DetachFrameDuringExplosion(Frame frame, float mass, GameObject parentGo)
{
DetachFrameFromTransformDuringExplosion(this.transform, frame, mass, parentGo, this.NetIdentity.netId, this.Definition.Id);
DetachFrameFromTransformDuringExplosion(this.transform, frame, mass, parentGo, this.NetIdentity.netId, this.Definition.Id, this.Colors);
}
public static void DetachFrameFromTransformDuringExplosion(Transform tr, Frame frame, float mass, GameObject parentGo, uint vehicleNetId, int vehicleModelId)
public static void DetachFrameFromTransformDuringExplosion(Transform tr, Frame frame, float mass, GameObject parentGo, uint vehicleNetId, int vehicleModelId, int[] vehicleColors)
{
if (! tr.IsParentOf(frame.transform)) // already detached ?
return;
@ -260,7 +260,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
Object.Destroy(parentGo, VehicleManager.Instance.explosionLeftoverPartsLifetime * Random.Range(0.8f, 1.2f));
var netScript = parentGo.GetComponentOrThrow<NetworkedVehicleDetachedPart>();
netScript.InitializeOnServer(vehicleNetId, vehicleModelId, frame.gameObject.name, mass, rigidBody);
netScript.InitializeOnServer(vehicleNetId, vehicleModelId, vehicleColors, frame.gameObject.name, mass, rigidBody);
NetManager.Spawn(parentGo);
}

View file

@ -5,6 +5,7 @@ using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Importing.Items;
using SanAndreasUnity.Importing.Items.Definitions;
using SanAndreasUnity.Importing.Vehicles;
using SanAndreasUnity.Utilities;
using UnityEngine;
@ -107,6 +108,7 @@ namespace SanAndreasUnity.Net
Transform transformDummy = new GameObject($"{def.GameName}_detached_part_{m_net_frameName}").transform;
vehicleInfo = new VehicleInfo();
vehicleInfo.frames = geometryParts.AttachFrames(transformDummy, Importing.Conversion.MaterialFlags.Vehicle);
SetColors(vehicleInfo.frames, colors);
vehicleInfo.numReferences = 1;
s_dummyObjectsPerVehicle.Add(m_net_vehicleNetId, vehicleInfo);
}
@ -118,7 +120,7 @@ namespace SanAndreasUnity.Net
throw new System.Exception($"Failed to find frame by name {m_net_frameName}");
// detach frame from dummy object
Vehicle.DetachFrameFromTransformDuringExplosion(vehicleInfo.frames.transform, frame, m_net_mass, this.gameObject, m_net_vehicleNetId, m_net_vehicleModelId);
Vehicle.DetachFrameFromTransformDuringExplosion(vehicleInfo.frames.transform, frame, m_net_mass, this.gameObject, m_net_vehicleNetId, m_net_vehicleModelId, colors);
}
else
@ -131,6 +133,23 @@ namespace SanAndreasUnity.Net
this.NetworkRigidBody.UpdateClient();
}
public static void SetColors(FrameContainer frames, int[] colors)
{
var props = new MaterialPropertyBlock();
var indices = CarColors.FromIndices(colors);
int[] vehicleColorIds = Vehicle.CarColorIds;
for (int i = 0; i < vehicleColorIds.Length; ++i)
props.SetColor(vehicleColorIds[i], indices[i]);
foreach (var mr in frames.Select(f => f.GetComponent<MeshRenderer>()).Where(mr => mr != null))
{
mr.SetPropertyBlock(props);
}
}
}
}