#----------------------------------------------------------
#Function for the event procedure
#----------------------------------------------------------
RAND_MAX = 0x7fffffff
RIGHT = 0
LEFT  = 1
$direction = RIGHT

def drawep(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.length
  num.times { |i|
    pt_x = $invader_list_x[i]
    pt_y = $invader_list_y[i]
    if (most_right == -1)
      most_right = pt_x
    end
    if (most_left == -1)
      most_left = pt_x
    end
    if (pt_x < most_left)
      most_left = pt_x
    end
    if (pt_x > most_right)
      most_right = pt_x
    end

    if (rand(RAND_MAX) * 200.0 / (RAND_MAX + 1)).to_i == 0
      $invader_missile_list_x.push(pt_x + 5)
      $invader_missile_list_y.push(pt_y)
    end
  }

  if ($direction == RIGHT)
    diff_x = 1
    if (most_right > 380)
      $direction = LEFT
      diff_x = -1
      diff_y = 1
    end
  else
    diff_x = -1
    if (most_left < 20)
      $direction = RIGHT
      diff_x = 1
      diff_y = 1
    end
  end

  num.times { |i|
    pt_x = $invader_list_x[i]
    pt_y = $invader_list_y[i]
    pt_x += diff_x
    pt_y += diff_y
    $invader_list_x[i] = pt_x
    $invader_list_y[i] = pt_y

    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)
    end
  }

  $newvdra_001.setForeColor("#ff00ff")
  num = $invader_missile_list_x.length
  i = 0
  while (i < num)
    pt_x = $invader_missile_list_x[i]
    pt_y = $invader_missile_list_y[i]
    pt_y = pt_y + 3
    $invader_missile_list_y[i] = pt_y
    $newvdra_001.drawFillRect(pt_x - 2, pt_y, 3, 6)
    if (pt_y > 400)
      $invader_missile_list_x.delete_at(i)
      $invader_missile_list_y.delete_at(i)
      num -= 1
      i -= 1
    end
    i += 1
  end

  $newvdra_001.setForeColor("#00ff00")
  num = $my_missile_list_x.length
  i = 0
  while (i < num)
    pt_x = $my_missile_list_x[i]
    pt_y = $my_missile_list_y[i]
    pt_y = pt_y - 10
    $my_missile_list_y[i] = pt_y
    $newvdra_001.drawFillRect(pt_x - 2, pt_y, 3, 6)
    if (pt_y < 0)
      $my_missile_list_x.delete_at(i)
      $my_missile_list_y.delete_at(i)
      num -= 1
      i -= 1
    end
    i += 1
  end

  $newvdra_001.setForeColor("#ffff00")
  num = $fence_list_x.length
  num.times { |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)
end
