133 lines
4.2 KiB
C#
133 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
using BITKit.Entities;
|
|
using BITKit.BITInputSystem;
|
|
using UnityEngine.InputSystem;
|
|
namespace BITKit.Vehicles
|
|
{
|
|
public enum WheelType
|
|
{
|
|
None,
|
|
Motor,
|
|
Steel,
|
|
}
|
|
[System.Serializable] //代表串行化,添加该标记的目的在于,使类成员可以在属性面板中显示
|
|
public class AxisInfo
|
|
{
|
|
public WheelCollider[] wheels;
|
|
public Transform[] wheelModels;
|
|
public WheelType wheelType;
|
|
}
|
|
public class Vehicle : NetworkInteractivable, BITController.IVehicleActions, IAnchor
|
|
{
|
|
[Header(Constant.Header.Settings)]
|
|
public float maxMotorTorque = 64;
|
|
public float maxSteeringAngle = 45;
|
|
public float brakeTorque;
|
|
[Header(Constant.Header.Components)]
|
|
public new Rigidbody rigidbody;
|
|
public Transform driveAnchor;
|
|
[Header(Constant.Header.Gameobjects)]
|
|
public List<AxisInfo> axisInfos = new();
|
|
|
|
[Header(Constant.Header.InternalVariables)]
|
|
float vertical;
|
|
float horizontal;
|
|
bool isbraking;
|
|
NetworkIdentity driver;
|
|
BITController controller;
|
|
public override void Excute(NetworkIdentity identity, string action)
|
|
{
|
|
if (driver is null)
|
|
{
|
|
driver = identity;
|
|
controller.Vehicle.Enable();
|
|
controller.Vehicle.SetCallbacks(this);
|
|
}
|
|
else
|
|
{
|
|
if (driver == identity)
|
|
{
|
|
driver = null;
|
|
}
|
|
controller.Vehicle.Disable();
|
|
}
|
|
}
|
|
public virtual void OnVertical(InputAction.CallbackContext context)
|
|
{
|
|
vertical = context.ReadValue<float>();
|
|
}
|
|
public virtual void OnHorizontal(InputAction.CallbackContext context)
|
|
{
|
|
horizontal = context.ReadValue<float>();
|
|
}
|
|
public virtual void OnBrake(InputAction.CallbackContext context)
|
|
{
|
|
isbraking = context.performed;
|
|
}
|
|
public virtual Location GetLocation() => new(driveAnchor);
|
|
public virtual Rigidbody GetRigidbody() => rigidbody;
|
|
void Start()
|
|
{
|
|
controller = new();
|
|
controller.Vehicle.Enable();
|
|
}
|
|
void Update()
|
|
{
|
|
if (driver is not null && driver == NetworkClient.localPlayer)
|
|
{
|
|
|
|
|
|
|
|
var torque = maxMotorTorque * vertical;
|
|
var steel = horizontal * maxSteeringAngle;
|
|
axisInfos.ForEach(x =>
|
|
{
|
|
switch (x.wheelType)
|
|
{
|
|
case WheelType.Motor:
|
|
if (isbraking)
|
|
{
|
|
x.wheels.ForEach(x =>
|
|
{
|
|
x.brakeTorque = brakeTorque;
|
|
});
|
|
}
|
|
else
|
|
{
|
|
|
|
x.wheels.ForEach(x =>
|
|
{
|
|
x.motorTorque = torque;
|
|
});
|
|
x.wheels.ForEach(x =>
|
|
{
|
|
x.brakeTorque = 0;
|
|
});
|
|
}
|
|
|
|
break;
|
|
case WheelType.Steel:
|
|
var steelAngle = horizontal * 45;
|
|
x.wheelModels.ForEach(x =>
|
|
{
|
|
x.localEulerAngles = new(torque * Time.deltaTime, steelAngle, 0);
|
|
});
|
|
x.wheels.ForEach(x =>
|
|
{
|
|
x.steerAngle = steel;
|
|
});
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
vertical = horizontal = default;
|
|
isbraking = default;
|
|
}
|
|
}
|
|
}
|
|
} |