2021-02-18 09:09:07 +01:00
using System.Linq ;
using System.Threading.Tasks ;
2021-11-24 16:52:31 -06:00
using Content.Server.Administration.Logs ;
2021-06-09 22:19:39 +02:00
using Content.Server.UserInterface ;
2020-10-13 13:40:05 +02:00
using Content.Shared.Audio ;
2021-06-09 22:19:39 +02:00
using Content.Shared.Crayon ;
2021-12-03 15:35:57 +01:00
using Content.Server.Decals ;
using Content.Shared.Decals ;
2021-11-28 14:56:53 +01:00
using Content.Shared.Database ;
2021-06-09 22:19:39 +02:00
using Content.Shared.Interaction ;
using Content.Shared.Interaction.Helpers ;
2021-09-26 15:18:45 +02:00
using Content.Shared.Popups ;
2021-07-10 17:35:33 +02:00
using Content.Shared.Sound ;
2020-10-13 13:40:05 +02:00
using Robust.Server.GameObjects ;
2021-03-21 09:12:03 -07:00
using Robust.Shared.Audio ;
2020-10-13 13:40:05 +02:00
using Robust.Shared.GameObjects ;
using Robust.Shared.IoC ;
using Robust.Shared.Localization ;
using Robust.Shared.Maths ;
2021-03-21 09:12:03 -07:00
using Robust.Shared.Player ;
2021-02-18 09:09:07 +01:00
using Robust.Shared.Players ;
2020-10-13 13:40:05 +02:00
using Robust.Shared.Prototypes ;
using Robust.Shared.Serialization ;
2021-03-05 01:08:38 +01:00
using Robust.Shared.Serialization.Manager.Attributes ;
2021-05-04 15:37:16 +02:00
using Robust.Shared.ViewVariables ;
2020-10-13 13:40:05 +02:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Crayon
2020-10-13 13:40:05 +02:00
{
[RegisterComponent]
2021-03-05 01:08:38 +01:00
public class CrayonComponent : SharedCrayonComponent , IAfterInteract , IUse , IDropped , ISerializationHooks
2020-10-13 13:40:05 +02:00
{
2021-12-08 17:17:12 +01:00
[Dependency] private readonly IEntityManager _entMan = default ! ;
2020-10-13 13:40:05 +02:00
[Dependency] private readonly IPrototypeManager _prototypeManager = default ! ;
2021-03-05 01:08:38 +01:00
2021-08-13 21:31:37 -07:00
[DataField("useSound")]
private SoundSpecifier ? _useSound = null ;
2021-03-05 01:08:38 +01:00
2020-10-13 13:40:05 +02:00
[ViewVariables]
public Color Color { get ; set ; }
[ViewVariables(VVAccess.ReadWrite)]
public int Charges { get ; set ; }
2021-03-05 01:08:38 +01:00
2020-10-13 13:40:05 +02:00
[ViewVariables(VVAccess.ReadWrite)]
2021-05-04 15:37:16 +02:00
[DataField("capacity")]
2021-03-05 01:08:38 +01:00
public int Capacity { get ; set ; } = 30 ;
2020-10-13 13:40:05 +02:00
[ViewVariables] private BoundUserInterface ? UserInterface = > Owner . GetUIOrNull ( CrayonUiKey . Key ) ;
2021-03-05 01:08:38 +01:00
void ISerializationHooks . AfterDeserialization ( )
2020-10-13 13:40:05 +02:00
{
Color = Color . FromName ( _color ) ;
}
2021-06-19 19:41:26 -07:00
protected override void Initialize ( )
2020-10-13 13:40:05 +02:00
{
base . Initialize ( ) ;
if ( UserInterface ! = null )
{
UserInterface . OnReceiveMessage + = UserInterfaceOnReceiveMessage ;
}
Charges = Capacity ;
// Get the first one from the catalog and set it as default
2021-12-03 15:35:57 +01:00
var decal = _prototypeManager . EnumeratePrototypes < DecalPrototype > ( ) . FirstOrDefault ( x = > x . Tags . Contains ( "crayon" ) ) ;
SelectedState = decal ? . ID ? ? string . Empty ;
2020-10-13 13:40:05 +02:00
Dirty ( ) ;
}
private void UserInterfaceOnReceiveMessage ( ServerBoundUserInterfaceMessage serverMsg )
{
switch ( serverMsg . Message )
{
case CrayonSelectMessage msg :
// Check if the selected state is valid
2021-12-03 15:35:57 +01:00
if ( _prototypeManager . TryIndex < DecalPrototype > ( msg . State , out var prototype ) & & prototype . Tags . Contains ( "crayon" ) )
2020-10-13 13:40:05 +02:00
{
2021-12-03 15:35:57 +01:00
SelectedState = msg . State ;
Dirty ( ) ;
2020-10-13 13:40:05 +02:00
}
break ;
default :
break ;
}
}
2021-11-30 15:20:38 +01:00
public override ComponentState GetComponentState ( )
2020-10-13 13:40:05 +02:00
{
return new CrayonComponentState ( _color , SelectedState , Charges , Capacity ) ;
}
// Opens the selection window
bool IUse . UseEntity ( UseEntityEventArgs eventArgs )
{
2021-12-08 17:17:12 +01:00
if ( _entMan . TryGetComponent ( eventArgs . User , out ActorComponent ? actor ) )
2020-10-13 13:40:05 +02:00
{
2021-05-12 13:42:18 +02:00
UserInterface ? . Toggle ( actor . PlayerSession ) ;
if ( UserInterface ? . SessionHasOpen ( actor . PlayerSession ) = = true )
2020-10-13 13:40:05 +02:00
{
// Tell the user interface the selected stuff
UserInterface . SetState (
new CrayonBoundUserInterfaceState ( SelectedState , Color ) ) ;
}
return true ;
}
return false ;
}
2021-02-03 14:05:31 +01:00
async Task < bool > IAfterInteract . AfterInteract ( AfterInteractEventArgs eventArgs )
2020-10-13 13:40:05 +02:00
{
if ( ! eventArgs . InRangeUnobstructed ( ignoreInsideBlocker : false , popup : true ,
2021-02-03 14:05:31 +01:00
collisionMask : Shared . Physics . CollisionGroup . MobImpassable ) )
{
return true ;
}
2020-10-13 13:40:05 +02:00
if ( Charges < = 0 )
{
2021-06-21 02:13:54 +02:00
eventArgs . User . PopupMessage ( Loc . GetString ( "crayon-interact-not-enough-left-text" ) ) ;
2021-02-03 14:05:31 +01:00
return true ;
2020-10-13 13:40:05 +02:00
}
2021-12-08 17:17:12 +01:00
if ( ! eventArgs . ClickLocation . IsValid ( _entMan ) )
2021-07-25 01:44:26 -07:00
{
eventArgs . User . PopupMessage ( Loc . GetString ( "crayon-interact-invalid-location" ) ) ;
return true ;
}
2021-02-03 14:05:31 +01:00
2021-12-03 15:35:57 +01:00
if ( ! EntitySystem . Get < DecalSystem > ( ) . TryAddDecal ( SelectedState , eventArgs . ClickLocation . Offset ( new Vector2 ( - 0.5f , - 0.5f ) ) , out _ , Color . FromName ( _color ) , cleanable : true ) )
return false ;
2020-10-13 13:40:05 +02:00
2021-08-13 21:31:37 -07:00
if ( _useSound ! = null )
SoundSystem . Play ( Filter . Pvs ( Owner ) , _useSound . GetSound ( ) , Owner , AudioHelpers . WithVariation ( 0.125f ) ) ;
2020-10-13 13:40:05 +02:00
// Decrease "Ammo"
Charges - - ;
Dirty ( ) ;
2021-12-14 00:22:58 +13:00
EntitySystem . Get < AdminLogSystem > ( ) . Add ( LogType . CrayonDraw , LogImpact . Low , $"{_entMan.ToPrettyString(eventArgs.User):user} drew a {_color:color} {SelectedState}" ) ;
2021-02-03 14:05:31 +01:00
return true ;
2020-10-13 13:40:05 +02:00
}
void IDropped . Dropped ( DroppedEventArgs eventArgs )
{
2021-12-08 17:17:12 +01:00
if ( _entMan . TryGetComponent ( eventArgs . User , out ActorComponent ? actor ) )
2021-05-12 13:42:18 +02:00
UserInterface ? . Close ( actor . PlayerSession ) ;
2020-10-13 13:40:05 +02:00
}
}
}