Advanced Contact Listener

  ... the case you feel the need for a new feature or want to submit one.

Advanced Contact Listener

Postby rkpost » Wed Nov 10, 2010 6:19 am

Hi Nicolas,

Suppose that you have a game, a little complex game with a lot of rules about "who" collides with "who". For example, a Cowboy game, where we have bullets, sheriffs, bad guys, barrels, bottles, horses and pretty girls.

Everything is a caos because, if a bullet touch a Sheriff, you lose life, but if collides with a bad guy, you get some points. If you touch a horse, maybe the good guy runs fast, a bad guy could kidnap a girl, ......... a lot of crazy rules.

With the actual model, we have only one way to program all this rules, with a single class implementing ContactListener and then setting to PhysicWorld.

When we have one, two, five possible interactions, one class is ok, but for more complex games is a bit chaotic define all those interactions in one class.

For my actual game (isn't about cowbows, hehe), I need to define a couple of dozen of interactions and I crash with this problem.

I imagine a class like,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MultiContactListener implements ContactListener {
  2.  
  3.     private List<ContactListener> mListeners = new ArrayList<ContactListener>();
  4.  
  5.     public MultiContactListener() {
  6.     }
  7.  
  8.     public MultiContactListener(ContactListener pListener) {
  9.         this.mListeners.add(pListener);
  10.     }
  11.  
  12.     @Override
  13.     public void beginContact(Contact pContact) {
  14.         for(int i = 0; i < mListeners.size(); i++) {
  15.             this.mListeners.get(i).beginContact(pContact);
  16.         }
  17.     }
  18.  
  19.     @Override
  20.     public void endContact(Contact pContact) {
  21.         for(int i = 0; i < mListeners.size(); i++) {
  22.             this.mListeners.get(i).endContact(pContact);
  23.         }
  24.     }
  25.  
  26.     public void addContactListener(ContactListener pListener) {
  27.         this.mListeners.add(pListener);
  28.     }
  29.  
  30.     public void removeContactListener(ContactListener pListener) {
  31.         this.mListeners.remove(pListener);
  32.     }
  33.  
  34. }
  35.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


Something like that solves the problem of handle only one listener, but... maybe we can do something a little more complex, with more options and facilities.

I couldn't find the best place to create all contacts yet or avoid a lot of instanceof, or something like that.
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: Advanced Contact Listener

Postby Nicolas Gramlich » Fri Nov 12, 2010 1:48 pm

Hi,

usually I add a switch in beginContact on the 'userdata' I attached to the bodies when they were created.
From the cases of the switch it jumps to nicely named methods.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Advanced Contact Listener

Postby rkpost » Fri Nov 12, 2010 4:07 pm

beginContact is call it twice when two body collided?
rkpost
 
Posts: 201
Joined: Tue Sep 14, 2010 5:52 am
Location: Buenos Aires

Re: Advanced Contact Listener

Postby Nicolas Gramlich » Sat Nov 13, 2010 12:18 am

I'm not sure, but I assume only once :?

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany


Return to Features

Who is online

Users browsing this forum: No registered users and 10 guests