let warningText = GetCurrentScriptDirectory~"img\WarningWords.png";
let warningBox = GetCurrentScriptDirectory~"img\WarningBox.png";
let imgLoadingScreen = "script\Ikareimu\img\LoadingScreen.png";

let bgcounter = 0;
let bgspeed = 0;
let bgshake = false;

function Wait(frames){
  loop(frames){
    yield;
  }
}

sub WaitForZeroEnemy {
  while(GetEnemyNum != 0){
    yield;
  }
}

task CreateLoadingScreen {
  let obj = Obj_Create(OBJ_EFFECT);
  LoadGraphic(imgLoadingScreen);
  ObjEffect_SetTexture(obj, imgLoadingScreen);
  ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_SetLayer(obj, 8);
  ObjEffect_CreateVertex(obj, 4);
  ObjEffect_SetRenderState(obj,ALPHA);
  ObjEffect_SetVertexUV(obj, 0, 0, 0);
  ObjEffect_SetVertexUV(obj, 1, 640, 0);
  ObjEffect_SetVertexUV(obj, 2, 640, 480);
  ObjEffect_SetVertexUV(obj, 3, 0, 480);
  ObjEffect_SetVertexXY(obj, 0, 0, 0);
  ObjEffect_SetVertexXY(obj, 1, 640, 0);
  ObjEffect_SetVertexXY(obj, 2, 640, 480);
  ObjEffect_SetVertexXY(obj, 3, 0, 480);
  SetCommonData("LoadingScreen", obj);
  let alpha = 0;
  loop(20){
    ObjEffect_SetVertexColor(obj, 0, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 1, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 2, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 3, alpha, 255, 255, 255);
    yield ;
    alpha+=13;
  }
  ObjEffect_SetVertexColor(obj, 0, 255, 255, 255, 255);
  ObjEffect_SetVertexColor(obj, 1, 255, 255, 255, 255);
  ObjEffect_SetVertexColor(obj, 2, 255, 255, 255, 255);
  ObjEffect_SetVertexColor(obj, 3, 255, 255, 255, 255);
}


task DeleteLoadingScreen {
  let obj = GetCommonData("LoadingScreen");
  let alpha = 255;
  loop(20){
    ObjEffect_SetVertexColor(obj, 0, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 1, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 2, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(obj, 3, alpha, 255, 255, 255);
    yield ;
    alpha-=13;
  }
  Obj_Delete(obj);
  DeleteGraphic(imgLoadingScreen);
}

task Warning {
  PlaySE("script\Ikareimu\sfx\warning.wav");
  WarningBox;
  let size = 570;
  loop(20){
    WarningText(size, warningText);
    size-=20;
    yield;
  }
  let text = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(text, warningText);
  ObjEffect_SetRenderState(text, ALPHA);
  ObjEffect_SetPrimitiveType(text, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_SetLayer(text, 7);
  ObjEffect_CreateVertex(text, 4);
  ObjEffect_SetVertexUV(text, 0, 0, 0);
  ObjEffect_SetVertexUV(text, 1, 512, 0);
  ObjEffect_SetVertexUV(text, 2, 512, 256);
  ObjEffect_SetVertexUV(text, 3, 0, 256);
  ObjEffect_SetVertexXY(text, 0, GetCenterX-170, GetCenterY-85);
  ObjEffect_SetVertexXY(text, 1, GetCenterX+170, GetCenterY-85);
  ObjEffect_SetVertexXY(text, 2, GetCenterX+170, GetCenterY+85);
  ObjEffect_SetVertexXY(text, 3, GetCenterX-170, GetCenterY+85);
  loop(300){yield;}
  size = 85;
  loop(21){
    ObjEffect_SetVertexXY(text, 0, GetCenterX-170-(85-size)*10, GetCenterY-size);
    ObjEffect_SetVertexXY(text, 1, GetCenterX+170+(85-size)*10, GetCenterY-size);
    ObjEffect_SetVertexXY(text, 2, GetCenterX+170+(85-size)*10, GetCenterY+size);
    ObjEffect_SetVertexXY(text, 3, GetCenterX-170-(85-size)*10, GetCenterY+size);
    size-=4.25;
    yield;
  }
}

task WarningText(size, graphic){
  let text = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(text, graphic);
  ObjEffect_SetRenderState(text, ALPHA);
  ObjEffect_SetPrimitiveType(text, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_SetLayer(text, 7);
  ObjEffect_CreateVertex(text, 4);
  ObjEffect_SetVertexUV(text, 0, 0, 0);
  ObjEffect_SetVertexUV(text, 1, 512, 0);
  ObjEffect_SetVertexUV(text, 2, 512, 256);
  ObjEffect_SetVertexUV(text, 3, 0, 256);
  ObjEffect_SetVertexXY(text, 0, GetCenterX-size, GetCenterY-size/2);
  ObjEffect_SetVertexXY(text, 1, GetCenterX+size, GetCenterY-size/2);
  ObjEffect_SetVertexXY(text, 2, GetCenterX+size, GetCenterY+size/2);
  ObjEffect_SetVertexXY(text, 3, GetCenterX-size, GetCenterY+size/2);
  let alpha = 255;
  loop(51){
    ObjEffect_SetVertexColor(text, 0, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(text, 1, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(text, 2, alpha, 255, 255, 255);
    ObjEffect_SetVertexColor(text, 3, alpha, 255, 255, 255);
    alpha -= 5;
    yield;
  }
  Obj_Delete(text);
  return;
}

task WarningBox {
  let box = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(box, warningBox);
  ObjEffect_SetRenderState(box, ALPHA);
  ObjEffect_SetPrimitiveType(box, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_SetLayer(box, 7);
  ObjEffect_CreateVertex(box, 4);
  ObjEffect_SetVertexUV(box, 0, 0, 0);
  ObjEffect_SetVertexUV(box, 1, 512, 0);
  ObjEffect_SetVertexUV(box, 2, 512, 205);
  ObjEffect_SetVertexUV(box, 3, 0, 205);
  ObjEffect_SetVertexColor(box, 0, 170, 255, 255, 255);
  ObjEffect_SetVertexColor(box, 1, 170, 255, 255, 255);
  ObjEffect_SetVertexColor(box, 2, 170, 255, 255, 255);
  ObjEffect_SetVertexColor(box, 3, 170, 255, 255, 255);
  ObjEffect_SetVertexXY(box, 0, GetClipMinX, GetCenterY-10);
  ObjEffect_SetVertexXY(box, 1, GetClipMaxX, GetCenterY-10);
  ObjEffect_SetVertexXY(box, 2, GetClipMaxX, GetCenterY-10);
  ObjEffect_SetVertexXY(box, 3, GetClipMinX, GetCenterY-10);
  let size = 0;
  loop(46){
    ObjEffect_SetVertexXY(box, 0, GetClipMinX, GetCenterY-10-size);
    ObjEffect_SetVertexXY(box, 1, GetClipMaxX, GetCenterY-10-size);
    ObjEffect_SetVertexXY(box, 2, GetClipMaxX, GetCenterY-10+size);
    ObjEffect_SetVertexXY(box, 3, GetClipMinX, GetCenterY-10+size);
    size+=2;
    yield;
  }
  loop(274){yield;}
  size = 90;
  loop(21){
    ObjEffect_SetVertexXY(box, 0, GetClipMinX, GetCenterY-10-size);
    ObjEffect_SetVertexXY(box, 1, GetClipMaxX, GetCenterY-10-size);
    ObjEffect_SetVertexXY(box, 2, GetClipMaxX, GetCenterY-10+size);
    ObjEffect_SetVertexXY(box, 3, GetClipMinX, GetCenterY-10+size);
    size-=4.5;
    yield;
  }
}

function IntString(number){
  let str = ToString(number);
  let str2 = "";
  let i = 0;
  while (str[i] != '.'){
    str2 = str2 ~ [str[i]];
    i++;
  }
  return(str2); 
}

sub ResetGameState {
  SetCommonData("polarity", 1);
  SetCommonData("ShieldState", 2);
  SetCommonData("power", 0);
  SetCommonData("chaining", "reset");
  SetPlayerX(GetCenterX);
  SetPlayerY(GetClipMaxY-50);
  bgcounter = 0;
}