2019-04-24 01:59:56 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Mirror;
|
2019-04-26 01:39:07 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
2019-04-27 15:36:44 +02:00
|
|
|
|
using SanAndreasUnity.Net;
|
2019-04-24 01:59:56 +02:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours
|
|
|
|
|
{
|
|
|
|
|
public partial class Ped
|
|
|
|
|
{
|
2019-05-06 01:51:25 +02:00
|
|
|
|
public NetworkTransform NetTransform { get; private set; }
|
|
|
|
|
|
2019-04-28 15:56:29 +02:00
|
|
|
|
[Range(1f / 60f, 0.5f)] [SerializeField] float m_inputSendInterval = 1f / 30f;
|
|
|
|
|
float m_timeSinceSentInput = 0f;
|
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
[SyncVar(hook=nameof(Net_OnIdChanged))] int m_net_pedId = 0;
|
2019-05-25 20:16:45 +02:00
|
|
|
|
|
|
|
|
|
struct StateSyncData
|
|
|
|
|
{
|
|
|
|
|
public string state;
|
|
|
|
|
public string additionalData;
|
|
|
|
|
}
|
|
|
|
|
[SyncVar(hook=nameof(Net_OnStateChanged))] StateSyncData m_net_stateData;
|
|
|
|
|
//[SyncVar] string m_net_additionalStateData = "";
|
|
|
|
|
//[SyncVar(hook=nameof(Net_OnStateChanged))] string m_net_state = "";
|
2019-04-24 01:59:56 +02:00
|
|
|
|
//[SyncVar] Weapon m_net_weapon = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Awake_Net()
|
|
|
|
|
{
|
2019-05-06 01:51:25 +02:00
|
|
|
|
this.NetTransform = this.GetComponentOrThrow<NetworkTransform>();
|
2019-04-24 01:59:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 02:50:28 +02:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartClient();
|
|
|
|
|
|
|
|
|
|
if (this.isServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-04-24 15:54:17 +02:00
|
|
|
|
//this.PlayerModel.Load(m_net_pedId);
|
2019-04-24 02:50:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 23:51:39 +02:00
|
|
|
|
void Start_Net()
|
|
|
|
|
{
|
|
|
|
|
this.ApplySyncRate(PedManager.Instance.pedSyncRate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ApplySyncRate(float newSyncRate)
|
|
|
|
|
{
|
2019-05-25 00:50:49 +02:00
|
|
|
|
float newSyncInterval = 1.0f / newSyncRate;
|
|
|
|
|
|
2019-05-24 23:51:39 +02:00
|
|
|
|
foreach (var comp in this.GetComponents<NetworkBehaviour>())
|
2019-05-25 00:50:49 +02:00
|
|
|
|
comp.syncInterval = newSyncInterval;
|
|
|
|
|
|
|
|
|
|
// also change it for NetworkTransform, because it can be disabled
|
|
|
|
|
if (this.NetTransform != null)
|
|
|
|
|
this.NetTransform.syncInterval = newSyncInterval;
|
2019-05-24 23:51:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
void Update_Net()
|
|
|
|
|
{
|
2019-04-28 15:56:29 +02:00
|
|
|
|
|
|
|
|
|
// update syncvars
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
if (this.PedDef != null && this.PedDef.Id != m_net_pedId)
|
|
|
|
|
m_net_pedId = this.PedDef.Id;
|
2019-04-26 02:23:54 +02:00
|
|
|
|
|
2019-04-28 15:56:29 +02:00
|
|
|
|
string newStateName = this.CurrentState != null ? this.CurrentState.GetType().Name : "";
|
2019-05-25 20:16:45 +02:00
|
|
|
|
if (newStateName != m_net_stateData.state)
|
|
|
|
|
{
|
|
|
|
|
// state changed
|
|
|
|
|
|
|
|
|
|
// obtain additional data from state
|
|
|
|
|
byte[] data = this.CurrentState != null ? this.CurrentState.GetAdditionalNetworkData() : null;
|
|
|
|
|
// assign additional data
|
|
|
|
|
m_net_stateData.additionalData = data != null ? System.Text.Encoding.UTF8.GetString(data) : "";
|
|
|
|
|
// assign new state
|
|
|
|
|
m_net_stateData.state = newStateName;
|
|
|
|
|
}
|
2019-04-28 15:56:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send input to server
|
|
|
|
|
if (!NetStatus.IsServer && this.IsControlledByLocalPlayer && PedSync.Local != null)
|
|
|
|
|
{
|
|
|
|
|
m_timeSinceSentInput += Time.unscaledDeltaTime;
|
|
|
|
|
if (m_timeSinceSentInput >= m_inputSendInterval)
|
|
|
|
|
{
|
|
|
|
|
m_timeSinceSentInput = 0f;
|
|
|
|
|
PedSync.Local.SendInput();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-26 02:23:54 +02:00
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 03:11:07 +02:00
|
|
|
|
void FixedUpdate_Net()
|
|
|
|
|
{
|
2019-04-28 15:56:29 +02:00
|
|
|
|
|
2019-04-27 03:11:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
void Net_OnIdChanged(int newId)
|
|
|
|
|
{
|
2019-04-26 01:45:11 +02:00
|
|
|
|
Debug.LogFormat("ped (net id {0}) changed model id to {1}", this.netId, newId);
|
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
if (this.isServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-04-26 23:39:33 +02:00
|
|
|
|
//m_net_pedId = newId;
|
2019-04-26 01:39:07 +02:00
|
|
|
|
|
2019-04-26 23:42:43 +02:00
|
|
|
|
if (newId > 0)
|
|
|
|
|
F.RunExceptionSafe( () => this.PlayerModel.Load(newId) );
|
2019-04-24 01:59:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 20:16:45 +02:00
|
|
|
|
void Net_OnStateChanged(StateSyncData newStateData)
|
2019-04-24 01:59:56 +02:00
|
|
|
|
{
|
2019-04-27 02:37:51 +02:00
|
|
|
|
//Debug.LogFormat("ped (net id {0}) changed state to {1}", this.netId, newStateName);
|
2019-04-26 01:45:11 +02:00
|
|
|
|
|
2019-04-24 01:59:56 +02:00
|
|
|
|
if (this.isServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-04-26 23:39:33 +02:00
|
|
|
|
//m_net_state = newStateName;
|
2019-04-26 01:39:07 +02:00
|
|
|
|
|
2019-05-25 20:16:45 +02:00
|
|
|
|
if (string.IsNullOrEmpty(newStateData.state))
|
2019-04-27 02:48:52 +02:00
|
|
|
|
{
|
|
|
|
|
// don't do anything, this only happens when creating the ped
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 01:39:07 +02:00
|
|
|
|
// forcefully change the state
|
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe( () => {
|
2019-05-25 20:16:45 +02:00
|
|
|
|
var newState = this.States.FirstOrDefault(state => state.GetType().Name == newStateData.state);
|
2019-04-26 01:39:07 +02:00
|
|
|
|
if (null == newState)
|
|
|
|
|
{
|
2019-05-25 20:16:45 +02:00
|
|
|
|
Debug.LogErrorFormat("New ped state '{0}' could not be found", newStateData.state);
|
2019-04-26 01:39:07 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-25 20:16:45 +02:00
|
|
|
|
byte[] data = string.IsNullOrEmpty(newStateData.additionalData) ? null : System.Text.Encoding.UTF8.GetBytes(newStateData.additionalData);
|
|
|
|
|
newState.OnSwitchedStateByServer(data);
|
2019-04-26 01:39:07 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2019-04-24 01:59:56 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|