LINUX.ORG.RU

История изменений

Исправление rtxtxtrx, (текущая версия) :

Где тут альтернатива ООП?

// Компоненты
class Position {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

class Velocity {
    constructor(vx, vy) {
        this.vx = vx;
        this.vy = vy;
    }
}

class Renderable {
    constructor(sprite) {
        this.sprite = sprite;
    }
}

// Сущности
const createEntity = (() => {
    let entityId = 0;
    return () => entityId++;
})();

// Менеджер сущностей
class EntityManager {
    constructor() {
        this.entities = new Map();
    }

    addComponent(entity, component) {
        if (!this.entities.has(entity)) {
            this.entities.set(entity, []);
        }
        this.entities.get(entity).push(component);
    }

    getComponents(entity) {
        return this.entities.get(entity) || [];
    }
}

// Системы
class MovementSystem {
    update(entityManager) {
        for (let [entity, components] of entityManager.entities) {
            let position = components.find(c => c instanceof Position);
            let velocity = components.find(c => c instanceof Velocity);

            if (position && velocity) {
                position.x += velocity.vx;
                position.y += velocity.vy;
            }
        }
    }
}

class RenderSystem {
    render(entityManager, context) {
        for (let [entity, components] of entityManager.entities) {
            let position = components.find(c => c instanceof Position);
            let renderable = components.find(c => c instanceof Renderable);

            if (position && renderable) {
                context.drawImage(renderable.sprite, position.x, position.y);
            }
        }
    }
}

// Использование ECS
const entityManager = new EntityManager();

const entity1 = createEntity();
const entity2 = createEntity();

entityManager.addComponent(entity1, new Position(100, 100));
entityManager.addComponent(entity1, new Velocity(1, 1));
entityManager.addComponent(entity1, new Renderable(new Image()));

entityManager.addComponent(entity2, new Position(200, 200));
entityManager.addComponent(entity2, new Velocity(-1, -1));
entityManager.addComponent(entity2, new Renderable(new Image()));

const movementSystem = new MovementSystem();
const renderSystem = new RenderSystem();

function gameLoop(context) {
    movementSystem.update(entityManager);
    renderSystem.render(entityManager, context);
}

function startGame(canvas) {
    const context = canvas.getContext('2d');
    setInterval(() => gameLoop(context), 16); // ~60 FPS
}

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
startGame(canvas);

Исходная версия rtxtxtrx, :

Где тут альтернатива ООП?

// Компоненты
class Position {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

class Velocity {
    constructor(vx, vy) {
        this.vx = vx;
        this.vy = vy;
    }
}

class Renderable {
    constructor(sprite) {
        this.sprite = sprite;
    }
}

// Сущности
let entityId = 0;

function createEntity() {
    return entityId++;
}

// Менеджер сущностей
class EntityManager {
    constructor() {
        this.entities = new Map();
    }

    addComponent(entity, component) {
        if (!this.entities.has(entity)) {
            this.entities.set(entity, []);
        }
        this.entities.get(entity).push(component);
    }

    getComponents(entity) {
        return this.entities.get(entity) || [];
    }
}

// Системы
class MovementSystem {
    update(entityManager) {
        for (let [entity, components] of entityManager.entities) {
            let position = components.find(c => c instanceof Position);
            let velocity = components.find(c => c instanceof Velocity);

            if (position && velocity) {
                position.x += velocity.vx;
                position.y += velocity.vy;
            }
        }
    }
}

class RenderSystem {
    render(entityManager, context) {
        for (let [entity, components] of entityManager.entities) {
            let position = components.find(c => c instanceof Position);
            let renderable = components.find(c => c instanceof Renderable);

            if (position && renderable) {
                context.drawImage(renderable.sprite, position.x, position.y);
            }
        }
    }
}

// Использование ECS
const entityManager = new EntityManager();

const entity1 = createEntity();
const entity2 = createEntity();

entityManager.addComponent(entity1, new Position(100, 100));
entityManager.addComponent(entity1, new Velocity(1, 1));
entityManager.addComponent(entity1, new Renderable(new Image()));

entityManager.addComponent(entity2, new Position(200, 200));
entityManager.addComponent(entity2, new Velocity(-1, -1));
entityManager.addComponent(entity2, new Renderable(new Image()));

const movementSystem = new MovementSystem();
const renderSystem = new RenderSystem();

function gameLoop(context) {
    movementSystem.update(entityManager);
    renderSystem.render(entityManager, context);
}

function startGame(canvas) {
    const context = canvas.getContext('2d');
    setInterval(() => gameLoop(context), 16); // ~60 FPS
}

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
startGame(canvas);