lyra-wasm-scripting-test/guests/csharp/dotnet-guest-test/LyraApi/Ecs/World.cs

36 lines
1.1 KiB
C#
Raw Normal View History

namespace LyraApi.Ecs;
using ExampleWorld.wit.imports.lyra.api;
using LyraApi;
public class World(IEcs.EcsWorld world)
{
private IEcs.EcsWorld Inner { get; set; } = world;
private Entity Spawn(List<IEcs.ComponentInfo> infos, params object[] comps)
{
byte[] bytes = comps.SelectMany(c => MarshalUtils.GetBytes(c)).ToArray();
return new Entity(Inner.Spawn(bytes, infos));
}
public Entity Spawn<T1>(T1 c1) where T1 : IComponent
{
List<IEcs.ComponentInfo> infos = [ComponentInfo.FromType<T1>().info];
return Spawn(infos, c1);
}
public IEnumerable<(Entity, T1)> View<T1>() where T1 : IComponent
{
List<Component> comps = [Component.FromComponent<T1>()];
List<IEcs.ComponentInfo> infos = comps.Select(c => c.GetComponentInfo().info).ToList();
IEcs.EcsDynamicView dynamicView = Inner.View(infos);
return new ViewResult(comps, dynamicView).Get<T1>();
}
2024-11-10 00:13:15 +00:00
public T? GetResource<T>() where T : IResource
{
IEcs.WorldResourceResult result = Inner.GetResource(Utils.ToWasmTypeId(T.TypeId));
return (T?) T.FromWasmResult(result);
}
}