#include_function "lib\Ikareimu\System\Polarity.dnh"
#include_function "lib\Ikareimu\System\Power.dnh"
#include_function "lib\Ikareimu\System\Chaining.dnh"
/******************************************
* Variables                               *
******************************************/

let SystemBase;
let LifeCounter;
let Score1;
let Score10;
let Score100;
let Score1000;
let Score10000;
let Score100000;
let Score1000000;
let Score10000000;
let Score100000000;
let Score1000000000;

let ChargeCounter1;
let ChargeCounter2;
let ChargeCounter3;
let ChargeCounter4;
let ChargeCounter5;
let ChargeCounter6;
let ChargeCounter7;
let ChargeCounter8;
let ChargeCounter9;
let ChargeCounter10;
let ChargeCounter11;
let ChargeCounter12;
let ChargeFlash = 1;

let ChainCounter1;
let ChainCounter2;
let ChainCountLast = 0;
let ChainPolarityLast = NONE;

let imgSystemBase   = "lib\Ikareimu\img\system.png";
let imgNumbers      = "lib\Ikareimu\img\numbers.png";
let imgPowerCharge  = "lib\Ikareimu\img\charge.png";
let imgChainCounter = "lib\Ikareimu\img\chaincounter.png";

let ObjectsCreated = false;

/******************************************
* Public  Functions                       *
******************************************/

// Run this once and only once in the @MainLoop,
// not @Initialize.
sub System_Initialize {
  LoadGraphic(imgNumbers);
  LoadGraphic(imgSystemBase);
  LoadGraphic(imgPowerCharge);
  LoadGraphic(imgChainCounter);
  SetRateScoreSystemEnable(false);
  SetDefaultStatusVisible(false);
}


// Run this once a frame in the @MainLoop.
sub System_Run {
  if(!ObjectsCreated){
    System_createBase;
    System_createScore;
    System_createLife;
    System_createCharge;
    System_createChain;
    ObjectsCreated = true;
  }
  System_updateLife;
  System_updateScore;
  System_updateCharge;
  System_updateChain;
}

/******************************************
* Private Functions                       *
******************************************/

sub System_createBase {
  SystemBase = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(SystemBase, imgSystemBase);
  ObjEffect_SetLayer(SystemBase, 8);
  ObjEffect_SetPrimitiveType(SystemBase, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(SystemBase, 4);
  ObjEffect_SetVertexXY(SystemBase, 0, GetClipMaxX+18, GetClipMinY);
  ObjEffect_SetVertexXY(SystemBase, 1, GetClipMaxX+86, GetClipMinY);
  ObjEffect_SetVertexXY(SystemBase, 2, GetClipMaxX+86, GetClipMinY+444);
  ObjEffect_SetVertexXY(SystemBase, 3, GetClipMaxX+18, GetClipMinY+444);
  ObjEffect_SetVertexUV(SystemBase, 0, 0, 0);
  ObjEffect_SetVertexUV(SystemBase, 1, 68, 0);
  ObjEffect_SetVertexUV(SystemBase, 2, 68, 444);
  ObjEffect_SetVertexUV(SystemBase, 3, 0, 444);
}

sub System_createScore {
  Score1 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score1, imgNumbers);
  ObjEffect_SetLayer(Score1, 8);
  ObjEffect_SetPrimitiveType(Score1, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score1, 4);
  
  Score10 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score10, imgNumbers);
  ObjEffect_SetLayer(Score10, 8);
  ObjEffect_SetPrimitiveType(Score10, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score10, 4);
  
  Score100 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score100, imgNumbers);
  ObjEffect_SetLayer(Score100, 8);
  ObjEffect_SetPrimitiveType(Score100, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score100, 4);
  
  Score1000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score1000, imgNumbers);
  ObjEffect_SetLayer(Score1000, 8);
  ObjEffect_SetPrimitiveType(Score1000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score1000, 4);
  
  Score10000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score10000, imgNumbers);
  ObjEffect_SetLayer(Score10000, 8);
  ObjEffect_SetPrimitiveType(Score10000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score10000, 4);
  
  Score100000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score100000, imgNumbers);
  ObjEffect_SetLayer(Score100000, 8);
  ObjEffect_SetPrimitiveType(Score100000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score100000, 4);
  
  Score1000000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score1000000, imgNumbers);
  ObjEffect_SetLayer(Score1000000, 8);
  ObjEffect_SetPrimitiveType(Score1000000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score1000000, 4);
  
  Score10000000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score10000000, imgNumbers);
  ObjEffect_SetLayer(Score10000000, 8);
  ObjEffect_SetPrimitiveType(Score10000000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score10000000, 4);
  
  Score100000000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score100000000, imgNumbers);
  ObjEffect_SetLayer(Score100000000, 8);
  ObjEffect_SetPrimitiveType(Score100000000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score100000000, 4);
  
  Score1000000000 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(Score1000000000, imgNumbers);
  ObjEffect_SetLayer(Score1000000000, 8);
  ObjEffect_SetPrimitiveType(Score1000000000, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(Score1000000000, 4);
}

sub System_updateScore {
  let num2 = GetScore;
  let num1 = floor(num2/1000000000);
  System_setNumberXY(Score1000000000, num1, GetClipMaxX+18, GetClipMinY+18);
  num2 -= num1*1000000000;
  num1 = floor(num2/100000000);
  System_setNumberXY(Score100000000, num1, GetClipMaxX+34, GetClipMinY+18);
  num2 -= num1*100000000;
  num1 = floor(num2/10000000);
  System_setNumberXY(Score10000000, num1, GetClipMaxX+50, GetClipMinY+18);
  num2 -= num1*10000000;
  num1 = floor(num2/1000000);
  System_setNumberXY(Score1000000, num1, GetClipMaxX+66, GetClipMinY+18);
  num2 -= num1*1000000;
  num1 = floor(num2/100000);
  System_setNumberXY(Score100000, num1, GetClipMaxX+82, GetClipMinY+18);
  num2 -= num1*100000;
  num1 = floor(num2/10000);
  System_setNumberXY(Score10000, num1, GetClipMaxX+98, GetClipMinY+18);
  num2 -= num1*10000;
  num1 = floor(num2/1000);
  System_setNumberXY(Score1000, num1, GetClipMaxX+114, GetClipMinY+18);
  num2 -= num1*1000;
  num1 = floor(num2/100);
  System_setNumberXY(Score100, num1, GetClipMaxX+130, GetClipMinY+18);
  num2 -= num1*100;
  num1 = floor(num2/10);
  System_setNumberXY(Score10, num1, GetClipMaxX+146, GetClipMinY+18);
  num2 -= num1*10;
  num1 = floor(num2);
  System_setNumberXY(Score1, num1, GetClipMaxX+162, GetClipMinY+18);
}

sub System_createLife {
  LifeCounter = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(LifeCounter, imgNumbers);
  ObjEffect_SetLayer(LifeCounter, 8);
  ObjEffect_SetPrimitiveType(LifeCounter, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(LifeCounter, 4);
}

sub System_updateLife {
  System_setNumberXY(LifeCounter, GetPlayerLife-1, GetClipMaxX+50, GetClipMaxY-16);
}

sub System_createCharge {
  ChargeCounter1 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter1, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter1, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter1, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter1, 4);
  
  ChargeCounter2 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter2, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter2, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter2, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter2, 4);
  
  ChargeCounter3 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter3, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter3, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter3, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter3, 4);
  
  ChargeCounter4 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter4, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter4, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter4, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter4, 4);
  
  ChargeCounter5 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter5, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter5, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter5, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter5, 4);
  
  ChargeCounter6 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter6, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter6, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter6, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter6, 4);
  
  ChargeCounter7 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter7, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter7, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter7, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter7, 4);
  
  ChargeCounter8 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter8, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter8, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter8, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter8, 4);
  
  ChargeCounter9 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter9, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter9, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter9, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter9, 4);
  
  ChargeCounter10 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter10, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter10, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter10, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter10, 4);
  
  ChargeCounter11 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter11, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter11, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter11, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter11, 4);
  
  ChargeCounter12 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChargeCounter12, imgPowerCharge);
  ObjEffect_SetLayer(ChargeCounter12, 8);
  ObjEffect_SetPrimitiveType(ChargeCounter12, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChargeCounter12, 4);
}

sub System_updateCharge {
  ChargeFlash++;
  let pow = GetPower;
  
  if(pow < 10){
    let chargepixels = floor(pow/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter1, GetClipMaxX+18, GetClipMaxY-38, chargepixels);
  }
  else if(pow >=10){
    System_fullCharge(ChargeCounter1, GetClipMaxX+18, GetClipMaxY-38);
  }
  
  if(pow<20){
    let chargepixels = floor((pow-10)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter2, GetClipMaxX+18, GetClipMaxY-54, chargepixels);
  }
  else if(pow>=20){
    System_fullCharge(ChargeCounter2, GetClipMaxX+18, GetClipMaxY-54);
  }
  
  if(pow<30){
    let chargepixels = floor((pow-20)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter3, GetClipMaxX+18, GetClipMaxY-70, chargepixels);
  }
  else if(pow>=30){
    System_fullCharge(ChargeCounter3, GetClipMaxX+18, GetClipMaxY-70);
  }
  
  if(pow<40){
    let chargepixels = floor((pow-30)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter4, GetClipMaxX+18, GetClipMaxY-86, chargepixels);
  }
  else if(pow>=40){
    System_fullCharge(ChargeCounter4, GetClipMaxX+18, GetClipMaxY-86);
  }
  
  if(pow<50){
    let chargepixels = floor((pow-40)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter5, GetClipMaxX+18, GetClipMaxY-102, chargepixels);
  }
  else if(pow>=50){
    System_fullCharge(ChargeCounter5, GetClipMaxX+18, GetClipMaxY-102);
  }
  
  if(pow<60){
    let chargepixels = floor((pow-50)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter6, GetClipMaxX+18, GetClipMaxY-118, chargepixels);
  }
  else if(pow>=60){
    System_fullCharge(ChargeCounter6, GetClipMaxX+18, GetClipMaxY-118);
  }
  
  if(pow<70){
    let chargepixels = floor((pow-60)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter7, GetClipMaxX+18, GetClipMaxY-134, chargepixels);
  }
  else if(pow>=70){
    System_fullCharge(ChargeCounter7, GetClipMaxX+18, GetClipMaxY-134);
  }
  
  if(pow<80){
    let chargepixels = floor((pow-70)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter8, GetClipMaxX+18, GetClipMaxY-150, chargepixels);
  }
  else if(pow>=80){
    System_fullCharge(ChargeCounter8, GetClipMaxX+18, GetClipMaxY-150);
  }
  
  if(pow<90){
    let chargepixels = floor((pow-80)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter9, GetClipMaxX+18, GetClipMaxY-166, chargepixels);
  }
  else if(pow>=90){
    System_fullCharge(ChargeCounter9, GetClipMaxX+18, GetClipMaxY-166);
  }
  
  if(pow<100){
    let chargepixels = floor((pow-90)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter10, GetClipMaxX+18, GetClipMaxY-182, chargepixels);
  }
  else if(pow>=100){
    System_fullCharge(ChargeCounter10, GetClipMaxX+18, GetClipMaxY-182);
  }
  
  if(pow<110){
    let chargepixels = floor((pow-100)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter11, GetClipMaxX+18, GetClipMaxY-198, chargepixels);
  }
  else if(pow>=110){
    System_fullCharge(ChargeCounter11, GetClipMaxX+18, GetClipMaxY-198);
  }
  
  if(pow<120){
    let chargepixels = floor((pow-110)/10*17);
    if(chargepixels < 0){
      chargepixels = 0;
    }
    System_partialCharge(ChargeCounter12, GetClipMaxX+18, GetClipMaxY-214, chargepixels);
  }
  else if(pow>=120){
    System_fullCharge(ChargeCounter12, GetClipMaxX+18, GetClipMaxY-214);
  }
  
  
}

function System_partialCharge(obj, x, y, charge){
  ObjEffect_SetVertexXY(obj, 0, x, y+18-charge);
  ObjEffect_SetVertexXY(obj, 1, x+15, y+18-charge);
  ObjEffect_SetVertexXY(obj, 2, x+15, y+18);
  ObjEffect_SetVertexXY(obj, 3, x, y+18);
  if(ChargeFlash%6 == 3){
    if(GetPolarity == RED){
      ObjEffect_SetVertexUV(obj, 0, 0, 18-charge);
      ObjEffect_SetVertexUV(obj, 1, 15, 18-charge);
      ObjEffect_SetVertexUV(obj, 2, 15, 18);
      ObjEffect_SetVertexUV(obj, 3, 0, 18);
    }
    else if (GetPolarity == BLUE){
      ObjEffect_SetVertexUV(obj, 0, 0, 36-charge);
      ObjEffect_SetVertexUV(obj, 1, 15, 36-charge);
      ObjEffect_SetVertexUV(obj, 2, 15, 36);
      ObjEffect_SetVertexUV(obj, 3, 0, 36);
    }
  }
  else if(ChargeFlash%6 == 0){
    if(GetPolarity == RED){
      ObjEffect_SetVertexUV(obj, 0, 15, 18-charge);
      ObjEffect_SetVertexUV(obj, 1, 30, 18-charge);
      ObjEffect_SetVertexUV(obj, 2, 30, 18);
      ObjEffect_SetVertexUV(obj, 3, 15, 18);
    }
    else if(GetPolarity == BLUE){
      ObjEffect_SetVertexUV(obj, 0, 15, 36-charge);
      ObjEffect_SetVertexUV(obj, 1, 30, 36-charge);
      ObjEffect_SetVertexUV(obj, 2, 30, 36);
      ObjEffect_SetVertexUV(obj, 3, 15, 36);
    }
  }
}

function System_fullCharge(obj, x, y){
  ObjEffect_SetVertexXY(obj, 0, x, y);
  ObjEffect_SetVertexXY(obj, 1, x+15, y);
  ObjEffect_SetVertexXY(obj, 2, x+15, y+18);
  ObjEffect_SetVertexXY(obj, 3, x, y+18);
  if(ChargeFlash%6 == 3){
    if(GetPolarity == RED){
      ObjEffect_SetVertexUV(obj, 0, 0, 0);
      ObjEffect_SetVertexUV(obj, 1, 15, 0);
      ObjEffect_SetVertexUV(obj, 2, 15, 18);
      ObjEffect_SetVertexUV(obj, 3, 0, 18);
    }
    else if(GetPolarity == BLUE){
      ObjEffect_SetVertexUV(obj, 0, 0, 18);
      ObjEffect_SetVertexUV(obj, 1, 15, 18);
      ObjEffect_SetVertexUV(obj, 2, 15, 36);
      ObjEffect_SetVertexUV(obj, 3, 0, 36);
    }
  }
  else if(ChargeFlash%6 == 0){
    if(GetPolarity == RED){
      ObjEffect_SetVertexUV(obj, 0, 15, 0);
      ObjEffect_SetVertexUV(obj, 1, 30, 0);
      ObjEffect_SetVertexUV(obj, 2, 30, 18);
      ObjEffect_SetVertexUV(obj, 3, 15, 18);
    }
    else if(GetPolarity == BLUE){
      ObjEffect_SetVertexUV(obj, 0, 15, 18);
      ObjEffect_SetVertexUV(obj, 1, 30, 18);
      ObjEffect_SetVertexUV(obj, 2, 30, 36);
      ObjEffect_SetVertexUV(obj, 3, 15, 36);
    }
  }
}

sub System_createChain {
  ChainCounter1 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChainCounter1, imgChainCounter);
  ObjEffect_SetLayer(ChainCounter1, 8);
  ObjEffect_SetPrimitiveType(ChainCounter1, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounter1, 4);
  ObjEffect_SetVertexXY(ChainCounter1, 0, GetClipMaxX+21, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounter1, 1, GetClipMaxX+39, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounter1, 2, GetClipMaxX+39, GetClipMinY+78);
  ObjEffect_SetVertexXY(ChainCounter1, 3, GetClipMaxX+21, GetClipMinY+78);
  
  ChainCounter2 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChainCounter2, imgChainCounter);
  ObjEffect_SetLayer(ChainCounter2, 8);
  ObjEffect_SetPrimitiveType(ChainCounter2, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounter2, 4);
  ObjEffect_SetVertexXY(ChainCounter2, 0, GetClipMaxX+43, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounter2, 1, GetClipMaxX+61, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounter2, 2, GetClipMaxX+61, GetClipMinY+78);
  ObjEffect_SetVertexXY(ChainCounter2, 3, GetClipMaxX+43, GetClipMinY+78);
}

sub System_updateChain {
  let ChainUVx1;
  let ChainUVx2;
  let ChainUVy1;
  let ChainUVy2;
  if(Chain_GetInfo(CHAIN_POLARITY) == RED){
    ChainUVy1 = 0;
    ChainUVy2 = 18;
  }
  else if(Chain_GetInfo(CHAIN_POLARITY) == BLUE){
    ChainUVy1 = 18;
    ChainUVy2 = 36;
  }
  else if(Chain_GetInfo(CHAIN_POLARITY) == NONE){
    ChainUVy1 = 0;
    ChainUVy2 = 0;
  }
  if(Chain_GetInfo(CHAIN_COUNT)>= 1){
    ChainUVx1 = 0;
    ChainUVx2 = 18;
  }
  else {
    ChainUVx1 = 0;
    ChainUVx2 = 0;
  }
  ObjEffect_SetVertexUV(ChainCounter1, 0, ChainUVx1, ChainUVy1);
  ObjEffect_SetVertexUV(ChainCounter1, 1, ChainUVx2, ChainUVy1);
  ObjEffect_SetVertexUV(ChainCounter1, 2, ChainUVx2, ChainUVy2);
  ObjEffect_SetVertexUV(ChainCounter1, 3, ChainUVx1, ChainUVy2);
  
  if(Chain_GetInfo(CHAIN_COUNT)>= 2){
    ChainUVx1 = 0;
    ChainUVx2 = 18;
  }
  else {
    ChainUVx1 = 0;
    ChainUVx2 = 0;
  }
  ObjEffect_SetVertexUV(ChainCounter2, 0, ChainUVx1, ChainUVy1);
  ObjEffect_SetVertexUV(ChainCounter2, 1, ChainUVx2, ChainUVy1);
  ObjEffect_SetVertexUV(ChainCounter2, 2, ChainUVx2, ChainUVy2);
  ObjEffect_SetVertexUV(ChainCounter2, 3, ChainUVx1, ChainUVy2);
  
  if((ChainCountLast!=1||ChainPolarityLast!=Chain_GetInfo(CHAIN_POLARITY)) && Chain_GetInfo(CHAIN_COUNT)==1){
    System_ChainBurst(GetClipMaxX+30, Chain_GetInfo(CHAIN_POLARITY));
  }
  if((ChainCountLast!=2||ChainPolarityLast!=Chain_GetInfo(CHAIN_POLARITY)) && Chain_GetInfo(CHAIN_COUNT)==2){
    System_ChainBurst(GetClipMaxX+52, Chain_GetInfo(CHAIN_POLARITY));
  }
  if(ChainCountLast!=0 && Chain_GetInfo(CHAIN_COUNT)==0){
    System_ChainFlash(Chain_GetInfo(CHAIN_POLARITY));
  }
  ChainCountLast = Chain_GetInfo(CHAIN_COUNT);
  ChainPolarityLast = Chain_GetInfo(CHAIN_POLARITY);
}

task System_ChainBurst(x, polarity) {
  let ChainCounterBurst = Obj_Create(OBJ_EFFECT);
  let y = GetClipMinY+69;
  ObjEffect_SetTexture(ChainCounterBurst, imgChainCounter);
  ObjEffect_SetLayer(ChainCounterBurst, 8);
  ObjEffect_SetPrimitiveType(ChainCounterBurst, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounterBurst, 4);
  if(polarity == RED){
    ObjEffect_SetVertexUV(ChainCounterBurst, 0, 18, 0);
    ObjEffect_SetVertexUV(ChainCounterBurst, 1, 36, 0);
    ObjEffect_SetVertexUV(ChainCounterBurst, 2, 36, 18);
    ObjEffect_SetVertexUV(ChainCounterBurst, 3, 18, 18);
  }
  if(polarity == BLUE){
    ObjEffect_SetVertexUV(ChainCounterBurst, 0, 18, 18);
    ObjEffect_SetVertexUV(ChainCounterBurst, 1, 36, 18);
    ObjEffect_SetVertexUV(ChainCounterBurst, 2, 36, 36);
    ObjEffect_SetVertexUV(ChainCounterBurst, 3, 18, 36);
  }
  ascent(i in 0..3) {
    ObjEffect_SetVertexXY(ChainCounterBurst, 0, x+i*5, y-i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 1, x-i*5, y-i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 2, x-i*5, y+i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 3, x+i*5, y+i*5);
    yield ;
  }
  descent(i in 1..4) {
    ObjEffect_SetVertexXY(ChainCounterBurst, 0, x+i*5, y-i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 1, x-i*5, y-i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 2, x-i*5, y+i*5);
    ObjEffect_SetVertexXY(ChainCounterBurst, 3, x+i*5, y+i*5);
    yield ;
  }
  Obj_Delete(ChainCounterBurst);
}

task System_ChainFlash(polarity) {  
  let ChainCounterFlash1 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChainCounterFlash1, imgChainCounter);
  ObjEffect_SetLayer(ChainCounterFlash1, 8);
  ObjEffect_SetPrimitiveType(ChainCounterFlash1, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounterFlash1, 4);
  ObjEffect_SetVertexXY(ChainCounterFlash1, 0, GetClipMaxX+21, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash1, 1, GetClipMaxX+39, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash1, 2, GetClipMaxX+39, GetClipMinY+78);
  ObjEffect_SetVertexXY(ChainCounterFlash1, 3, GetClipMaxX+21, GetClipMinY+78);
  
  let ChainCounterFlash2 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChainCounterFlash2, imgChainCounter);
  ObjEffect_SetLayer(ChainCounterFlash2, 8);
  ObjEffect_SetPrimitiveType(ChainCounterFlash2, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounterFlash2, 4);
  ObjEffect_SetVertexXY(ChainCounterFlash2, 0, GetClipMaxX+43, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash2, 1, GetClipMaxX+61, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash2, 2, GetClipMaxX+61, GetClipMinY+78);
  ObjEffect_SetVertexXY(ChainCounterFlash2, 3, GetClipMaxX+43, GetClipMinY+78);
  
  let ChainCounterFlash3 = Obj_Create(OBJ_EFFECT);
  ObjEffect_SetTexture(ChainCounterFlash3, imgChainCounter);
  ObjEffect_SetLayer(ChainCounterFlash3, 8);
  ObjEffect_SetPrimitiveType(ChainCounterFlash3, PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(ChainCounterFlash3, 4);
  ObjEffect_SetVertexXY(ChainCounterFlash3, 0, GetClipMaxX+65, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash3, 1, GetClipMaxX+83, GetClipMinY+60);
  ObjEffect_SetVertexXY(ChainCounterFlash3, 2, GetClipMaxX+83, GetClipMinY+78);
  ObjEffect_SetVertexXY(ChainCounterFlash3, 3, GetClipMaxX+65, GetClipMinY+78);
  
  System_ChainBurst(GetClipMaxX+74, polarity);
  
  let ChainCounterFlash = 0;
  while (ChainCounterFlash<60 && Chain_GetInfo(CHAIN_COUNT)==0){
    if(ChainCounterFlash%4==0){
      if(polarity==RED){
        ObjEffect_SetVertexUV(ChainCounterFlash1, 0, 0, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 1, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 2, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 3, 0, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 0, 0, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 1, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 2, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 3, 0, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 0, 0, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 1, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 2, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 3, 0, 18);
      }
      if(polarity==BLUE){
        ObjEffect_SetVertexUV(ChainCounterFlash1, 0, 0, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 1, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 2, 18, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 3, 0, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 0, 0, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 1, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 2, 18, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 3, 0, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 0, 0, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 1, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 2, 18, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 3, 0, 36);
      }
    }
    if(ChainCounterFlash%4==2){
      if(polarity==RED){
        ObjEffect_SetVertexUV(ChainCounterFlash1, 0, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 1, 36, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 2, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 3, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 0, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 1, 36, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 2, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 3, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 0, 18, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 1, 36, 0);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 2, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 3, 18, 18);
      }
      if(polarity==BLUE){
        ObjEffect_SetVertexUV(ChainCounterFlash1, 0, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 1, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 2, 36, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash1, 3, 18, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 0, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 1, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 2, 36, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash2, 3, 18, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 0, 18, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 1, 36, 18);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 2, 36, 36);
        ObjEffect_SetVertexUV(ChainCounterFlash3, 3, 18, 36);
      }
    }
    ChainCounterFlash++;
    OutputDebugString(0, "ChainCounterFlash", ChainCounterFlash);
    yield;
  }
  Obj_Delete(ChainCounterFlash1);
  Obj_Delete(ChainCounterFlash2);
  Obj_Delete(ChainCounterFlash3);
}

function System_setNumberXY(id, num, drawleft, drawtop){
  if(num == 1){
    ObjEffect_SetVertexXY(id, 0, drawleft+5, drawtop);
    ObjEffect_SetVertexXY(id, 1, drawleft+11, drawtop);
    ObjEffect_SetVertexXY(id, 2, drawleft+11, drawtop+12);
    ObjEffect_SetVertexXY(id, 3, drawleft+5, drawtop+12);
    ObjEffect_SetVertexUV(id, 0, 16, 0);
    ObjEffect_SetVertexUV(id, 1, 22, 0);
    ObjEffect_SetVertexUV(id, 2, 22, 12);
    ObjEffect_SetVertexUV(id, 3, 16, 12);
  }
  else {
    ObjEffect_SetVertexXY(id, 0, drawleft, drawtop);
    ObjEffect_SetVertexXY(id, 1, drawleft+16, drawtop);
    ObjEffect_SetVertexXY(id, 2, drawleft+16, drawtop+12);
    ObjEffect_SetVertexXY(id, 3, drawleft, drawtop+12);
    if(num == 0){
      ObjEffect_SetVertexUV(id, 0, 0, 0);
      ObjEffect_SetVertexUV(id, 1, 16, 0);
      ObjEffect_SetVertexUV(id, 2, 16, 12);
      ObjEffect_SetVertexUV(id, 3, 0, 12);
    }
    if(num == 2){
      ObjEffect_SetVertexUV(id, 0, 22, 0);
      ObjEffect_SetVertexUV(id, 1, 38, 0);
      ObjEffect_SetVertexUV(id, 2, 38, 12);
      ObjEffect_SetVertexUV(id, 3, 22, 12);
    }
    if(num == 3){
      ObjEffect_SetVertexUV(id, 0, 38, 0);
      ObjEffect_SetVertexUV(id, 1, 54, 0);
      ObjEffect_SetVertexUV(id, 2, 54, 12);
      ObjEffect_SetVertexUV(id, 3, 38, 12);
    }
    if(num == 4){
      ObjEffect_SetVertexUV(id, 0, 54, 0);
      ObjEffect_SetVertexUV(id, 1, 70, 0);
      ObjEffect_SetVertexUV(id, 2, 70, 12);
      ObjEffect_SetVertexUV(id, 3, 54, 12);
    }
    if(num == 5){
      ObjEffect_SetVertexUV(id, 0, 70, 0);
      ObjEffect_SetVertexUV(id, 1, 86, 0);
      ObjEffect_SetVertexUV(id, 2, 86, 12);
      ObjEffect_SetVertexUV(id, 3, 70, 12);
    }
    if(num == 6){
      ObjEffect_SetVertexUV(id, 0, 86, 0);
      ObjEffect_SetVertexUV(id, 1, 102, 0);
      ObjEffect_SetVertexUV(id, 2, 102, 12);
      ObjEffect_SetVertexUV(id, 3, 86, 12);
    }
    if(num == 7){
      ObjEffect_SetVertexUV(id, 0, 102, 0);
      ObjEffect_SetVertexUV(id, 1, 118, 0);
      ObjEffect_SetVertexUV(id, 2, 118, 12);
      ObjEffect_SetVertexUV(id, 3, 102, 12);
    }
    if(num == 8){
      ObjEffect_SetVertexUV(id, 0, 118, 0);
      ObjEffect_SetVertexUV(id, 1, 134, 0);
      ObjEffect_SetVertexUV(id, 2, 134, 12);
      ObjEffect_SetVertexUV(id, 3, 118, 12);
    }
    if(num == 9){
      ObjEffect_SetVertexUV(id, 0, 134, 0);
      ObjEffect_SetVertexUV(id, 1, 150, 0);
      ObjEffect_SetVertexUV(id, 2, 150, 12);
      ObjEffect_SetVertexUV(id, 3, 134, 12);
    }
  }
}