public static void Run(World world) { /* Define components */ var posEntity = ecs.new_component(world, Caches.AddUnmanagedString("Position"), Heap.SizeOf <Position>()); var velEntity = ecs.new_component(world, Caches.AddUnmanagedString("Velocity"), Heap.SizeOf <Velocity>()); /* Obtain variables to component types */ var posType = ecs.type_from_entity(world, posEntity); var velType = ecs.type_from_entity(world, velEntity); /* Register system */ ecs.new_system(world, Caches.AddUnmanagedString("Move"), SystemKind.OnUpdate, "Position, Velocity", Move); ECS_SYSTEM(world, Move, SystemKind.OnUpdate, "Position, Velocity"); /* Create an entity */ var e = ecs.new_entity(world); /* Set entity identifier using builtin component */ ecs.set_ptr(world, e, ecs.TEcsId, (void *)Caches.AddUnmanagedString("MyEntity").Ptr()); /* Set values for entity. */ ecs.set(world, e, new Position { X = 0, Y = 0 }); ecs.set(world, e, new Velocity { X = 1, Y = 1 }); /* Run systems */ ecs.progress(world, 0); ecs.progress(world, 0); ecs.progress(world, 0); }
public static EntityId ECS_SYSTEM <T1>(World world, SystemAction <T1> systemImpl, SystemKind kind, SystemSignatureBuilder signatureBuilder) where T1 : unmanaged { SystemActionDelegate del = delegate(ref Rows rows) { var set1 = (T1 *)_ecs.column(ref rows, Heap.SizeOf <T1>(), 1); systemImpl(ref rows, new Span <T1>(set1, (int)rows.count), ecs.get_delta_time(world)); }; // ensure our system doesnt get GCd and that our Component is registered Caches.AddSystemAction(world, del); var systemNamePtr = Caches.AddUnmanagedString(systemImpl.Target.GetType().FullName); return(ecs.new_system(world, systemNamePtr, kind, signatureBuilder.Build(), del)); }
public static void Run(World world) { /* Define component */ ECS_COMPONENT <Message>(world); /* Define a system called PrintMessage that is executed every frame, and * subscribes for the 'Message' component */ ECS_SYSTEM(world, PrintMessage, SystemKind.OnUpdate, "Message"); /* Create new entity, add the component to the entity */ var e = ecs.new_entity <Message>(world); ecs.set(world, e, new Message { text = Caches.AddUnmanagedString("Hello Flecs#!") }); /* Set target FPS for main loop to 1 frame per second */ ecs.set_target_fps(world, 1); /* Run systems */ ecs.progress(world, 0); }
public TestSystem(World world) : base(world, SystemKind.OnUpdate) { SetComponent(CreateEntity <Message>(), new Message { Value = Caches.AddUnmanagedString("Hello Flecs#!") }); }
public static void Run(World world) { /* Define components */ var worldPosType = Caches.AddComponentTypedef <Position>(world, "WorldPosition"); ECS_COMPONENT <Position>(world); ECS_COMPONENT <Velocity>(world); /* Move entities with Position and Velocity */ ECS_SYSTEM(world, Move, SystemKind.OnUpdate, "Position, Velocity"); /* Transform local coordinates to world coordinates. A CASCADE column * guarantees that entities are evaluated breadth-first, according to the * hierarchy. This system will depth-sort based on parents that have the * WorldPosition component. */ ECS_SYSTEM(world, Transform, SystemKind.OnUpdate, "CASCADE.WorldPosition, WorldPosition, Position"); /* Create root of the hierarchy which moves around */ var root = ecs.new_entity(world); ecs.set_ptr(world, root, ecs.TEcsId, Caches.AddUnmanagedString("Root").Ptr()); ecs.add(world, root, worldPosType); ecs.set(world, root, new Position { X = 0, Y = 0 }); ecs.set(world, root, new Velocity { X = 1, Y = 2 }); /* Create children that don't move and are relative to the parent */ var child1 = ecs.new_child(world, root); ecs.set_id(world, child1, "Child1"); ecs.add(world, child1, worldPosType); ecs.set(world, child1, new Position { X = 100, Y = 100 }); { var gChild1 = ecs.new_child(world, child1); ecs.set_id(world, gChild1, "GChild1"); ecs.add(world, gChild1, worldPosType); ecs.set(world, gChild1, new Position { X = 1000, Y = 1000 }); } var child2 = ecs.new_child(world, root); ecs.set_id(world, child2, "Child2"); ecs.add(world, child2, worldPosType); ecs.set(world, child2, new Position { X = 200, Y = 200 }); { var gChild2 = ecs.new_child(world, child2); ecs.set_id(world, gChild2, "GChild2"); ecs.add(world, gChild2, worldPosType); ecs.set(world, gChild2, new Position { X = 2000, Y = 2000 }); } /* Run systems */ ecs.progress(world, 0); ecs.progress(world, 0); ecs.progress(world, 0); }