Exemple #1
0
    protected override void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }

        var mouseState = Mouse.GetState();

        var playerPosition = new Point(mouseState.X, mouseState.Y);
        var playerRadius   = 20;

        if (mouseState.LeftButton == ButtonState.Pressed)
        {
            for (int i = 0; i < 32; i++)
            {
                var bullet = registry_.CreateEntity();
                registry_.AddComponent(bullet, new Position {
                    Point = new Point(playerPosition.X + (i - 16) * 8, playerPosition.Y)
                });
                registry_.AddComponent(bullet, new Velocity {
                    Point = new Point(0, -10)
                });
                registry_.AddComponent(bullet, new Sprite {
                    SourceRectangle = sprites_["laserBlue01"]
                });
            }
        }

        registry_.Loop((EntityData entIdx, ref Position position, ref Velocity velocity) =>
        {
            position.Point += velocity.Point;
            if (position.Point.Y < -100)
            {
                var entUid = registry_.EntityUIDFromIdx(entIdx);
                registry_.DeleteEntity(entUid);
            }
        });

        base.Update(gameTime);
    }
Exemple #2
0
    static void Benchmark(int entityCount, bool randomComponents)
    {
        Console.WriteLine($"Benchmarking {entityCount} entities, ARCHETYPES, random insertion order: {randomComponents}");

        Registry registry = new Registry();

        registry.RegisterComponent <Int1>();
        registry.RegisterComponent <Int2>();
        registry.RegisterComponent <Int3>();
        registry.RegisterComponent <Int4>();
        registry.RegisterComponent <Int5>();
        registry.RegisterComponent <Int6>();

        List <ulong> ids = new List <ulong>();

        for (int i = 0; i < entityCount; i++)
        {
            ids.Add(registry.CreateEntity());
        }

        List <int> indices1 = new List <int>();
        List <int> indices2 = new List <int>();
        List <int> indices3 = new List <int>();
        List <int> indices4 = new List <int>();
        List <int> indices5 = new List <int>();
        List <int> indices6 = new List <int>();

        for (int i = 0; i < entityCount; i += (xorshift(1) + 1))
        {
            indices1.Add(i);
        }
        for (int i = 0; i < entityCount; i += (xorshift(2) + 1))
        {
            indices2.Add(i);
        }
        for (int i = 0; i < entityCount; i += (xorshift(3) + 1))
        {
            indices3.Add(i);
        }
        for (int i = 0; i < entityCount; i += (xorshift(4) + 1))
        {
            indices4.Add(i);
        }
        for (int i = 0; i < entityCount; i += (xorshift(5) + 1))
        {
            indices5.Add(i);
        }
        for (int i = 0; i < entityCount; i += (xorshift(6) + 1))
        {
            indices6.Add(i);
        }

        if (randomComponents)
        {
            xorshuffle(indices1);
            xorshuffle(indices2);
            xorshuffle(indices3);
            xorshuffle(indices4);
            xorshuffle(indices5);
            xorshuffle(indices6);
        }


        //lr($"added entities", registry);

        foreach (int i in indices1)
        {
            registry.AddComponent(ids[i], new Int1(xorshift()));
            //lr($"added comp Int1 to {ids[i]}", registry);
        }

        foreach (int i in indices2)
        {
            registry.AddComponent(ids[i], new Int2(i, xorshift()));
            //lr($"added comp Int2 to {ids[i]}", registry);
        }

        foreach (int i in indices3)
        {
            registry.AddComponent(ids[i], new Int3(i, i, xorshift()));
            //lr($"added comp Int3 to {ids[i]}", registry);
        }

        foreach (int i in indices4)
        {
            registry.AddComponent(ids[i], new Int4(i, i, i, xorshift()));
            //lr($"added comp Int4 to {ids[i]}", registry);
        }

        foreach (int i in indices5)
        {
            registry.AddComponent(ids[i], new Int5(i, i, i, i, xorshift()));
            //lr($"added comp Int5 to {ids[i]}", registry);
        }

        foreach (int i in indices6)
        {
            registry.AddComponent(ids[i], new Int6(i, i, i, i, i, xorshift()));
        }

        Measure();
        registry.Loop((EntityData index, ref Int1 int1, ref Int2 int2) => { int2.x = int1.x; });
        Measure("Propagated x to Int2");
        registry.Loop((EntityData index, ref Int2 int2, ref Int3 int3) => { int3.x = int2.x; });
        Measure("Propagated x to Int3");
        registry.Loop((EntityData index, ref Int3 int3, ref Int4 int4) => { int4.x = int3.x; });
        Measure("Propagated x to Int4");
        registry.Loop((EntityData index, ref Int4 int4, ref Int5 int5) => { int5.x = int4.x; });
        Measure("Propagated x to Int5");
        registry.Loop((EntityData index, ref Int5 int5, ref Int6 int6) => { int6.x = int5.x; });
        Measure("Propagated x to Int6");

        registry.Loop((EntityData index, ref Int2 int2, ref Int3 int3, ref Int4 int4) => { int3.y = int2.y; int4.y = int3.y; });
        Measure("Propagated y to Int3 and Int4");
        registry.Loop((EntityData index, ref Int3 int3, ref Int4 int4, ref Int5 int5) => { int4.y = int3.y; int5.y = int4.y; });
        Measure("Propagated y to Int4 and Int5");
        registry.Loop((EntityData index, ref Int4 int4, ref Int5 int5, ref Int6 int6) => { int5.y = int4.y; int6.y = int5.y; });
        Measure("Propagated y to Int5 and Int6");

        ulong checkSum = 0;

        registry.Loop((EntityData index, ref Int6 int6) =>
        {
            checkSum ^= (ulong)(int6.x + int6.y);
        });

        Console.WriteLine($"checksum: {checkSum}");
    }
Exemple #3
0
    static unsafe void Main(string[] args)
    {
        Benchmark(100000, false);
        state = 42;
        Benchmark(100000, false);
        Benchmark(100000, false);
        Benchmark(100000, true);
        Benchmark(100000, true);
        //Console.ReadKey();
        return;

        reg = new Registry();
        lr("created registry");

        reg.RegisterComponent <Position>();
        reg.RegisterComponent <Velocity>();
        reg.RegisterComponent <Name>();

        lr("registered components");

        var empty = reg.CreateEntity();

        lr("created empty entity");

        reg.AddComponent(empty, new Velocity(15, 15));

        lr("added velocity to empty");

        var pe = reg.CreateEntity(new Position(1, 2));

        lr("created 1 entity w position");

        reg.AddComponent(pe, new Velocity(1, 2));

        lr("added velocity to it");

        reg.CreateEntity(new Velocity(3, 4));
        reg.CreateEntity(new Position(5, 6));

        lr("created 2 entities with 1 comp");
//
//        var entity = reg.CreateEntity(new Position(1, 2), new Velocity(9, 8));
//        reg.AddComponent(entity, new Name());

        var e1 = reg.CreateEntity(new Position(7, 8), new Velocity(7, 8));
        var e2 = reg.CreateEntity(new Position(9, 1099), new Velocity(9, 10));

        lr("created 2 entities w 2 comps");

        var e3 = reg.CreateEntity(new Velocity(33, 33));

        lr("created new with velocity");

        reg.AddComponent(e3, new Position(33, 33));

        lr("added position to new");

        reg.DestroyEntity(e1);

        lr("destroyed 1st entity created last step");

        reg.DestroyEntity(e2);

        lr("destroyed 2nd ent created last step");

//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 82));
//        reg.CreateEntity(new Position(12, 22), new Velocity(92, 1182));


        Console.ReadKey();
    }