2020-05-31 17:07:22 +00:00
|
|
|
|
using UnityEngine;
|
2019-04-29 18:27:17 +00:00
|
|
|
|
using SanAndreasUnity.Net;
|
2019-04-29 22:18:19 +00:00
|
|
|
|
using Mirror;
|
2019-04-29 22:40:26 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2019-04-30 22:33:01 +00:00
|
|
|
|
using System.Linq;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.Vehicles
|
|
|
|
|
{
|
2019-04-29 22:18:19 +00:00
|
|
|
|
|
|
|
|
|
public class VehicleController : NetworkBehaviour
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2019-04-29 22:11:14 +00:00
|
|
|
|
private Vehicle m_vehicle;
|
2019-05-22 16:59:16 +00:00
|
|
|
|
bool IsControlledByLocalPlayer => Ped.Instance != null && Ped.Instance.CurrentVehicle == m_vehicle && Ped.Instance.CurrentVehicleSeat.IsDriver;
|
2019-04-29 22:18:19 +00:00
|
|
|
|
|
2019-04-29 23:23:31 +00:00
|
|
|
|
[SyncVar] int m_net_id;
|
2019-04-30 22:33:01 +00:00
|
|
|
|
[SyncVar] string m_net_carColors;
|
2019-04-29 23:23:31 +00:00
|
|
|
|
[SyncVar] float m_net_acceleration;
|
|
|
|
|
[SyncVar] float m_net_steering;
|
|
|
|
|
[SyncVar] float m_net_braking;
|
2019-04-30 00:37:29 +00:00
|
|
|
|
[SyncVar] Vector3 m_net_linearVelocity;
|
|
|
|
|
[SyncVar] Vector3 m_net_angularVelocity;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2019-04-29 23:23:31 +00:00
|
|
|
|
// is it better to place syncvars in Vehicle class ? - that way, there is no need for hooks
|
|
|
|
|
// - or we could assign/read syncvars in Update()
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2019-04-29 22:18:19 +00:00
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2019-04-29 22:40:26 +00:00
|
|
|
|
//m_vehicle = GetComponent<Vehicle>();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 23:41:18 +00:00
|
|
|
|
internal void OnAfterCreateVehicle()
|
|
|
|
|
{
|
|
|
|
|
m_vehicle = this.GetComponent<Vehicle>();
|
|
|
|
|
m_net_id = m_vehicle.Definition.Id;
|
2019-04-30 22:33:01 +00:00
|
|
|
|
if (m_vehicle.Colors != null)
|
|
|
|
|
m_net_carColors = string.Join(";", m_vehicle.Colors);
|
2019-04-29 23:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 22:18:19 +00:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartClient();
|
|
|
|
|
|
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
{
|
2019-04-29 22:40:26 +00:00
|
|
|
|
F.RunExceptionSafe( () => {
|
2019-05-05 22:26:55 +00:00
|
|
|
|
// load vehicle on clients
|
2019-04-30 22:33:01 +00:00
|
|
|
|
int[] colors = string.IsNullOrEmpty(m_net_carColors) ? null : m_net_carColors.Split(';').Select(s => int.Parse(s)).ToArray();
|
|
|
|
|
m_vehicle = Vehicle.Create(this.gameObject, m_net_id, colors, this.transform.position, this.transform.rotation);
|
2019-05-05 22:26:55 +00:00
|
|
|
|
|
|
|
|
|
// update rigid body status
|
|
|
|
|
this.EnableOrDisableRigidBody();
|
2019-04-29 22:40:26 +00:00
|
|
|
|
});
|
2019-04-29 22:18:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 22:26:55 +00:00
|
|
|
|
public override void OnStartAuthority()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartAuthority();
|
|
|
|
|
this.EnableOrDisableRigidBody();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnStopAuthority()
|
|
|
|
|
{
|
|
|
|
|
base.OnStopAuthority();
|
|
|
|
|
this.EnableOrDisableRigidBody();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2019-04-29 18:27:17 +00:00
|
|
|
|
|
2019-04-29 23:41:18 +00:00
|
|
|
|
this.ProcessSyncvars();
|
|
|
|
|
|
|
|
|
|
|
2019-04-29 22:11:14 +00:00
|
|
|
|
var driverSeat = m_vehicle.DriverSeat;
|
2019-04-29 18:27:17 +00:00
|
|
|
|
|
|
|
|
|
if (null == driverSeat || null == driverSeat.OccupyingPed)
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
this.ResetInput();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 16:59:16 +00:00
|
|
|
|
if (null == Ped.Instance || driverSeat.OccupyingPed != Ped.Instance)
|
2019-04-29 18:27:17 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// local ped is occupying driver seat
|
|
|
|
|
|
2019-04-29 22:59:21 +00:00
|
|
|
|
if (!GameManager.CanPlayerReadInput())
|
|
|
|
|
this.ResetInput();
|
|
|
|
|
else
|
|
|
|
|
this.ReadInput();
|
2019-04-29 18:27:17 +00:00
|
|
|
|
|
2019-05-05 23:07:35 +00:00
|
|
|
|
// why do we send input ?
|
|
|
|
|
// - so that everyone knows if the gas/brake is pressed, and can simulate wheel effects
|
|
|
|
|
// - so that server can predict position and velocity of rigid body
|
2019-04-29 22:59:21 +00:00
|
|
|
|
PedSync.Local.SendVehicleInput(m_vehicle.Accelerator, m_vehicle.Steering, m_vehicle.Braking);
|
2019-05-05 23:07:35 +00:00
|
|
|
|
|
|
|
|
|
// TODO: also send velocity of rigid body
|
|
|
|
|
|
2019-04-29 22:59:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 23:41:18 +00:00
|
|
|
|
void ProcessSyncvars()
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
m_net_acceleration = m_vehicle.Accelerator;
|
|
|
|
|
m_net_steering = m_vehicle.Steering;
|
|
|
|
|
m_net_braking = m_vehicle.Braking;
|
2019-04-30 00:37:29 +00:00
|
|
|
|
m_net_linearVelocity = m_vehicle.RigidBody.velocity;
|
|
|
|
|
m_net_angularVelocity = m_vehicle.RigidBody.angularVelocity;
|
2019-04-29 23:41:18 +00:00
|
|
|
|
}
|
2019-05-22 16:59:16 +00:00
|
|
|
|
else if (!this.IsControlledByLocalPlayer) // don't do it on client who controls vehicle
|
2019-04-29 23:41:18 +00:00
|
|
|
|
{
|
|
|
|
|
m_vehicle.Accelerator = m_net_acceleration;
|
|
|
|
|
m_vehicle.Steering = m_net_steering;
|
|
|
|
|
m_vehicle.Braking = m_net_braking;
|
2019-04-30 00:37:29 +00:00
|
|
|
|
if (VehicleManager.Instance.syncLinearVelocity)
|
|
|
|
|
m_vehicle.RigidBody.velocity = m_net_linearVelocity;
|
|
|
|
|
if (VehicleManager.Instance.syncAngularVelocity)
|
|
|
|
|
m_vehicle.RigidBody.angularVelocity = m_net_angularVelocity;
|
2019-04-29 23:41:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 22:59:21 +00:00
|
|
|
|
void ResetInput()
|
|
|
|
|
{
|
|
|
|
|
m_vehicle.Accelerator = 0;
|
|
|
|
|
m_vehicle.Steering = 0;
|
|
|
|
|
m_vehicle.Braking = 0;
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2019-04-29 22:59:21 +00:00
|
|
|
|
void ReadInput()
|
|
|
|
|
{
|
2020-05-31 17:07:22 +00:00
|
|
|
|
var accel = Input.GetAxis("Vertical");
|
|
|
|
|
var brake = Input.GetButton("Brake") ? 1.0f : 0.0f;
|
2019-04-29 22:11:14 +00:00
|
|
|
|
var speed = Vector3.Dot(m_vehicle.Velocity, m_vehicle.transform.forward);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
if (speed * accel < 0f)
|
|
|
|
|
{
|
|
|
|
|
brake = Mathf.Max(brake, 0.75f);
|
|
|
|
|
accel = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 22:11:14 +00:00
|
|
|
|
m_vehicle.Accelerator = accel;
|
|
|
|
|
m_vehicle.Steering = Input.GetAxis("Horizontal");
|
|
|
|
|
m_vehicle.Braking = brake;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
2019-04-29 18:27:17 +00:00
|
|
|
|
|
2019-05-05 22:26:55 +00:00
|
|
|
|
void EnableOrDisableRigidBody()
|
|
|
|
|
{
|
2019-05-22 16:59:16 +00:00
|
|
|
|
if (NetStatus.IsServer || this.IsControlledByLocalPlayer)
|
2019-05-05 22:26:55 +00:00
|
|
|
|
{
|
|
|
|
|
// enable rigid body
|
|
|
|
|
m_vehicle.RigidBody.isKinematic = false;
|
|
|
|
|
m_vehicle.RigidBody.detectCollisions = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// disable rigid body
|
|
|
|
|
if (VehicleManager.Instance.disableRigidBodyOnClients)
|
|
|
|
|
{
|
|
|
|
|
m_vehicle.RigidBody.isKinematic = true;
|
|
|
|
|
m_vehicle.RigidBody.detectCollisions = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|