SanAndreasUnity/Assets/Scripts/Behaviours/Ped/Ped_Networking.cs

118 lines
3.5 KiB
C#
Raw Normal View History

2019-04-23 23:59:56 +00:00
using System.Collections.Generic;
using UnityEngine;
using Mirror;
2019-04-25 23:39:07 +00:00
using System.Linq;
using SanAndreasUnity.Utilities;
2019-04-27 13:36:44 +00:00
using SanAndreasUnity.Net;
2019-04-23 23:59:56 +00:00
namespace SanAndreasUnity.Behaviours
{
public partial class Ped
{
public NetworkTransform NetTransform { get; private set; }
public float DefaultTransformSyncInterval { get; private set; }
[Range(1f / 60f, 0.5f)] [SerializeField] float m_inputSendInterval = 1f / 30f;
float m_timeSinceSentInput = 0f;
2019-04-23 23:59:56 +00:00
[SyncVar(hook=nameof(Net_OnIdChanged))] int m_net_pedId = 0;
[SyncVar(hook=nameof(Net_OnStateChanged))] string m_net_state = "";
//[SyncVar] Weapon m_net_weapon = null;
void Awake_Net()
{
this.NetTransform = this.GetComponentOrThrow<NetworkTransform>();
this.DefaultTransformSyncInterval = this.NetTransform.syncInterval;
2019-04-23 23:59:56 +00:00
}
2019-04-24 00:50:28 +00:00
public override void OnStartClient()
{
base.OnStartClient();
if (this.isServer)
return;
2019-04-24 13:54:17 +00:00
//this.PlayerModel.Load(m_net_pedId);
2019-04-24 00:50:28 +00:00
}
2019-04-23 23:59:56 +00:00
void Update_Net()
{
// update syncvars
if (NetStatus.IsServer)
{
if (this.PedDef != null && this.PedDef.Id != m_net_pedId)
m_net_pedId = this.PedDef.Id;
2019-04-26 00:23:54 +00:00
string newStateName = this.CurrentState != null ? this.CurrentState.GetType().Name : "";
if (newStateName != m_net_state)
m_net_state = newStateName;
}
// 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 00:23:54 +00:00
2019-04-23 23:59:56 +00:00
}
2019-04-27 01:11:07 +00:00
void FixedUpdate_Net()
{
2019-04-27 01:11:07 +00:00
}
2019-04-23 23:59:56 +00:00
void Net_OnIdChanged(int newId)
{
2019-04-25 23:45:11 +00:00
Debug.LogFormat("ped (net id {0}) changed model id to {1}", this.netId, newId);
2019-04-23 23:59:56 +00:00
if (this.isServer)
return;
2019-04-26 21:39:33 +00:00
//m_net_pedId = newId;
2019-04-25 23:39:07 +00:00
2019-04-26 21:42:43 +00:00
if (newId > 0)
F.RunExceptionSafe( () => this.PlayerModel.Load(newId) );
2019-04-23 23:59:56 +00:00
}
2019-04-25 23:39:07 +00:00
void Net_OnStateChanged(string newStateName)
2019-04-23 23:59:56 +00:00
{
2019-04-27 00:37:51 +00:00
//Debug.LogFormat("ped (net id {0}) changed state to {1}", this.netId, newStateName);
2019-04-25 23:45:11 +00:00
2019-04-23 23:59:56 +00:00
if (this.isServer)
return;
2019-04-26 21:39:33 +00:00
//m_net_state = newStateName;
2019-04-25 23:39:07 +00:00
if (string.IsNullOrEmpty(newStateName))
{
// don't do anything, this only happens when creating the ped
return;
}
2019-04-25 23:39:07 +00:00
// forcefully change the state
F.RunExceptionSafe( () => {
var newState = this.States.FirstOrDefault(state => state.GetType().Name == newStateName);
if (null == newState)
{
Debug.LogErrorFormat("New ped state '{0}' could not be found", newStateName);
}
else
{
newState.OnSwitchedStateByServer();
2019-04-25 23:39:07 +00:00
}
});
2019-04-23 23:59:56 +00:00
}
}
}