This commit is contained in:
CortexCore
2023-11-02 20:58:55 +08:00
parent f0f348c246
commit ee3ecec6cb
168 changed files with 58830 additions and 379 deletions

View File

@@ -16,10 +16,10 @@ namespace BITFALL
[Serializable]
public record InstanceBullet : SpawnBullet
{
public Vector3 Velocity;
public Vector3 currentPos;
public float currentSpeed = 64;
public float ElapsedTime;
public BITBullet model;
}
[Serializable]
public class BulletServiceSingleton : IBulletService
@@ -31,7 +31,8 @@ namespace BITFALL
public static Action<SpawnBullet> Spawn;
[Header(Constant.Header.Settings)]
public LayerMask layerMask;
[SerializeField] private LayerMask layerMask;
[SerializeField] private Material material;
[Header(Constant.Header.Providers)]
[SerializeField, SerializeReference, SubclassSelector]
@@ -41,25 +42,50 @@ namespace BITFALL
[SerializeField, SerializeReference, SubclassSelector]
private IDamageService damageService;
[Header(Constant.Header.Settings)]
[SerializeField] private UnityPool<BITBullet> pool = new();
[Header(Constant.Header.InternalVariables)]
private readonly List<InstanceBullet> instances = new();
private readonly RaycastHit[] _raycastHits = new RaycastHit[16];
private const float dragCoefficient = 0.045f; // 子弹的阻力系数
private const float bulletMass = 0.015f; // 子弹的质量,单位:千克
private Mesh _mesh;
private void Start()
{
_mesh = GetComponent<MeshFilter>().mesh;
Spawn += SpawnBullet;
}
private void OnDestroy()
{
Spawn -= SpawnBullet;
}
private void Update()
{
var camera = Camera.main;
if (!camera) return;
var rot = camera.transform.rotation * Quaternion.Euler(0, 180, 0);
foreach (var bullet in instances.ToArray())
{
if (bullet.ElapsedTime >= 0.04f)
{
// Graphics.DrawMesh(_mesh, bullet.currentPos, bullet.rot, material,
// LayerMask.NameToLayer("TransparentFX"));
var martix = Matrix4x4.TRS(
bullet.currentPos,
rot,
Vector3.one + Vector3.one * Mathf.Clamp(bullet.ElapsedTime, 0, 16));
Graphics.DrawMesh(_mesh, martix, material,
LayerMask.NameToLayer("TransparentFX"));
}
}
}
private void FixedUpdate()
{
foreach (var bullet in instances.ToArray())
{
var size = Physics.RaycastNonAlloc(bullet.currentPos, bullet.forward, _raycastHits, bullet.currentSpeed * Time.fixedDeltaTime, layerMask);
var size = Physics.RaycastNonAlloc(bullet.currentPos, bullet.Velocity, _raycastHits, Vector3.Distance(default,bullet.Velocity) * Time.fixedDeltaTime, layerMask);
var validHit = false;
foreach (var raycastHit in _raycastHits.Take(size).OrderBy(x => Vector3.Distance(bullet.pos, x.point)))
{
@@ -69,43 +95,73 @@ namespace BITFALL
break;
}
if (validHit ||bullet.currentSpeed <= 0)
if (validHit ||bullet.currentSpeed <= 0 || bullet.Velocity.sqrMagnitude <= 0.01f || bullet.ElapsedTime >= 8)
{
instances.TryRemove(bullet);
pool.Return(bullet.model);
//pool.Return(bullet.model);
}
else
{
// 计算子弹的下坠距离
float distance = 0.5f * Physics.gravity.y * bullet.ElapsedTime * bullet.ElapsedTime;
// // 计算子弹的下坠距离
// float distance =Mathf.Clamp(
// 0.5f * Physics.gravity.y * bullet.ElapsedTime * bullet.ElapsedTime,
// Physics.gravity.y
// ,0
// ) ;
//
// bullet.currentSpeed -= bullet.startSpeed * Time.fixedDeltaTime;
// bullet.currentPos += (Vector3)bullet.forward * (bullet.currentSpeed * Time.fixedDeltaTime);
// bullet.currentPos += Vector3.up * distance;
// bullet.model.transform.position = bullet.currentPos;
//
// bullet.ElapsedTime += Time.fixedDeltaTime;
bullet.currentSpeed -= bullet.startSpeed * Time.fixedDeltaTime;
bullet.currentPos += (Vector3)bullet.forward * (bullet.currentSpeed * Time.fixedDeltaTime);
bullet.currentPos += Vector3.up * distance;
bullet.model.transform.position = bullet.currentPos;
// 计算重力下坠
bullet.ElapsedTime += Time.fixedDeltaTime;
bullet.ElapsedTime+=Time.fixedDeltaTime;
bullet.Velocity+= Physics.gravity * (Time.fixedDeltaTime * bullet.ElapsedTime);
// // 计算空气阻力
// float airDragForce = 0.5f * dragCoefficient * 1.225f * bullet.Velocity.magnitude * bullet.Velocity.magnitude;
//
// if (float.IsNaN(airDragForce))
// {
// bullet.Velocity=Vector3.zero;
// continue;
// }
//
// Vector3 airDragDirection = -bullet.Velocity.normalized;
// Vector3 airDrag = airDragDirection * airDragForce / bulletMass;
var newVelocity =bullet.Velocity * Time.fixedDeltaTime;
bullet.currentPos+=newVelocity;
float CalculateElevationAngle(float distance)
{
float g = Mathf.Abs(Physics.gravity.y);
float angle = 0.5f * Mathf.Asin(distance * g / (bullet.startSpeed * bullet.startSpeed));
return angle * Mathf.Rad2Deg;
}
}
}
}
private async void RpcSpawnBullet(SpawnBullet x)
{
await UniTask.SwitchToMainThread();
var instance = pool.Get();
InstanceBullet bullet = new()
{
initiator = x.initiator,
currentPos = x.pos,
startSpeed = x.startSpeed,
pos = x.pos,
rot = x.rot,
forward = x.forward,
initialDamage = x.initialDamage,
model = instance,
InitialForce = x.InitialForce,
Velocity =x.forward * x.startSpeed,
};
var instanceTransform = instance.transform;
instanceTransform.SetPositionAndRotation(x.pos, x.rot);
instanceTransform.forward = x.forward;
instances.Add(bullet);
}
private void SpawnBullet(SpawnBullet x)
@@ -156,7 +212,7 @@ namespace BITFALL
{
"BulletHit",
};
if (raycastHit.transform.TryGetComponent<ITag>(out var _tag))
if (raycastHit.collider.TryGetComponent<ITag>(out var _tag))
{
tags.AddRange(_tag.GetTags());
}
@@ -191,5 +247,7 @@ namespace BITFALL
}
void IBulletService.Spawn(SpawnBullet bullet) => SpawnBullet(bullet);
}
}