#----------------------------------------------------------
#Function for the event procedure
#----------------------------------------------------------
use mpfc;
sub update_ep {
  my ($object) = @_;
  $num = $#invader_list_x +1;
  if ($num == 0){
    $newvtim_001->setProperty("running",False);
    mpfc::WSGIappMessageDialog()->setProperty("labelString",
                              "あなたの勝ちです。");
    mpfc::WSGIappMessageDialog()->setVisible($mpfc::True);
  }
  $key = mpfc::WSGIappKeyboard()->getKey();
  if ( mpfc::WSGIappKeyboard()->isPressed() != False){
    if ($key == $mpfc::WSK_Right){
      $my_base_x+=5;
      if ($my_base_x > 380){
        $my_base_x = 380;
      }
    }elsif ($key == $mpfc::WSK_Left){
      $my_base_x-=5;
      if ($my_base_x < 0){
        $my_base_x = 0;
      }
    }elsif ($key == $mpfc::WSK_space){
      push(@my_missile_list_x, $my_base_x+5);
      push(@my_missile_list_y, $my_base_y);
      mpfc::WSGIappKeyboard()->setKey(0);
    }
  }
  $num = $#invader_missile_list_x +1;
  for($i=0; $i < $num; $i++){
    $mpt_x = $invader_missile_list_x[$i];
    $mpt_y = $invader_missile_list_y[$i];
    if ($my_base_x < $mpt_x && $mpt_x < $my_base_x + 10 &&
        $my_base_y < $mpt_y && $mpt_y < $my_base_y + 10){
      $newvtim_001->setProperty("running",False);
      mpfc::WSGIappMessageDialog()->setProperty("labelString",
                                       "あなたはやられました。");
      mpfc::WSGIappMessageDialog()->setVisible($mpfc::True);
      break;
    }

    $fnum = $#fence_list_x+1;
    for($j=0; $j < $fnum; $j++){
      $fpt_x = $fence_list_x[$j];
      $fpt_y = $fence_list_y[$j];
      if ($fpt_x < $mpt_x && $mpt_x < $fpt_x + 5 &&
          $fpt_y < $mpt_y && $mpt_y < $fpt_y + 5){
        splice(@fence_list_x,$j,1);
        splice(@fence_list_y,$j,1);
        splice(@invader_missile_list_x,$i,1);
        splice(@invader_missile_list_y,$i,1);
        $num--;
        $i--;
        break;
      }
    }
  }
  $num = $#my_missile_list_x +1;
  for($i=0; $i < $num; $i++){
    $mpt_x = $my_missile_list_x[$i];
    $mpt_y = $my_missile_list_y[$i];
    $fnum = $#fence_list_x +1;
    for($j=0; $j < $fnum; $j++){
      $fpt_x = $fence_list_x[$j];
      $fpt_y = $fence_list_y[$j];
      if ($fpt_x < $mpt_x && $mpt_x < $fpt_x + 5 &&
          $fpt_y < $mpt_y && $mpt_y < $fpt_y + 5){
        splice(@fence_list_x,$j,1);
        splice(@fence_list_y,$j,1);
        splice(@my_missile_list_x,$i,1);
        splice(@my_missile_list_y,$i,1);
        $num--;
        $i--;
        break;
      }
    }
    $fnum = $#invader_list_x +1;
    for($j=0; $j < $fnum; $j++){
      $fpt_x = $invader_list_x[$j];
      $fpt_y = $invader_list_y[$j];
      if ($fpt_x < $mpt_x && $mpt_x < $fpt_x + 10 &&
          $fpt_y < $mpt_y && $mpt_y < $fpt_y + 10){
        splice(@invader_list_x,$j,1);
        splice(@invader_list_y,$j,1);
        splice(@my_missile_list_x,$i,1);
        splice(@my_missile_list_y,$i,1);
        $num--;
        $i--;
        $val = $newvlab_001->getProperty("labelString");
        $score = $val->getLong();
        $newvlab_001->setProperty("labelString",int($score + 10));
        break;
      }
    }
  }
  $newvdra_001->redraw();
}
1;
