2023-06-30 18:57:28 -04:00
using Content.Shared.Chemistry ;
2023-02-18 19:00:31 -06:00
using Content.Shared.Chemistry.Reagent ;
2023-03-22 23:49:42 -04:00
using Content.Shared.Rounding ;
2022-08-07 01:50:52 -07:00
using Robust.Client.GameObjects ;
2023-02-18 19:00:31 -06:00
using Robust.Shared.Prototypes ;
2022-08-07 01:50:52 -07:00
namespace Content.Client.Chemistry.Visualizers ;
public sealed class SolutionContainerVisualsSystem : VisualizerSystem < SolutionContainerVisualsComponent >
{
2023-02-18 19:00:31 -06:00
[Dependency] private readonly IPrototypeManager _prototype = default ! ;
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < SolutionContainerVisualsComponent , MapInitEvent > ( OnMapInit ) ;
}
private void OnMapInit ( EntityUid uid , SolutionContainerVisualsComponent component , MapInitEvent args )
{
var meta = MetaData ( uid ) ;
component . InitialName = meta . EntityName ;
component . InitialDescription = meta . EntityDescription ;
}
2022-08-07 01:50:52 -07:00
protected override void OnAppearanceChange ( EntityUid uid , SolutionContainerVisualsComponent component , ref AppearanceChangeEvent args )
{
2023-07-01 00:10:47 +02:00
// Check if the solution that was updated is the one set as represented
if ( ! string . IsNullOrEmpty ( component . SolutionName ) )
{
if ( AppearanceSystem . TryGetData < string > ( uid , SolutionContainerVisuals . SolutionName , out var name ,
args . Component ) & & name ! = component . SolutionName )
{
return ;
}
}
2023-02-02 17:34:53 +01:00
if ( ! AppearanceSystem . TryGetData < float > ( uid , SolutionContainerVisuals . FillFraction , out var fraction , args . Component ) )
2022-08-07 01:50:52 -07:00
return ;
if ( args . Sprite = = null )
return ;
2023-02-18 19:00:31 -06:00
if ( ! args . Sprite . LayerMapTryGet ( component . FillLayer , out var fillLayer ) )
2022-08-07 01:50:52 -07:00
return ;
2023-01-23 19:33:11 -05:00
// Currently some solution methods such as overflowing will try to update appearance with a
// volume greater than the max volume. We'll clamp it so players don't see
// a giant error sign and error for debug.
if ( fraction > 1f )
{
Logger . Error ( "Attempted to set solution container visuals volume ratio on " + ToPrettyString ( uid ) + " to a value greater than 1. Volume should never be greater than max volume!" ) ;
fraction = 1f ;
}
2023-02-18 19:00:31 -06:00
if ( component . Metamorphic )
{
if ( args . Sprite . LayerMapTryGet ( component . BaseLayer , out var baseLayer ) )
{
var hasOverlay = args . Sprite . LayerMapTryGet ( component . OverlayLayer , out var overlayLayer ) ;
if ( AppearanceSystem . TryGetData < string > ( uid , SolutionContainerVisuals . BaseOverride ,
out var baseOverride ,
args . Component ) )
{
_prototype . TryIndex < ReagentPrototype > ( baseOverride , out var reagentProto ) ;
if ( reagentProto ? . MetamorphicSprite is { } sprite )
{
args . Sprite . LayerSetSprite ( baseLayer , sprite ) ;
args . Sprite . LayerSetVisible ( fillLayer , false ) ;
if ( hasOverlay )
args . Sprite . LayerSetVisible ( overlayLayer , false ) ;
return ;
}
else
{
if ( hasOverlay )
args . Sprite . LayerSetVisible ( overlayLayer , true ) ;
args . Sprite . LayerSetSprite ( baseLayer , component . MetamorphicDefaultSprite ) ;
}
}
}
}
2023-03-22 23:49:42 -04:00
int closestFillSprite = ContentHelpers . RoundToLevels ( fraction , 1 , component . MaxFillLevels + 1 ) ;
2022-08-07 01:50:52 -07:00
if ( closestFillSprite > 0 )
{
if ( component . FillBaseName = = null )
return ;
args . Sprite . LayerSetVisible ( fillLayer , true ) ;
var stateName = component . FillBaseName + closestFillSprite ;
args . Sprite . LayerSetState ( fillLayer , stateName ) ;
2023-02-02 17:34:53 +01:00
if ( component . ChangeColor & & AppearanceSystem . TryGetData < Color > ( uid , SolutionContainerVisuals . Color , out var color , args . Component ) )
2023-01-14 13:21:15 +13:00
args . Sprite . LayerSetColor ( fillLayer , color ) ;
2022-08-07 01:50:52 -07:00
}
else
{
if ( component . EmptySpriteName = = null )
args . Sprite . LayerSetVisible ( fillLayer , false ) ;
else
{
args . Sprite . LayerSetState ( fillLayer , component . EmptySpriteName ) ;
if ( component . ChangeColor )
args . Sprite . LayerSetColor ( fillLayer , component . EmptySpriteColor ) ;
}
}
2023-02-18 19:00:31 -06:00
2022-08-07 01:50:52 -07:00
}
}