package PCG;

import javax.media.j3d.*;
import java.util.Enumeration;
import javax.vecmath.*;

public class DetectCollisionBehavior extends Behavior {

	private Node m_oNode = null;
	private PointSound m_oPointSound = null;
	private Node m_oTargetNode = null;

	private WakeupOnCollisionEntry oWakeupOnCollisionEntry = null;

        public DetectCollisionBehavior( Group oGroup, Node oTargetNode,
                Bounds oBounds, String sURL ) {

		m_oNode = oGroup;
	        setSchedulingBounds( oBounds );
		m_oTargetNode = oTargetNode;

                MediaContainer oMediaContainer = new MediaContainer( sURL );
                PointSound oPointSound = new PointSound( oMediaContainer, 1f,
                        0, 0, 0 );

		// Create a distance gain array for the point sound
		Point2f[] distanceGain = {
			new Point2f(  9.0f, 1.0f ),    // Full volume
			new Point2f( 10.0f, 0.5f ),    // Half volume
			new Point2f( 20.0f, 0.25f ),   // Quarter volume
			new Point2f( 30.0f, 0.0f ),    // Zero volume
		};
                oPointSound.setDistanceGain( distanceGain );
                oPointSound.setSchedulingBounds( oBounds );

                oPointSound.setEnable( false );
                oPointSound.setLoop( 0 );
		oPointSound.setContinuousEnable( false );
		oPointSound.setReleaseEnable( false );
		oPointSound.setCapability( PointSound.ALLOW_ENABLE_WRITE );

                oGroup.addChild( oPointSound );

		Point3f oSoundPos = new Point3f( 0.0f, 0.0f, 0.0f );
		oPointSound.setPosition( oSoundPos );

		m_oPointSound = oPointSound;
        }

        public void initialize() {
		oWakeupOnCollisionEntry = new WakeupOnCollisionEntry( m_oNode );
		wakeupOn( oWakeupOnCollisionEntry );
        }

        public void processStimulus( Enumeration criteria ) {

		while (criteria.hasMoreElements()) {
		        if (criteria.nextElement() instanceof WakeupOnCollisionEntry) {

                                m_oPointSound.setEnable( true );

				wakeupOn( oWakeupOnCollisionEntry );
			}
		}
        }

}
