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

106 lines
2.9 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
{
[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;
}
2019-04-27 01:11:07 +00:00
void FixedUpdate_Net()
{
// send input to server
//if (!NetStatus.IsServer && this.IsControlledByLocalPlayer && PedSync.Local != null)
// PedSync.Local.SendInput(this.IsWalkOn, this.IsRunOn, this.IsSprintOn, this.Movement, this.IsJumpOn);
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
{
this.SwitchState(newState.GetType());
}
});
2019-04-23 23:59:56 +00:00
}
}
}