Esempio n. 1
0
        public static void Run(World world)
        {
            /* Register components */
            var massType = Caches.AddComponentTypedef <float>(world, "Mass");

            /* Create two base entities */
            var baseEntity = ecs.new_entity(world);

            ecs.set_typedef(world, baseEntity, massType, 10f);

            /* Create instances which share the Mass component from a base */
            var instance = ecs.new_instance(world, baseEntity);

            /* Print value before overriding Mass. The component is not owned, as it is shared with the base entity. */
            Console.WriteLine("Before overriding:");
            Console.WriteLine("instance: {0} (owned = {1})", ecs.ecs_get <float>(world, instance, massType), ecs.has_owned(world, instance, massType));

            /* Override Mass of instance, which will give instance a private copy of the Mass component. */
            ecs.set_typedef(world, instance, massType, 20f);

            /* Print values after overriding Mass. The value of Mass for instance_1 will
             * be the value assigned in the override (20). Instance now owns Mass,
             * confirming it has a private copy of the component. */
            Console.WriteLine("\nAfter overriding:");
            Console.WriteLine("instance: {0} (owned = {1})", ecs.ecs_get <float>(world, instance, massType), ecs.has_owned(world, instance, massType));

            /* Remove override of Mass. This will remove the private copy of the Mass component from instance. */
            ecs.remove(world, instance, massType);

            /* Print value after removing the override Mass. The component is no longer
             * owned, as the instance is again sharing the component with base. */
            Console.WriteLine("\nAfter removing override:");
            Console.WriteLine("instance: {0} (owned = {1})", ecs.ecs_get <float>(world, instance, massType), ecs.has_owned(world, instance, massType));
        }
Esempio n. 2
0
        public static void Run(World world)
        {
            /* Define components */
            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 e = ECS_ENTITY(world, "Root", "WorldPosition, Position, Velocity");

            ecs.set(world, e, new Position {
                X = 0, Y = 0
            });
            ecs.set(world, e, new Velocity {
                X = 1, Y = 2
            });

            /* Create children that don't move and are relative to the parent */
            var child1 = ECS_ENTITY(world, "Child1", "WorldPosition, Position, CHILDOF | Root");

            ecs.set(world, child1, new Position {
                X = 100, Y = 100
            });
            {
                var gChild1 = ECS_ENTITY(world, "GChild1", "WorldPosition, Position, CHILDOF | Child1");
                ecs.set(world, gChild1, new Position {
                    X = 1000, Y = 1000
                });
            }

            var child2 = ECS_ENTITY(world, "Child2", "WorldPosition, Position, CHILDOF | Root");

            ecs.set(world, child2, new Position {
                X = 200, Y = 200
            });
            {
                var gChild2 = ECS_ENTITY(world, "GChild2", "WorldPosition, Position, CHILDOF | Child2");
                ecs.set(world, gChild2, new Position {
                    X = 2000, Y = 2000
                });
            }


            /* Run systems */
            ecs.progress(world, 0);
            ecs.progress(world, 0);
            ecs.progress(world, 0);
        }
Esempio n. 3
0
        public static void Run(World world)
        {
            var healthType  = Caches.AddComponentTypedef <float>(world, "Health");
            var staminaType = Caches.AddComponentTypedef <float>(world, "Stamina");
            var manaType    = Caches.AddComponentTypedef <float>(world, "Mana");

            ECS_SYSTEM(world, Regenerate, SystemKind.OnUpdate, "?Health, ?Stamina, ?Mana");

            /* Create three entities that will all match with the Regenerate system */
            ecs.new_entity(world, healthType);
            ecs.new_entity(world, staminaType);
            ecs.new_entity(world, manaType);

            /* Run systems */
            ecs.progress(world, 0);
        }
Esempio n. 4
0
        public static void Run(World world)
        {
            var fooType = Caches.AddComponentTypedef <int>(world, "Foo");
            var barType = Caches.AddComponentTypedef <int>(world, "Bar");

            var systemEntity = ECS_SYSTEM(world, GetChildrenSystem, SystemKind.Manual, "EcsId");

            /* Create two parents */
            var parent_1 = ecs.new_entity(world);
            var parent_2 = ecs.new_entity(world);

            /* Get type variables for parents so they can be used as filter */
            var parent_1Type = ecs.type_from_entity(world, parent_1);
            var parent_2Type = ecs.type_from_entity(world, parent_2);

            /* Create two children for each parent */
            var child_1_1 = ecs.new_child(world, parent_1, fooType);
            var child_1_2 = ecs.new_child(world, parent_1, barType);

            var child_2_1 = ecs.new_child(world, parent_2, fooType);
            var child_2_2 = ecs.new_child(world, parent_2, barType);

            /* Set ids so it's easier to see which children were resolved */
            ecs.set_id(world, child_1_1, "child_1_1");
            ecs.set_id(world, child_1_2, "child_1_2");
            ecs.set_id(world, child_2_1, "child_2_1");
            ecs.set_id(world, child_2_2, "child_2_2");

            /* Collect children for parent_1 */
            ecs.run_w_filter(world, systemEntity, 0, 0, 0, parent_1Type, IntPtr.Zero);

            PrintChildren(world, "parent_1");
            Console.WriteLine("---\n");

            EntityList.Clear();

            /* Collect children for parent_2 */
            ecs.run_w_filter(world, systemEntity, 0, 0, 0, parent_2Type, IntPtr.Zero);

            PrintChildren(world, "parent_2");

            /* Cleanup */
            EntityList.Clear();
        }
Esempio n. 5
0
        public static void Run(World world)
        {
            /* Register components */
            var massType = Caches.AddComponentTypedef <float>(world, "Mass");

            /* Create base entity. Create entity as disabled, which will prevent it from
             * being matched with systems. This is a common approach to creating
             * entities that are only used as templates for other entities, or in this
             * case, for providing initial component values. */
            var baseEntity = ecs.new_entity(world, ecs.TEcsDisabled);

            ecs.set_typedef(world, baseEntity, massType, 10f);

            /* Create instances which share the Mass component from a base */
            var instance = ecs.new_instance(world, baseEntity);

            /* Add component without setting it. This will initialize the new component
             * with the value from the base, which is a common approach to initializing
             * components with a value when they are added. */
            ecs.add(world, instance, massType);

            /* Print value of mass. The value will be equal to base, and the instance will own the component. */
            Console.WriteLine("instance: {0} (owned = {1})", ecs.ecs_get <float>(world, instance, massType), ecs.has_owned(world, instance, massType));
        }
Esempio n. 6
0
        public void SystemOnFrame_1_type_3_component_typedefs()
        {
            var positionTypeId = ECS_COMPONENT <Position>(world);
            var velTypeId      = Caches.AddComponentTypedef <Position>(world, "CustomVelocity");
            var massTypeId     = Caches.AddComponentTypedef <Mass>(world, "CustomMass");

            var systemEntityId = ECS_SYSTEM(world, Iter, SystemKind.OnUpdate, "Position, CustomVelocity, CustomMass");

            var ctx = Heap.Alloc <SysTestData>();

            ecs.set_context(world, (IntPtr)ctx);

            var e_1 = ECS_ENTITY(world, "e_1", "Position, CustomVelocity, CustomMass");
            var e_2 = ECS_ENTITY(world, "e_2", "Position, CustomVelocity, CustomMass");
            var e_3 = ECS_ENTITY(world, "e_3", "Position, CustomVelocity, CustomMass");

            // when setting a typedef'd component, we need to use the set_typedef method so that the type is not bound to the actual
            // struct we are sending.
            ecs.set_typedef(world, e_1, velTypeId, new Position {
                x = 10, y = 10
            });

            ecs.progress(world, 1);

            Assert.IsTrue(ctx->count == 3);
            Assert.IsTrue(ctx->invoked == 1);
            Assert.IsTrue(ctx->system.Value == systemEntityId.Value);
            Assert.IsTrue(ctx->column_count == 3);

            Assert.IsTrue(ctx->e[0] == e_1.Value);
            Assert.IsTrue(ctx->e[1] == e_2.Value);
            Assert.IsTrue(ctx->e[2] == e_3.Value);

            Assert.IsTrue(ctx->GetC(0, 0) == ecs.type_to_entity(world, positionTypeId).Value);
            Assert.IsTrue(ctx->GetS(0, 0) == 0);
            Assert.IsTrue(ctx->GetC(0, 1) == ecs.type_to_entity(world, velTypeId).Value);
            Assert.IsTrue(ctx->GetS(0, 1) == 0);
            Assert.IsTrue(ctx->GetC(0, 2) == ecs.type_to_entity(world, massTypeId).Value);
            Assert.IsTrue(ctx->GetS(0, 2) == 0);

            var p = (Position *)ecs.get_ptr(world, e_1, positionTypeId);

            Assert.IsTrue(p->x == 10);
            Assert.IsTrue(p->y == 20);

            p = (Position *)ecs.get_ptr(world, e_2, positionTypeId);
            Assert.IsTrue(p->x == 10);
            Assert.IsTrue(p->y == 20);

            p = (Position *)ecs.get_ptr(world, e_3, positionTypeId);
            Assert.IsTrue(p->x == 10);
            Assert.IsTrue(p->y == 20);

            var v = (Position *)ecs.get_ptr(world, e_1, velTypeId);

            Assert.IsTrue(v->x == 30);
            Assert.IsTrue(v->y == 40);

            v = (Position *)ecs.get_ptr(world, e_2, velTypeId);
            Assert.IsTrue(v->x == 30);
            Assert.IsTrue(v->y == 40);

            v = (Position *)ecs.get_ptr(world, e_3, velTypeId);
            Assert.IsTrue(v->x == 30);
            Assert.IsTrue(v->y == 40);
        }
Esempio n. 7
0
        public static void Run(World world)
        {
            /* Define components */
            ECS_COMPONENT <Position>(world);
            var forceType = Caches.AddComponentTypedef <Position>(world, "Force");
            var massType  = Caches.AddComponentTypedef <int>(world, "Mass");

            /* Define a system called Move that is executed every frame, and subscribes
             * for the 'Position', 'Force' and 'Mass' components. The Mass component
             * will be either shared or owned. */
            ECS_SYSTEM(world, Move, SystemKind.OnUpdate, "Position, Force, Mass");

            /* Create two base entities */
            var heavyEntity = ECS_ENTITY(world, "HeavyEntity", "Mass");

            ecs.set_typedef(world, heavyEntity, massType, 100);

            var lightEntity = ECS_ENTITY(world, "LightEntity", "Mass");

            ecs.set_typedef(world, lightEntity, massType, 10);

            /* Create regular entity with Position, Force and Mass */
            var instance0 = ECS_ENTITY(world, "Instance0", "Position, Force, Mass");

            ecs.set(world, instance0, new Position {
                X = 0, Y = 0
            });
            ecs.set_typedef(world, instance0, forceType, new Position {
                X = 10, Y = 10
            });
            ecs.set_typedef(world, instance0, massType, 2);

            /* Create instances which share the Mass component from a base */
            var instance1 = ECS_ENTITY(world, "Instance1", "Position, Force, INSTANCEOF | LightEntity");

            ecs.set(world, instance1, new Position {
                X = 0, Y = 0
            });
            ecs.set_typedef(world, instance1, forceType, new Position {
                X = 10, Y = 10
            });

            var instance2 = ECS_ENTITY(world, "Instance2", "Position, Force, INSTANCEOF | LightEntity");

            ecs.set(world, instance2, new Position {
                X = 0, Y = 0
            });
            ecs.set_typedef(world, instance2, forceType, new Position {
                X = 10, Y = 10
            });

            var instance3 = ECS_ENTITY(world, "Instance3", "Position, Force, INSTANCEOF | HeavyEntity");

            ecs.set(world, instance3, new Position {
                X = 0, Y = 0
            });
            ecs.set_typedef(world, instance3, forceType, new Position {
                X = 10, Y = 10
            });

            var instance4 = ECS_ENTITY(world, "Instance4", "Position, Force, INSTANCEOF | HeavyEntity");

            ecs.set(world, instance4, new Position {
                X = 0, Y = 0
            });
            ecs.set_typedef(world, instance4, forceType, new Position {
                X = 10, Y = 10
            });

            /* Run systems */
            ecs.progress(world, 0);
            ecs.progress(world, 0);
            ecs.progress(world, 0);
        }