/* $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" 
 */

// Filename:`Humanoid.java'
//
//
// Description:
//	Humanoid video game engine.	  
//	  
// Author:
//	Peter Pilgrim
//	Sun Aug 15 22:28:54 BST 1999
//	  
// RCS HEADER ``Humanoid.java''
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
// 
//
package xenon.humanoid;

import java.beans.*;				// for PropertyChangeListener
import java.awt.*;				// for Component, Graphics
import java.awt.event.*;			// for ActionEvent, ActionListener

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

public class Humanoid 
{
    public final static String CORE_MESSAGE=
    "Please note this stack trace, because it may be a possible bug\n"+
    "------------------------------";

    // peterp@xenonsoft [109] > find xenon -name "*.class" -print | awk '/.class$/ { printf "\"%s\",\n", $0 }' | grep -v '\$'
    
    // In Emacs find & replace '.class"' to ''
    //
    private String loadClassList[] = {

	"xenon.humanoid.HumanoidGameEngine",
	"xenon.humanoid.Planetscape",
	"xenon.humanoid.Starship",
	"xenon.humanoid.ApplicationVersion",
	"xenon.humanoid.CollisionHandler",
	"xenon.humanoid.Human",
	"xenon.humanoid.Lander",
	"xenon.humanoid.PlanetExplosion",
	"xenon.humanoid.Pod",
	"xenon.humanoid.Explosion",
	"xenon.humanoid.BonusFlyer",
	"xenon.humanoid.Implosion",
	"xenon.humanoid.Bomber",
	"xenon.humanoid.Swarmer",
	"xenon.humanoid.Baiter",
	"xenon.humanoid.GameUtilities",
	"xenon.humanoid.Bomblet",
	"xenon.humanoid.LaserBlast",
	"xenon.humanoid.OvalExplosion",
//	"xenon.humanoid.HumanoidApplet",
	"xenon.gamekit.GameEngine",
	"xenon.gamekit.CollisionDetector",
	"xenon.gamekit.ImageRenderer",
	"xenon.gamekit.WorldObject",
	"xenon.gamekit.FriendObject",
	"xenon.gamekit.FriendObject",
	"xenon.gamekit.GraphicsUtils",
	"xenon.gamekit.DoubleBufferedContainer",
	"xenon.gamekit.ScoreEntry",
	"xenon.gamekit.ObjectCounter",
	"xenon.gamekit.HighScore",
	"xenon.gamekit.CollisionListener",
	"xenon.gamekit.ScoreTuple",
	"xenon.gamekit.EnemyObject",
	"xenon.gamekit.CollisionMapDispatcher",
	"xenon.gamekit.BulletinLayout",
	"xenon.gamekit.CollisionAdapter",
	"xenon.soundkit.Envelope",
	"xenon.soundkit.SoundEngine",
	"xenon.soundkit.StaticWave",
	"xenon.soundkit.Wave",
	"xenon.soundkit.DelaySoundFilter",
	"xenon.soundkit.SoundFilter",
	"xenon.soundkit.ReverseClipFilter",
	"xenon.soundkit.PPMCAudioPlayer",
	"xenon.soundkit.SoundFilterDataBuffer",
	"xenon.soundkit.WhiteNoiseWave",
	"xenon.soundkit.AudioClipWave",
    };


    protected Frame		 frame;
    protected HumanoidGameEngine myGameEngine;
    protected Planetscape	 planet;
    protected Starship		 ship;
    
    public Humanoid()
    {
	preloadClasses();
	
	frame = new Frame( "Humanoid V"+ApplicationVersion.getVersionString()+
			   "  XeNoNSoFT [c] 1999");
	
	myGameEngine = new HumanoidGameEngine( 
	    600, 320, 0, 0,
	    48, Planetscape.WORLD_HEIGHT/2, 
	    Planetscape.WORLD_WIDTH, Planetscape.WORLD_HEIGHT 
	);

	//
	// Load game icon
	//
	Image gameIcon = Toolkit.getDefaultToolkit().getImage( 
	    getClass().getResource( "images/humanoid.gif") );
	GraphicsUtils.loadImageCompletely( frame, gameIcon );
	frame.setIconImage( gameIcon );
	
	// Set the animated background object
	planet = new Planetscape( myGameEngine );
	planet.setVisible(true);
	planet.setActive(true);
	myGameEngine.setBackgroundObject( planet );

	// Set the animation hero player object
	ship = new Starship( myGameEngine );
	ship.setVisible(false);
	ship.setActive(true);
	myGameEngine.setPlayerObject( ship );
	
	Panel panel = new Panel();
	panel.setLayout( new BorderLayout( 2, 2 ) );
	panel.add( myGameEngine.getDoubleBufferedContainer() );
	frame.add( panel, BorderLayout.CENTER );
	frame.addWindowListener( new CloseWindowListener( this ) );

	frame.pack();
	myGameEngine.startGameAnimation();
	frame.setVisible(true);

    }

    protected void preloadClasses()
    {
	Frame       frame = new Frame(  "Preloading classes." );
	ProgressBar pbar = new ProgressBar( 0, 0, 100 );
        pbar.setTextFont( new Font( "Helvetica", Font.PLAIN, 14 ) );
	pbar.setTextColor( Color.white );
	pbar.setBackground( new Color( 64, 32, 240 ) );
	frame.add( pbar );
	frame.pack();

	// Center the dialog on the screen
	Dimension currSize = frame.getSize();
	if (currSize.width < 450 )
	    currSize.width = 450;
	if (currSize.height < 64 )
	    currSize.height = 64;
	Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (screen.width-currSize.width)/2;
	int y = (screen.height-currSize.height)/2;
	frame.setBounds(x, y, currSize.width, currSize.height);
	frame.setVisible(true);
	
	System.out.println( "Please, wait a mo ..." );

	pbar.setMinimum(0);
	pbar.setValue(0);
	pbar.setMaximum(loadClassList.length);

	for (int k=0; k<loadClassList.length; ++k ) {
	    String className = loadClassList[k];
	    try {
		pbar.setMessage( "Loading ...  "+className );
		pbar.setValue(k);
		Class.forName( className );
		try { Thread.sleep( 25 ); } catch (InterruptedException ie ) { }
	    }
	    catch ( Exception e ) {
		System.err.println( "cannot find or load class:`"+className+"'" );
	    }
	    catch ( Throwable t ) {
		System.err.println( 
		    "Throwable caught serious failure: "+t.getMessage() );
		System.err.println( CORE_MESSAGE );
		t.printStackTrace( System.err );
		// setMessage( t.getMessage() );
		System.out.println("\n*** death 69 ****"); System.exit(69);
	    }
	}

	frame.setVisible(false);
	frame.dispose();
    }
    
    public static void main( String[] args )
    {
	new Humanoid();
    }

    protected static class CloseWindowListener extends WindowAdapter 
    {
	protected Humanoid	humanoid;
	
	public CloseWindowListener( Humanoid humanoid )
	{
	    this.humanoid = humanoid;
	}
	
	public void windowClosing(WindowEvent e) 
	{
	    System.out.println("Good Bye Humanoid Player!");
	    System.exit(0);
	}
    }
    
}

// fini
