#!/usr/bin/perl -w
use strict;

use SWF qw(:ALL);

## BEGIN STEP 1 ##

# Scaling
SWF::setScale(20);
srand(time());

# Creates the racket model
my $rm = new SWF::Shape();
$rm->setLine(5*20, 207, 96, 0);     # note, had to scale width by hand
$rm->drawLineTo(0, 1);
$rm->setLine(5*20, 255, 200, 0);    # note, had to scale width by hand
$rm->movePenTo(0,1);
$rm->drawLineTo(0, 50);

# Draws the white line at the middle of the screen
my $wl = new SWF::Shape();
$wl->setLine(8*20, 255, 255, 255);  # note, had to scale width by hand
$wl->drawLineTo(0, 480);

# Draws the "grass" border
my $gm = new SWF::Shape();
$gm->setLine(100*20, 48, 96, 48);   # note, had to scale width by hand
$gm->drawLine(640,0);

# Draws the ball
my $bm = new SWF::Shape;
$bm->setLine(12*20, 255, 200, 0);   # note, had to scale width by hand
$bm->drawLine(1,1);

# Creates a font
my $f=new SWF::Font("fonts/arial.fdb");
  
# Writes the score for player
my $sp = new SWF::TextField();
$sp->setBounds(100,30);
$sp->setName("pScore");
$sp->setFont($f);
$sp->setColor(255, 255, 255);
$sp->addString("Player : 0");

# Writes the score for computer
my $sc = new SWF::TextField();
$sc->setBounds(100,30);
$sc->setName("cScore");
$sc->setFont($f);
$sc->setColor(255, 255, 255);
$sc->addString("Computer : 0");

# Write the "signature"
my $signature = new SWF::Text();
$signature->setFont($f);
$signature->setColor(150, 150, 255);
$signature->setHeight(9);
$signature->addString("(c) Armel GRIGNON, october 2001 - See this script at mingshop.arpane.net");

# Now adds the objects to the movie
# Creates the movie and the main sprite
my $m = new SWF::Movie();
$m->setRate(96);
$m->setBackground(207, 96, 0);
$m->setDimension(640,480);

# Adds the two rackets (within sprites)
my $srp = new SWF::Sprite();
my $srp_shape = $srp->add($rm);
$srp->nextFrame();
my $racket_player = $m->add($srp);
$racket_player->moveTo(10, 215);
$racket_player->setName("racketPlayer");
my $src = new SWF::Sprite();
$src->add($rm);
$src->nextFrame();
my $racket_computer = $m->add($src);
$racket_computer->moveTo(630, 215);
$racket_computer->setName("racketComputer");

# Adds the "grass"
my $grass_top = $m->add($gm);
$grass_top->moveTo(0,0);
my $grass_bottom = $m->add($gm);
$grass_bottom->moveTo(0,480);

# Adds the while line
my $whiteline = $m->add($wl);
$whiteline->moveTo(316,0);

# Adds the ball to the movie (within a sprite)
my $sb = new SWF::Sprite();
$sb->add($bm);
$sb->nextFrame();
my $ball = $m->add($sb);
$ball->moveTo(316,240);
$ball->setName("zeBall");

# Adds scores
my $score_player = $m->add($sp);
$score_player->moveTo((320-$f->getWidth("Player : 0")/3), 20);
my $score_computer = $m->add($sc);
$score_computer->moveTo(330, 20);

# Finally, adds signature
my $signit = $m->add($signature);
$signit->moveTo(5, 460);

# Hides the mouse (action script code)
$m->add(new SWF::Action('Mouse.hide();'));

## END   STEP 1 ##

## BEGIN STEP 2 ##

# When the movie loads, sets some variables
$m->add(new SWF::Action("
    // The two first ones will control the ball (going left or right ? going up or down ?)
    xDirection = 1;
    yDirection = 1;
    // The 'accelerator' will increase automatically so that the ball goes faster and faster
    accelerator = 1;
    // What is the angle ? 45 degrees !
    xStep = 1;
    yStep = 2;
    // Sets initial scores and the score to get for winning
    playerScore = 0;
    computerScore = 0;
    points2Win = 3;
    // What is the computer 'max speed' ?
    computerSpeed=4;
  "));

# Two frames : the first one with mouse tracking & computer playing, the second goes previous
# Player racket moves with mouse
$m->nextFrame();
$m->add(new SWF::Action("
    // Player's y is mouse's one
    racketPlayer._y=_ymouse;
    // If the racket is out of the ground, place it
    if (racketPlayer._y<52) {racketPlayer._y=52;}
    if (racketPlayer._y>425-48) {racketPlayer._y=425-48;}

    // Let's make the computer move. It goes to the ball's y, with a computerSpeed speed
    if (zeBall._y>racketComputer._y+35) {racketComputer._y += computerSpeed;}
    if (zeBall._y<racketComputer._y+15) {racketComputer._y -= computerSpeed;}
    // If the racket is out of the ground, place it
    if (racketComputer._y<52) {racketComputer._y=52;}
    if (racketComputer._y>425-48) {racketComputer._y=425-48;}
  "));

$m->nextFrame();

# How does the ball behave ?
$m->add(new SWF::Action("
    // Let's move the ball
    zeBall._x+=xDirection*xStep*accelerator;
    zeBall._y+=yDirection*yStep*accelerator;

    // if the ball hits the top or bottom side of the ground, then bounces
    if (zeBall._y>425) {yDirection = -1;}
    if (zeBall._y<55) {yDirection = 1;}

    // If the ball hits the racket player, bounces
    if (zeBall._x>625 && zeBall._x<640 && zeBall._y>racketComputer._y && zeBall._y<racketComputer._y+50) {
        xDirection = -1;
        // Considering the 'impact' with the racket, won't bounce the same way... let's change the angle
        if (zeBall._y<racketComputer._y+20) {yDirection=-1; yStep=2;}
        if (zeBall._y<racketComputer._y+10) {yDirection=-1; yStep=4;}
        if (zeBall._y>racketComputer._y+30) {yDirection=1; yStep=2;}
        if (zeBall._y>racketComputer._y+40) {yDirection=1; yStep=4;}
        if (zeBall._y>racketComputer._y+20 && zeBall._y<racketComputer._y+30) {yDirection=0;}
    }
    if (zeBall._x<15 && zeBall._x>0 && zeBall._y>racketPlayer._y && zeBall._y<racketplayer._y+50) {
        xDirection = 1;
        // Considering the 'impact' with the racket, won't bounce the same way...
        if (zeBall._y<racketPlayer._y+20) {yDirection=-1; yStep=2;}
        if (zeBall._y<racketPlayer._y+10) {yDirection=-1; yStep=4;}
        if (zeBall._y>racketPlayer._y+30) {yDirection=1; yStep=2;}
        if (zeBall._y>racketPlayer._y+40) {yDirection=1; yStep=4;}
        if (zeBall._y>racketPlayer._y+20 && zeBall._y<racketPlayer._y+30) {yDirection=0;}
    }
    accelerator+=1/2000;
    prevFrame();
    play();
  "));


# What if out of ground ?
$m->add(new SWF::Action("
    // If the computer scores
    if (zeBall._x<-60) {
      // Increase its number of points
      computerScore++;
      // Reset anything for the ball to restart from the center of the ground
      accelerator = 1;
      zeBall._x = 316;
      zeBall._y = 240;
      xDirection = 1;
      yStep=2;
      // Updates the display
      cScore = 'Computer : ' + computerScore;
    }
    // if the player scores, does the 'same'
    if (zeBall._x>700) {
      playerScore++;
      accelerator = 1;
      zeBall._x = 316;
      zeBall._y = 240;
      xDirection = -1;
      yStep=2;
      pScore = 'Player : ' + playerScore;
    }
  ")); 

# What if one wins ?
$m->add(new SWF::Action("
    if (computerScore==points2Win) {gotoFrame(5);} else {
      if (playerScore==points2Win) {gotoFrame(4);}
    }
  "));


$m->nextFrame();

## END   STEP 2 ##

## BEGIN STEP 3 ##

# Let's show the winning message for player
my $wm = new SWF::TextField();
$wm->setFont($f);
$wm->setColor(165, 255, 155);
$wm->setHeight(80);
$wm->addString("You win !");
my $i=$m->add($wm);
# the '1.55' below is manual placing, too complicated to calculate by head ;)
$i->moveTo((640-1.55*$f->getWidth("You win !"))/2, 200);
$m->add(new SWF::Action("stop();"));

$m->nextFrame();
$m->nextFrame();

# Let's show the winning message for player
$i->moveTo(0, -100);           # Hides the message in case player wins !
$wm = new SWF::TextField();
$wm->setFont($f);
$wm->setColor(165, 255, 155);
$wm->setHeight(80);
$wm->addString("Computer wins !");
$i=$m->add($wm);
$i->moveTo((640-1.55*$f->getWidth("Computer wins !"))/2, 200);
$m->add(new SWF::Action("stop();"));

$m->nextFrame(); 

## END   STEP 3 ##

$m->save('ming-pong.swf');




