1
This commit is contained in:
@@ -2,12 +2,9 @@
|
||||
"name": "BITKit.Vehicles",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:99a47d73d3ad3374b9d12c982228df71",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:30817c1a0e6d646d99c048fc403f5979",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
@@ -15,9 +12,7 @@
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"MIRROR"
|
||||
],
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -1,10 +1,11 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using BITKit.Entities;
|
||||
using BITKit.BITInputSystem;
|
||||
using BITKit.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace BITKit.Vehicles
|
||||
{
|
||||
public enum WheelType
|
||||
@@ -13,121 +14,115 @@ namespace BITKit.Vehicles
|
||||
Motor,
|
||||
Steel,
|
||||
}
|
||||
[System.Serializable] //代表串行化,添加该标记的目的在于,使类成员可以在属性面板中显示
|
||||
|
||||
[System.Serializable]
|
||||
public class AxisInfo
|
||||
{
|
||||
public WheelCollider[] wheels;
|
||||
public Transform[] wheelModels;
|
||||
public WheelType wheelType;
|
||||
}
|
||||
public class Vehicle : NetworkInteractivable, BITController.IVehicleActions, IAnchor
|
||||
|
||||
public sealed class Vehicle : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float maxMotorTorque = 64;
|
||||
public float maxSteeringAngle = 45;
|
||||
public float brakeTorque;
|
||||
[Header(Constant.Header.Settings)] public float maxMotorTorque = 64;
|
||||
[SerializeField] private float maxSteeringAngle = 45;
|
||||
[SerializeField] private float brakeTorque;
|
||||
[SerializeField] private Optional<float> optionalVertical;
|
||||
[SerializeField] private Optional<float> optionalHorizontal;
|
||||
[SerializeField] private Optional<float> optionalUpRight;
|
||||
|
||||
[Header(Constant.Header.Components)]
|
||||
public new Rigidbody rigidbody;
|
||||
public Transform driveAnchor;
|
||||
[SerializeField]
|
||||
private new Rigidbody rigidbody;
|
||||
|
||||
[SerializeField] private Transform driveAnchor;
|
||||
|
||||
[Header(Constant.Header.Gameobjects)]
|
||||
public List<AxisInfo> axisInfos = new();
|
||||
[SerializeField] private List<AxisInfo> axisInfos = new();
|
||||
[SerializeField] private Transform steeringWheel;
|
||||
|
||||
[Header(Constant.Header.Events)]
|
||||
[SerializeField] private UnityEvent onHighSpeed;
|
||||
[SerializeField] private UnityEvent OnEntryHighSpeed;
|
||||
[SerializeField] private UnityEvent OnExitHighSpeed;
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
float vertical;
|
||||
float horizontal;
|
||||
bool isbraking;
|
||||
NetworkIdentity driver;
|
||||
BITController controller;
|
||||
public override void Excute(NetworkIdentity identity, string action)
|
||||
private float vertical;
|
||||
private float horizontal;
|
||||
private bool isBraking;
|
||||
private readonly ValidHandle highSpeedHandle = new();
|
||||
private IEntity _entity;
|
||||
private void Start()
|
||||
{
|
||||
if (driver is null)
|
||||
highSpeedHandle.AddListener(x =>
|
||||
{
|
||||
driver = identity;
|
||||
controller.Vehicle.Enable();
|
||||
controller.Vehicle.SetCallbacks(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (driver == identity)
|
||||
if (x)
|
||||
{
|
||||
driver = null;
|
||||
OnEntryHighSpeed.Invoke();
|
||||
}
|
||||
controller.Vehicle.Disable();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnExitHighSpeed.Invoke();
|
||||
}
|
||||
onHighSpeed.Invoke(x);
|
||||
});
|
||||
highSpeedHandle.Invoke();
|
||||
}
|
||||
public virtual void OnVertical(InputAction.CallbackContext context)
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var torque = maxMotorTorque *(optionalVertical.Allow ? optionalVertical.Value : vertical);
|
||||
var steel =maxSteeringAngle * (optionalHorizontal.Allow ? optionalHorizontal.Value : horizontal);
|
||||
axisInfos.ForEach(x =>
|
||||
{
|
||||
switch (x.wheelType)
|
||||
{
|
||||
case WheelType.Motor:
|
||||
if (isBraking)
|
||||
{
|
||||
x.wheels.ForEach(wheelCollider => { wheelCollider.brakeTorque = brakeTorque; });
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
x.wheels.ForEach(wheelCollider => { wheelCollider.motorTorque = torque; });
|
||||
x.wheels.ForEach(wheelCollider => { wheelCollider.brakeTorque = 0; });
|
||||
}
|
||||
|
||||
break;
|
||||
case WheelType.Steel:
|
||||
var steelAngle = horizontal * 45;
|
||||
x.wheelModels.ForEach(trans =>
|
||||
{
|
||||
trans.localEulerAngles = new Vector3(torque * Time.deltaTime, steelAngle, 0);
|
||||
});
|
||||
if (steeringWheel is not null)
|
||||
steeringWheel.localEulerAngles = new Vector3(0, 0, steelAngle);
|
||||
x.wheels.ForEach(wheelCollider => { wheelCollider.steerAngle = steel; });
|
||||
break;
|
||||
}
|
||||
});
|
||||
if (optionalUpRight.Allow)
|
||||
{
|
||||
rigidbody.AddRelativeTorque(rigidbody.transform.eulerAngles.z * optionalUpRight.Value * transform.forward, ForceMode.VelocityChange);
|
||||
}
|
||||
|
||||
highSpeedHandle.SetElements(this, rigidbody.velocity.magnitude > 4);
|
||||
}
|
||||
public void OnVertical(InputAction.CallbackContext context)
|
||||
{
|
||||
vertical = context.ReadValue<float>();
|
||||
}
|
||||
public virtual void OnHorizontal(InputAction.CallbackContext context)
|
||||
|
||||
public void OnHorizontal(InputAction.CallbackContext context)
|
||||
{
|
||||
horizontal = context.ReadValue<float>();
|
||||
}
|
||||
public virtual void OnBrake(InputAction.CallbackContext context)
|
||||
|
||||
public 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;
|
||||
}
|
||||
isBraking = context.performed;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user