start method

  1. @override
Future<void> start(
  1. ServiceContext context
)
override

Starts the service, once: while the engine initializes (after its world exists, before initialize() completes) in the order the services were declared, or at once when added to an initialized engine. Throwing fails initialize() with an EngineInitException whose stage is service:<Type>.

Implementation

@override
Future<void> start(ServiceContext context) async {
  final physics =
      _given ?? PhysicsEngineFactory.create3D(workerThreads: workerThreads);
  final reason = physics.unavailableReason;
  if (reason != null) throw Physics3DUnsupportedError(reason);
  final game = context.engine;
  final engineContext = context.context;
  final system = PhysicsSystem3D.lazy(() {
    final layers = engineContext.physicsLayers;
    return physics.createWorld(
      PhysicsWorldSettings3D(
        gravity: gravity,
        layerMasks: [for (var i = 0; i < 16; i++) layers.maskOf(i)],
        maxBodies: maxBodies,
        reportContactStay: reportContactStay,
        reportActivation: reportActivation,
      ),
    );
  }, geometry: AssetColliderGeometry3D(game.assets));
  _game = game;
  _engine = physics;
  _system = system;
  _queries = Physics3DQueries(system);
  if (addSystem) game.world.addSystem(system);
}