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

sub drawep {
  my ($object) = @_;
  $newvdra_001->setForeColor("#000000");
  $newvdra_001->drawRect(0,0,400,400);
  $newvdra_001->setForeColor("#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){
      $newvdra_001->drawImage($pt_x,$pt_y,10,10,"1.xpm",mpfc::WS_CENTER);
    }else{
      $newvdra_001->drawImage($pt_x,$pt_y,10,10,"2.xpm",mpfc::WS_CENTER);
    }
  }

  $newvdra_001->setForeColor("#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];
    $newvdra_001->drawFillRect($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--;
    }
  }

  $newvdra_001->setForeColor("#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];
    $newvdra_001->drawFillRect($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--;
    }
  }

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