add: MessageBus

This commit is contained in:
h z
2025-03-03 11:08:25 +00:00
parent d2f0ab8153
commit 684763f0cc
15 changed files with 211 additions and 64 deletions

View File

@@ -1,27 +1,27 @@
using Godot;
using Polonium.Resources;
using Polonium.MessageManager;
namespace Polonium.Agents;
public abstract partial class World : Node
public abstract partial class World : MessageBus
{
protected WorldModel Model { get; set; }
public HashSet<Agent> Agents { get; } = new();
public Knowledge CommonKnowledge { get; set; }
public override void _Ready()
{
Model = GetNode<WorldModel>("WorldModel");
Model.WorldKnowledge.PostCode = "WorldKnowledge";
Register(Model.WorldKnowledge);
CommonKnowledge = GetNode<Knowledge>("CommonKnowledge");
Model.CommonKnowledgeUpdated += CommonKnowledgeUpdated;
Model.PrivateKnowledgeUpdated += ForwardPrivateKnowledgeUpdated;
CommonKnowledge.PostCode = "CommonKnowledge";
Register(CommonKnowledge);
base._Ready();
}
public void RegisterAgent(Agent agent)
public void Register(Agent agent)
{
Agents.Add(agent);
agent.ActionExecuted += Model.ActionExecuted;
Register(agent.Knowledge);
}
public virtual void Enter()
@@ -34,12 +34,4 @@ public abstract partial class World : Node
PoloniumRegistry.Instance.CurrentWorld = null;
}
public void CommonKnowledgeUpdated(KnowledgePatch update) => update.Patch(CommonKnowledge);
public void ForwardPrivateKnowledgeUpdated(SignalHeader header, KnowledgePatch update)
{
foreach (Agent receiver in header.ResolveReceivers())
receiver.UpdateKnowledge(update);
}
}