using Cysharp.Threading.Tasks; using GameFramework.Event; using SepCore.AsyncTask; using SepCore.CustomComponent; using SepCore.Definition; using SepCore.Entity; using SepCore.Event; using Random = UnityEngine.Random; namespace SepCore.Battle { public sealed class DropService { private const float VacuumMaxDuration = 0.8f; private const float VacuumAbsorbSpeed = 30f; private TbGlobalConfig _globalConfig; private bool _isListening; private float _vacuumTimer; public void Subscribe() { if (_isListening) { return; } _globalConfig = GameEntry.Luban.Global; GameEntry.Event.Subscribe(DropSpawnRequestedEventArgs.EventId, OnDropSpawnRequested); _isListening = true; } public void Unsubscribe() { if (!_isListening) { return; } _globalConfig = null; GameEntry.Event.Unsubscribe(DropSpawnRequestedEventArgs.EventId, OnDropSpawnRequested); _isListening = false; } public void StartVacuum() { _vacuumTimer = VacuumMaxDuration; GameEntry.SimulationWorld.TriggerFullMapMagnet(); GameEntry.SimulationWorld.SyncPlayerAbsorbSettings(999f, VacuumAbsorbSpeed, _globalConfig.CollectDistance, _globalConfig.RecoilDistance, _globalConfig.RecoilDuration, _globalConfig.DashAccelerationDuration, _globalConfig.DashMaxSpeedMultiplier, _globalConfig.TurnSpeed); } public bool UpdateVacuum(float elapseSeconds, Player player) { _vacuumTimer -= elapseSeconds; if (GameEntry.SimulationWorld.PickupCount > 0 && _vacuumTimer > 0f) { return false; } CollectAllRemainingDrops(player); return true; } public void HideAllDrops() { _vacuumTimer = 0f; var entityGroup = GameEntry.Entity.GetEntityGroup(_globalConfig.DropGroupName); var entities = entityGroup.GetAllEntities(); foreach (var entity in entities) { GameEntry.Entity.HideEntity(entity.Id); } } private void CollectAllRemainingDrops(Player player) { var entityGroup = GameEntry.Entity.GetEntityGroup(_globalConfig.DropGroupName); var entities = entityGroup.GetAllEntities(); foreach (var entity in entities) { if (entity is UnityGameFramework.Runtime.Entity runtimeEntity && runtimeEntity.Logic is DropEntity drop) { drop.TryCollect(player); } } } private void OnDropSpawnRequested(object sender, GameEventArgs e) { var eventArgs = (DropSpawnRequestedEventArgs)e; int idx = Random.Range(0, _globalConfig.DropEntityAssetNames.Count); var dropEntityAssetName = _globalConfig.DropEntityAssetNames[idx]; var data = new DropData(_globalConfig.DropEntityCoinCount, _globalConfig.DropEntityExpCount, GameEntry.Entity.NextId(), dropEntityAssetName) { Position = eventArgs.Position }; GameEntry.Entity.ShowDropAsync(data).Forget(); } } }