mercredi 20 novembre 2019

Libgdx, design pattern for box2D contact method

I have a problem to design my Java implementation I got a classic ContactListener who implement box2D.ContactListener. All my class extends BaseEntity My "Dream" is to get multiple method contact in my entity class (who extends BaseEntity) But the parameter is Typed with child class

Example : ContactListener :

    public void beginContact(Contact contact) {
        Fixture fa = contact.getFixtureA();
        Fixture fb = contact.getFixtureB();
        BaseEntity entityA = (BaseEntity) fa.getBody().getUserData();
        BaseEntity entityB = (BaseEntity) fb.getBody().getUserData();
        debugEvent("Begin Contact", entityA, entityB);

        entityA.contact(entityB);
        entityB.contact(entityA);
    }

And in my Player


    public Player(Body body) {
        super("player", body);
    }

    public void contact(Wall wall) {
        System.out.println("playerCLass : contact with wall");
    }
    public void contact(Bullet bullet) {
        System.out.println("playerCLass : contact with bullet");
    }
}

So redefine a same method and just with typed parameter JVM know the implementation to use.

And same for all class extends BaseEntity.

In BulletClass

public void contact(Player player) {
        System.out.println("wallClass : contact with player");
    }

I dont need necessary this type of "collision management" but i really want something generic where i can split game logic.

Thanks for reading. fabien

Aucun commentaire:

Enregistrer un commentaire