SanAndreasUnity/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs

78 lines
2 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
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
[SyncVar] int m_net_id = 0;
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 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-29 22:18:19 +00:00
}
}
2020-05-31 17:07:22 +00:00
private void Update()
{
2019-04-29 22:11:14 +00:00
var driverSeat = m_vehicle.DriverSeat;
if (null == driverSeat || null == driverSeat.OccupyingPed)
{
if (NetStatus.IsServer)
this.ResetInput();
return;
}
if (driverSeat.OccupyingPed != Ped.Instance)
return;
// local ped is occupying driver seat
this.ResetInput();
2020-05-31 17:07:22 +00:00
if (!GameManager.CanPlayerReadInput()) return;
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
}
void ResetInput()
{
2019-04-29 22:11:14 +00:00
m_vehicle.Accelerator = 0;
m_vehicle.Steering = 0;
m_vehicle.Braking = 0;
}
2020-05-31 17:07:22 +00:00
}
}