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;
|
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-04-29 22:18:19 +00:00
|
|
|
|
|
2019-04-29 23:23:31 +00:00
|
|
|
|
[SyncVar] int m_net_id;
|
|
|
|
|
[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-29 22:18:19 +00:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
base.OnStartClient();
|
|
|
|
|
|
|
|
|
|
if (!NetStatus.IsServer)
|
|
|
|
|
{
|
|
|
|
|
// load vehicle on clients
|
2019-04-29 22:40:26 +00:00
|
|
|
|
F.RunExceptionSafe( () => {
|
|
|
|
|
m_vehicle = Vehicle.Create(this.gameObject, m_net_id, null, this.transform.position, this.transform.rotation);
|
2019-04-30 22:11:02 +00:00
|
|
|
|
// disable rigid body
|
|
|
|
|
if (VehicleManager.Instance.disableRigidBodyOnClients)
|
|
|
|
|
{
|
|
|
|
|
m_vehicle.RigidBody.isKinematic = true;
|
|
|
|
|
m_vehicle.RigidBody.detectCollisions = false;
|
|
|
|
|
}
|
2019-04-29 22:40:26 +00:00
|
|
|
|
});
|
2019-04-29 22:18:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (driverSeat.OccupyingPed != Ped.Instance)
|
|
|
|
|
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-04-29 22:59:21 +00:00
|
|
|
|
PedSync.Local.SendVehicleInput(m_vehicle.Accelerator, m_vehicle.Steering, m_vehicle.Braking);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|