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

104 lines
2.7 KiB
C#
Raw Normal View History

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-24 01:59:56 +02: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 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-04-24 01:59:56 +02:00
void Update_Net()
{
2019-04-24 15:54:17 +02:00
if (!this.isServer)
return;
2019-04-26 02:23:54 +02:00
if (this.PedDef != null && this.PedDef.Id != m_net_pedId)
2019-04-24 15:54:17 +02:00
m_net_pedId = this.PedDef.Id;
2019-04-26 02:23:54 +02:00
string newStateName = this.CurrentState != null ? this.CurrentState.GetType().Name : "";
if (newStateName != m_net_state)
m_net_state = newStateName;
2019-04-24 01:59:56 +02:00
//m_net_weapon = this.CurrentWeapon;
}
2019-04-27 03:11:07 +02:00
void FixedUpdate_Net()
{
// send input to server
}
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-04-26 01:39:07 +02:00
void Net_OnStateChanged(string newStateName)
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
if (string.IsNullOrEmpty(newStateName))
{
// 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( () => {
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-24 01:59:56 +02:00
}
}
}