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

51 lines
1.4 KiB
C#
Raw Permalink Normal View History

namespace LyraApi.Ecs;
using ExampleWorld.wit.imports.lyra.api;
using LyraApi;
using LyraApi.Asset;
public class World(IEcs.EcsWorld world)
{
internal IEcs.EcsWorld inner = 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);
}
/* public AssetManager? GetAssetManager()
{
IAsset.AssetManager? assetManager = IAsset.AssetManager.FromWorld(inner);
if (assetManager != null)
{
return new AssetManager(assetManager);
}
else
{
return null;
}
} */
}