#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
#include <newwin000.h>
#include <WSDkeyboard.h>
#include <WSCmessageDialog.h>
extern WSClistData invader_list_x;
extern WSClistData invader_list_y;
extern WSClistData fence_list_x;
extern WSClistData fence_list_y;
extern WSClistData invader_missile_list_x;
extern WSClistData invader_missile_list_y;
extern WSClistData my_missile_list_x;
extern WSClistData my_missile_list_y;
extern long        my_base_x;
extern long        my_base_y;

void update_ep(WSCbase* object){
  long num,i;
  num = invader_list_x.getNum();
  if (num == 0){
    newvtim_001->setProperty(WSNrunning,False);
    WSGIappMessageDialog()->setProperty(WSNlabelString,"당신의 이김입니다.");
    WSGIappMessageDialog()->setVisible(True);
  }
  int key = WSGIappKeyboard()->getKey();
  if (WSGIappKeyboard()->isPressed() != False){
    if (key == WSK_Right){
      my_base_x+=5;
      if (my_base_x > 380){
        my_base_x = 380;
      }
    }else if (key == WSK_Left){
      my_base_x-=5;
      if (my_base_x < 0){
        my_base_x = 0;
      }
    }else if (key == WSK_space){
      my_missile_list_x.add((void*)( my_base_x +5 ));
      my_missile_list_y.add((void*)( my_base_y    ));
      WSGIappKeyboard()->setKey(0);
    }
  }
  num = invader_missile_list_x.getNum();
  for(i=0; i<num; i++){
    long mpt_x = (long)invader_missile_list_x[i];
    long mpt_y = (long)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(WSNrunning,False);
      WSGIappMessageDialog()->setProperty(WSNlabelString,"당신이 쳤습니다.");
      WSGIappMessageDialog()->setVisible(True);
      break;
    }
    long fnum = fence_list_x.getNum();
    long j;
    for(j=0; j<fnum; j++){
      long fpt_x = (long)fence_list_x[j];
      long fpt_y = (long)fence_list_y[j];
      if (fpt_x < mpt_x && mpt_x < fpt_x + 5 &&
          fpt_y < mpt_y && mpt_y < fpt_y + 5){
        fence_list_x.delPos(j);
        fence_list_y.delPos(j);
        invader_missile_list_x.delPos(i);
        invader_missile_list_y.delPos(i);
        num--;
        i--;
        break;
      }
    }
  }
  num = my_missile_list_x.getNum();
  for(i=0; i<num; i++){
    long mpt_x = (long)my_missile_list_x[i];
    long mpt_y = (long)my_missile_list_y[i];
    long j;
    long fnum = fence_list_x.getNum();
    for(j=0; j<fnum; j++){
      long fpt_x = (long)fence_list_x[j];
      long fpt_y = (long)fence_list_y[j];
      if (fpt_x < mpt_x && mpt_x < fpt_x + 5 &&
          fpt_y < mpt_y && mpt_y < fpt_y + 5){
        fence_list_x.delPos(j);
        fence_list_y.delPos(j);
        my_missile_list_x.delPos(i);
        my_missile_list_y.delPos(i);
        num--;
        i--;
        break;
      }
    }
    fnum = invader_list_x.getNum();
    for(j=0; j<fnum; j++){
      long fpt_x = (long)invader_list_x[j];
      long fpt_y = (long)invader_list_y[j];
      if (fpt_x < mpt_x && mpt_x < fpt_x + 10 &&
          fpt_y < mpt_y && mpt_y < fpt_y + 10){
        invader_list_x.delPos(j);
        invader_list_y.delPos(j);
        my_missile_list_x.delPos(i);
        my_missile_list_y.delPos(i);
        num--;
        i--;
        long score = newvlab_001->getProperty(WSNlabelString);
        newvlab_001->setProperty(WSNlabelString,score+10);
        break;
      }
    }
  }
  newvdra_001->redraw();
}
static WSCfunctionRegister  op("update_ep",(void*)update_ep);
