guerro323 / GameHost

MIT License
0 stars 0 forks source link

Integrating flecs ecs for Simulation application #21

Open guerro323 opened 4 years ago

guerro323 commented 4 years ago

Adding flecs (https://github.com/SanderMertens/flecs) to the Simulation application would be a good addition. I don't want to use the current DefaultEcs package for simulation since the architecture isn't suited for my games (lots of entity update, serialization, need to have continuous data, ...). DefaultEcs is still used for other parts of the application tho (since it does support managed data well and its architecture is perfect for the software/framework part)

flecs is extremely performant, but there need to be new bindings based on the V2 (I'm currently working on it, which should also come under a nugget package)

guerro323 commented 4 years ago

i made it run, but i need to experiment a bit more before officialy integrating it in GameHost

guerro323 commented 4 years ago

did not crash, and work perfectly with this small code sample, so i guess it's i n t e g r a t i o n t i m e

var world = flecs.ecs_init();

var components = new Dictionary<Type, SWIGTYPE_p_ecs_vector_t>();

SWIGTYPE_p_ecs_vector_t ECS_COMPONENT<T>()
{
    if (!components.TryGetValue(typeof(T), out var typeId))
    {
        var entityId = flecs.ecs_new_component(world, typeof(T).Name, (uint) Unsafe.SizeOf<T>());
        typeId = flecs.ecs_type_from_entity(world, entityId);
    }

    return typeId;
}

ulong Set<T>(ulong entity, T value)
{
    return flecs._ecs_set_ptr(world, entity, flecs.ecs_type_to_entity(world, ECS_COMPONENT<T>()), (uint) Unsafe.SizeOf<T>(), Unsafe.AsPointer(ref value));
}

T Get<T>(ulong entity)
{
    return Unsafe.AsRef<T>(flecs._ecs_get_ptr(world, entity, ECS_COMPONENT<T>()));
}

var e1 = flecs._ecs_new(world, null);
var e2 = flecs._ecs_new(world, null);

Console.WriteLine($"{e1} {e2}");

Set(e1, new Vector3(1, 2, 3));
Console.WriteLine(Get<Vector3>(e1));
guerro323 commented 4 years ago

This issue will be closed once it will be fully integrated to GameHost

guerro323 commented 4 years ago

finally, i don't know if it will be integrated, most of the bindings generator are kinda broken with flecs, and I don't want to spend time making the bindings by myself (way too much try-&-fail when implement bindings, not worth it)

so instead i'm making an ECS that somewhat look like flecs, but not really (table based instead of traditional chunk based)