///////////////////////////////////////////////////////////////
//
// world.cpp 
// Jesse Doane 
// April 2000
//
// the world contains and updates the asteroids 
//
#include "world.h"

/////////////////////////////////////////////////////////////////////////
//
// create a hard-coded world with three asteroids

World::World( void ){

Asteroid *a1, *a2, *a3, *a4;

a1 = new Asteroid( new Vertex( -3, 1 ) );

a1->AddEdge( new Edge( 90.0, 30.0, 90.0, 90.0 ) );
a1->AddEdge( new Edge( 30.0, 30.0, 90.0, 30.0 ) );
a1->AddEdge( new Edge( 90.0, 90.0, 30.0, 90.0 ) );
a1->AddEdge( new Edge( 30.0, 90.0, 30.0, 30.0 ) );

a2 = new Asteroid( new Vertex( 1, 4 ) );

a2->AddEdge( new Edge( 140.0, 120.0, 150.0, 130.0 ) );
a2->AddEdge( new Edge( 150.0, 130.0, 140.0, 140.0 ) );
a2->AddEdge( new Edge( 140.0, 140.0, 130.0, 130.0 ) );
a2->AddEdge( new Edge( 130.0, 130.0, 140.0, 120.0 ) );

a3 = new Asteroid( new Vertex( -2, 1 ) );

a3->AddEdge( new Edge( 300.0, 180.0, 310.0, 200.0 ) );
a3->AddEdge( new Edge( 310.0, 200.0, 290.0, 220.0 ) );
a3->AddEdge( new Edge( 290.0, 220.0, 260.0, 200.0 ) );
a3->AddEdge( new Edge( 260.0, 200.0, 300.0, 180.0 ) );

asteroids.PushFront( a1, 0 );
asteroids.PushFront( a2, 0 );
asteroids.PushFront( a3, 0 );

asteroids_a.PushFront( a1, 0 );
asteroids_a.PushFront( a2, 0 );
asteroids_a.PushFront( a3, 0 );
}
////////////////////////////////////////////////////////////////////

void World::Update( void ){

bool c1 = true;
bool c2 = true;
Asteroid* actor1;
Asteroid* actor2;

asteroids.SetCurRear();

// test all asteroids for collision 
while( c1 ){
	
	c1 = asteroids.Forward( &actor1 );
	asteroids_a.SetCurRear();

	while( c2 ){

		c2 = asteroids_a.Forward( &actor2 );
		if( actor1 != actor2 ){

			actor1->Collision( actor2 );
		}
	}
	c2 = true;
}

// move all asteroids
c1 = true;
asteroids.SetCurRear();
while( c1 ){

	c1 = asteroids.Forward( &actor1 );
	actor1->Move();
}}
////////////////////////////////////////////////////////////////////

void World::Draw( UCHAR* canvas ){

bool c = true;
Asteroid* actor;
asteroids.SetCurRear();

while( c ){

	c = asteroids.Forward( &actor );
	actor->Draw( canvas );
}}
