script_enemy_main {
  #include_function ".\CommonFunctions.dnh"
  
  let imgDeathWave = GetCurrentScriptDirectory ~ "system\deathwave.png";
  let imgDeathParticle = GetCurrentScriptDirectory ~ "system\deathparticle.png";
  @Initialize {
  
    LoadGraphic(imgDeathWave);
    LoadGraphic(imgDeathParticle);
    SetLife(1);
    DeathEffect;
  }
  
  @MainLoop {
    yield;
  }
  
  @DrawLoop {
  }
  
  @Finalize {
    DeleteGraphic(imgDeathWave);
    DeleteGraphic(imgDeathParticle);
  }
  
  task DeathEffect {
    yield;
	loop(30){yield;}
	
	
    loop(8){
      loop(4){
        DeathParticles;
      }
      yield;
    }
    DeathWave;
    loop(140){ yield; }
    VanishEnemy;
    
    task DeathWave {
      let obj = ObjEffect_InitializeRect(imgDeathWave, 6);
      ObjEffect_SetUVRect(obj, 0, 0, 64, 64);
      ObjEffect_SetXYRect(obj, -32, -32, 32, 32);
      Obj_SetPosition(obj, GetX, GetY);
      
      let scale = 0;
      descent(i in 0..90){
        ObjEffect_SetScale(obj, scale, scale);
        ObjEffect_SetColor(obj, 4, i*200/90, 255, 255, 255);
        scale += i/400;
        yield;
      }
      Obj_Delete(obj);
    }
    
    task DeathParticles {
      let obj = ObjEffect_InitializeRect(imgDeathParticle, 6);
      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(0.5, 4);
      let angle = rand(0, 360);
      let scale = rand(5, 8);
      let rotate = rand(0, 360);
      let rotatespeed = rand(-3, 3);
      descent(i in 0..120){
        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/20*i, 255, 255, 255);
        }
        
        rotate += rotatespeed;
        yield;
      }
    }
  }
}