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 23:32:54 +00:00
|
|
|
|
bool IsControlledByLocalPlayer => m_vehicle.IsControlledByLocalPlayer;
|
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-05-23 20:48:18 +00:00
|
|
|
|
[SyncVar(hook=nameof(OnNetPositionChanged))] Vector3 m_net_position;
|
|
|
|
|
[SyncVar(hook=nameof(OnNetRotationChanged))] Quaternion m_net_rotation;
|
2019-04-30 00:37:29 +00:00
|
|
|
|
[SyncVar] Vector3 m_net_linearVelocity;
|
|
|
|
|
[SyncVar] Vector3 m_net_angularVelocity;
|
2019-05-22 19:18:12 +00:00
|
|
|
|
|
|
|
|
|
struct WheelSyncData
|
|
|
|
|
{
|
|
|
|
|
public float brakeTorque;
|
|
|
|
|
public float motorTorque;
|
|
|
|
|
public float steerAngle;
|
|
|
|
|
//public float travel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WheelSyncList : SyncList<WheelSyncData> { }
|
|
|
|
|
|
|
|
|
|
WheelSyncList m_net_wheelsData;
|
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>();
|
2019-05-23 22:54:03 +00:00
|
|
|
|
this.GetComponent<NetworkTransform>().enabled = ! VehicleManager.Instance.syncVehicleTransformUsingSyncVars;
|
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-05-23 20:48:18 +00:00
|
|
|
|
m_net_position = m_vehicle.transform.position;
|
|
|
|
|
m_net_rotation = m_vehicle.transform.rotation;
|
2019-04-30 00:37:29 +00:00
|
|
|
|
m_net_linearVelocity = m_vehicle.RigidBody.velocity;
|
|
|
|
|
m_net_angularVelocity = m_vehicle.RigidBody.angularVelocity;
|
2019-05-22 19:18:12 +00:00
|
|
|
|
|
|
|
|
|
// wheels
|
|
|
|
|
m_net_wheelsData.Clear();
|
|
|
|
|
foreach (var wheel in m_vehicle.Wheels) {
|
|
|
|
|
m_net_wheelsData.Add(new WheelSyncData() {
|
|
|
|
|
brakeTorque = wheel.Collider.brakeTorque,
|
|
|
|
|
motorTorque = wheel.Collider.motorTorque,
|
|
|
|
|
steerAngle = wheel.Collider.steerAngle,
|
|
|
|
|
//travel = wheel.Travel,
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-04-29 23:41:18 +00:00
|
|
|
|
}
|
2019-05-22 17:37:17 +00:00
|
|
|
|
else
|
2019-04-29 23:41:18 +00:00
|
|
|
|
{
|
2019-05-22 19:18:12 +00:00
|
|
|
|
if (!this.IsControlledByLocalPlayer)
|
2019-05-22 17:37:17 +00:00
|
|
|
|
{
|
2019-05-22 19:18:12 +00:00
|
|
|
|
// only assign input on other clients
|
2019-05-22 17:37:17 +00:00
|
|
|
|
m_vehicle.Accelerator = m_net_acceleration;
|
|
|
|
|
m_vehicle.Steering = m_net_steering;
|
|
|
|
|
m_vehicle.Braking = m_net_braking;
|
2019-05-22 19:18:12 +00:00
|
|
|
|
|
|
|
|
|
// only update wheels on other clients
|
|
|
|
|
for (int i=0; i < m_vehicle.Wheels.Count && i < m_net_wheelsData.Count; i++) {
|
|
|
|
|
var w = m_vehicle.Wheels[i];
|
|
|
|
|
var data = m_net_wheelsData[i];
|
|
|
|
|
w.Collider.brakeTorque = data.brakeTorque;
|
|
|
|
|
w.Collider.motorTorque = data.motorTorque;
|
|
|
|
|
w.Collider.steerAngle = data.steerAngle;
|
|
|
|
|
//w.Travel = data.travel;
|
|
|
|
|
}
|
2019-05-22 17:37:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 20:48:18 +00:00
|
|
|
|
// position and rotation will be applied in syncvar hooks
|
|
|
|
|
|
2019-05-22 17:37:17 +00:00
|
|
|
|
// apply velocity on all clients
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 20:48:18 +00:00
|
|
|
|
void OnNetPositionChanged(Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-23 22:54:03 +00:00
|
|
|
|
if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars)
|
|
|
|
|
m_vehicle.RigidBody.MovePosition(pos);
|
2019-05-23 20:48:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnNetRotationChanged(Quaternion rot)
|
|
|
|
|
{
|
|
|
|
|
if (NetStatus.IsServer)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-23 22:54:03 +00:00
|
|
|
|
if (VehicleManager.Instance.syncVehicleTransformUsingSyncVars)
|
|
|
|
|
m_vehicle.RigidBody.MoveRotation(rot);
|
2019-05-23 20:48:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|