script_enemy_main {
  #include_function ".\lib\enemy.dnh"
  
  let imgDeathParticle = GetCurrentScriptDirectory ~ "lib\img\maple.png";
  let sfxDeath = GetCurrentScriptDirectory ~ "lib\sfx\death.wav";
  @Initialize {
    LoadGraphic(imgDeathParticle);
    SetLife(1);
    DeathEffect;
  }
  
  @MainLoop {
    yield;
  }
  
  @DrawLoop {
  }
  
  @Finalize {
    DeleteGraphic(imgDeathParticle);
  }
  
  task DeathEffect {
    yield;
    LoadGraphic(imgDeathParticle);
    PlaySE(sfxDeath);
    loop(8){
      loop(5){
        DeathParticles;
      }
      yield;
    }
    loop(140){ yield; }
    VanishEnemy;
    
    task DeathParticles {
      let obj = WrapObj_Create(OBJ_EFFECT);
      ObjEffect_SetTexture(obj, imgDeathParticle);
      ObjEffect_SetLayer(obj, 6);
      ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
      ObjEffect_CreateVertex(obj, 4);
      ObjEffect_SetUVRect(obj, 0, 0, 32, 32);
      ObjEffect_SetXYRect(obj, -16, -16, 16, 16);
      ObjEffect_SetRenderState(obj, ADD);
      Obj_SetPosition(obj, GetX, GetY);
      
      let speed = rand(1, 4);
      let angle = rand(0, 360);
      let scale = rand(3, 8);
      let rotate = rand(0, 360);
      let rotatespeed = rand(-3, 3);
      descent(i in 0..120){
        if(i > 100){
          ObjEffect_SetColor(obj, 4, 255, 255/20*(120-i), 255/20*(120-i), 255/20*(120-i));
        }
        Obj_SetPosition(obj, Obj_GetX(obj)+cos(angle)*speed, Obj_GetY(obj)+sin(angle)*speed);
        ObjEffect_SetAngle(obj, 0, 0, rotate);
        ObjEffect_SetScale(obj, scale, scale);
        
        if(i < 20){
          ObjEffect_SetColor(obj, 4, 255, 255/20*i, 255/20*i, 255/20*i);
        }
        
        rotate += rotatespeed;
        yield;
      }
    }
  }
}