Skip to content

Commit

Permalink
Fix #12
Browse files Browse the repository at this point in the history
  • Loading branch information
tma02 committed Dec 12, 2024
1 parent 4b4abd4 commit 51a416f
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions Teemaw.Calico/ScriptMod/SoundManagerScriptModFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Teemaw.Calico.LexicalTransformer;
using static Teemaw.Calico.LexicalTransformer.Operation;
using static Teemaw.Calico.LexicalTransformer.TransformationPatternFactory;
using static Teemaw.Calico.Util.WeaveUtil;

namespace Teemaw.Calico.ScriptMod;

Expand All @@ -20,30 +21,49 @@ public static IScriptMod Create(IModInterface mod)
.Matching(CreateGlobalsPattern())
.Do(Append)
.With(
"""
const CALICO_PERSIST = ["dive_scrape", "reel_slow", "reel_fast"]
var calico_players = {}
func _ready():
for child in get_children():
if (child is AudioStreamPlayer3D || child is AudioStreamPlayer) && !CALICO_PERSIST.has(child.name):
calico_players[child.name] = child
calico_players[child.name].connect("finished", self, "calico_remove_child", [child.name])
remove_child(child)
func calico_remove_child(id):
print("[calico] Cleaning up sfx ", id)
remove_child(calico_players[id])
func calico_get_player_or_null(id):
if !calico_players.has(id):
return get_node_or_null(id)
if calico_players[id].get_parent() == null:
add_child(calico_players[id])
return calico_players[id]
"""))
$"""
const CALICO_PERSIST = [{
// This is the first place we dynamically determine patch content based on state available to
// GDWeave. I'm not yet certain that this is the best pattern to follow moving forward.
new Func<string>(() => {
List<string> persistedPlayerNodes = ["dive_scrape", "reel_slow", "reel_fast"];
if (IsModLoaded(mod, "Sulayre.Lure"))
{
// Lure expects bark_cat in the node tree as a template
persistedPlayerNodes.Add("bark_cat");
}
var array = persistedPlayerNodes
.Append("bark_cat")
.Select(node => "\"" + node + "\"")
.ToArray();
return string.Join(", ", array);
})()
}]
var calico_players = {/* Hack: {} can't be escaped in a raw string */"{}"}
func _ready():
print("[calico] caching player sfx")
for child in get_children():
if (child is AudioStreamPlayer3D || child is AudioStreamPlayer) && !CALICO_PERSIST.has(child.name):
calico_players[child.name] = child
calico_players[child.name].connect("finished", self, "calico_remove_child", [child.name])
remove_child(child)
func calico_remove_child(id):
print("[calico] Cleaning up sfx ", id)
remove_child(calico_players[id])
func calico_get_player_or_null(id):
if !calico_players.has(id):
return get_node_or_null(id)
if calico_players[id].get_parent() == null:
add_child(calico_players[id])
return calico_players[id]
"""))
.AddRule(new TransformationRuleBuilder()
.Named("get_node_or_null")
.Matching(CreateGdSnippetPattern("var node = get_node_or_null"))
Expand Down

0 comments on commit 51a416f

Please sign in to comment.