1
\$\begingroup\$

Background: I'm using Universal Tween Engine, which allows generic accessors to be implemented to permit tweening of arbitrary atttributes.

I have a hierarchy of game entity classes that share a common base class (say, Entity). I've implemented a TweenAccessor on Entity for basic attribute tweening (position, color, rotation, etc). I'd also like subclasses of Entity to be able to implement their own, more specialized tweens, in addition to having access to Entity's base set of tweens. However, it seems that when I register a class and a subclass accessor, the base class' accessor is never called during tweening.

For example, assume base class Entity and subclass Bullet:

Tween.registerAccessor(Entity.class, new EntityAccessor());
Tween.registerAccessor(Bullet.class, new BulletAccessor());

Attempting to do any of the tweens defined in EntityAccessor will not be called -- BulletAccessor will always be used instead. Is there any way to configure Universal Tween Engine to work in such a hierarchical fashion?

\$\endgroup\$
0
\$\begingroup\$

Although it's a little awkward, I ended up having subclasses' accessors compose the parent accessor as a member and have getValues/setValues fall back onto it if no tween is found.

public class BulletAccessor implements TweenAccessor<Bullet> {

    private EntityAccessor entityAccessor;

    // Special care needs to be taken to ensure these values don't
    // overlap with any of the parents' or their tween accessors.
    public static final int SOME_PROPERTY = 50;
    public static final int ANOTHER_PROPERTY = 51;

    public BulletAccessor() {
        entityAccessor = new EntityAccessor();
    }

    @Override
    public int getValues(Bullet bullet, int type, float[] values) {
        switch(type) {
            case SOME_PROPERTY:
                // ...
                return 2;
            case ANOTHER_PROPERTY:
                // ...
                return 1;
            default:
                return entityAccessor.getValues(bullet, type, values);
        }
    }

    @Override
    public void setValues(Mob mob, int type, float[] values) {
        switch(type) {
            case SOME_PROPERTY:
                // ...
                break;
            case ANOTHER_PROPERTY:
                // ...
                break;
            default:
                entityAccessor.setValues(bullet, type, values);
                break;
        }
    }
}
\$\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.