#----------------------------------------------------------
#Function for the event procedure
#----------------------------------------------------------
include Mpfc
# Mpfc 를 include 함으로서 Mpfc:: 를 생략해서 쓸스가 있습니다.
# 단 True, False 에대해서는 Ruby 본래의 true, false 와 구별하기위해서
# 명시적으로 Mpfc:: 를 붙여서 써있습니다.

def update_ep(object)
  num = $invader_list_x.length
  if (num == 0)
    $newvtim_001.setProperty("running", Mpfc::False)
    WSGIappMessageDialog().setProperty("labelString", "당신의 이김입니다.")
    WSGIappMessageDialog().setVisible(Mpfc::True)
  end

  key = WSGIappKeyboard().getKey()
  if (WSGIappKeyboard().isPressed() != Mpfc::False)
    case key
    when WSK_Right
      $my_base_x += 5
      if ($my_base_x > 380)
	$my_base_x = 380
      end
    when WSK_Left
      $my_base_x -= 5
      if ($my_base_x < 0)
	$my_base_x = 0
      end
    when WSK_space
      $my_missile_list_x.push($my_base_x + 5)
      $my_missile_list_y.push($my_base_y)
      WSGIappKeyboard().setKey(0)
    end
  end

  num = $invader_missile_list_x.length
  # $B6lFy$N:v!#(B되풀이할 변수 i 를 도중에서 사용할경우에,
  # for 나 times 에서는 생각대로 안움직였기에 while 로 대용하고있습니다.
  i = 0
  while (i < num)
    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) then
      $newvtim_001.setProperty("running", Mpfc::False)
      WSGIappMessageDialog().setProperty("labelString", "당신은 쳤습니다.")
      WSGIappMessageDialog().setVisible(Mpfc::True)
      break
    end

    fnum = $fence_list_x.length
    fnum.times { |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) then
	$fence_list_x.delete_at(j)
	$fence_list_y.delete_at(j)
	$invader_missile_list_x.delete_at(i)
	$invader_missile_list_y.delete_at(i)
	num -= 1
	i -= 1
	break
      end
    }
    i += 1
  end

  num = $my_missile_list_x.length
  i = 0
  while (i < num)
    mpt_x = $my_missile_list_x[i]
    mpt_y = $my_missile_list_y[i]
    fnum = $fence_list_x.length
    fnum.times { |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) then
	$fence_list_x.delete_at(j)
	$fence_list_y.delete_at(j)
	$my_missile_list_x.delete_at(i)
	$my_missile_list_y.delete_at(i)
	num -= 1
	i -= 1
	break
      end
    }
    fnum = $invader_list_x.length
    fnum.times { |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) then
	$invader_list_x.delete_at(j)
	$invader_list_y.delete_at(j)
	$my_missile_list_x.delete_at(i)
	$my_missile_list_y.delete_at(i)
	num -= 1
	i -= 1
	score = $newvlab_001.getProperty("labelString").getLong()
	$newvlab_001.setProperty("labelString", (score + 10).to_s)
	break
      end
    }
    i += 1
  end

  $newvdra_001.redraw()
end
