<?php // This is just here so that the coloring would work. It does not exist in the ActionScript.

//////////////////////////////////////// 
// Vectrix - Sebastian Ahlman 2006 
// Main source code file. 
////////////////////////////////////////

// Set up the global variables.
speed 6;
focalLength 300;
framenumber 0;
bonusCountdown 200;
bonusPointsCollected 0;

// Generate pseudo-random numbers
function randRange(min:Numbermax:Number):Number
{
    var 
randomNum:Number Math.floor(Math.random() * (max min 1)) + min;
    return 
randomNum;
}

// Function to create 3D-vectors.
CreateVector = function(x,y,z)
{
    var 
point = new Object();
    
point.x;
    
point.y;
    
point.z;
    return 
point;
}

// Calculates the distance between two points in 3D space.
VectorLenght = function(point1point2)
{
    
vector CreateVector(point1.point2.x
        
point1.point2.ypoint1.point2.z);
    
    
temp = (vector.vector.x) + (vector.vector.y) +
        (
vector.vector.z);
        
    return 
Math.sqrt(temp);
}

// Transform a point from 3D to 2D space.
TransformPoint = function(pointIn3D)
{
    var 
pointIn2D = new Object();
    var 
scaleRatio focalLength/(focalLength pointIn3D.z);
    
pointIn2D.pointIn3D.scaleRatio;
    
pointIn2D.pointIn3D.scaleRatio;
    return 
pointIn2D;
}

// Init. the scene.
this.createEmptyMovieClip("scene"1);
scene._x 300;
scene._y 300;
scene.depth 1;
scene.objects = new Array();
scene.bursterflames = new Array();

// Transforms a sprite (gives it the correct size
// and transforms it to 2D from a given 3D point)
TransformSprite = function(objectxyz)
{    
    var 
scaleRatio focalLength/(focalLength z);
    
object._x scaleRatio;
    
object._y scaleRatio;
    
object._xscale object._yscale 100 scaleRatio;
    
object.swapDepths(Math.round(-z));
}

// Draw the asteroids and the particles on the screen.
drawAsteroid = function()
{
    if(
this._name.substr(05) == "light")
        
this.-= this.velocity 0.5 + (framenumber 0.03);
    else
        
this.-= this.velocity + (framenumber 0.03);
    if(
this.<= -250)
    {
        if(
this._name.substr(05) == "light")
        {
            if(
bonusCountdown == 200)
                break;
            else
                
removeMovieClip(this);
        }
        
this.randRange(-200200);
        
this.randRange(-250250);
        
this.velocity randRange(520);
        
this.550;
    }
    
TransformSprite(thisthis.xthis.ythis.z);
}

// Draw the burster flame particles.
drawBursterFlame = function()
{
    
this.-= 15 + (framenumber 0.03);
    if(
this.<= -300removeMovieClip(this);
    
TransformSprite(thisthis.xthis.ythis.z);
}

// Draw the ship onscreen and make it react to commands.
drawShip = function()
{
    if(
Key.isDown(Key.LEFT)) key_left true; else key_left false;
    if(
Key.isDown(Key.RIGHT)) key_right true; else key_right false;
    if(
Key.isDown(Key.UP)) key_up true; else key_up false;
    if(
Key.isDown(Key.DOWN)) key_down true; else key_down false;
    if(
Key.isDown(Key.TAB)) key_tab true; else key_tab false;
    if(
Key.isDown(Key.SHIFT)) key_shift true; else key_shift false;
    
    var 
screenPoints = new Array();
    for (var 
i=0shipArray.lengthi++)
    {
        var 
thisPoint shipArray[i];        
        
        
// React to controls
        
if(key_left)
        {
            if(
ship.loc.> -200thisPoint.-= speed;
        }
        
        if(
key_right)
        {
            if(
ship.loc.200thisPoint.+= speed;
        }
        
        if(
key_up)
        {
            if(
ship.loc.> -250thisPoint.-= speed;
        }
        
        if(
key_down)
        {
            if(
ship.loc.250thisPoint.+= speed;
        }
        
        if(
key_tab)
        {
            if(
ship.loc.550thisPoint.+= speed;
        }
        
        if(
key_shift)
        {
            if(
ship.loc.> -50thisPoint.-= speed;
        }
        
screenPoints[i] = TransformPoint(thisPoint);
    }
    
    
this.clear();
    
this.lineStyle(2,0xFFFFFF,100);
    
    
// Draw the bottom of the ship
    
this.moveTo(screenPoints[0].xscreenPoints[0].y);
    
this.lineTo(screenPoints[1].xscreenPoints[1].y);
    
this.lineTo(screenPoints[2].xscreenPoints[2].y);
    
this.lineTo(screenPoints[3].xscreenPoints[3].y);
    
this.lineTo(screenPoints[4].xscreenPoints[4].y);
    
this.lineTo(screenPoints[2].xscreenPoints[2].y);
    
this.moveTo(screenPoints[4].xscreenPoints[4].y);
    
this.lineTo(screenPoints[5].xscreenPoints[5].y);
    
this.lineTo(screenPoints[6].xscreenPoints[6].y);
    
this.lineTo(screenPoints[7].xscreenPoints[7].y);
    
this.lineTo(screenPoints[0].xscreenPoints[0].y);
    
this.moveTo(screenPoints[1].xscreenPoints[1].y);
    
this.lineTo(screenPoints[7].xscreenPoints[7].y);
    
    
// Draw the upper part of the main body
    
this.moveTo(screenPoints[1].xscreenPoints[1].y);
    
this.lineTo(screenPoints[8].xscreenPoints[8].y);
    
this.lineTo(screenPoints[9].xscreenPoints[9].y);
    
this.lineTo(screenPoints[2].xscreenPoints[2].y);
    
this.moveTo(screenPoints[9].xscreenPoints[9].y);
    
this.lineTo(screenPoints[10].xscreenPoints[10].y);
    
this.lineTo(screenPoints[5].xscreenPoints[5].y);
    
this.moveTo(screenPoints[10].xscreenPoints[10].y);
    
this.lineTo(screenPoints[11].xscreenPoints[11].y);
    
this.lineTo(screenPoints[6].xscreenPoints[6].y);
    
this.moveTo(screenPoints[11].xscreenPoints[11].y);
    
this.lineTo(screenPoints[8].xscreenPoints[8].y);
    
this.lineTo(screenPoints[7].xscreenPoints[7].y);
    
this.moveTo(screenPoints[9].xscreenPoints[9].y);
    
this.lineTo(screenPoints[4].xscreenPoints[4].y);
    
this.moveTo(screenPoints[8].xscreenPoints[8].y);
    
this.lineTo(screenPoints[0].xscreenPoints[0].y);
    
this.moveTo(screenPoints[9].xscreenPoints[9].y);
    
this.lineTo(screenPoints[3].xscreenPoints[3].y);
    
    
// Nose
    
this.moveTo(screenPoints[6].xscreenPoints[6].y);
    
this.lineTo(screenPoints[12].xscreenPoints[12].y);
    
this.lineTo(screenPoints[11].xscreenPoints[11].y);
    
this.moveTo(screenPoints[5].xscreenPoints[5].y);
    
this.lineTo(screenPoints[12].xscreenPoints[12].y);
    
this.lineTo(screenPoints[10].xscreenPoints[10].y);
    
    
// Tail
    
this.moveTo(screenPoints[13].xscreenPoints[13].y);
    
this.lineTo(screenPoints[14].xscreenPoints[14].y);
    
this.lineTo(screenPoints[15].xscreenPoints[15].y);
    
this.lineTo(screenPoints[16].xscreenPoints[16].y);
    
this.lineTo(screenPoints[14].xscreenPoints[14].y);
    
this.moveTo(screenPoints[16].xscreenPoints[16].y);
    
this.lineTo(screenPoints[13].xscreenPoints[13].y);
}

// The points that make up the ship.
shipArray = [
    
// Ship points
    
CreateVector(-20, -35, -25),
    
CreateVector(-10, -40, -20),
    
CreateVector(10, -40, -20),
    
CreateVector(20, -35, -25),
    
CreateVector(10, -4020),
    
CreateVector(10, -4030),
    
CreateVector(-10, -4030),
    
CreateVector(-10, -4020),
    
CreateVector(-10, -50, -20),
    
CreateVector(10, -50, -20),
    
CreateVector(10, -5030),
    
CreateVector(-10, -5030),
    
CreateVector(0, -4550),
    
CreateVector(-5, -50, -25),
    
CreateVector(0, -60, -25),
    
CreateVector(5, -50, -25),
    
CreateVector(0, -5020),
    
CreateVector(0, -5040// Center of ship, reference point.
];

// Add some asteroids and particles to the scene.
for (05i++)
{    
    
scene.obj scene.attachMovie("asteroid1""asteroid1_"+iscene.depth++);
    
scene.obj.randRange(-200200);
    
scene.obj.randRange(-250250);
    
scene.obj.550;
    
scene.obj.velocity randRange(520);
    
scene.obj.display drawAsteroid;
    
scene.objects.push(scene.obj);
}
for (
03i++)
{    
    
scene.obj scene.attachMovie("asteroid2""asteroid2_"+iscene.depth++);
    
scene.obj.randRange(-200200);
    
scene.obj.randRange(-250250);
    
scene.obj.550;
    
scene.obj.velocity randRange(520);
    
scene.obj.display drawAsteroid;
    
scene.objects.push(scene.obj);
}
for ( 
i05i++)
{    
    
scene.obj scene.attachMovie("asteroid3""asteroid3_"+iscene.depth++);
    
scene.obj.randRange(-200200);
    
scene.obj.randRange(-250250);
    
scene.obj.550;
    
scene.obj.velocity randRange(520);
    
scene.obj.display drawAsteroid;
    
scene.objects.push(scene.obj);
}
for ( 
i050i++)
{    
    
scene.obj scene.attachMovie("particle""particle"+iscene.depth++);
    
scene.obj.randRange(-200200);
    
scene.obj.randRange(-250250);
    
scene.obj.randRange(-200550);
    
scene.obj.velocity randRange(520);
    
scene.obj.display drawAsteroid;
    
scene.objects.push(scene.obj);
}

// Create the ship and add it to the scene.
ship scene.createEmptyMovieClip("ship"1);
ship.loc shipArray[17];
ship.display drawShip;
scene.objects.push(ship);

RemoveLightningBall = function()
{
    
this._visible false
}

// Do the frame
doframe = function()
{
    
framenumber++
    
bonusCountdown --;
    if(
bonusCountdown == 0)
    {
        
bonusCountdown 200;
        
scene.ball scene.attachMovie("lightning_ball""lightning_ball"+framenumberscene.depth++);
        
scene.ball.randRange(-200200);
        
scene.ball.randRange(-250250);
        
scene.ball.550;
        
scene.ball.display drawAsteroid;
        
scene.ball.remove RemoveLightningBall;
        
scene.objects.push(scene.ball);
    }
    
    
scene.bursterflame scene.attachMovie("redparticle""redparticle"+framenumberscene.depth++);
    
scene.bursterflame.ship.loc.x;
    
scene.bursterflame.ship.loc.y;
    
scene.bursterflame.ship.loc.50;
    
scene.bursterflame.display drawBursterFlame;
    
scene.bursterflames.push(scene.bursterflame);
    
    for(
0scene.objects.lengthi++)
    {
        
currentObject scene.objects[i];
        
currentObject.display();
        if(
currentObject._name.substr(08) == "asteroid")
        {
            
// Check for collisions with the asteroids.
            
if(VectorLenght(CreateVector(ship.loc.xship.loc.y
                
ship.loc.z), CreateVector(currentObject.x,
                
currentObject.ycurrentObject.z)) < 40)
            {
                
finalBonusPoints "You got " bonusPointsCollected " bonus points";
                
finalscore "Your final score: " + (framenumber bonusPointsCollected);
                if((
framenumber bonusPointsCollected) < 50)
                    
result "Were you sleeping?";
                else if((
framenumber bonusPointsCollected) < 200)
                    
result "Come on! You can do better!";
                else if((
framenumber bonusPointsCollected) < 500)
                    
result "Well, not all that bad...";
                else if((
framenumber bonusPointsCollected) < 1000)
                    
result "That was actually pretty good!";
                else if((
framenumber bonusPointsCollected) < 1500)
                    
result "You are really good at this!";
                else if((
framenumber bonusPointsCollected) < 2000)
                    
result "You just keep amazing me...";
                else
                    
result "Did you find some way to cheat?";
                
backgroundSound.stop();
                
crashSound = new Sound(this);
                
crashSound.attachSound("crashmusic");
                
crashSound.start();
                
gotoAndStop(3);
                for(
0scene.objects.lengthj++)
                    
scene.removeMovieClip(scene.objects.pop());
                
scene.onEnterFrame null;
            }
        }
        else if(
currentObject._name.substr(05) == "light")
        {
            if(
VectorLenght(CreateVector(ship.loc.xship.loc.y
                
ship.loc.z), CreateVector(currentObject.x,
                
currentObject.ycurrentObject.z)) < 40)
            {
                
currentObject.remove();
                
scene.objects.splice(i1);
                
bonusPointsCollected += 100;
                
ballSound = new Sound(this);
                
ballSound.attachSound("lightning_ball_sound");
                
ballSound.start();
            }
        }
    }
    for(
0scene.bursterflames.lengthi++)
    {
        
scene.bursterflames[i].display();
    }
}

scene.onEnterFrame doframe;
stop();

// This line is also needed to make the coloring work.
?>