#----------------------------------------------------------
#Function for the event procedure
#----------------------------------------------------------
use mpfc;
$direction = 0;

sub drawep {
  my ($object) = @_;
  mpfc::WSCvdrawingArea_setForeColor($newvdra_001,"#000000");
  mpfc::WSCvdrawingArea_drawRect($newvdra_001,0,0,400,400);
  mpfc::WSCvdrawingArea_setForeColor($newvdra_001,"#ffffff");

  $most_right = -1;
  $most_left = -1;
  $diff_x = 0;
  $diff_y = 0;

  $num = $#invader_list_x +1;
  for($i=0; $i<$num; $i++){
    $pt_x = $invader_list_x[$i];
    $pt_y = $invader_list_y[$i];
    if ($most_right == -1){
      $most_right = $pt_x;
    }
    if ($most_left == -1){
      $most_left = $pt_x;
    }
    if ($pt_x < $most_left){
      $most_left = $pt_x;
    }
    if ($pt_x > $most_right){
      $most_right = $pt_x;
    }
    if ( rand(200) < 1 ){
      push(@invader_missile_list_x,$pt_x);
      push(@invader_missile_list_y,$pt_y);
    }
  }

  if ($direction == 0){
    $diff_x = 1;
    if ($most_right > 380){
      $direction = 1;
      $diff_x = -1;
      $diff_y = 1;
    }
  }else{
    $diff_x = -1;
    if ($most_left < 20){
      $direction = 0;
      $diff_x = 1;
      $diff_y = 1;
    }
  }

  for($i=0; $i<$num; $i++){
    $invader_list_x[$i] += $diff_x;
    $invader_list_y[$i] += $diff_y;
    $pt_x = $invader_list_x[$i];
    $pt_y = $invader_list_y[$i];
    if (($pt_x/20 + $pt_y/20)%2 != 0){
      mpfc::WSCvdrawingArea_drawImage( $newvdra_001,
                        $pt_x,$pt_y,10,10,"1.xpm",mpfc::WS_CENTER);
    }else{
      mpfc::WSCvdrawingArea_drawImage( $newvdra_001,
                        $pt_x,$pt_y,10,10,"2.xpm",mpfc::WS_CENTER);
    }
  }

  mpfc::WSCvdrawingArea_setForeColor($newvdra_001,"#ff00ff");
  $num = $#invader_missile_list_x +1;
  for($i=0; $i<$num; $i++){
    $pt_x = $invader_missile_list_x[$i];
    $invader_missile_list_y[$i] += 3;
    $pt_y = $invader_missile_list_y[$i];
    mpfc::WSCvdrawingArea_drawFillRect($newvdra_001,$pt_x -2,$pt_y,3,6);
    if ($pt_y > 400){
      splice(@invader_missile_list_x,$i,1);
      splice(@invader_missile_list_y,$i,1);
      $num--;
      $i--;
    }
  }

  mpfc::WSCvdrawingArea_setForeColor($newvdra_001,"#00ff00");
  $num = $#my_missile_list_x +1;
  for($i=0; $i < $num; $i++){
    $pt_x = $my_missile_list_x[$i];
    $my_missile_list_y[$i] -=10;
    $pt_y = $my_missile_list_y[$i];
    mpfc::WSCvdrawingArea_drawFillRect($newvdra_001,$pt_x-2,$pt_y,3,6);
    if ($py_y < 0){
      splice(@my_missile_list_x,$i,1);
      splice(@my_missile_list_y,$i,1);
      $i--;
      $num--;
    }
  }

  mpfc::WSCvdrawingArea_setForeColor($newvdra_001,"#ffff00");
  $num = $#fence_list_x +1;
  for($i=0; $i < $num; $i++){
    $pt_x = $fence_list_x[$i];
    $pt_y = $fence_list_y[$i];
    mpfc::WSCvdrawingArea_drawFillRect($newvdra_001,$pt_x,$pt_y,5,5);
  }
  mpfc::WSCvdrawingArea_drawImage($newvdra_001,
           $my_base_x,$my_base_y,10,10,"base.xpm",mpfc::WS_CENTER);
}
1;
