SanAndreasUnity/Assets/Scripts/Networking/PedSync.cs

143 lines
4.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-04-27 22:19:22 +00:00
public void SendInput(bool isWalkOn, bool isRunOn, bool isSprintOn, Vector3 movementInput, bool isJumpOn, Vector3 heading)
2019-04-27 01:11:07 +00:00
{
2019-04-27 22:19:22 +00:00
this.CmdSendingInput(isWalkOn, isRunOn, isSprintOn, movementInput, isJumpOn, heading);
2019-04-27 01:11:07 +00:00
}
public void SendInput()
{
Ped ped = m_ped;
2019-04-27 22:19:22 +00:00
this.SendInput(ped.IsWalkOn, ped.IsRunOn, ped.IsSprintOn, ped.Movement, ped.IsJumpOn, ped.Heading);
}
2019-04-27 01:11:07 +00:00
[Command]
2019-04-27 22:19:22 +00:00
void CmdSendingInput(bool isWalkOn, bool isRunOn, bool isSprintOn, Vector3 movementInput, bool isJumpOn, Vector3 heading)
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-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-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-04-28 22:02:46 +00:00
public void PedStartedEnteringVehicle(Ped ped)
2019-04-28 20:57:04 +00:00
{
NetStatus.ThrowIfNotOnServer();
2019-04-28 22:22:41 +00:00
if (this.isLocalPlayer)
return;
// send message to client
this.TargetPedStartedEnteringVehicle(this.connectionToClient, ped.gameObject, ped.CurrentVehicle.gameObject,
ped.CurrentVehicleSeatAlignment);
}
[TargetRpc]
void TargetPedStartedEnteringVehicle(NetworkConnection conn, GameObject pedGo,
GameObject vehicleGo, Vehicle.SeatAlignment seatAlignment)
{
if (null == pedGo || null == vehicleGo)
return;
2019-04-28 21:17:50 +00:00
2019-04-28 22:22:41 +00:00
pedGo.GetComponent<Ped>().GetStateOrLogError<VehicleEnteringState>()
.EnterVehicle(vehicleGo.GetComponent<Vehicle>(), seatAlignment, false);
2019-04-28 21:17:50 +00:00
}
2019-04-28 22:02:46 +00:00
public void PedEnteredVehicle(Ped ped)
2019-04-28 21:17:50 +00:00
{
NetStatus.ThrowIfNotOnServer();
2019-04-28 23:46:13 +00:00
if (this.isLocalPlayer)
return;
this.TargetPedEnteredVehicle(this.connectionToClient, ped.gameObject, ped.CurrentVehicle.gameObject,
ped.CurrentVehicleSeatAlignment);
}
[TargetRpc]
void TargetPedEnteredVehicle(NetworkConnection conn, GameObject pedGo,
GameObject vehicleGo, Vehicle.SeatAlignment seatAlignment)
{
if (null == pedGo || null == vehicleGo)
return;
pedGo.GetComponent<Ped>().GetStateOrLogError<VehicleSittingState>()
.EnterVehicle(vehicleGo.GetComponent<Vehicle>(), seatAlignment);
2019-04-28 21:17:50 +00:00
}
2019-04-27 01:11:07 +00:00
}
}