/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// File:`OvalExplosion.java'
//
//
// DESCRIPTION
//	Oval Explosion
// 
// Author:
//
//	Peter Pilgrim
//	Mon May 24 22:37:47 BST 1999
// 
// RCS HEADER ``OvalExplosion.java''
// 
//	$Author$
//	$Date$
//	$Source$
//	$Revision$    $State$    $Locker$
//

package xenon.humanoid;

import java.awt.*;
import java.awt.event.*;

import xenon.gamekit.*;				// for GameEngine, WorldObject, etc
import xenon.soundkit.*;			// for SoundEngine, PPMCAudioPlayer, etc

/**
 * This class is another world object but animate the OvalExplosion effect
 * where a starship or other entity suddenly appears to be
 * materialising before your very eyes.
 *
 * This object is special because it <em>carrys a surrogate</em> world
 * object. When the materialising is done, the OvalExplosion object
 * disappears, and the <em>surrogate</em> appears.
 *
 * @author Peter Pilgrim
 * @version Fri May 21 00:35:51 BST 1999 */
public class OvalExplosion extends WorldObject
{
    public final static int	FRAME_COUNT=36;
    
    protected static Envelope	env1=null, env2=null;
    protected static SoundEngine se1=null, se2=null;

    protected int		radius;    
    protected int		delta_radius;
    protected WorldObject	surrogate;	// surrogate
    protected Runnable		runnable;

    protected Color		outerColor;
    protected Color		innerColor;
    protected int		initialDelay;	// initial delay before animation

    protected int		frameCount;	// Number of frames for the animation    

    protected boolean		imploded=false;
    
    public OvalExplosion( GameEngine gameEngine, WorldObject surrogate,
			  int wx, int wy, Color color )
    {
	this( gameEngine, surrogate, wx, wy, color, 0 );
    }
    
    public OvalExplosion( GameEngine gameEngine, WorldObject surrogate,
			  int wx, int wy, Color color, int initialDelay )
    {
	this( gameEngine, surrogate, wx, wy, color, 0, FRAME_COUNT );
    }
    
    public OvalExplosion( GameEngine gameEngine, WorldObject surrogate,
			  int wx, int wy, Color outerColor, 
			  int initialDelay, int frameCount )
    {
	super( gameEngine );

	if (initialDelay < 1) initialDelay=0;

	this.surrogate = surrogate;
	this.wx = wx;
	this.wy = wy;
	this.outerColor = outerColor;
	this.initialDelay = initialDelay;
	this.radius = 0;
	this.frameCount = frameCount;

	innerColor = Color.white;
    }

    public Color getInnerColor() 
    {
	return innerColor;
    }
    
    public boolean loadAudioClips()
    {
	// FIXME: whoosh sound
	if (env1 == null ) {
	    /* 
	       public Envelope( int pitch_step_unit, 
		     int pi1, int pn1, int pi2, int pn2, int pi3, int pn3,
		     int amplitude_step_unit , 
		     int ad, int an, int dd, int dn, int sd, int sn, int rd, int rn ) 
	       public SoundEngine( int sampling_rate, double time_duration, 
			int volume, int pitch, int duration,
			Wave wave, Envelope envelope )
	    */
	    env1 = new Envelope( 
		1,  4,4, -8,4, 32,4,     1, 25 ,7, -1, 10, 0, 0, -2, 128 );
	    
	    se1 = new SoundEngine( 
		8000, 1.11, 
		85, 212, 70,
		StaticWave.SQUARE_WAVE, env1 );
	    se1.setFlags( se1.getFlags() | SoundEngine.NOISE_MIX_GATE_FLAG );
	    se1.makeWave();
	}
	
	return (true);
    }
    
    public void setSurrogate( WorldObject newSurrogate )
    {
	surrogate = newSurrogate;
    }

    public WorldObject getSurrogate()
    {
	return (surrogate);
    }

    public void setOuterColor( Color newColor )
    {
	outerColor = newColor;
    }
    
    public Color getOuterColor() 
    {
	return outerColor;
    }
    
    public void setInnerColor( Color newColor )
    {
	innerColor = newColor;
    }
    
    public WorldObject update()
    {
	if ( initialDelay > 0 ) {
	    // Ignore update until initial delay is passed
	    --initialDelay;
	    return (this);
	}
	
	if ( !imploded ) {
	    imploded = true;			/* !!! */
	    radius = 10;
	    PPMCAudioPlayer mcap = gameEngine.getMCAudioPlayer();
	    mcap.play( 7, se1.getSoundData() );
	}
	else {
	    radius += 7;
	    wx += dx_vel;
	    wy += dy_vel;
	    
	    if (frameCount == 0 ) {
		// The OvalExplosion destroys itself and adds the surrogate
		// world object to the game engine.
		destroy();
		if ( surrogate != null ) {
		    gameEngine.addWorldObject( surrogate );
		    // surrogate.wx = wx;
		    // surrogate.wy = wy;
		    surrogate.setVisible( true );
		    surrogate.setActive( true );
		}
	    }

	    if (frameCount > 0 ) --frameCount;
	}
	
	return (this);
    }
    
    public WorldObject  drawObject( Graphics g )
    {
	// paint the OvalExplosion
	if ( isVisible() && imploded ) {

	    translateWorldToScreen();

	    int r2 = radius / 2;
	    int x1 = scrx - r2; 
	    int y1 = scry - r2;
	    
	    g.setColor( outerColor );
	    g.fillOval( x1, y1, radius, radius );
	    
	    int r4 = 8*radius / 9 ;
	    int rt = r4 / 2;
	    int x2 = scrx - rt; 
	    int y2 = scry - rt;
	    
	    g.setColor( innerColor );
	    g.fillOval( x2, y2, r4, r4 );
	}

	return (this);
    }


    /** Returns a boolean true if this object can be seen byte the radar */
    public boolean isRadarable()
    {
	return (false);
    }
}

// fini
