We need a way of creating new entities that come in from the server. Right now we need to manually check the entityName to then manually create it and add it to the world: https://github.com/MavonEngine/Core/blob/dd1f339b0a86b7758774c8981680f248374ef02a/packages/multiplayer-template/client/src/GameSyncManager.ts#L79
We can reduce this by having the entities register themselves:
type EntityFactory = (id: string, data: Record<string, unknown>) => NetworkedActor
const registry = new Map<string, EntityFactory>()
export default {
register(typeName: string, factory: EntityFactory) {
registry.set(typeName, factory)
},
create(typeName: string, id: string, data: Record<string, unknown>): NetworkedActor | null {
const factory = registry.get(typeName)
return factory ? factory(id, data) : null
},
}
That way each entity just calls register($typeName, (id, data) => new Self()
https://github.com/MavonEngine/Core/pull/13