@hiddentao/clockwork-engine
Version:
A TypeScript/PIXI.js game engine for deterministic, replayable games with built-in rendering
184 lines • 7.37 kB
TypeScript
import type { Color, RenderingLayer } from "../platform";
import { DisplayNode } from "../platform/DisplayNode";
import { GameState } from "../types";
/**
* Abstract base implementation for game object renderers.
* Provides common functionality for managing visual representations of game entities
* including creation, updating, removal, and lifecycle management.
*
* Platform-agnostic: works with any RenderingLayer implementation.
*
* Child classes must implement create() to define how their specific items are rendered
* and getId() to provide unique identification for tracking.
*
* @template T The type of game object being rendered
*/
export declare abstract class AbstractRenderer<T> {
protected gameNode: DisplayNode;
protected rendering: RenderingLayer;
protected itemNodes: Map<string, DisplayNode>;
protected items: Map<string, T>;
protected gameState: GameState;
constructor(gameNode: DisplayNode);
/**
* Creates the visual representation for a game object.
* Must return a DisplayNode that will be added to the display tree.
*
* @param item The game object to create visuals for
* @returns A DisplayNode containing the item's visual elements
*/
protected abstract create(item: T): DisplayNode;
/**
* Updates the game state for this renderer.
* Allows renderers to react to game state changes (e.g., pause animations, change visual appearance).
*
* @param state The new game state
*/
updateGameState(state: GameState): void;
/**
* Extracts a unique identifier from a game object.
* Used for tracking and preventing duplicate renders.
*
* @param item The game object to identify
* @returns Unique string identifier for the item
*/
abstract getId(item: T): string;
/**
* Adds a new item to the renderer, creating its visual representation.
* If the item already exists, delegates to update() instead of creating a duplicate.
*
* @param item The game object to render
*/
add(item: T): void;
/**
* Updates an existing item's visual representation.
* Modifies the node properties without recreating the entire display object.
* If the item doesn't exist, it will be created.
*
* @param item The game object with updated state
*/
update(item: T): void;
/**
* Removes an item from the renderer by its unique identifier.
* Destroys the node and cleans up all references.
*
* @param id Unique identifier of the item to remove
*/
remove(id: string): void;
/**
* Replaces all rendered items with a new set in a single operation.
* Efficiently handles additions, updates, and removals by comparing
* the new item list against currently rendered items.
*
* @param items Array of all items that should be currently rendered
*/
setItems(items: T[]): void;
/**
* Calculates transparency for objects based on their health status.
* Objects become progressively transparent as health decreases below the threshold.
*
* @param currentHealth Current health of the object
* @param maxHealth Maximum health of the object
* @param threshold Health threshold below which transparency begins (defaults to half max health)
* @returns Alpha value for visual transparency
*/
protected calculateHealthBasedAlpha(currentHealth: number, maxHealth: number, threshold?: number): number;
/**
* Creates a filled rectangle as a DisplayNode.
*
* @param width Width of the rectangle
* @param height Height of the rectangle
* @param color Fill color
* @param x X position (defaults to center horizontally)
* @param y Y position (defaults to center vertically)
* @returns A new DisplayNode with the rectangle drawn
*/
protected createRectangle(width: number, height: number, color: Color, x?: number, y?: number): DisplayNode;
/**
* Creates a filled circle as a DisplayNode.
*
* @param radius Radius of the circle
* @param color Fill color
* @param x X position (defaults to origin)
* @param y Y position (defaults to origin)
* @returns A new DisplayNode with the circle drawn
*/
protected createCircle(radius: number, color: Color, x?: number, y?: number): DisplayNode;
/**
* Creates a filled polygon as a DisplayNode.
*
* @param points Array of coordinates in sequence
* @param color Fill color
* @returns A new DisplayNode with the polygon drawn
*/
protected createPolygon(points: number[], color: Color): DisplayNode;
/**
* Creates a rectangular border outline without fill.
*
* @param width Width of the rectangle
* @param height Height of the rectangle
* @param strokeWidth Width of the border line
* @param color Border color
* @param alpha Transparency of the border (defaults to opaque)
* @returns A new DisplayNode with the border rectangle drawn
*/
protected createBorderRectangle(width: number, height: number, strokeWidth: number, color: Color, alpha?: number): DisplayNode;
/**
* Creates a positioned node with a body graphic as its child.
*
* @param x X position
* @param y Y position
* @param rotation Rotation in radians (optional)
* @param bodyNode The node to use as the body
* @returns A DisplayNode with the body node as a child
*/
protected createStandardNode(x: number, y: number, rotation: number | undefined, bodyNode: DisplayNode): DisplayNode;
/**
* Updates a node's visual properties based on the item's current state.
* Checks the needsRepaint flag and only calls repaintNode if needed.
*
* @param node The DisplayNode to update
* @param item The game object with current state
*/
protected updateNode(node: DisplayNode, item: T): void;
/**
* Repaints a node's visual properties based on the item's current state.
* Default implementation performs no updates. Child classes should override
* this method to implement specific repaint behavior.
*
* @param _node The DisplayNode to repaint
* @param _item The game object with current state
*/
protected repaintNode(_node: DisplayNode, _item: T): void;
/**
* Completely clears the renderer and cleans up all resources.
*/
clear(): void;
/**
* Retrieves the DisplayNode for a specific item.
*
* @param id Unique identifier of the item
* @returns The DisplayNode or undefined if not found
*/
getNode(id: string): DisplayNode | undefined;
/**
* Retrieves a specific game object by its identifier.
*
* @param id Unique identifier of the item
* @returns The game object or undefined if not found
*/
getItem(id: string): T | undefined;
/**
* Returns a map of all currently tracked game objects.
*
* @returns Map containing all items keyed by their identifiers
*/
getAllItems(): Map<string, T>;
/**
* Forces a complete visual refresh of all currently rendered items.
* Calls the update logic for every tracked item without changing
* the set of rendered items.
*/
rerender(): void;
}
//# sourceMappingURL=AbstractRenderer.d.ts.map