Custom World Loader

Altruist's built-in world loader reads the standard JSON schema (see Worlds). If your world data uses a different format, implement IWorldLoader3D.

Implement IWorldLoader3D

[Service(typeof(IWorldLoader3D))]
[ConditionalOnConfig("altruist:game")]
public class MyWorldLoader : IWorldLoader3D
{
    private readonly IPhysxWorldEngineFactory3D? _engineFactory;
    private readonly IWorldPartitioner3D _partitioner;
    private readonly IPhysxBodyApiProvider3D? _bodyApi;
    private readonly IPhysxColliderApiProvider3D? _colliderApi;
    private readonly List<IWorldObject3D> _spawned = new();

    public IReadOnlyList<IWorldObject3D> SpawnedWorldObjects => _spawned;

    public MyWorldLoader(
        IWorldPartitioner3D partitioner,
        IPhysxWorldEngineFactory3D? engineFactory = null,
        IPhysxBodyApiProvider3D? bodyApi = null,
        IPhysxColliderApiProvider3D? colliderApi = null)
    {
        _partitioner = partitioner;
        _engineFactory = engineFactory;
        _bodyApi = bodyApi;
        _colliderApi = colliderApi;
    }

    public async Task<IGameWorldManager3D> LoadFromJson(IWorldIndex3D index, string json)
    {
        // Parse your custom format
        var myData = MyParser.Parse(json);

        // Create physics world (optional)
        PhysxWorld3D? physx = null;
        if (_engineFactory != null)
        {
            var engine = _engineFactory.GetExistingOrCreate(index.Gravity, index.FixedDeltaTime);
            physx = new PhysxWorld3D(engine);
        }

        // Create world manager
        var manager = new GameWorldManager3D(index, physx, _partitioner, _bodyApi, _colliderApi);

        // Spawn objects from your data
        foreach (var obj in myData.Objects)
        {
            var worldObj = new AnonymousWorldObject3D(
                Transform3D.From(obj.Position, obj.Rotation, obj.Scale),
                archetype: obj.Type);

            await manager.SpawnStaticObject(worldObj);
            _spawned.Add(worldObj);
        }

        return manager;
    }

    public async Task<IGameWorldManager3D> LoadFromIndex(IWorldIndex3D index)
    {
        if (string.IsNullOrWhiteSpace(index.DataPath))
            return await LoadFromJson(index, "{}"); // Empty world

        var json = File.ReadAllText(index.DataPath);
        return await LoadFromJson(index, json);
    }
}

The [Service(typeof(IWorldLoader3D))] attribute replaces the built-in loader automatically.

Note:

See Worlds for the built-in JSON schema and archetype mapping system.