Skip to content

Commit

Permalink
Add support for persisting events as sfx so it can be paused on menu …
Browse files Browse the repository at this point in the history
…screen
  • Loading branch information
isadorasophia committed Dec 21, 2024
1 parent f6fbd01 commit fde9cbe
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 25 deletions.
26 changes: 21 additions & 5 deletions src/Murder.Editor/CustomComponents/EventListenerEditorComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ImGuiNET;
using Murder.Assets.Graphics;
using Murder.Components;
using Murder.Core.Sounds;
using Murder.Editor.Attributes;
using Murder.Editor.CustomFields;
using Murder.Editor.ImGuiExtended;
Expand Down Expand Up @@ -143,16 +144,31 @@ protected override bool DrawAllMembersWithTable(ref object target)

ImGui.TableNextColumn();

bool value = info.Persist;
if (ImGui.Checkbox($"##dropdown_checkbox_{i}", ref value))
SoundLayer? layer = info.Persisted;
if (layer is null)
{
events = events.SetItem(i, info.WithPersist(value));
fileChanged = true;
bool isPersist = false;
if (ImGui.Checkbox($"##dropdown_checkbox_{i}", ref isPersist))
{
events = events.SetItem(i, info.WithPersist(SoundLayer.Ambience));
fileChanged = true;
}
}
else
{
ImGui.PushID($"##dropdown_layer_{i}");

if (CustomField.DrawValue(ref info, fieldName: nameof(SpriteEventInfo.Persisted)))
{
events = events.SetItem(i, info);
fileChanged = true;
}

ImGui.PopID();
}

ImGui.SameLine();
ImGuiHelpers.HelpTooltip("Whether this event should persist.");

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Murder.Editor/CustomEditors/SpriteEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private bool AddTestSounds(SpriteInformation info)
continue;
}

builder[message] = new(message, sound.Value, persist: false);
builder[message] = new(message, sound.Value, persisted: null);
}

info.Stage.AddOrReplaceComponentOnEntity(
Expand Down
2 changes: 1 addition & 1 deletion src/Murder.Editor/EditorScene_Shortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private void ReloadAtlasWithChangesToggled(bool value)
{
Architect.EditorSettings.AlwaysBuildAtlasOnStartup = value;

// Persist changes immediately.
// Persisted changes immediately.
Architect.EditorData.SaveAsset(Architect.EditorSettings);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Murder.Editor/Stage/Stage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public void Draw(Rectangle? rectToDrawStage = null)

public void PersistInfo(Guid guid)
{
// Persist the last position.
// Persisted the last position.
Architect.EditorSettings.StageInfo[guid] = new(
_renderContext.Camera.Position.Point(),
_renderContext.Camera.Size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@ public readonly struct SpriteEventInfo
public readonly SoundEventId? Sound;

[Default("Whether this should be persisted (such as ambience sounds)")]
public readonly bool Persist = false;
public readonly SoundLayer? Persisted = null;

public SpriteEventInfo(string id) => Id = id;

public SpriteEventInfo(string id, SoundEventId? sound, bool persist)
public SpriteEventInfo(string id, SoundEventId? sound, SoundLayer? persisted)
{
Id = id;
Sound = sound;
Persist = persist;
Persisted = persisted;
}

public SpriteEventInfo(string id, SoundEventId? sound)
{
Id = id;
Sound = sound;
}

public SpriteEventInfo WithPersist(bool persist) =>
new(Id, Sound, persist);
public SpriteEventInfo WithPersist(SoundLayer persisted) =>
new(Id, Sound, persisted);
}

[SoundPlayer]
Expand Down
7 changes: 6 additions & 1 deletion src/Murder/Core/Engine/MonoWorld.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Bang;
using Bang.Systems;
using Murder.Core.Graphics;
using Murder.Core.Sounds;
using Murder.Services;

namespace Murder.Core
{
Expand All @@ -22,12 +24,16 @@ public override void Pause()
{
base.Pause();
Game.Instance.Pause();

SoundServices.Pause(SoundLayer.Sfx);
}

public override void Resume()
{
base.Resume();
Game.Instance.Resume();

SoundServices.Resume(SoundLayer.Sfx);
}

public void PreDraw()
Expand All @@ -37,7 +43,6 @@ public void PreDraw()
{
if (system is IMonoPreRenderSystem preRenderSystem)
{

if (DIAGNOSTICS_MODE)
{
_stopwatch.Reset();
Expand Down
2 changes: 1 addition & 1 deletion src/Murder/Data/Save/SavedWorldBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ internal async ValueTask<SavedWorld> CreateAsync(ImmutableArray<Entity> entities
instance.SetName($"{instance.Name} ({e.EntityId})");
}

// Persist entity id.
// Persisted entity id.
instance.Id = e.EntityId;
instance.IsDeactivated = e.IsDeactivated;
instance.ActivateWithParent = e.IsActivateWithParent();
Expand Down
3 changes: 2 additions & 1 deletion src/Murder/Game_Scenes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bang;
using Murder.Core;
using Murder.Core.Sounds;
using Murder.Diagnostics;

namespace Murder
Expand Down Expand Up @@ -80,7 +81,7 @@ protected void DoPendingWorldTransition()
}

// TODO: Cross fade? Review this flag here!
// SoundPlayer.Stop(fadeOut: true);
SoundPlayer.Stop(SoundLayer.Sfx, fadeOut: true);

GameLogger.Verify(_sceneLoader is not null);

Expand Down
2 changes: 1 addition & 1 deletion src/Murder/Services/SoundServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static ImmutableDictionary<string, SpriteEventInfo> ReplaceIdentifiers(
foreach ((string id, SpriteEventInfo info) in source)
{
string newIdentifier = converter(id);
builder[newIdentifier] = new(newIdentifier, info.Sound, info.Persist);
builder[newIdentifier] = new(newIdentifier, info.Sound, info.Persisted);
}

return builder.ToImmutable();
Expand Down
4 changes: 2 additions & 2 deletions src/Murder/Systems/Effects/EventListenerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void OnMessage(World world, Entity entity, IMessage message)

// For now, infer that any sound event id fired from a sprite that persists
// is actually an ambience sound.
if (info.Persist)
if (info.Persisted is SoundLayer specificLayer)
{
properties |= SoundProperties.Persist;
layer = SoundLayer.Ambience;
layer = specificLayer;
}

_ = SoundServices.Play(sound, entity, layer, properties);
Expand Down

0 comments on commit fde9cbe

Please sign in to comment.