### Manage minECS Worlds and Entities Source: https://github.com/yagestudios/minecs/blob/main/README.md The `World` object is central to minECS, managing all entities and their components. This example demonstrates creating a world, adding entities, attaching components with initial values, and stepping the world to process systems. ```typescript import { createWorld, addEntity, addComponent, stepWorld } from "@min-ecs/core"; import { Position, Velocity } from "./components"; const world = createWorld(); const entity = addEntity(world); addComponent(world, Position, entity); addComponent(world, Velocity, entity, { x: 30, y: 30, }); console.log({ ...world(Position, entity) }); // Logs initial Position: { x: 0, y: 0, type: "Position" } stepWorld(world); // Executes systems like MovementSystem console.log({ ...world(Position, entity) }); // Logs updated Position: { x: 30, y: 30, type: "Position" } ``` -------------------------------- ### Implement minECS System Initialization Source: https://github.com/yagestudios/minecs/blob/main/README.md Systems can define an `init` function that executes once when a new entity matching the system's criteria is added to the world. This is useful for setting up initial component states. ```typescript import { SystemImpl, System, World } from "@min-ecs/core"; import { Position } from "./components"; @System(Position) class PositionInitializationSystem extends SystemImpl { init = (world: World, eid: number) => { const position = world(Position, eid); // Initialize position if not already set or based on other factors if (position.x === 0 && position.y === 0) { console.log(`Initializing position for entity ${eid}`); position.x = Math.random() * 100; position.y = Math.random() * 100; } }; run = (world: World, eid: number) => { /* ... */ }; } ``` -------------------------------- ### Manually Run minECS Systems Source: https://github.com/yagestudios/minecs/blob/main/README.md Systems marked with `depth = -1` or any system can be executed manually. Use `system.runAll(world)` to run for all entities or `system.run(world, entity)` for a specific entity. ```typescript import { createWorld, getSystem } from "@min-ecs/core"; import { ManualOnlySystem } from "./systems"; // Assuming system is in a separate file const world = createWorld(); const entity = addEntity(world); // ... add components to entity ... // Get an instance of the system const manualSystem = getSystem(world, ManualOnlySystem); // Run manually for all entities in the world manualSystem.runAll(world); // Run manually for a specific entity manualSystem.run(world, entity); ``` -------------------------------- ### Implement minECS System Cleanup Source: https://github.com/yagestudios/minecs/blob/main/README.md Systems can define a `cleanup` function that is called when a component is removed or an entity is deleted. This ensures proper state management and resource deallocation. ```typescript import { SystemImpl, System, World } from "@min-ecs/core"; import { Velocity } from "./components"; @System(Velocity) class VelocityCleanupSystem extends SystemImpl { cleanup = (world: World, eid: number) => { const velocity = world(Velocity, eid); console.log(`Cleaning up velocity for entity ${eid} with values x:${velocity.x}, y:${velocity.y}`); // Perform any necessary cleanup actions for the velocity component }; run = (world: World, eid: number) => { /* ... */ }; } ``` -------------------------------- ### Define minECS Components with Schema and Decorators Source: https://github.com/yagestudios/minecs/blob/main/README.md Learn how to define components in minECS using the `Schema` class and the `@Component` decorator. Components are automatically registered and provide type safety for your game's data structures. ```typescript import { Schema, Component, type, defaultValue } from "@min-ecs/core"; @Component() class Position extends Schema { @type("number") @defaultValue(0) x: number; @type("number") @defaultValue(0) y: number; } @Component() class Velocity extends Schema { @type("number") @defaultValue(0) x: number; @type("number") @defaultValue(0) y: number; } ``` -------------------------------- ### Manage Entities and Components Dynamically Source: https://github.com/yagestudios/minecs/blob/main/README.md Provides essential functions for managing the lifecycle of entities and their associated components within the Minecs world. This includes creating the world, adding/removing entities, and attaching/detaching components. ```typescript const world = createWorld(); const entity = addEntity(world); addComponent(world, SomeComponent, entity); removeComponent(world, SomeComponent, entity); removeEntity(world, entity); // Cleans up all components associated with the entity ``` -------------------------------- ### Define minECS Systems with @System Decorator Source: https://github.com/yagestudios/minecs/blob/main/README.md Systems in minECS process entities based on their components. Use the `@System` decorator to declare component dependencies, enabling systems to operate efficiently within the engine or manually. ```typescript import { SystemImpl, System, World } from "@min-ecs/core"; import { Position, Velocity } from "./components"; // Assuming components are in a separate file @System(Position, Velocity) class MovementSystem extends SystemImpl { run = (world: World, entity: number) => { const position = world(Position, entity); const velocity = world(Velocity, entity); position.x += velocity.x; position.y += velocity.y; }; } ``` -------------------------------- ### Serialize and Deserialize minECS Worlds Source: https://github.com/yagestudios/minecs/blob/main/README.md minECS provides robust serialization capabilities, allowing you to save and load game states using JSON, Binary, or base64 formats. This ensures persistence and easy state transfer. ```typescript import { serializeWorld, deserializeWorld, SerialMode, createWorld } from "@min-ecs/core"; const world = createWorld(); // ... add entities and components to world ... // Serialize const json = serializeWorld(SerialMode.JSON, world); const buffer = serializeWorld(SerialMode.BINARY, world); const base64 = serializeWorld(SerialMode.BASE64, world); // Deserialize const newWorldFromJSON = deserializeWorld(json); const newWorldFromBinary = deserializeWorld(buffer); const newWorldFromBase64 = deserializeWorld(base64); ``` -------------------------------- ### Define Complex Components with Decorators Source: https://github.com/yagestudios/minecs/blob/main/README.md Demonstrates how to define complex component structures using decorators like `@Component`, `@type`, `@defaultValue`, and `@nullable`. This allows for nested objects, arrays, primitive types, and nullable fields, enhancing component flexibility. ```typescript @Component() class Complex extends Schema { @type(Position) position: Position; @type("string") @defaultValue("hello") message: string; @type(["number"]) numbers: number[]; @type("boolean") @defaultValue(true) flag: boolean; @type("object") obj: any; @type("string") @nullable() nullableString: string | null; } @Component() class NestedComplex extends Schema { @type(Complex) complex: Complex; @type([Complex]) complexes: Complex[]; } ``` -------------------------------- ### Control minECS System Execution Order with Depth Source: https://github.com/yagestudios/minecs/blob/main/README.md The `depth` static property on a system controls its execution order relative to other systems. Lower depth values execute earlier. Systems with `depth < 0` are not run automatically. ```typescript import { SystemImpl, System } from "@min-ecs/core"; @System(...) class EarlySystem extends SystemImpl { static depth = 0; // Runs early run = (world: World, eid: number) => { /* ... */ }; } @System(...) class LateSystem extends SystemImpl { static depth = 10; // Runs later run = (world: World, eid: number) => { /* ... */ }; } @System(...) class ManualOnlySystem extends SystemImpl { static depth = -1; // Does not run automatically run = (world: World, eid: number) => { /* ... */ }; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.