package org.jtrfp.trcl.obj;

import java.awt.Color;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.jtrfp.trcl.Model;
import org.jtrfp.trcl.Tunnel;
import org.jtrfp.trcl.beh.Behavior;
import org.jtrfp.trcl.beh.CollidesWithTerrain;
import org.jtrfp.trcl.beh.HeadingXAlwaysPositiveBehavior;
import org.jtrfp.trcl.beh.LoopingPositionBehavior;
import org.jtrfp.trcl.core.TR;
import org.jtrfp.trcl.file.DirectionVector;

public class TunnelEntranceObject extends WorldObject {
    private final Tunnel tunnel;
    public TunnelEntranceObject(TR tr, Tunnel tunnel) {
	super(tr);
	this.tunnel=tunnel;
	addBehavior(new TunnelEntranceBehavior());
	setVisible(false);
	DirectionVector entrance = tunnel.getSourceTunnel().getEntrance();
	setPosition(new Vector3D(TR.legacy2Modern(entrance.getZ()),TR.legacy2Modern(entrance.getY())-45000,TR.legacy2Modern(entrance.getX())));
	try{Model m = tr.getResourceManager().getBINModel("SHIP.BIN", tr.getGlobalPalette(), tr.getGPU().getGl());
	setModel(m);}
	catch(Exception e){e.printStackTrace();}
    }//end constructor

    private class TunnelEntranceBehavior extends Behavior{
	@Override
	public void _proposeCollision(WorldObject other){
	    WorldObject p = getParent();
	      if(other instanceof Player){
	        if(p.getPosition().distance(other.getPosition())<CollisionManager.SHIP_COLLISION_DISTANCE*3){
		 //Turn off overworld
		 final TR tr = getTr();
		 tr.getOverworldSystem().deactivate();
		 //Turn on tunnel
		 tunnel.activate();
		 //Move player to tunnel
		 tr.getWorld().setFogColor(Color.black);
		 tr.getBackdropSystem().tunnelMode();
		 
		 final ProjectileFactory [] pfs = tr.getResourceManager().getProjectileFactories();
		 for(ProjectileFactory pf:pfs){
		     Projectile [] projectiles = pf.getProjectiles();
		     for(Projectile proj:projectiles){
			 ((WorldObject)proj).getBehavior().probeForBehavior(LoopingPositionBehavior.class).setEnable(false);
		     }//end for(projectiles)
		 }//end for(projectileFactories)
		 
		 tr.getPlayer().setPosition(Tunnel.TUNNEL_START_POS);
		 tr.getPlayer().setDirection(Tunnel.TUNNEL_START_DIRECTION);
		 tr.getPlayer().getBehavior().probeForBehavior(LoopingPositionBehavior.class).setEnable(false);
		 tr.getPlayer().getBehavior().probeForBehavior(HeadingXAlwaysPositiveBehavior.class).setEnable(true);
		 tr.getPlayer().getBehavior().probeForBehavior(CollidesWithTerrain.class).setEnable(false);
	        }//end if(close)
	    }//end if(Player)
	}//end _proposeCollision
    }//end TunnelEntranceBehavior
}//end TunnelEntrance

@@ -114,16 +114,57 @@
 ctor3D;%0A
+import org.jtrfp.trcl.AbstractSubmitter;%0A
 import o
@@ -398,32 +398,83 @@
 sitionBehavior;%0A
+import org.jtrfp.trcl.beh.tun.TunnelEntryListener;%0A
 import org.jtrfp
@@ -2096,24 +2096,99 @@
 ctories)%0A%09%09 
+p.getBehavior().probeForBehaviors(TELsubmitter, TunnelEntryListener.class);
 %0A%09%09 tr.getPl
@@ -2700,16 +2700,226 @@
 ehavior%0A
+    private final AbstractSubmitter%3CTunnelEntryListener%3E TELsubmitter = new AbstractSubmitter%3CTunnelEntryListener%3E()%7B%0A%09@Override%0A%09public void submit(TunnelEntryListener tel)%7Btel.notifyTunnelEntered();%7D %0A    %7D;%0A
 %7D//end T

Support added for TunnelEntryListener