/* $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:`LaserBlast.java'
//
//
// DESCRIPTION
//	Laser blast
// 
// Author
//
//	Peter Pilgrim
//	Fri Jun 19 00:09:21 BST 1998
// 
// RCS HEADER ``Laserblast.java''
// 
//	$Author$
//	$Date$
//	$Source$
//	$Revision$    $State$    $Locker$
//

package xenon.humanoid;

import java.awt.*;
import java.awt.image.*;

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

public class LaserBlast extends WorldObject
{
    public final static int MAXIMUM_EXTENT=300;
    public final static int INCREMENT=16;

    public int		extent = 0;
    public int		extent_grow = 0;

    // Private object instance counter
    private static ObjectCounter	objCounter = new ObjectCounter();


    /** Constructor take a single game object */
    public LaserBlast( GameEngine gameEngine, int wx, int wy, int dx_vel, int dy_vel  ) 
    {
	super( gameEngine );

	this.wx = wx;
	this.wy = wy;
	this.dx_vel = dx_vel;
	this.dy_vel = dy_vel;

	this.extent = 0;
	this.extent_grow = INCREMENT;

	// Get the bounds
	this.width  = this.extent;
	this.height = 3;

	objCounter.increment();			/* ! */
    }

    public String getDescriptionType()
    {
	return ("Laserblast");
    }

    public static int getInstanceCount() 
    {
	return objCounter.getCounter();
    }
    
    /** update world object */
    public WorldObject	update()
    {
	wx += dx_vel;

	// Update the bounds
	width  = this.extent;
	height = 3;

	// System.out.println( "extent:"+extent+"  grow:"+extent_grow );
	
	if ( extent_grow >= 0 ) {
	    extent += extent_grow;
	    if ( extent > MAXIMUM_EXTENT ) {
		extent = MAXIMUM_EXTENT;
		extent_grow = -INCREMENT;
	    }
	}
	else {
	    extent += extent_grow;
	    if ( extent < 1 ) {
		// Laser removes itself from sprite list
		destroy();
	    }
	}
	
	return (this);
    }
    
    /** draw object if displayed */
    public WorldObject	drawObject( Graphics g ) 
    {
	if ( isVisible() ) {
	    
	    translateWorldToScreen();
	    
	    Color laserColor = Color.green;;
	
	    if ( extent >= 50 && extent < 100 ) 
		laserColor = Color.cyan;
	    else if ( extent >= 100 && extent < 150 ) 
		laserColor = Color.yellow;
	    else if ( extent >= 150 ) 
		laserColor = Color.white;
	    
	    g.setColor( laserColor );

	    // System.out.println( "laser ("+scrx+"," + scry + ") world ("+wx+"," + wy+ ")" );
	    
	    if ( dx_vel >= 0 )
		g.drawLine( scrx, scry, scrx + extent, scry );
	    else
		g.drawLine( scrx, scry, scrx - extent, scry );
	    
	}

	return (this);
    }
    
    /** undraw object if displayed */
    public WorldObject undrawObject( Graphics g )
    {
	return (this);
    }

    public void	destroy()
    {
	objCounter.decrement();			/* ! */
	super.destroy();
    }
    
    // =============================================================================
    //		I N N E R    C L A S S E S
    // =============================================================================

    private static class ObjectCounter {
	private int	total=0;

	public synchronized void increment() 
	{
	    ++total;
	}

	public synchronized void decrement() 
	{
	    if ( total > 0 ) --total;
	}

	public synchronized int getCounter() 
	{
	    return (total);
	}
    }
    
    /** Get the bounds of this object 
     * The bounds are returned int term screen coordinate space,
     * because this gets us around the wrap around world problem
     */
    public Rectangle	getBounds( Rectangle rect )
    {
	if ( dx_vel >= 0 ) {
	    rect.x = scrx;
	    rect.y = scry;
	    rect.width = extent;
	    rect.height = 3;
	}
	else {
	    rect.x = scrx - extent;
	    rect.y = scry;
	    rect.width = extent;
	    rect.height = 3;
	}
	
	return rect;
    }

    /** Get the radar color for this world object */
    public Color  getRadarColor()
    {
	return Color.yellow;
    }

}

// fini
