189 lines
6.9 KiB
C#
189 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using BITFALL.Bullet;
|
|
using UnityEngine;
|
|
using BITKit;
|
|
using BITKit.Core.Entites;
|
|
using BITKit.Entities;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEditor;
|
|
using UnityEngine.Animations;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITFALL
|
|
{
|
|
[Serializable]
|
|
public record InstanceBullet : SpawnBullet
|
|
{
|
|
public Vector3 currentPos;
|
|
public float currentSpeed = 64;
|
|
public float ElapsedTime;
|
|
public BITBullet model;
|
|
}
|
|
[Serializable]
|
|
public class BulletServiceSingleton : IBulletService
|
|
{
|
|
public void Spawn(SpawnBullet bullet)=>BulletService.Spawn?.Invoke(bullet);
|
|
}
|
|
public class BulletService : MonoBehaviour, IBulletService
|
|
{
|
|
public static Action<SpawnBullet> Spawn;
|
|
|
|
[Header(Constant.Header.Settings)]
|
|
public LayerMask layerMask;
|
|
|
|
[Header(Constant.Header.Providers)]
|
|
[SerializeField, SerializeReference, SubclassSelector]
|
|
private IEntitiesService entitiesService;
|
|
[SerializeField, SerializeReference, SubclassSelector]
|
|
private INetProvider netProvider;
|
|
[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 void Start()
|
|
{
|
|
Spawn += SpawnBullet;
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
Spawn -= SpawnBullet;
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
foreach (var bullet in instances.ToArray())
|
|
{
|
|
var size = Physics.RaycastNonAlloc(bullet.currentPos, bullet.forward, _raycastHits, bullet.currentSpeed * Time.fixedDeltaTime, layerMask);
|
|
var validHit = false;
|
|
foreach (var raycastHit in _raycastHits.Take(size).OrderBy(x => Vector3.Distance(bullet.pos, x.point)))
|
|
{
|
|
if (!IsValidHit(raycastHit, bullet))
|
|
continue;
|
|
validHit = true;
|
|
break;
|
|
}
|
|
|
|
if (validHit ||bullet.currentSpeed <= 0)
|
|
{
|
|
instances.TryRemove(bullet);
|
|
pool.Return(bullet.model);
|
|
}
|
|
else
|
|
{
|
|
// 计算子弹的下坠距离
|
|
float distance = 0.5f * Physics.gravity.y * bullet.ElapsedTime * bullet.ElapsedTime;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
private async void RpcSpawnBullet(SpawnBullet x)
|
|
{
|
|
await UniTask.SwitchToMainThread();
|
|
var instance = pool.Get();
|
|
InstanceBullet bullet = new()
|
|
{
|
|
initiator = x.initiator,
|
|
currentPos = x.pos,
|
|
pos = x.pos,
|
|
rot = x.rot,
|
|
forward = x.forward,
|
|
initialDamage = x.initialDamage,
|
|
model = instance,
|
|
InitialForce = x.InitialForce,
|
|
};
|
|
var instanceTransform = instance.transform;
|
|
instanceTransform.SetPositionAndRotation(x.pos, x.rot);
|
|
instanceTransform.forward = x.forward;
|
|
instances.Add(bullet);
|
|
}
|
|
private void SpawnBullet(SpawnBullet x)
|
|
{
|
|
RpcSpawnBullet(x);
|
|
}
|
|
|
|
private bool IsValidHit(RaycastHit raycastHit, InstanceBullet bullet)
|
|
{
|
|
if (layerMask.Includes(raycastHit.collider.gameObject.layer) is false) return false;
|
|
raycastHit.collider.TryGetComponentAny<IPhysicsInfo>(out var physicsInfo);
|
|
var force = (raycastHit.point - (Vector3)bullet.pos).normalized * (physicsInfo?.AddForceMultiple ?? 64);
|
|
if (raycastHit.collider.TryGetComponent<IDamagable>(out var damagable))
|
|
{
|
|
if (damagable.Entity.Id == bullet.initiator) return false;
|
|
if (layerMask.Includes(raycastHit.collider.gameObject.layer) is false) return false;
|
|
var msg = new DamageMessage()
|
|
{
|
|
Target = damagable.Entity,
|
|
Hit = damagable,
|
|
Damage = bullet.initialDamage,
|
|
};
|
|
damageService.Execute(msg);
|
|
}
|
|
|
|
var _rigidbody = (raycastHit.rigidbody,damagable?.Rigidbody) switch
|
|
{
|
|
(null, null) => null,
|
|
(null, not null) => damagable?.Rigidbody,
|
|
(not null, null) => raycastHit.rigidbody,
|
|
(not null, not null) => damagable?.Rigidbody,
|
|
};
|
|
if (_rigidbody is not null && _rigidbody.gameObject.layer is not 0)
|
|
{
|
|
_rigidbody.AddForceAtPositionAsync(force, raycastHit.point, ForceMode.Impulse).Forget();
|
|
}
|
|
|
|
|
|
List<string> tags = new()
|
|
{
|
|
"BulletHit",
|
|
};
|
|
if (raycastHit.transform.TryGetComponent<ITag>(out var _tag))
|
|
{
|
|
tags.AddRange(_tag.GetTags());
|
|
}
|
|
|
|
Location location = new()
|
|
{
|
|
position = raycastHit.point,
|
|
forward = raycastHit.normal,
|
|
};
|
|
var vfx = DI.Get<VFXService>().Spawn(location, tags.ToArray());
|
|
var constraint = vfx.gameObject.GetOrAddComponent<ParentConstraint>();
|
|
var sourceTransform = raycastHit.transform;
|
|
while (constraint.sourceCount>0)
|
|
{
|
|
constraint.RemoveSource(0);
|
|
}
|
|
constraint.AddSource(new ConstraintSource()
|
|
{
|
|
sourceTransform = sourceTransform,
|
|
weight = 1,
|
|
});
|
|
|
|
var positionOffset = sourceTransform.InverseTransformPoint(raycastHit.point);
|
|
var rotationOffset = Quaternion.Inverse(sourceTransform.rotation) * transform.rotation;
|
|
|
|
constraint.SetTranslationOffset(0, positionOffset);
|
|
//constraint.SetRotationOffset(0, rotationOffset.eulerAngles);
|
|
|
|
constraint.weight = 1;
|
|
constraint.constraintActive = true;
|
|
return true;
|
|
}
|
|
|
|
void IBulletService.Spawn(SpawnBullet bullet) => SpawnBullet(bullet);
|
|
}
|
|
}
|