lyra-wasm-scripting-test/guests/csharp/dotnet-guest-test/ExampleWorldImpl.cs

56 lines
1.6 KiB
C#

namespace ExampleWorld;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ExampleWorld.wit.imports.lyra.api;
using LyraApi;
using LyraApi.Ecs;
[StructLayout(LayoutKind.Sequential)]
struct Vec3(float x, float y, float z) : IComponent
{
/// <summary>
/// The X component.
/// </summary>
public float X { get; set; } = x;
/// <summary>
/// The Y component
/// </summary>
public float Y { get; set; } = y;
/// <summary>
/// The Z component
/// </summary>
public float Z { get; set; } = z;
public static string HostName => "Vec3";
public static ulong HostSize => (ulong)Marshal.SizeOf<Vec3>();
public static ulong HostAlignment => (ulong)MarshalUtils.AlignmentOf<Vec3>();
public static ulong TypeId => 4124409524;
public static object? TakeFromBytes(byte[] bytes)
{
byte[] taken = bytes.Take((int)HostSize).ToArray();
return MarshalUtils.FromBytes<Vec3>(taken);
}
}
public class ExampleWorldImpl : IExampleWorld
{
public static void OnInit(IEcs.EcsWorld gameWorld, IEcs.Entity owningEntity)
{
var world = new World(gameWorld);
Entity entity = world.Spawn(new Vec3(7.0f, 30.0f, 18.0f));
Console.WriteLine("C#: Spawned entity with id {0}", entity.Id);
foreach ((Entity en, Vec3 comp) in world.View<Vec3>())
{
Console.WriteLine("C#: Found entity at ({0}, {1}, {2})", comp.X, comp.Y, comp.Z);
}
}
public static void OnUpdate(IEcs.EcsWorld gameWorld, IEcs.Entity owningEntity)
{
throw new NotImplementedException();
}
}