Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matchstick prediction issues #31418

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Content.Client/Lightning/ClientMatchstickSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Content.Shared.Light.EntitySystems;

namespace Content.Client.Light.EntitySystems;

public sealed class MatchstickSystem : SharedMatchstickSystem
{
}
9 changes: 0 additions & 9 deletions Content.Server/Light/Components/MatchboxComponent.cs

This file was deleted.

29 changes: 0 additions & 29 deletions Content.Server/Light/Components/MatchstickComponent.cs

This file was deleted.

29 changes: 0 additions & 29 deletions Content.Server/Light/EntitySystems/MatchboxSystem.cs

This file was deleted.

136 changes: 30 additions & 106 deletions Content.Server/Light/EntitySystems/MatchstickSystem.cs
Original file line number Diff line number Diff line change
@@ -1,123 +1,47 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Light.Components;
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Smoking;
using Content.Shared.Temperature;
using Content.Shared.Light.Components;
using Content.Server.Atmos.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;

namespace Content.Server.Light.EntitySystems
{
public sealed class MatchstickSystem : EntitySystem
{
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedItemSystem _item = default!;
[Dependency] private readonly SharedPointLightSystem _lights = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;

private readonly HashSet<Entity<MatchstickComponent>> _litMatches = new();

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MatchstickComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<MatchstickComponent, IsHotEvent>(OnIsHotEvent);
SubscribeLocalEvent<MatchstickComponent, ComponentShutdown>(OnShutdown);
}

private void OnShutdown(Entity<MatchstickComponent> ent, ref ComponentShutdown args)
{
_litMatches.Remove(ent);
}

public override void Update(float frameTime)
{
base.Update(frameTime);

foreach (var match in _litMatches)
{
if (match.Comp.CurrentState != SmokableState.Lit || Paused(match) || match.Comp.Deleted)
continue;

var xform = Transform(match);

if (xform.GridUid is not {} gridUid)
return;
using Robust.Shared.Timing;
using Content.Shared.Light.EntitySystems;

var position = _transformSystem.GetGridOrMapTilePosition(match, xform);
namespace Content.Server.Light.EntitySystems;

_atmosphereSystem.HotspotExpose(gridUid, position, 400, 50, match, true);
}
}

private void OnInteractUsing(Entity<MatchstickComponent> ent, ref InteractUsingEvent args)
{
if (args.Handled || ent.Comp.CurrentState != SmokableState.Unlit)
return;

var isHotEvent = new IsHotEvent();
RaiseLocalEvent(args.Used, isHotEvent);

if (!isHotEvent.IsHot)
return;
public sealed class MatchstickSystem : SharedMatchstickSystem
{
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;

Ignite(ent, args.User);
args.Handled = true;
}
public override void Initialize()
{
base.Initialize();
}

private void OnIsHotEvent(EntityUid uid, MatchstickComponent component, IsHotEvent args)
{
args.IsHot = component.CurrentState == SmokableState.Lit;
}
public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<MatchstickComponent>();

public void Ignite(Entity<MatchstickComponent> matchstick, EntityUid user)
while (query.MoveNext(out var uid, out var match))
{
var component = matchstick.Comp;
if (match.CurrentState != SmokableState.Lit)
continue;

// Play Sound
_audio.PlayPvs(component.IgniteSound, matchstick, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));
var xform = Transform(uid);

// Change state
SetState(matchstick, component, SmokableState.Lit);
_litMatches.Add(matchstick);
matchstick.Owner.SpawnTimer(component.Duration * 1000, delegate
{
SetState(matchstick, component, SmokableState.Burnt);
_litMatches.Remove(matchstick);
});
}
if (xform.GridUid is not { } gridUid)
continue;

private void SetState(EntityUid uid, MatchstickComponent component, SmokableState value)
{
component.CurrentState = value;
var position = _transformSystem.GetGridOrMapTilePosition(uid, xform);

if (_lights.TryGetLight(uid, out var pointLightComponent))
{
_lights.SetEnabled(uid, component.CurrentState == SmokableState.Lit, pointLightComponent);
}

if (EntityManager.TryGetComponent(uid, out ItemComponent? item))
{
switch (component.CurrentState)
{
case SmokableState.Lit:
_item.SetHeldPrefix(uid, "lit", component: item);
break;
default:
_item.SetHeldPrefix(uid, "unlit", component: item);
break;
}
}
_atmosphereSystem.HotspotExpose(gridUid, position, 400, 50, uid, true);

if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
// Check if the match has expired.
var burnoutTime = match.TimeMatchWillBurnOut;
if (burnoutTime != null && _timing.CurTime > burnoutTime)
{
_appearance.SetData(uid, SmokingVisuals.Smoking, component.CurrentState, appearance);
SetState(uid, match, SmokableState.Burnt);
match.TimeMatchWillBurnOut = null;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions Content.Shared/Lightning/Components/MatchboxComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Robust.Shared.GameStates;

namespace Content.Shared.Light.Components;

// TODO make changes in icons when different threshold reached
// e.g. different icons for 10% 50% 100%
[RegisterComponent, NetworkedComponent]
public sealed partial class MatchboxComponent : Component
{
}

35 changes: 35 additions & 0 deletions Content.Shared/Lightning/Components/MatchstickComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Content.Shared.Smoking;
using Robust.Shared.Audio;
using Robust.Shared.Serialization;
using Robust.Shared.GameStates;

namespace Content.Shared.Light.Components;

[NetworkedComponent, RegisterComponent]
[AutoGenerateComponentState, AutoGenerateComponentPause]
public sealed partial class MatchstickComponent : Component
{
/// <summary>
/// Current state to matchstick. Can be <code>Unlit</code>, <code>Lit</code> or <code>Burnt</code>.
/// </summary>
[DataField("state")]
[AutoNetworkedField]
public SmokableState CurrentState = SmokableState.Unlit;

/// <summary>
/// How long will matchstick last in seconds.
/// </summary>
[DataField("duration")]
public int Duration = 10;

/// <summary>
/// The time that the match will burn out. If null, that means the match is unlit.
/// </summary>
[AutoNetworkedField, AutoPausedField]
public TimeSpan? TimeMatchWillBurnOut = null;

/// <summary>
/// Sound played when you ignite the matchstick.
/// </summary>
[DataField("igniteSound", required: true)] public SoundSpecifier IgniteSound = default!;
}
28 changes: 28 additions & 0 deletions Content.Shared/Lightning/EntitySystems/MatchboxSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Content.Shared.Light.Components;
using Content.Shared.Storage.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Smoking;

namespace Content.Shared.Light.EntitySystems;

public sealed class SharedMatchboxSystem : EntitySystem
{
[Dependency] private readonly SharedMatchstickSystem _stickSystem = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MatchboxComponent, InteractUsingEvent>(OnInteractUsing, before: new[] { typeof(SharedStorageSystem) });
}

private void OnInteractUsing(EntityUid uid, MatchboxComponent component, InteractUsingEvent args)
{
if (args.Handled || !EntityManager.TryGetComponent(args.Used, out MatchstickComponent? matchstick))
return;

if (matchstick.CurrentState == SmokableState.Unlit)
_stickSystem.Ignite((args.Used, matchstick), args.User);

args.Handled = true;
}
}
86 changes: 86 additions & 0 deletions Content.Shared/Lightning/EntitySystems/SharedMatchstickSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Smoking;
using Content.Shared.Temperature;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;
using Content.Shared.Light.Components;
using Robust.Shared.Timing;

namespace Content.Shared.Light.EntitySystems;

public abstract class SharedMatchstickSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedItemSystem _item = default!;
[Dependency] private readonly SharedPointLightSystem _lights = default!;
[Dependency] private readonly IGameTiming _timing = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MatchstickComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<MatchstickComponent, IsHotEvent>(OnIsHotEvent);
}

private void OnInteractUsing(Entity<MatchstickComponent> ent, ref InteractUsingEvent args)
{
if (args.Handled || ent.Comp.CurrentState != SmokableState.Unlit)
return;

var isHotEvent = new IsHotEvent();
RaiseLocalEvent(args.Used, isHotEvent);

if (!isHotEvent.IsHot)
return;

Ignite(ent, args.User);
args.Handled = true;
}

private void OnIsHotEvent(EntityUid uid, MatchstickComponent component, IsHotEvent args)
{
args.IsHot = component.CurrentState == SmokableState.Lit;
}

public void Ignite(Entity<MatchstickComponent> matchstick, EntityUid user)
{
// Play Sound
_audio.PlayPredicted(matchstick.Comp.IgniteSound, matchstick, user, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));

// Change state
SetState(matchstick, matchstick.Comp, SmokableState.Lit);
matchstick.Comp.TimeMatchWillBurnOut = _timing.CurTime + TimeSpan.FromSeconds(matchstick.Comp.Duration);

}

protected void SetState(EntityUid uid, MatchstickComponent component, SmokableState value)
{
component.CurrentState = value;

if (_lights.TryGetLight(uid, out var pointLightComponent))
{
_lights.SetEnabled(uid, component.CurrentState == SmokableState.Lit, pointLightComponent);
}

if (EntityManager.TryGetComponent(uid, out ItemComponent? item))
{
switch (component.CurrentState)
{
case SmokableState.Lit:
_item.SetHeldPrefix(uid, "lit", component: item);
break;
default:
_item.SetHeldPrefix(uid, "unlit", component: item);
break;
}
}

if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
{
_appearance.SetData(uid, SmokingVisuals.Smoking, component.CurrentState, appearance);
}
}
}
Loading