@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
74 lines (66 loc) • 2.13 kB
JavaScript
/**
* Marks an entity as replicated and stores its peer-shared `network_id`.
*
* Replication is opt-in: only entities with this component are visible to
* the network layer. Add it before adding any other replicated component;
* remove it to stop replication (the entity stays alive locally).
*
* `network_id` is assigned automatically by {@link NetworkSystem.link} when
* the component is added to an entity. If the value is non-negative at link
* time, the system uses it as an explicit hint (typical when a packet from a
* remote peer carries a `network_id` that must map to a fresh local entity).
*
* `owner_peer_id` is the peer that has authority over this entity. Default
* value `-1` means "the local peer" (server-spawned entities on the server,
* or unowned). Game logic uses this to decide whether to trust incoming
* mutations or to predict locally.
*
* @author Alex Goldring
* @copyright Company Named Limited (c) 2025
*/
export class NetworkIdentity {
/**
* Peer-shared identifier. Negative until {@link NetworkSystem.link} runs.
* @type {number}
*/
network_id = -1;
/**
* Peer that authoritatively owns this entity, or -1 for "local / server-owned".
* @type {number}
*/
owner_peer_id = -1;
/**
* Reserved for game-defined replication policy bits (priority class,
* always-relevant flag, interpolation profile, etc.). Layout is up to the game.
* @type {number}
*/
replication_flags = 0;
/**
*
* @param {NetworkIdentity} other
* @returns {boolean}
*/
equals(other) {
return this.network_id === other.network_id
&& this.owner_peer_id === other.owner_peer_id
&& this.replication_flags === other.replication_flags
;
}
/**
*
* @returns {number}
*/
hash() {
return this.network_id ^ this.owner_peer_id ^ this.replication_flags;
}
}
/**
* @readonly
* @type {string}
*/
NetworkIdentity.typeName = "NetworkIdentity";
/**
* @readonly
* @type {boolean}
*/
NetworkIdentity.prototype.isNetworkIdentity = true;