View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.utils;
2   
3   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdatedEvent;
4   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldObjectUpdatedEvent.DestroyWorldObject;
5   import cz.cuni.amis.pogamut.base.communication.worldview.react.ObjectEventReact;
6   import cz.cuni.amis.pogamut.base3d.worldview.object.event.WorldObjectDisappearedEvent;
7   import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
8   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.IncomingProjectile;
9   
10  /**
11   * Clean-up module for destroying {@link IncomingProjectile} objects within worldview.
12   * 
13   * Whenever projectile disappears from the view (becomes non-visible), it is immediately destroyed (this class rises {@link DestroyWorldObject} event for it)
14   * as bot does not know what happens to projectile afterwards (whether it is still flying or already hit something). If we would not do this,
15   * world view would have got littered with in-fact-non-existing {@link IncomingProjectile} instances leaking memory. 
16   * 
17   * Automatically used by {@link UT2004Bot}.
18   * 
19   * You may at any time {@link ProjectileCleanUp#enable()} or {@link ProjectileCleanUp#disable()} this clean-up behavior (it is enabled by default).
20   * 
21   * @author Jimmy
22   */
23  public class ProjectileCleanUp {
24  	
25  	private UT2004Bot bot;
26  	
27  	private ObjectEventReact<IncomingProjectile, WorldObjectDisappearedEvent<IncomingProjectile>> destroyProjectile;
28  
29  	public ProjectileCleanUp(UT2004Bot bot) {
30  		this.bot = bot;
31  		destroyProjectile = new ObjectEventReact<IncomingProjectile, WorldObjectDisappearedEvent<IncomingProjectile>>(IncomingProjectile.class, WorldObjectDisappearedEvent.class, bot.getWorldView()) {
32  
33  			@Override
34  			protected void react(WorldObjectDisappearedEvent<IncomingProjectile> event) {
35  				ProjectileCleanUp.this.bot.getWorldView().notifyAfterPropagation(
36  						new IWorldObjectUpdatedEvent.DestroyWorldObject(event.getObject(), event.getSimTime())
37  				);				
38  			}
39  			
40  		};
41  		destroyProjectile.enable();
42  	}
43  	
44  	public void enable() {
45  		destroyProjectile.enable();
46  	}
47  	
48  	public void disable() {
49  		destroyProjectile.disable();
50  	}
51  
52  }