3
\$\begingroup\$

Currently I am using builder-pattern to create my Entities

PooledEngine engine = new PooledEngine();

...

CharacterComponent character = engine.createComponent(CharacterComponent.class);
TextureComponent texture = engine.createComponent(TextureComponent.class);
SizeComponent size = engine.createComponent(SizeComponent.class);
TransformComponent transform = engine.createComponent(TransformComponent.class);
PhysicsComponent physics = engine.createComponent(PhysicsComponent.class);

Entity entity = engine.createEntity();
entity.flag = Constants.CHARACTER;

entity.add(character);
entity.add(texture);
entity.add(size);
entity.add(transform);
entity.add(physics);

engine.addEntity(entity);

Because PooledEngine class didnt support the creation of casting an Entity to another class that extends Entity. Example engine.createEntity(Character.class);, I am having hard thinking how do I wrap these components into single class.

Objectives

class interface ICharacter {
    void attack();
}

class interface IMoveable {
    void move();
}

class Character extends Entity implements ICharacter, IMoveable {

    private Body body;
    private Vector2 position;

    public Character() {
        PhysicsComponent physics = Mappers.physics.get(this);
        TransformComponent transform = Mappers.transform.get(this);

        body = physics.body;
        position = transform.position;
    }

    @Override
    public void attack() {

    }

    @Override
    public void move() {
        ...
        position.add(x, y);
    }

}
\$\endgroup\$
2
\$\begingroup\$

You are totally defeating the purpose of using an Entity-Component-System by creating that Character class. The whole drive behind ECS architecture is the decoupling/separation of data(Components) and logic(Systems). What you need to do is create separate Systems to handle attack and movement logic.

Read these two articles, they'll help you get a better understanding of ECS architecture and how to use it:

What is an entity system framework for game development?

Why use an entity system framework for game development?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.