HyCodeYourTale

ECS Architecture

ECS Architecture

Dokumentace k Entity Component System architektuře v Hytale.

Soubory

  • COMPONENTS.md - Komponenty a jejich registrace

  • SYSTEMS.md - Systémy a event handling
  • Co je ECS?

    Entity    = ID (kontejner)
    Component = DATA (co entita má)
    System = LOGIKA (co entita dělá)

    Tradiční OOP vs ECS

    // Tradiční OOP
    class Player extends Entity {
    int health;
    Vector3 position;

    void move() { / logika / }
    void takeDamage() { / logika / }
    }

    // ECS
    Entity player = createEntity();
    addComponent(player, HealthComponent);
    addComponent(player, TransformComponent);

    // Systémy obsahují logiku
    MovementSystem.update();
    DamageSystem.update();

    Rychlý Přehled

    Registrace Komponenty

    private ComponentType myType;

    @Override
    protected void setup() {
    myType = getEntityStoreRegistry().registerComponent(
    MyComponent.class,
    MyComponent::new
    );
    }

    Přístup ke Komponentám

    // Čtení (vždy kontroluj null!)
    MyComponent comp = store.getComponent(ref, myType);
    if (comp != null) {
    // Použij komponentu
    }

    // Zápis
    store.addComponent(ref, myType, new MyComponent());
    store.ensureAndGetComponent(ref, myType); // Vytvoří pokud neexistuje

    Registrace Systému

    @Override
    protected void setup() {
    getEntityStoreRegistry().registerSystem(new MyEventSystem());
    }

    EntityEventSystem

    public class MySystem extends EntityEventSystem {

    public MySystem() {
    super(BreakBlockEvent.class);
    }

    @Override
    public void handle(int i, ArchetypeChunk chunk,
    Store store, CommandBuffer buffer,
    BreakBlockEvent event) {

    Ref ref = chunk.getReferenceTo(i);
    Player player = store.getComponent(ref, Player.getComponentType());
    // ...
    }

    @Override
    public Query getQuery() {
    return PlayerRef.getComponentType();
    }
    }

    Klíčové Třídy

    | Třída | Účel |
    |-------|------|
    | Component | Interface pro komponenty |
    | ComponentType | Identifikátor typu komponenty |
    | Store | Úložiště komponent |
    | Ref | Reference na entitu |
    | EntityEventSystem | Systém pro ECS eventy |
    | RefChangeSystem | Systém pro změny komponent |
    | Query | Filtrování entit |
    | CommandBuffer | Bezpečné modifikace |

    Výhody ECS

  • Performance: Cache-friendly, paralelní zpracování

  • Flexibilita: Snadné přidávání/odebírání chování

  • Údržba: Oddělení dat od logiky

  • Škálovatelnost: Tisíce entit efektivně

Last updated: 20. ledna 2026