quantum-space-buddies/QSB/RoastingSync/TransformSync/RoastingStickTransformSync.cs

90 lines
3.3 KiB
C#
Raw Normal View History

2021-03-31 10:30:51 +01:00
using OWML.Utils;
using QSB.Player;
2021-04-11 17:05:02 +01:00
using QSB.TransformSync;
2021-03-30 17:28:05 +01:00
using System.Linq;
2021-03-31 10:30:51 +01:00
using UnityEngine;
2021-03-30 17:28:05 +01:00
2021-04-11 17:05:02 +01:00
namespace QSB.RoastingSync.TransformSync
2021-03-30 17:28:05 +01:00
{
2021-04-21 11:02:17 +01:00
internal class RoastingStickTransformSync : QSBNetworkTransform
2021-03-30 17:28:05 +01:00
{
2021-03-31 10:30:51 +01:00
private Transform _stickTip;
private Transform _networkStickTip => gameObject.transform.GetChild(0);
2021-03-31 10:40:21 +01:00
private const float SmoothTime = 0.1f;
private Vector3 _positionSmoothVelocity;
private Quaternion _rotationSmoothVelocity;
2021-03-31 10:30:51 +01:00
2021-03-31 15:07:18 +01:00
private Transform GetPivot()
2021-03-31 10:30:51 +01:00
=> Resources.FindObjectsOfTypeAll<RoastingStickController>().First().transform.Find("Stick_Root/Stick_Pivot");
2021-04-21 11:02:17 +01:00
protected override GameObject InitLocalTransform()
2021-03-31 10:30:51 +01:00
{
var pivot = GetPivot();
Player.RoastingStick = pivot.gameObject;
_stickTip = pivot.Find("Stick_Tip");
2021-04-21 11:02:17 +01:00
return pivot.gameObject;
2021-03-31 10:30:51 +01:00
}
2021-04-21 11:02:17 +01:00
protected override GameObject InitRemoteTransform()
2021-03-31 10:30:51 +01:00
{
var newPivot = Instantiate(GetPivot());
newPivot.parent = null;
newPivot.gameObject.SetActive(false);
Destroy(newPivot.Find("Stick_Tip/Props_HEA_RoastingStick/RoastingStick_Arm").gameObject);
Destroy(newPivot.Find("Stick_Tip/Props_HEA_RoastingStick/RoastingStick_Arm_NoSuit").gameObject);
var mallowRoot = newPivot.Find("Stick_Tip/Mallow_Root");
mallowRoot.gameObject.SetActive(false);
var oldMarshmallow = mallowRoot.GetComponent<Marshmallow>();
2021-03-31 15:02:59 +01:00
// Recreate particle system
Destroy(mallowRoot.Find("MallowSmoke").GetComponent<RelativisticParticleSystem>());
var newSystem = mallowRoot.Find("MallowSmoke").gameObject.AddComponent<CustomRelativisticParticleSystem>();
newSystem.Init(Player);
// Create new marshmallow
2021-03-31 10:30:51 +01:00
var newMarshmallow = mallowRoot.gameObject.AddComponent<QSBMarshmallow>();
newMarshmallow._fireRenderer = oldMarshmallow.GetValue<MeshRenderer>("_fireRenderer");
newMarshmallow._smokeParticles = oldMarshmallow.GetValue<ParticleSystem>("_smokeParticles");
newMarshmallow._mallowRenderer = oldMarshmallow.GetValue<MeshRenderer>("_mallowRenderer");
newMarshmallow._rawColor = oldMarshmallow.GetValue<Color>("_rawColor");
newMarshmallow._toastedColor = oldMarshmallow.GetValue<Color>("_toastedColor");
newMarshmallow._burntColor = oldMarshmallow.GetValue<Color>("_burntColor");
Destroy(oldMarshmallow);
2021-03-31 15:02:59 +01:00
2021-03-31 10:30:51 +01:00
Player.RoastingStick = newPivot.gameObject;
Player.Marshmallow = newMarshmallow;
mallowRoot.gameObject.SetActive(true);
_stickTip = newPivot.Find("Stick_Tip");
2021-04-21 11:02:17 +01:00
return newPivot.gameObject;
2021-03-31 10:30:51 +01:00
}
2021-04-21 11:02:17 +01:00
/*
2021-03-31 10:30:51 +01:00
protected override void UpdateTransform()
{
base.UpdateTransform();
if (_stickTip == null)
{
DebugLog.ToConsole($"Warning - _stickTip is null for player {PlayerId}", OWML.Common.MessageType.Warning);
return;
}
if (HasAuthority)
{
_networkStickTip.localPosition = _stickTip.localPosition;
_networkStickTip.localRotation = _stickTip.localRotation;
return;
}
2021-03-31 10:40:21 +01:00
_stickTip.localPosition = Vector3.SmoothDamp(_stickTip.localPosition, _networkStickTip.localPosition, ref _positionSmoothVelocity, SmoothTime);
_stickTip.localRotation = QuaternionHelper.SmoothDamp(_stickTip.localRotation, _networkStickTip.localRotation, ref _rotationSmoothVelocity, SmoothTime);
2021-03-31 10:30:51 +01:00
}
2021-04-21 11:02:17 +01:00
*/
2021-03-31 10:30:51 +01:00
public override bool IsReady => Locator.GetPlayerTransform() != null
&& Player != null
&& QSBPlayerManager.PlayerExists(Player.PlayerId)
2021-04-18 18:46:55 +01:00
&& Player.PlayerStates.IsReady
2021-03-31 10:30:51 +01:00
&& NetId.Value != uint.MaxValue
&& NetId.Value != 0U;
2021-03-30 17:28:05 +01:00
}
2021-03-31 10:30:51 +01:00
}