using Content.Server.Interfaces.GameObjects;
using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Mobs
{
public static class StandingStateHelper
{
///
/// Set's the mob standing state to down.
///
/// The mob in question
/// Whether to play a sound when falling down or not
/// Whether to make the mob drop all the items on his hands
/// Whether or not to check if the entity can fall.
/// False if the mob was already downed or couldn't set the state
public static bool Down(IEntity entity, bool playSound = true, bool dropItems = true, bool force = false)
{
if (!force && !EffectBlockerSystem.CanFall(entity))
{
return false;
}
if (!entity.TryGetComponent(out AppearanceComponent appearance))
{
return false;
}
var newState = SharedSpeciesComponent.MobState.Down;
appearance.TryGetData(SharedSpeciesComponent.MobVisuals.RotationState, out var oldState);
if (newState != oldState)
{
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
}
if (playSound)
{
IoCManager.Resolve().GetEntitySystem()
.PlayFromEntity(AudioHelpers.GetRandomFileFromSoundCollection("bodyfall"), entity, AudioHelpers.WithVariation(0.25f));
}
if(dropItems)
{
DropAllItemsInHands(entity, false);
}
return true;
}
///
/// Sets the mob's standing state to standing.
///
/// The mob in question.
/// False if the mob was already standing or couldn't set the state
public static bool Standing(IEntity entity)
{
if (!entity.TryGetComponent(out AppearanceComponent appearance)) return false;
appearance.TryGetData(SharedSpeciesComponent.MobVisuals.RotationState, out var oldState);
var newState = SharedSpeciesComponent.MobState.Standing;
if (newState == oldState)
return false;
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
return true;
}
public static void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
{
if (!entity.TryGetComponent(out IHandsComponent hands)) return;
foreach (var heldItem in hands.GetAllHeldItems())
{
hands.Drop(heldItem.Owner, doMobChecks);
}
}
}
}