using System; using System.Collections; using System.Collections.Generic; using System.Linq; 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 struct MeleeDamageMessage:IDamageType{} public class MeleeService : MonoBehaviour,IMeleeService { internal static MeleeService Singleton; [RuntimeInitializeOnLoadMethod] private static void Initialize() { Queue.Clear(); } private static readonly Queue Queue = new(); public static void Melee(MeleeCommand command)=>Queue.Enqueue(command); [SerializeField] private LayerMask detectLayer; 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 List(); foreach (var x in colliders.Where(x=>x.GetComponent() is not null)) { var damageable = x.GetComponent(); if (damaged.Contains(damageable.Entity)) { continue; } damaged.Add(damageable.Entity); damageable.GiveDamage(new DamageMessage() { initiator = UnityEntitiesService.Get(command.PlayerId) as IEntity, damageType = new MeleeDamageMessage { }, target = damageable.Entity, damage = 64, hit = damageable, }); var direction = (damageable.Rigidbody.position - (Vector3)command.Position).normalized; damageable.Rigidbody.AddForceAtPositionAsync(direction * 512 ,command.Position,ForceMode.Impulse).Forget(); if (x.TryGetComponent(out var _tag)) { DI.Get().Spawn(new Location { position = x.transform.position, rotation = Quaternion.identity, forward = direction },_tag.GetTags()); } } } void IMeleeService.Melee(MeleeCommand command) => Melee(command); } }