BITFALL/Assets/Artists/Scripts/MeleeService/MeleeService.cs

123 lines
3.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITFALL.Combat;
using BITKit;
using BITKit.Entities;
using BITKit.Entities.Melee;
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Triggers;
using Unity.Collections;
using UnityEngine;
using UnityEngine.VFX;
namespace BITFALL.Melee
{
[Serializable]
public class MeleeServiceSingleton : IMeleeService
{
public void Melee(MeleeCommand command) => MeleeService.Melee(command);
}
public class MeleeService : MonoBehaviour,IMeleeService
{
internal static MeleeService Singleton;
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
Queue.Clear();
}
private static readonly Queue<MeleeCommand> Queue = new();
public static void Melee(MeleeCommand command)=>Queue.Enqueue(command);
[SerializeField] private LayerMask detectLayer;
[SerializeReference, SubclassSelector] private IDamageService damageService;
private void Awake()
{
Singleton = this;
}
private void FixedUpdate()
{
if (Queue.TryDequeue(out var command) is false) return;
var colliders = Physics.OverlapSphere(command.Position, command.Range,detectLayer);
var _damaged = new Dictionary<ulong, IUnityEntity>();
var hits = colliders
.Where(x => x.GetComponent<IDamagable>() is not null)
.Where(x => MathV.IsForward(command.Position, command.Forward, x.transform.position))
.Select(x => x.GetComponent<IDamagable>())
.Where(damageable =>damageable.UnityEntity?.Id != command.PlayerId)
.Where(damageable =>
{
if (command.IgnoreTags is null
|| !(command.IgnoreTags?.Length > 0)
|| damageable.UnityEntity is null
|| !damageable.UnityEntity.TryGetComponent<ITag>(out var iTags)) return true;
return !MathE.Contains(iTags.GetTags(), command.IgnoreTags);
})
.Where(x => x.UnityEntity is null || _damaged.TryAdd(x.UnityEntity.Id, x.UnityEntity))
;
if (command.Limit is not 0)
{
hits = hits.Take(command.Limit);
}
foreach (var x in hits)
{
try
{
var damageable = x;
if (command.ForceHitStun)
{
if (damageable is not null && damageable.UnityEntity is not null && damageable.UnityEntity.TryGetComponent<IMeleeCombat>(out var combat))
{
combat.HitStun();
}
}
var damageMsg =
new DamageMessage()
{
Initiator = UnityEntitiesService.Get(command.PlayerId) as IUnityEntity,
DamageType = new MeleeDamageMessage
{
},
Target = damageable.UnityEntity,
Damage = command.Damage is 0 ? 64 : command.Damage,
Hit = damageable,
};
if (command.PlayerId !=default)
{
damageMsg.Initiator = UnityEntitiesService.Get(command.PlayerId) as IUnityEntity;
}
damageService.Execute(damageMsg);
damageable.Rigidbody.AddForceAtPositionAsync(command.Force, command.Position, ForceMode.Impulse)
.Forget();
if ((x as MonoBehaviour)!.TryGetComponent<ITag>(out var _tag))
{
DI.Get<VFXService>().Spawn(new Location
{
position = x.Rigidbody.position,
//rotation = Quaternion.identity,
}, _tag.GetTags());
}
}
catch (UnassignedReferenceException e)
{
Debug.LogWarning(x);
Debug.LogException(e);
}
}
}
void IMeleeService.Melee(MeleeCommand command) => Melee(command);
}
}