let Timer = -1;
let imgTimer = GetCurrentScriptDirectory ~ "bosslib\img\timer.png";

task RunTimer {
  DrawTimer;
  
  let realTimer = -1;
  loop {
    if(GetGameSpeed >= 0 && Timer > 0){
      Timer -= 1/(GetGameSpeed+1);
      if(Timer < 0){ Timer = 0; }
    }
    realTimer = Timer/60;
    yield;
  }
  
  task DrawTimer {
    yield;
    let seconds = Obj_Create(OBJ_EFFECT);
    Obj_SetPosition(seconds, GetCenterX+40, GetClipMinY+16);
    ObjEffect_SetTexture(seconds, imgTimer);
    ObjEffect_SetLayer(seconds, 7);
    ObjEffect_SetPrimitiveType(seconds, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_CreateVertex(seconds, 24);
    
    let decimal = Obj_Create(OBJ_EFFECT);
    Obj_SetPosition(decimal, GetCenterX+30, GetClipMinY+30);
    ObjEffect_SetTexture(decimal, imgTimer);
    ObjEffect_SetLayer(decimal, 7);
    ObjEffect_SetPrimitiveType(decimal, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_CreateVertex(decimal, 12);
    ascent(j in 0..4){
      let minx = (j*26)-(4*26);
      ObjEffect_SetVertexXY(seconds, (j*6)  , minx,    0);
      ObjEffect_SetVertexXY(seconds, (j*6)+1, minx,    0);
      ObjEffect_SetVertexXY(seconds, (j*6)+2, minx+32, 0);
      ObjEffect_SetVertexXY(seconds, (j*6)+3, minx,    32);
      ObjEffect_SetVertexXY(seconds, (j*6)+4, minx+32, 32);
      ObjEffect_SetVertexXY(seconds, (j*6)+5, minx+32, 32);
      
      if(j < 2){
        minx = j*13;
        ObjEffect_SetVertexXY(decimal, (j*6)  , minx,    0);
        ObjEffect_SetVertexXY(decimal, (j*6)+1, minx,    0);
        ObjEffect_SetVertexXY(decimal, (j*6)+2, minx+16, 0);
        ObjEffect_SetVertexXY(decimal, (j*6)+3, minx,    16);
        ObjEffect_SetVertexXY(decimal, (j*6)+4, minx+16, 16);
        ObjEffect_SetVertexXY(decimal, (j*6)+5, minx+16, 16);
      }
    }
    ObjEffect_SetVertexUV(seconds, 18, 64, 32);
    ObjEffect_SetVertexUV(seconds, 19, 64, 32);
    ObjEffect_SetVertexUV(seconds, 20, 96, 32);
    ObjEffect_SetVertexUV(seconds, 21, 64, 64);
    ObjEffect_SetVertexUV(seconds, 22, 96, 64);
    ObjEffect_SetVertexUV(seconds, 23, 96, 64);
    loop {
      let digits = [9, 9, 9, 9, 9];
      if(realTimer >=0 ){
        digits = NumberToDigitArray(realTimer);
      }
      ascent(i in 0..3){
        let digit = digits[i];
        let x = digit%8*32;
        let y = floor((digit)/8)*32;
        ObjEffect_SetVertexUV(seconds, (i*6)  , x   , y);
        ObjEffect_SetVertexUV(seconds, (i*6)+1, x   , y);
        ObjEffect_SetVertexUV(seconds, (i*6)+2, x+32, y);
        ObjEffect_SetVertexUV(seconds, (i*6)+3, x   , y+32);
        ObjEffect_SetVertexUV(seconds, (i*6)+4, x+32, y+32);
        ObjEffect_SetVertexUV(seconds, (i*6)+5, x+32, y+32);
        
        if(i < 2){
          digit = digits[i+3];
          x = digit%8*32;
          y = floor(digit/8)*32;
          ObjEffect_SetVertexUV(decimal, (i*6)  , x   , y);
          ObjEffect_SetVertexUV(decimal, (i*6)+1, x   , y);
          ObjEffect_SetVertexUV(decimal, (i*6)+2, x+32, y);
          ObjEffect_SetVertexUV(decimal, (i*6)+3, x   , y+32);
          ObjEffect_SetVertexUV(decimal, (i*6)+4, x+32, y+32);
          ObjEffect_SetVertexUV(decimal, (i*6)+5, x+32, y+32);
        }
      }
      
      if(GetPlayerY <= GetClipMinY+150 && GetPlayerY >=GetClipMinY+50){
        ObjEffect_SetColor(seconds, 24, 255-(GetClipMinY+150 - GetPlayerY), 255, 255, 255);
        ObjEffect_SetColor(decimal, 12, 255-(GetClipMinY+150 - GetPlayerY), 255, 255, 255);
      }
      yield;
    }
  }
  
  function NumberToDigitArray(number){
    let string = ToString(number);
    if(number < 100){ string = "0" ~ string; }
    if(number < 10){ string = "0" ~ string; }
    if(number >= 1000){ return [9, 9, 9, 9, 9]; }
    let digits = [];
    ascent(i in 0..length(string)){
      let char = string[i];
      alternative(char)
      case('0'){digits = digits ~ [0];}
      case('1'){digits = digits ~ [1];}
      case('2'){digits = digits ~ [2];}
      case('3'){digits = digits ~ [3];}
      case('4'){digits = digits ~ [4];}
      case('5'){digits = digits ~ [5];}
      case('6'){digits = digits ~ [6];}
      case('7'){digits = digits ~ [7];}
      case('8'){digits = digits ~ [8];}
      case('9'){digits = digits ~ [9];}
      others{}
    }
    return digits;
    
  }
}

function SetStbTimer(t){
  Timer = t*60;
}