task CreateStageEnemyTrain(x, y, speedE, angleE) {
let shootSFX4 = GetCurrentScriptDirectory ~ "SFX/ATTACK.wav";
let trainSFX = GetCurrentScriptDirectory ~ "SFX/TRAIN.wav";
let deadSFX = GetCurrentScriptDirectory ~ "SFX/BEAT.mp3";
let animFrame = 0; 
let animFrame2 = 0;
 let Xmin = -200;
    let Xmax = GetStgFrameWidth + 200;
    let Ymin = -32; 
    let Ymax = GetStgFrameHeight + 32;
	let count = 0; 
    
	let objEnemyTrain = ObjEnemy_Create(OBJ_ENEMY);
    TInitialize;
    task TInitialize 
    {
        LoadSound(shootSFX4);
        LoadSound(trainSFX);
        LoadSound(deadSFX);
        PlaySE(trainSFX);
        
        ObjEnemy_Regist(objEnemyTrain);
        ObjEnemy_SetLife(objEnemyTrain, 200);
        ObjEnemy_SetDamageRate(objEnemyTrain, 200, 200);
        ObjMove_SetPosition(objEnemyTrain, x, y);
        ObjMove_SetAngle(objEnemyTrain, angleE);
        ObjMove_SetSpeed(objEnemyTrain, speedE);
        
        TDrawLoop;
        TMainLoop;
        TFinalize;
        
    }
	function GetEnemyX{
return ObjMove_GetX(objEnemyTrain);
}
	function GetEnemyY{
return ObjMove_GetY(objEnemyTrain);
}
     task TMainLoop {
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0) {
            ObjEnemy_SetIntersectionCircleToShot(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32); 
            ObjEnemy_SetIntersectionCircleToPlayer(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32);
            if (count % 60 == 30) {bulletA;}
			
            count++;
			
			
			/// animation
			let speed;
            let dir;
			dir = ObjMove_GetAngle(objEnemyTrain);
		speed = ObjMove_GetSpeed(objEnemyTrain);
		
		
		            if (ObjMove_GetX(objEnemyTrain) < Xmin || ObjMove_GetY(objEnemyTrain) > Xmax || ObjMove_GetY(objEnemyTrain) < Ymin || ObjMove_GetY(objEnemyTrain) > Ymax) {
                Obj_Delete(objEnemyTrain); }

            yield;
        }
		
		
		}
    

		function angleToPlayer {
	let dir = atan2(GetPlayerY-ObjMove_GetY(objEnemyTrain),GetPlayerX-ObjMove_GetX(objEnemyTrain));
	return dir;
}
    
    task bulletA{
	
	if (GetCommonData("Difficulty", 3) == 3){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(20);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
 	if (GetCommonData("Difficulty", 2) == 2){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(35);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
  	if (GetCommonData("Difficulty", 1) == 1){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(60);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
   	if (GetCommonData("Difficulty", 0) == 0){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(100);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 }
 
 task TExplosion{
    let path = GetCurrentScriptDirectory() ~ "sprite/pow.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
    Obj_SetRenderPriority(obj, 0.60);
    ObjPrim_SetTexture(obj, path);
    ObjSprite2D_SetSourceRect(obj, 0, 0, 100, 100);
    ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
 
    let alpha = 230;
    let scale = 0.5;
    loop(12)
    {
        scale += 0.2;
        alpha -= 8;
        ObjRender_SetColor(obj, alpha, alpha, alpha);
        ObjRender_SetPosition(obj, GetEnemyX, GetEnemyY, 0);
        ObjRender_SetScaleXYZ(obj, scale, scale, 0.5);
        yield;
    }
    Obj_Delete(obj);
}

        	
    
	task TDrawLoop {
	let imgenemy = GetCurrentScriptDirectory() ~ "sprite/trains.png";
    ObjPrim_SetTexture(objEnemyTrain, imgenemy);
    ObjSprite2D_SetSourceRect(objEnemyTrain, 0, 0, 322, 78);
	ObjRender_SetScaleXYZ(objEnemyTrain, 1, 1, 0);
    ObjSprite2D_SetDestCenter(objEnemyTrain);
	}
    task TFinalize 
    {
        //if(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) == 0) 
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0)//this checks if the enemy has still life remaining
        {
            yield;
        }

        if(!Obj_IsDeleted(objEnemyTrain))//This checks if the enemy is already deleted by other means like leaving the screen and getting killed through a differnt task and no effect is needed
        {
            PlaySE(deadSFX);
            TExplosion;
            wait(12);
            Obj_Delete(objEnemyTrain);
            DeleteShotInCircle(TYPE_CHILD, TYPE_ITEM, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 2); 
            yield;
        }
    }
	

}

task CreateStageEnemyTrainMiddle(x, y, speedE, angleE) {
let shootSFX4 = GetCurrentScriptDirectory ~ "SFX/ATTACK.wav";
let deadSFX = GetCurrentScriptDirectory ~ "SFX/BEAT.mp3";
let animFrame = 0; 
let animFrame2 = 0;
 let Xmin = -200;
    let Xmax = GetStgFrameWidth + 200;
    let Ymin = -32; 
    let Ymax = GetStgFrameHeight + 32;
	let count = 0; 
    
	let objEnemyTrain = ObjEnemy_Create(OBJ_ENEMY);
    TInitialize;
    task TInitialize 
    {
        LoadSound(shootSFX4);
        
        LoadSound(deadSFX);
        
        
        ObjEnemy_Regist(objEnemyTrain);
        ObjEnemy_SetLife(objEnemyTrain, 200);
        ObjEnemy_SetDamageRate(objEnemyTrain, 200, 200);
        ObjMove_SetPosition(objEnemyTrain, x, y);
        ObjMove_SetAngle(objEnemyTrain, angleE);
        ObjMove_SetSpeed(objEnemyTrain, speedE);
        
        TDrawLoop;
        TMainLoop;
        TFinalize;
        
    }
	function GetEnemyX{
return ObjMove_GetX(objEnemyTrain);
}
	function GetEnemyY{
return ObjMove_GetY(objEnemyTrain);
}
     task TMainLoop {
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0) {
            ObjEnemy_SetIntersectionCircleToShot(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32); 
            ObjEnemy_SetIntersectionCircleToPlayer(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32);
            if (count % 60 == 30) {bulletA;}
			
            count++;
			
			/// animation
			let speed;
            let dir;
			dir = ObjMove_GetAngle(objEnemyTrain);
		speed = ObjMove_GetSpeed(objEnemyTrain);
		
		
		            if (ObjMove_GetX(objEnemyTrain) < Xmin || ObjMove_GetY(objEnemyTrain) > Xmax || ObjMove_GetY(objEnemyTrain) < Ymin || ObjMove_GetY(objEnemyTrain) > Ymax) {
                Obj_Delete(objEnemyTrain); }

            yield;
        }
		
		
		}
    

		function angleToPlayer {
	let dir = atan2(GetPlayerY-ObjMove_GetY(objEnemyTrain),GetPlayerX-ObjMove_GetX(objEnemyTrain));
	return dir;
}
    
    task bulletA{
	if (GetCommonData("Difficulty", 3) == 3){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(20);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
 	if (GetCommonData("Difficulty", 2) == 2){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(35);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
  	if (GetCommonData("Difficulty", 1) == 1){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(60);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
   	if (GetCommonData("Difficulty", 0) == 0){
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	PlaySE(shootSFX4);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90), DS_BALL_SS_RED + DS_BALL_M_R_RED	, 0);
	wait(100);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 }
 
 
 task TExplosion{
    let path = GetCurrentScriptDirectory() ~ "sprite/pow.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
    Obj_SetRenderPriority(obj, 0.60);
    ObjPrim_SetTexture(obj, path);
    ObjSprite2D_SetSourceRect(obj, 0, 0, 100, 100);
    ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
 
    let alpha = 230;
    let scale = 0.5;
    loop(12)
    {
        scale += 0.2;
        alpha -= 8;
        ObjRender_SetColor(obj, alpha, alpha, alpha);
        ObjRender_SetPosition(obj, GetEnemyX, GetEnemyY, 0);
        ObjRender_SetScaleXYZ(obj, scale, scale, 0.5);
        yield;
    }
    Obj_Delete(obj);
}

        	
    
	task TDrawLoop {
	let imgenemy = GetCurrentScriptDirectory() ~ "sprite/trains.png";
    ObjPrim_SetTexture(objEnemyTrain, imgenemy);
    ObjSprite2D_SetSourceRect(objEnemyTrain, 322, 0, 622, 78);
	ObjRender_SetScaleXYZ(objEnemyTrain, 1, 1, 0);
    ObjSprite2D_SetDestCenter(objEnemyTrain);
	}
    task TFinalize 
    {
        //if(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) == 0) 
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0)//this checks if the enemy has still life remaining
        {
            yield;
        }

        if(!Obj_IsDeleted(objEnemyTrain))//This checks if the enemy is already deleted by other means like leaving the screen and getting killed through a differnt task and no effect is needed
        {
            PlaySE(deadSFX);
            TExplosion;
            wait(12);
            Obj_Delete(objEnemyTrain);
            DeleteShotInCircle(TYPE_CHILD, TYPE_ITEM, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 2); 
            yield;
        }
    }
	

}

task CreateStageEnemyTrainEnd(x, y, speedE, angleE) {
let shootSFX = GetCurrentScriptDirectory ~ "SFX/ATTACK4.wav";
let deadSFX = GetCurrentScriptDirectory ~ "SFX/BEAT.mp3";
let animFrame = 0; 
let animFrame2 = 0;
 let Xmin = -200;
    let Xmax = GetStgFrameWidth + 200;
    let Ymin = -32; 
    let Ymax = GetStgFrameHeight + 32;
	let count = 0; 
    
	let objEnemyTrain = ObjEnemy_Create(OBJ_ENEMY);
    TInitialize;
    task TInitialize 
    {
        LoadSound(shootSFX);
        
        LoadSound(deadSFX);
        
        
        ObjEnemy_Regist(objEnemyTrain);
        ObjEnemy_SetLife(objEnemyTrain, 200);
        ObjEnemy_SetDamageRate(objEnemyTrain, 200, 200);
        ObjMove_SetPosition(objEnemyTrain, x, y);
        ObjMove_SetAngle(objEnemyTrain, angleE);
        ObjMove_SetSpeed(objEnemyTrain, speedE);
        
        TDrawLoop;
        TMainLoop;
        TFinalize;
        
    }
	function GetEnemyX{
return ObjMove_GetX(objEnemyTrain);
}
	function GetEnemyY{
return ObjMove_GetY(objEnemyTrain);
}
     task TMainLoop {
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0) {
            ObjEnemy_SetIntersectionCircleToShot(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32); 
            ObjEnemy_SetIntersectionCircleToPlayer(objEnemyTrain, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 32);
            if (count % 60 == 30) {bulletA;}
			
            count++;
			
			
			/// animation
			let speed;
            let dir;
			dir = ObjMove_GetAngle(objEnemyTrain);
		speed = ObjMove_GetSpeed(objEnemyTrain);
		
		
		            if (ObjMove_GetX(objEnemyTrain) < Xmin || ObjMove_GetY(objEnemyTrain) > Xmax || ObjMove_GetY(objEnemyTrain) < Ymin || ObjMove_GetY(objEnemyTrain) > Ymax) {
                Obj_Delete(objEnemyTrain); }

            yield;
        }
		
		
		}
    

		function angleToPlayer {
	let dir = atan2(GetPlayerY-ObjMove_GetY(objEnemyTrain),GetPlayerX-ObjMove_GetX(objEnemyTrain));
	return dir;
}
    
    task bulletA{
	

  while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0){
	
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 5), rand(angleToPlayer,90),  DS_BALL_M_R_RED	, 0);
	wait(20);
	CreateShotA1(ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), rand(2, 4), rand(angleToPlayer,90), DS_BALL_SS_PURPLE, 0);
	

	yield;
	
 }
 }
 
 task TExplosion{
    let path = GetCurrentScriptDirectory() ~ "sprite/pow.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
    Obj_SetRenderPriority(obj, 0.60);
    ObjPrim_SetTexture(obj, path);
    ObjSprite2D_SetSourceRect(obj, 0, 0, 100, 100);
    ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
 
    let alpha = 230;
    let scale = 0.5;
    loop(12)
    {
        scale += 0.2;
        alpha -= 8;
        ObjRender_SetColor(obj, alpha, alpha, alpha);
        ObjRender_SetPosition(obj, GetEnemyX, GetEnemyY, 0);
        ObjRender_SetScaleXYZ(obj, scale, scale, 0.5);
        yield;
    }
    Obj_Delete(obj);
}

        	
    
	task TDrawLoop {
	let imgenemy = GetCurrentScriptDirectory() ~ "sprite/trains.png";
    ObjPrim_SetTexture(objEnemyTrain, imgenemy);
    ObjSprite2D_SetSourceRect(objEnemyTrain, 622, 0, 948, 78);
	ObjRender_SetScaleXYZ(objEnemyTrain, 1, 1, 0);
    ObjSprite2D_SetDestCenter(objEnemyTrain);
	}
    task TFinalize 
    {
        //if(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) == 0) 
        while(ObjEnemy_GetInfo(objEnemyTrain, INFO_LIFE) > 0)//this checks if the enemy has still life remaining
        {
            yield;
        }

        if(!Obj_IsDeleted(objEnemyTrain))//This checks if the enemy is already deleted by other means like leaving the screen and getting killed through a differnt task and no effect is needed
        {
            PlaySE(deadSFX);
            TExplosion;
            wait(12);
            Obj_Delete(objEnemyTrain);
            DeleteShotInCircle(TYPE_CHILD, TYPE_ITEM, ObjMove_GetX(objEnemyTrain), ObjMove_GetY(objEnemyTrain), 2); 
            yield;
        }
    }
	

}