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

91 lines
2.3 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-23 23:59:56 +00:00
namespace SanAndreasUnity.Behaviours
{
public partial class Ped
{
[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()
{
}
void Start_Net()
{
}
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()
{
2019-04-24 13:54:17 +00:00
if (!this.isServer)
return;
2019-04-26 00:23:54 +00:00
if (this.PedDef != null && this.PedDef.Id != m_net_pedId)
2019-04-24 13:54:17 +00:00
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;
2019-04-23 23:59:56 +00:00
//m_net_weapon = this.CurrentWeapon;
}
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-25 23:39:07 +00:00
m_net_pedId = newId;
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-25 23:45:11 +00:00
Debug.LogFormat("ped (net id {0}) changed state to {1}", this.netId, newStateName);
2019-04-23 23:59:56 +00:00
if (this.isServer)
return;
2019-04-25 23:39:07 +00:00
m_net_state = newStateName;
// 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
{
this.SwitchState(newState.GetType());
}
});
2019-04-23 23:59:56 +00:00
}
}
}