1
\$\begingroup\$

In the libgdx ashley wiki there's an example of Position and Velocity components, which is pretty easy to understand. My problem now is I don't know how do I integrate Sprite class as Component. In below example, If I use it like this the PositionComponent will be thrown away? because sprite has x and y fields. Also I want to use the method of Sprite setOriginCenter(), in that case do I still need to create another component which is OriginComponent? or completely leave it to SpriteComponent?. Do you think it's recommended to use this class (Sprite) in ECS framework?

class SpriteComponent implements Component {

    Sprite sprite;       

}
\$\endgroup\$
3
\$\begingroup\$

My problem now is I don't know how do I integrate Sprite class as Component.

Simple, write it like this:

public class SpriteComponent implements Component {
    public Sprite sprite;
}

**Note: implement Poolable as well if you are working with PooledComponents.


In below example, If I use it like this the PositionComponent will be thrown away? because sprite has x and y fields.

There's nothing wrong with having a SpriteComponent and a PositionComponent.

The x and y fields in the sprite are used to know where to position the sprite. Whereas, the x and y fields in your PositionComponent are used to identify the position of your entity.

If you have an entity that can have a position but no sprite then this setup seems logical.


Also I want to use the method of Sprite setOriginCenter(), in that case do I still need to create another component which is OriginComponent? or completely leave it to SpriteComponent?

No. I would not create this component as this data is held in the Sprite already. You can just access it from SpriteComponent.


my problem is I am using TextureComponent too, do I need to completely use SpriteComponent over TextureComponent? or I could use it both?

Depends what you need. If your entities can have either a texture or a sprite then yes, you may use both. However, if all entities that have sprites also have texures you may want to ask yourself what you are doing with both and if both are really needed.


Remember, Components are just 'data bags' for entities. So just think like: Am I ever going to have an entity in my game that can have this particular data? If so, will this entity already have this data within another component?

If you answered YES and then NO, then you should probably create a Component for it.

If you answered YES and then YES, then ask yourself: Do you really require both?

If you answered a third YES, then you should probably create a Component for it.

Cheers ;)

\$\endgroup\$
1
\$\begingroup\$

It may seem redundant to have a position component when the Sprite class has x and y properties but this is actually a very valid setup.

First, instead of calling it a SpriteComponent lets call it a DisplayComponent instead and implement it as follows:

class DisplayComponent implements Component {
    Sprite display;
}

The context of the component is now more explicit, it represents the visual representation of your entity. Now say at some point you wanted your entity to be invisible but you still wanted to track its position. All you need do is remove the DisplayComponent.

Also consider how the game loop works in an ECS System. Each System executes in sequence so you may need to do several operations on the entity's position before you actually display it. So keeping the position component separate is crucial. Typically there is a System dedicated to rendering/updating display components and you'd use the position component to update the x an y position of the sprite in the DisplayComponentfrom that System.

\$\endgroup\$
  • \$\begingroup\$ my problem is I am using TextureComponent too, do I need to completely use SpriteComponent over TextureComponent? or I could use it both? \$\endgroup\$ – ronscript Jul 17 '16 at 17:30

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.