351 lines
11 KiB
C#
351 lines
11 KiB
C#
using System;
|
|
using System.Linq;
|
|
using ECM2.Examples.FirstPerson;
|
|
using FishNet.Connection;
|
|
using FishNet.Managing;
|
|
using FishNet.Object;
|
|
using FishNet.Object.Synchronizing;
|
|
using FishNet.Serializing;
|
|
using FishNet.Serializing.Generated;
|
|
using FishNet.Transporting;
|
|
using KINEMATION.MagicBlend.Runtime;
|
|
using Obvious.Soap;
|
|
using RootMotion.FinalIK;
|
|
using SoapCustomVariable;
|
|
using UnityEngine;
|
|
|
|
public class MultiplayerAvatarControler : NetworkBehaviour
|
|
{
|
|
[SerializeField]
|
|
private FSMVariable playerState;
|
|
|
|
[SerializeField]
|
|
private Transform model;
|
|
|
|
[SerializeField]
|
|
private Transform eye;
|
|
|
|
[SerializeField]
|
|
private GameObjectVariable EyeGameObject;
|
|
|
|
[SerializeField]
|
|
private Transform rodParent;
|
|
|
|
[SerializeField]
|
|
private Transform handCatchTarget;
|
|
|
|
[SerializeField]
|
|
private Light flashLight;
|
|
|
|
[SerializeField]
|
|
private PlayerIK playerIK;
|
|
|
|
[SerializeField]
|
|
[Range(0.5f, 30f)]
|
|
[Tooltip("Low value => more smoothing, High value => less delay")]
|
|
private float posRotLerpSpeed = 5f;
|
|
|
|
[SerializeField]
|
|
private Waga waga;
|
|
|
|
[SerializeField]
|
|
private Rigidbody PrecisePickBone;
|
|
|
|
[SerializeField]
|
|
private MagicBlending magicBlending;
|
|
|
|
[SerializeField]
|
|
private GameObject distanceSource;
|
|
|
|
private FirstPersonCharacter firstPersonCharacter;
|
|
|
|
private Animator animatorSource;
|
|
|
|
private Animator animatorTarget;
|
|
|
|
private FullBodyBipedIK fullBodyBipedIK;
|
|
|
|
private Light localFlashlight;
|
|
|
|
private MultiplayerFishingSetControler remotePlayerSet;
|
|
|
|
private MagicBlendAsset remoteBlendAsset;
|
|
|
|
public readonly SyncVar<Quaternion> eyeRotation = new SyncVar<Quaternion>();
|
|
|
|
public readonly SyncVar<float> torsoLayerWeight = new SyncVar<float>();
|
|
|
|
public readonly SyncVar<Vector3> leftHandTargetPosition = new SyncVar<Vector3>();
|
|
|
|
public readonly SyncVar<State> state = new SyncVar<State>();
|
|
|
|
public readonly SyncVar<bool> isFlashLightOn = new SyncVar<bool>();
|
|
|
|
private bool NetworkInitialize___EarlyMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted;
|
|
|
|
private bool NetworkInitialize___LateMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted;
|
|
|
|
public Transform RodParent => rodParent;
|
|
|
|
public State State => state.Value;
|
|
|
|
public Waga Waga => waga;
|
|
|
|
public static event Action<MultiplayerAvatarControler> OnStartLocal;
|
|
|
|
public override void OnStartClient()
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
base.OnStartClient();
|
|
MultiplayerAvatarControler.OnStartLocal?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
public override void OnStopClient()
|
|
{
|
|
base.OnStopClient();
|
|
CleanupRemoteObjects();
|
|
}
|
|
|
|
public void Init(FirstPersonCharacter firstPersonCharacter)
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
this.firstPersonCharacter = firstPersonCharacter;
|
|
animatorSource = firstPersonCharacter.GetComponentInChildren<Animator>();
|
|
animatorTarget = GetComponentInChildren<Animator>();
|
|
fullBodyBipedIK = firstPersonCharacter.GetComponentInChildren<FullBodyBipedIK>();
|
|
localFlashlight = firstPersonCharacter.GetComponentInChildren<FlashLight>()?.GetComponent<Light>();
|
|
DisableRenderers();
|
|
flashLight.enabled = false;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
UpdateLocal();
|
|
}
|
|
else
|
|
{
|
|
UpdateRemote();
|
|
}
|
|
}
|
|
|
|
private void UpdateLocal()
|
|
{
|
|
if (!(firstPersonCharacter == null))
|
|
{
|
|
base.transform.SetPositionAndRotation(firstPersonCharacter.transform.position, firstPersonCharacter.transform.rotation);
|
|
SyncAnimators();
|
|
UpdateSyncVars((EyeGameObject.Value != null) ? EyeGameObject.Value.transform.localRotation : Quaternion.identity, animatorSource.GetLayerWeight(1), (fullBodyBipedIK.solver.leftHandEffector.target != null) ? fullBodyBipedIK.solver.leftHandEffector.target.position : Vector3.zero, playerState.Value, (bool)localFlashlight && localFlashlight.enabled);
|
|
}
|
|
}
|
|
|
|
private void UpdateRemote()
|
|
{
|
|
eye.localRotation = eyeRotation.Value;
|
|
if ((object)animatorTarget == null)
|
|
{
|
|
animatorTarget = GetComponentInChildren<Animator>();
|
|
}
|
|
animatorTarget.SetLayerWeight(1, torsoLayerWeight.Value);
|
|
playerIK.SetFishingLeftArm(enabled: false);
|
|
playerIK.SetAimIK(enabled: false);
|
|
playerIK.SetBipedLeftHandIK(enabled: false);
|
|
playerIK.SetBipedRightHandIK(enabled: false);
|
|
switch (state.Value)
|
|
{
|
|
case State.idle:
|
|
case State.move:
|
|
playerIK.SetAimIK(enabled: true);
|
|
break;
|
|
case State.fishing:
|
|
case State.baitFlies:
|
|
case State.fight:
|
|
if (remotePlayerSet == null || !remotePlayerSet.IsSpawned)
|
|
{
|
|
remotePlayerSet = base.Owner.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<MultiplayerFishingSetControler>() != null)?.GetComponent<MultiplayerFishingSetControler>();
|
|
}
|
|
if ((bool)remotePlayerSet && (bool)remotePlayerSet.SpawnedReel)
|
|
{
|
|
playerIK.SetFishingLeftArm(enabled: true, remotePlayerSet.SpawnedReel.FingersIKAnchor);
|
|
}
|
|
else
|
|
{
|
|
playerIK.SetFishingLeftArm(enabled: false);
|
|
}
|
|
playerIK.SetAimIK(enabled: true);
|
|
break;
|
|
case State.fishView:
|
|
case State.collectFish:
|
|
case State.throwFish:
|
|
handCatchTarget.position = Vector3.Lerp(handCatchTarget.position, leftHandTargetPosition.Value, Time.deltaTime * posRotLerpSpeed);
|
|
playerIK.SetBipedLeftHandIK(enabled: true, handCatchTarget);
|
|
break;
|
|
}
|
|
flashLight.enabled = isFlashLightOn.Value;
|
|
waga.gameObject.SetActive(state.Value == State.collectFish);
|
|
Vector3 eulerAngles = waga.transform.eulerAngles;
|
|
waga.transform.eulerAngles = new Vector3(eulerAngles.x, PrecisePickBone.transform.eulerAngles.y, eulerAngles.z);
|
|
if (remoteBlendAsset == null)
|
|
{
|
|
remoteBlendAsset = UnityEngine.Object.Instantiate(magicBlending.BlendAsset);
|
|
magicBlending.SetMagicBlendAsset(remoteBlendAsset);
|
|
}
|
|
if (remoteBlendAsset != null)
|
|
{
|
|
remoteBlendAsset.globalWeight = ((state.Value == State.collectFish) ? 1f : 0f);
|
|
}
|
|
distanceSource.SetActive(value: true);
|
|
}
|
|
|
|
private void SyncAnimators()
|
|
{
|
|
AnimatorControllerParameter[] parameters = animatorSource.parameters;
|
|
foreach (AnimatorControllerParameter animatorControllerParameter in parameters)
|
|
{
|
|
switch (animatorControllerParameter.type)
|
|
{
|
|
case AnimatorControllerParameterType.Bool:
|
|
{
|
|
bool value2 = animatorSource.GetBool(animatorControllerParameter.nameHash);
|
|
animatorTarget.SetBool(animatorControllerParameter.nameHash, value2);
|
|
break;
|
|
}
|
|
case AnimatorControllerParameterType.Float:
|
|
{
|
|
float value = animatorSource.GetFloat(animatorControllerParameter.nameHash);
|
|
animatorTarget.SetFloat(animatorControllerParameter.nameHash, value);
|
|
break;
|
|
}
|
|
case AnimatorControllerParameterType.Int:
|
|
{
|
|
int integer = animatorSource.GetInteger(animatorControllerParameter.nameHash);
|
|
animatorTarget.SetInteger(animatorControllerParameter.nameHash, integer);
|
|
break;
|
|
}
|
|
case AnimatorControllerParameterType.Trigger:
|
|
if (animatorSource.GetBool(animatorControllerParameter.nameHash) && !animatorTarget.GetBool(animatorControllerParameter.nameHash))
|
|
{
|
|
animatorTarget.SetTrigger(animatorControllerParameter.nameHash);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DisableRenderers()
|
|
{
|
|
Renderer[] componentsInChildren = model.GetComponentsInChildren<Renderer>();
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
componentsInChildren[i].enabled = false;
|
|
}
|
|
}
|
|
|
|
private void CleanupRemoteObjects()
|
|
{
|
|
if (remoteBlendAsset != null)
|
|
{
|
|
UnityEngine.Object.Destroy(remoteBlendAsset);
|
|
}
|
|
}
|
|
|
|
[ServerRpc]
|
|
private void UpdateSyncVars(Quaternion eyeRotation, float torsoLayerWeight, Vector3 keftHandTargetPosition, State state, bool isFlashLightOn)
|
|
{
|
|
RpcWriter___Server_UpdateSyncVars_2078261150(eyeRotation, torsoLayerWeight, keftHandTargetPosition, state, isFlashLightOn);
|
|
}
|
|
|
|
public override void NetworkInitialize___Early()
|
|
{
|
|
if (!NetworkInitialize___EarlyMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted)
|
|
{
|
|
NetworkInitialize___EarlyMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted = true;
|
|
base.NetworkInitialize___Early();
|
|
isFlashLightOn.InitializeEarly(this, 4u, isSyncObject: false);
|
|
state.InitializeEarly(this, 3u, isSyncObject: false);
|
|
leftHandTargetPosition.InitializeEarly(this, 2u, isSyncObject: false);
|
|
torsoLayerWeight.InitializeEarly(this, 1u, isSyncObject: false);
|
|
eyeRotation.InitializeEarly(this, 0u, isSyncObject: false);
|
|
RegisterServerRpc(0u, RpcReader___Server_UpdateSyncVars_2078261150);
|
|
}
|
|
}
|
|
|
|
public override void NetworkInitialize___Late()
|
|
{
|
|
if (!NetworkInitialize___LateMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted)
|
|
{
|
|
NetworkInitialize___LateMultiplayerAvatarControlerAssembly_002DCSharp_002Edll_Excuted = true;
|
|
base.NetworkInitialize___Late();
|
|
isFlashLightOn.InitializeLate();
|
|
state.InitializeLate();
|
|
leftHandTargetPosition.InitializeLate();
|
|
torsoLayerWeight.InitializeLate();
|
|
eyeRotation.InitializeLate();
|
|
}
|
|
}
|
|
|
|
public override void NetworkInitializeIfDisabled()
|
|
{
|
|
NetworkInitialize___Early();
|
|
NetworkInitialize___Late();
|
|
}
|
|
|
|
private void RpcWriter___Server_UpdateSyncVars_2078261150(Quaternion eyeRotation, float torsoLayerWeight, Vector3 keftHandTargetPosition, State state, bool isFlashLightOn)
|
|
{
|
|
if (!base.IsClientInitialized)
|
|
{
|
|
NetworkManager networkManager = base.NetworkManager;
|
|
networkManager.LogWarning("Cannot complete action because client is not active. This may also occur if the object is not yet initialized, has deinitialized, or if it does not contain a NetworkObject component.");
|
|
return;
|
|
}
|
|
if (!base.IsOwner)
|
|
{
|
|
NetworkManager networkManager2 = base.NetworkManager;
|
|
networkManager2.LogWarning("Cannot complete action because you are not the owner of this object. .");
|
|
return;
|
|
}
|
|
Channel channel = Channel.Reliable;
|
|
PooledWriter pooledWriter = WriterPool.Retrieve();
|
|
pooledWriter.WriteQuaternion32(eyeRotation);
|
|
pooledWriter.WriteSingle(torsoLayerWeight);
|
|
pooledWriter.WriteVector3(keftHandTargetPosition);
|
|
// GeneratedWriters___Internal.GWrite___SoapCustomVariable_002EStateFishNet_002ESerializing_002EGenerated(pooledWriter, state);
|
|
pooledWriter.WriteBoolean(isFlashLightOn);
|
|
SendServerRpc(0u, pooledWriter, channel, DataOrderType.Default);
|
|
pooledWriter.Store();
|
|
}
|
|
|
|
private void RpcLogic___UpdateSyncVars_2078261150(Quaternion P_0, float P_1, Vector3 P_2, State P_3, bool P_4)
|
|
{
|
|
eyeRotation.Value = P_0;
|
|
torsoLayerWeight.Value = P_1;
|
|
leftHandTargetPosition.Value = P_2;
|
|
state.Value = P_3;
|
|
isFlashLightOn.Value = P_4;
|
|
}
|
|
|
|
private void RpcReader___Server_UpdateSyncVars_2078261150(PooledReader PooledReader0, Channel channel, NetworkConnection conn)
|
|
{
|
|
Quaternion quaternion = PooledReader0.ReadQuaternion32();
|
|
float num = PooledReader0.ReadSingle();
|
|
Vector3 vector = PooledReader0.ReadVector3();
|
|
// State state = GeneratedReaders___Internal.GRead___SoapCustomVariable_002EStateFishNet_002ESerializing_002EGenerateds(PooledReader0);
|
|
bool flag = PooledReader0.ReadBoolean();
|
|
if (base.IsServerInitialized && OwnerMatches(conn))
|
|
{
|
|
// RpcLogic___UpdateSyncVars_2078261150(quaternion, num, vector, state, flag);
|
|
}
|
|
}
|
|
|
|
public virtual void Awake()
|
|
{
|
|
NetworkInitialize___Early();
|
|
NetworkInitialize___Late();
|
|
}
|
|
}
|