/* $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:`Planetscape.java'
//
//
// DESCRIPTION
//	Planetscape Humanoid
// 
// Author
//
//	Peter Pilgrim
//	Fri Jun 19 00:09:21 BST 1998
// 
// RCS HEADER ``Planetscape.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 Planetscape extends WorldObject
{
    public static int WORLD_WIDTH = 4800;
    public static int WORLD_HEIGHT = 280;

    protected static Point[] peaks = {
	new Point( 0, 20 ),
	new Point( 20, 40 ),
	new Point( 40, 20 ),
	new Point( 60, 20 ),
	new Point( 140,40 ),
	new Point( 180,20 ),
	new Point( 220, 20 ),
	new Point( 260, 60 ),
	new Point( 280, 42 ),
	new Point( 340, 65 ),
	new Point( 400, 20 ),
	new Point( 460, 22 ),
	new Point( 500, 73 ),
	new Point( 530, 61 ),
	new Point( 580, 20 ),
	new Point( 600, 20 ),
	new Point( 640, 30 ),
	new Point( 680, 60 ),
	new Point( 690, 80 ),
	new Point( 710, 80 ),
	new Point( 720, 60 ),
	new Point( 745, 40 ),
	new Point( 800, 20 ),
	new Point( 840, 20 ),
	new Point( 880, 51 ),
	new Point( 920, 24 ),
	new Point( 940, 20 ),
	new Point( 970, 53 ),
	new Point( 1000, 23 ),
	new Point( 1040, 20 ),
	new Point( 1080, 38 ),
	new Point( 1120, 51 ),
	new Point( 1160, 43 ),
	// **** 1/4 WORLD ****
	new Point( 1200, 20 ),
	new Point( 1300, 10 ),
	new Point( 1360, 11 ),
	new Point( 1400, 32 ),
	new Point( 1440, 10 ),
	new Point( 1500, 10 ),
	new Point( 1520, 33 ),
	new Point( 1560, 21 ),
	new Point( 1580, 10 ),
	new Point( 1620, 11 ),
	new Point( 1720, 33 ),
	new Point( 1800, 11 ),
	new Point( 1860, 42 ),
	new Point( 1900, 21 ),
	new Point( 1980, 45 ),
	new Point( 2030, 10 ),
	new Point( 2140, 21 ),
	new Point( 2200, 39 ),
	new Point( 2240, 41 ),
	new Point( 2280, 20 ),
	new Point( 2340, 20 ),
	new Point( 2360, 40 ),
	new Point( 2380, 20 ),
	// **** MID WORLD ****
	new Point( 2400, 20 ),
	new Point( 2450, 15 ),
	new Point( 2490, 28 ),
	new Point( 2510, 35 ),
	new Point( 2550, 12 ),
	new Point( 2600, 20 ),
	new Point( 2650, 18 ),
	new Point( 2680, 31 ),
	new Point( 2720, 48 ),
	new Point( 2775, 35 ),
	new Point( 2800, 20 ),
	new Point( 2840, 15 ),
	new Point( 2900, 39 ),
	new Point( 2950, 20 ),
	new Point( 3000, 10 ),
	new Point( 3050, 13 ),
	new Point( 3100, 10 ),
	new Point( 3200, 20 ),
	new Point( 3240, 30 ),
	new Point( 3300, 50 ),
	new Point( 3360, 20 ),
	new Point( 3440, 15 ),
	new Point( 3500, 22 ),
	new Point( 3555, 49 ),
	// **** 1/4 WORLD ****
	new Point( 3600, 20 ),
	new Point( 3660, 50 ),
	new Point( 3720, 20 ),
	new Point( 3800, 31 ),
	new Point( 3840, 42 ),
	new Point( 3900, 20 ),
	new Point( 3980, 20 ),
	new Point( 4040, 20 ),
	new Point( 4120, 30 ),
	new Point( 4200, 40 ),
	new Point( 4240, 60 ),
	new Point( 4300, 80 ),
	new Point( 4340, 45 ),
	new Point( 4400, 21 ),
	new Point( 4480, 38 ),
	new Point( 4520, 44 ),
	new Point( 4600, 12 ),
	new Point( 4660, 20 ),
	new Point( 4680, 20 ),
	new Point( 4700, 40 ),
	new Point( 4720, 20 ),
	new Point( 4740, 20 ),
	new Point( 4760, 50 ),
	new Point( 4780, 20 ),
	new Point( 4800, 20 ),
    };
    
    /** Constructor take a single game object */
    public Planetscape( GameEngine gameEngine ) 
    {
	super( gameEngine );
    }

    /** update world object */
    public WorldObject	update()
    {
	wx = gameEngine.world_xpos;
	wy = gameEngine.world_ypos;
	return (this);
    }
    
    /** draw object if displayed */
    public WorldObject	drawObject( Graphics g ) 
    {
	int screen_width  = gameEngine.screen_width;
	int screen_height = gameEngine.screen_height;
	int xlower = -screen_width/2;
	int xupper = 3*screen_width/2;
	int world_width   = gameEngine.world_width;
	
	// Paint the canvas in the default background color.
	g.setColor( component.getBackground() );
	// Dimension size = gameEngine.getSize();
	// g.fillRect( 0, 0, size.width, size.height );
	g.fillRect( 0, 0, screen_width, screen_height );

	if ( !isVisible() )
	    // Planet scape is Not visible just draw background and
	    // return immediately.
	    return (this);
	
	// g.setColor( Color.magenta );
	// Acornsoft's Planetoid `Red' is the chosen
	g.setColor( Color.red );

	// FIXME: Could do with a optimisation draw here.
	for (int k=0; k<peaks.length-1 ; ++k )
	{
	    int displacement = wx - ( world_width - screen_width );
	    
	    int x1 = peaks[k].x - wx;
	    int y1 = screen_height - peaks[k].y;
	    int x2 = peaks[k+1].x - wx;
	    int y2 = screen_height - peaks[k+1].y;

	    // Detect where the world has wrapped, and translate
	    // those x coordinates back to the screen to complete
	    // the illusion of scrolling wraparound landscape.
	    if ( wx > world_width - screen_width &&
		 peaks[k].x < screen_width )
	    {
		// Remap "Humanoid!" here we come
		x1 += world_width;
		x2 += world_width;
	    }
	    // System.out.println( "("+x1+","+y1+") ("+x2+","+y2+") " );

	    // VERY DUMB ASS CLIPPING MECHANISM
	    if ( x1 >= xlower && x2 <= xupper ) {
		g.drawLine( x1, y1, x2, y2 );
		g.drawLine( x1, y1-1, x2, y2-1 );
		g.drawLine( x1, y1-2, x2, y2-2 );
	    }
	}

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

// fini
