Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.9 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    components.ts
 * @brief   The replication marker components. `Replicated` is the authoring
 *          surface: put it on a server entity and the entity replicates —
 *          which fields flow is declared per component (`replicated`
 *          annotations / metadata), not here. `NetGhost` tags the client-side
 *          proxies so local simulation (physics, AI) can yield to the incoming
 *          authoritative state via Without(NetGhost) queries.
 */
import { defineComponent, defineTag } from '../../ecs/component';

export interface ReplicatedData {
    /** Stable network identity, allocated by the server (0 = unassigned). */
    netId: number;
    /** Owning connection id (0 = the server) — input/authority routing (N4). */
    owner: number;
    /**
     * Which {@link registerReplicationArchetype} builder gives this entity's
     * ghosts their shape. Empty means a bare ghost carrying only the replication
     * baseline. Never replicated as a FIELD — it rides the spawn as protocol
     * metadata, like `netId` and `owner`.
     */
    archetype: string;
}

export const Replicated = defineComponent<ReplicatedData>('Replicated', {
    netId: 0,
    owner: 0,
    archetype: '',
});

/** Client-side proxy of a server entity: state arrives over the wire. */
export const NetGhost = defineTag('NetGhost');

/**
 * Re-register after a user-component registry reset (project reload wipes the
 * per-context catalogue; the interned-by-name _id keeps old const handles and
 * fresh registrations pointing at the same storage). ReplicationPlugin.build
 * calls this, so any app built after a reset still serializes these.
 */
export function ensureReplicationComponentsRegistered(): void {
    defineComponent<ReplicatedData>('Replicated', { netId: 0, owner: 0, archetype: '' });
    defineTag('NetGhost');
}