SanAndreasUnity/Assets/Scripts/Networking/PedSync.cs

201 lines
6.2 KiB
C#
Raw Normal View History

2019-04-27 01:11:07 +00:00
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using System.Linq;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours;
2019-04-28 22:22:41 +00:00
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.Behaviours.Peds.States;
2019-04-27 01:11:07 +00:00
namespace SanAndreasUnity.Net
{
public class PedSync : NetworkBehaviour
{
Player m_player;
Ped m_ped { get { return m_player.OwnedPed; } }
2019-04-27 13:36:44 +00:00
public static PedSync Local { get; private set; }
2019-04-27 01:11:07 +00:00
void Awake()
{
m_player = this.GetComponentOrThrow<Player>();
}
2019-04-27 13:36:44 +00:00
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
Local = this;
}
2019-04-27 01:11:07 +00:00
void Start()
{
}
2019-07-07 00:09:06 +00:00
public void SendInput(bool isWalkOn, bool isRunOn, bool isSprintOn, Vector3 movementInput,
2019-07-12 23:20:12 +00:00
bool isJumpOn, Vector3 heading, Vector3 aimDir, Vector3 firePos, Vector3 fireDir, bool isAimOn, bool isFireOn)
2019-04-27 01:11:07 +00:00
{
2019-07-12 23:20:12 +00:00
this.CmdSendingInput(isWalkOn, isRunOn, isSprintOn, movementInput, isJumpOn, heading, aimDir, firePos, fireDir, isAimOn, isFireOn);
2019-04-27 01:11:07 +00:00
}
public void SendInput()
{
Ped ped = m_ped;
2019-07-08 00:00:26 +00:00
this.SendInput(ped.IsWalkOn, ped.IsRunOn, ped.IsSprintOn, ped.Movement, ped.IsJumpOn, ped.Heading,
2019-07-12 23:20:12 +00:00
ped.AimDirection, ped.FirePosition, ped.FireDirection, ped.IsAimOn, ped.IsFireOn);
}
2019-04-27 01:11:07 +00:00
[Command]
2019-07-07 00:09:06 +00:00
void CmdSendingInput(bool isWalkOn, bool isRunOn, bool isSprintOn, Vector3 movementInput,
2019-07-12 23:20:12 +00:00
bool isJumpOn, Vector3 heading, Vector3 aimDir, Vector3 firePos, Vector3 fireDir, bool isAimOn, bool isFireOn)
2019-04-27 01:11:07 +00:00
{
if (null == m_ped)
return;
Ped ped = m_ped;
ped.IsWalkOn = isWalkOn;
ped.IsRunOn = isRunOn;
ped.IsSprintOn = isSprintOn;
ped.Movement = movementInput;
ped.IsJumpOn = isJumpOn;
2019-04-27 22:19:22 +00:00
ped.Heading = heading;
2019-07-07 00:09:06 +00:00
ped.AimDirection = aimDir;
2019-07-12 23:20:12 +00:00
ped.NetFirePos = firePos;
2019-07-12 20:17:22 +00:00
ped.NetFireDir = fireDir;
2019-07-08 00:00:26 +00:00
ped.IsAimOn = isAimOn;
ped.IsFireOn = isFireOn;
2019-04-27 01:11:07 +00:00
}
2019-04-29 22:52:02 +00:00
public void SendVehicleInput(float acceleration, float steering, float braking) =>
this.CmdSendingVehicleInput(acceleration, steering, braking);
[Command]
void CmdSendingVehicleInput(float acceleration, float steering, float braking)
{
if (null == m_ped || null == m_ped.CurrentVehicle)
return;
var v = m_ped.CurrentVehicle;
v.Accelerator = acceleration;
v.Steering = steering;
v.Braking = braking;
}
2019-07-08 00:07:40 +00:00
public void OnButtonPressed(string buttonName) => this.CmdOnButtonPressed(buttonName);
[Command]
void CmdOnButtonPressed(string buttonName)
{
if (m_ped != null)
m_ped.OnButtonPressed(buttonName);
}
2019-04-28 17:13:01 +00:00
public void OnCrouchButtonPressed() => this.CmdOnCrouchButtonPressed();
[Command]
void CmdOnCrouchButtonPressed()
{
if (m_ped)
m_ped.OnCrouchButtonPressed();
}
2019-04-29 18:34:13 +00:00
public void OnSubmitButtonPressed() => this.CmdOnSubmitButtonPressed();
[Command]
void CmdOnSubmitButtonPressed()
{
if (m_ped)
m_ped.OnSubmitPressed();
}
2019-07-08 00:07:40 +00:00
public void OnFireButtonPressed() => this.CmdOnFireButtonPressed();
[Command]
void CmdOnFireButtonPressed()
{
if (m_ped != null)
m_ped.OnFireButtonPressed();
}
public void OnAimButtonPressed() => this.CmdOnAimButtonPressed();
[Command]
void CmdOnAimButtonPressed()
{
if (m_ped != null)
m_ped.OnAimButtonPressed();
}
public void OnNextWeaponButtonPressed() => this.CmdOnNextWeaponButtonPressed();
[Command]
void CmdOnNextWeaponButtonPressed()
{
if (m_ped != null)
m_ped.OnNextWeaponButtonPressed();
}
public void OnPreviousWeaponButtonPressed() => this.CmdOnPreviousWeaponButtonPressed();
[Command]
void CmdOnPreviousWeaponButtonPressed()
{
if (m_ped != null)
m_ped.OnPreviousWeaponButtonPressed();
}
2019-07-08 00:07:40 +00:00
2019-07-08 14:17:55 +00:00
public static void OnWeaponFired(Ped ped, Weapon weapon, Vector3 firePos)
{
foreach (var p in Player.AllPlayersEnumerable)
p.GetComponent<PedSync>().TargetOnWeaponFired(p.connectionToClient, ped.gameObject, weapon.gameObject, firePos);
}
[TargetRpc]
void TargetOnWeaponFired(NetworkConnection conn, GameObject pedGo, GameObject weaponGo, Vector3 firePos)
{
if (NetStatus.IsServer)
return;
if (null == weaponGo || null == pedGo)
return;
F.RunExceptionSafe( () => {
var ped = pedGo.GetComponent<Ped>();
var weapon = weaponGo.GetComponent<Weapon>();
if (ped.CurrentState != null)
ped.CurrentState.OnWeaponFiredFromServer(weapon, firePos);
});
}
public static void SendDamagedEvent(GameObject damagedPedGo, GameObject attackingPedGo, float damageAmount)
{
NetStatus.ThrowIfNotOnServer();
foreach (var p in Player.AllPlayersEnumerable)
{
p.GetComponent<PedSync>().TargetDamagedEvent(p.connectionToClient, damagedPedGo, attackingPedGo, damageAmount);
}
}
[TargetRpc]
private void TargetDamagedEvent(NetworkConnection conn, GameObject damagedPedGo,
GameObject attackingPedGo, float damageAmount)
{
F.RunExceptionSafe(() =>
{
Ped damagedPed = damagedPedGo != null ? damagedPedGo.GetComponent<Ped>() : null;
Ped attackingPed = attackingPedGo != null ? attackingPedGo.GetComponent<Ped>() : null;
if (damagedPed != null)
damagedPed.OnReceivedDamageEventFromServer(damageAmount, attackingPed);
});
}
2019-04-27 01:11:07 +00:00
}
}