Skip to content

Commit 568897b

Browse files
committed
refine example for event systems
1 parent 44f70fe commit 568897b

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

Sia.Examples/Example16_EventSystem.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
namespace Sia_Examples;
22

3+
using System.Collections.Immutable;
34
using Sia;
45

56
public static partial class Example16_EventSystem
67
{
78
[SiaTemplate(nameof(Position))]
89
public record RPosition(int X, int Y);
910

11+
[SiaTemplate(nameof(Tags))]
12+
public record RTags
13+
{
14+
[Sia(Item = "")]
15+
public ImmutableArray<string> List { get; init; } = [];
16+
}
17+
1018
public partial record struct Position
1119
{
1220
public readonly record struct MoveUp(int Count) : IEvent;
@@ -111,6 +119,31 @@ protected override void HandleEvent<TEvent>(
111119
}
112120
}
113121

122+
public class TagsTemplateEventSystem : TemplateEventSystemBase<Tags, RTags>
123+
{
124+
protected override void HandleEvent<TEvent>(in Identity id, in Tags snapshot, in TEvent e)
125+
{
126+
switch (e) {
127+
case Tags.Add(string tag):
128+
Console.WriteLine("Tag added: " + tag);
129+
Console.WriteLine("\tPrevious tags: " + string.Join(", ", snapshot.List));
130+
break;
131+
case Tags.Remove(string tag):
132+
Console.WriteLine("Tag removed: " + tag);
133+
Console.WriteLine("\tPrevious tags: " + string.Join(", ", snapshot.List));
134+
break;
135+
case Tags.Set(int index, string tag):
136+
Console.WriteLine("Tag set: " + tag + ", index: " + index);
137+
Console.WriteLine("\tPrevious tags: " + string.Join(", ", snapshot.List));
138+
break;
139+
case Tags.SetList(var list):
140+
Console.WriteLine("Tags changed: " + string.Join(", ", list));
141+
Console.WriteLine("\tPrevious tags: " + string.Join(", ", snapshot.List));
142+
break;
143+
}
144+
}
145+
}
146+
114147
public static void Run(World world)
115148
{
116149
var scheduler = new Scheduler();
@@ -119,22 +152,31 @@ public static void Run(World world)
119152
.Add<TestTemplateEventSystem>()
120153
.Add<TestEventSystem>()
121154
.Add<TestSnapshotEventSystem>()
155+
.Add<TagsTemplateEventSystem>()
122156
.RegisterTo(world, scheduler);
123157

124-
var e = world.CreateInArrayHost(HList.Create(new Position(1, 1)));
158+
var e = world.CreateInArrayHost(HList.Create(
159+
new Position(1, 1), new Tags([])));
160+
125161
var pos = new Position.View(e);
162+
var tags = new Tags.View(e);
126163

127164
e.Send(new Position.MoveUp(5));
128165
e.Send(new Position.MoveLeft(5));
166+
tags.Add("Test");
129167
scheduler.Tick();
130168

131169
e.Modify(new Position.TestCommand());
170+
tags.Remove("Test");
132171
scheduler.Tick();
133172

134173
pos.X += 10;
174+
tags.List = ["a", "b", "c"];
175+
tags.Set(1, "d");
135176
scheduler.Tick();
136177

137178
pos.Y += 5;
179+
tags.List = [];
138180
scheduler.Tick();
139181

140182
e.Remove<Position>();

0 commit comments

Comments
 (0)