Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Correspondent

A message producer and consumer for diff/delta-based networking of Entities. How it works: Comparing the local game state with a cached representation of the game state, it produces a diff. The diff contains two kinds of operations:

upsert

Either creates an entity or adds a component to an existing entity, or both. It can also update a component's data. In the following example

{
  upsert: {
    megaMan: {
      velocity: { value: 6 }
    }
  }
}

Would be the resulting diff if Mega Man's velocity is 6 while the cache says it's some other integer. When another client is applying this diff to its game state, it would set Mega Man's velocity to 6, adding the velocity component if necessary, and also creating the Mega Man entity if necessary.

When comparing components, it first passes each component through an associated identity function, if available, otherwise it grabs component.value. Either way, it assumes the resulting values should be compared by reference (=== in JavaScript.) This is handy for when a by reference comparison would be misleading. For example, if the velocity component were an array:

{
  upsert: {
    megaMan: {
      velocity: [3, 7]
    }
  }
}

Then two velocity components would never appear equal even if they contain the same value. An identity function that concatinates the numbers in to a JS string would solve this, since JS strings are equal by reference if and only if they contain the same value. remove

Either removes a component from an entity or removes an entire entity. Example:

{
  remove: {
    megaMan: {
      velocity: true
    },
    drWily: true
  }
}

Would remove the velocity component from Mega Man, and remove the Dr. Wily component altogether.

In addition to producing and consuming diffs, it provides a static function that will update the cache given a diff. The cache tells it what the world looked like last time so it knows what's changed when producing a diff. The cache is its memory.

It has a reference to the entire game state (the world, in ECS parlance) but it doesn't concern itself with every component of every entity. That could be too expensive in terms of CPU and network usage. Instead the system that's using it must tell it what entities to care about. It also must be told what types of components to care about. We supply it with a string identifier for each entity and each component type which it uses when producing and consuming diffs.

Hierarchy

  • Correspondent

Index

Constructors

constructor

Properties

_componentMap

_componentMap: Map<string, ComponentConstructor<Component<any>>> = ...

_componentOptsMap

_componentOptsMap: Map<string, Partial<IComponentOptsFull<any>>> = ...

_defaultComponentOpts

_defaultComponentOpts: IComponentOptsFull<any> = ...

_entityMap

_entityMap: IEntityStore

_isMineMap

_isMineMap: Map<string, boolean> = ...

_options

_options: IGlobalOpts

_world

_world: World

diff

Methods

_getRemoves

_getUpserts

consumeDiff

  • Applies diff to the allow-listed components of registered entities mentioned therein. Additionally, it will create entities mentioned in message and add their components if necessary. Conversely, it will remove entities that are ommitted by message.

    Parameters

    Returns Correspondent

createEntity

  • createEntity(id: string, isMine?: boolean): Entity

getComponentById

getComponentIterator

getComponentOpt

  • getComponentOpt<K>(componentId: string, optName: K): IComponentOptsFull<any>[K]
  • Type parameters

    • K: "write" | "read" | "writeCache" | "allow"

    Parameters

    • componentId: string
    • optName: K

    Returns IComponentOptsFull<any>[K]

getEntityById

  • getEntityById(id: string): null | Entity

isMine

  • isMine(entity: Entity): boolean

produceDiff

registerComponent

  • Add component of the given type to the allow list, meaning the data of any instances of registered entities will be sent and recieved over the socket. TODO should allowed components only be passed in constructor?

    Type parameters

    • TData

    Parameters

    Returns Correspondent

registerEntity

  • registerEntity(id: string, entity: Entity, isMine?: boolean): void

removeComponentById

  • removeComponentById(entityId: string, componentId: string): void

removeEntityById

  • removeEntityById(id: string): void

unregisterEntity

  • unregisterEntity(id: string): void

updateCache

Static createEmptyBox

Static createEmptyDiff

Static createEntityStore

  • createEntityStore(): IEntityStore

Static getUpsert

Static isEmptyDiff

Static setUpsert

Generated using TypeDoc