更改文件架构
This commit is contained in:
23
Packages/Common~/Scripts/Vehicle/BITKit.Vehicles.asmdef
Normal file
23
Packages/Common~/Scripts/Vehicle/BITKit.Vehicles.asmdef
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "BITKit.Vehicles",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:99a47d73d3ad3374b9d12c982228df71",
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:30817c1a0e6d646d99c048fc403f5979",
|
||||
"GUID:709caf8d7fb6ef24bbba0ab9962a3ad0",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"MIRROR"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 195464366cec50a429fbdbba42256217
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
133
Packages/Common~/Scripts/Vehicle/Vehicle.cs
Normal file
133
Packages/Common~/Scripts/Vehicle/Vehicle.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Vehicle/Vehicle.cs.meta
Normal file
11
Packages/Common~/Scripts/Vehicle/Vehicle.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3a28346176688e4ba74de01c1175260
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user