Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for generic component classes #629

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

InfectedBytes
Copy link

@InfectedBytes InfectedBytes commented Mar 14, 2021

This PR adds support for components inheriting from generic base classes.
The most simple (and probably most important) use case are components with just a single value. In this case you often end up with several components that only differ in the type of the embedded value. With this PR we can now use generic types:

abstract class SingleValueComponent<T> {
  public T value;
  public void set(T value) { this.value = value; }
}

// no more boilerplate code for simple components!
class TextureMeta extends SingleValueComponent<String> { } // used to only store the path to the asset
@Transient class TextureRef extends SingleValueComponent<Texture> { } // stores the actual asset

This works especially well with the artemis-odb-contrib AssetManager (and LibGDX):

class SimpleAssetManager<A extends SingleValueComponent<String>, B extends SingleValueComponent<B>> extends AssetManager<A, B> {
    private final com.badlogic.gdx.assets.AssetManager assets;
    private final Class<B> refClass;
    
    public Manager(com.badlogic.gdx.assets.AssetManager assets, Class<A> classA, Class<B> classB) {
        super(classA, classB);
        this.assets = assets;
        refClass = classB;
    }
    
    @Override
    protected void setup(final int entity, final A a, final B b) {
        b.value = assets.get(a.value, refClass);
    }

    public static <A extends SingleValueComponent<String>, B extends SingleValueComponent<B>> Manager<A, B> create(com.badlogic.gdx.assets.AssetManager assets, Class<A> classA, Class<B> classB) {
        return new Manager<>(assets, classA, classB);
    }
}

// adding a new asset manager is now as simple as:
final WorldConfiguration cfg = new WorldConfigurationBuilder()
    .with(SimpleAssetManager.create(assets, TextureMeta.class, Texture.class))
    .with(SimpleAssetManager.create(assets, FontMeta.class, Font.class))
    // ...
    .build();

This also works with more complicated scenarios with various (generic) classes in between, although this might be an unusual use case, see the newly added tests.

@DaanVanYperen
Copy link
Collaborator

Scope is limited to logic for generating classes so I'm for merging, assuming it doesn't cause issues for GWT. @junkdog?

@DaanVanYperen DaanVanYperen added this to the artemis-2.5.0 milestone Jul 13, 2021
Copy link
Collaborator

@DaanVanYperen DaanVanYperen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants