#include "./healthbar.dnh"
#include "./BasicFunctions.dnh"


let rez = GetScreenHeight()/480;
let datapath = GetCurrentScriptDirectory() ~ "data/Difficulty.dat";
let soloscript = 0;

function BossX(){
	return ObjMove_GetX(objBoss);
}

function BossY(){
	return ObjMove_GetY(objBoss);
}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){ 
    	yield; 
    } 
    if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0) {
    	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    	
    }
    move = 0;
    //DeleteShotAll(TYPE_ALL, TYPE_ITEM);
   
    
    Obj_Delete(objBoss); 
    wait(2);
    if(soloscript == 0){SetAutoDeleteObject(true);}
    else{Obj_Delete(bg1); Obj_Delete(bg2);}
    CloseScript(GetOwnScriptID()); 
    return;
}

task LSFinalize{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){ 
    	yield; 
    } 
    if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0) {
    	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    	
    }
    bulletclear(0);
    StartSlow(GetPlayerID(), 15);
    bmoveto(BossX-rand(30,40), BossY+rand(20,30), 60);
    SE_Play(bossclear,60);
   
    wait(40);
    endparticle(BossX, BossY);
    Obj_Delete(objBoss);
    StopSlow(GetPlayerID());
    shake(6, 60);
    
    SE_Play(bossclear,40);
    Obj_Delete(bg1);
    Obj_Delete(bg2);
    wait(90);
    CloseScript(GetOwnScriptID()); 
    return;

}

task endparticle(x,y){
	let scale = 3.5;
	let angle = rand(0,360);
	let cosin = 0;
	let n = 30;
	let alpha = 170;
	loop(3){
		loop(n){
			let path = GetCurrentScriptDirectory~"./img/maple.png";
			let leaf = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(leaf, BLEND_ADD_ARGB);
			ObjPrim_SetTexture(leaf, path);
			ObjSprite2D_SetSourceRect(leaf, 32, 0, 64, 32);
			ObjSprite2D_SetDestCenter(leaf);
			Obj_SetRenderPriorityI(leaf,24);
			ObjRender_SetAlpha(leaf, alpha);
			ObjRender_SetScaleXYZ(leaf, scale, scale, 1);
			let angle2 = rand(0,360);
			ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
			ObjRender_SetPosition(leaf, x + cosin * cos(angle), y + cosin * sin(angle),1);
			moveendparticle(leaf, x, y, cosin,angle,alpha);
			angle += 360/n;
		}
		angle = rand(0,360);
		wait(7);
	}

	task moveendparticle(leaf, x, y, cosin, angle,alpha){
		let n = 60;
		let cosinp = rand(1, 4);
		let sm = 2;
		let am = alpha/n;
		let smm = sm/n/3;
		let angle2 = rand(0,360);
		loop(n){
			ObjRender_SetPosition(leaf, x + cosin * cos(angle), y + cosin * sin(angle),1);
			ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
			ObjRender_SetAlpha(leaf, alpha);
			alpha += -am;
			angle2 += sm;
			sm -= smm;
			cosin += cosinp;
			wait(1)
		}
		Obj_Delete(leaf);
	}
}

task shake(dist, t){
	let tx = GetCameraFocusX();
	let ty = GetCameraFocusY();
	let cenx = 384/2;
	let ceny = 448/2;

	loop(t){

		Set2DCameraFocusX(cenx+rand(-dist,dist));
		Set2DCameraFocusY(ceny+rand(-dist,dist));
		SetCameraFocusX(tx+rand(dist,dist*2));
		SetCameraFocusY(ty+rand(dist,dist*2));
		yield;
	}

	Set2DCameraFocusY(448/2);
	Set2DCameraFocusX(384/2);;
	SetCameraFocusX(tx);
	SetCameraFocusY(ty);
}

let floaty = 0;

let sprite = ObjPrim_Create(OBJ_SPRITE_2D);
let sprite2 = ObjPrim_Create(OBJ_SPRITE_2D);

let diff = 0;
let mus = 0;

task TDrawLoop {
	//BGM_Play(bgm1,0, 0);
	LoadCommonDataAreaA2("mode", datapath);
	pd = GetAreaCommonData("mode", "pointdevice", 1);
	diff = GetAreaCommonData("mode", "difficulty", 0);
	lowq =  GetAreaCommonData("mode", "quality", 0);
	mus =  GetAreaCommonData("mode", "music", 0);
	SetAreaCommonData("mode", "start", 1); 
	if(pd == 0){SetPlayerDownStateFrame(30); SetPlayerLife(9999);}
	else{SetPlayerDownStateFrame(60);}
	SetShotAutoDeleteClip(32, 32, 32+384-(rez*384), 32+448-(rez*448));
	auraposition();
	floating;
	MagicCircle;
	BossLife;
	SpellDamage;
	EnemyMarker;
	damagesfx;
	bossanimation1(sprite);
	bossanimation2(sprite2);
	bossaura(objBoss);
	timersound;
	
}

let move = 0;
let spriteflip = 0;

task switchboss(name){
	if(name == "parsee"){
		bossalpha(sprite, 0, 1);
		bossalpha(sprite2, 255, -1);
		setauracolor("green");
	}
	if(name == "kisume"){
		bossalpha(sprite2, 0, 1);
		bossalpha(sprite, 255, -1);
		setauracolor("darkred");
	}

	task bossalpha(spr, salpha, posneg){
		let t = 15;
		let ap = 255/t * posneg;
		let alpha = salpha;
		loop(t){
			alpha += ap;
			ObjRender_SetAlpha(spr, alpha);
			wait(1);
		}
	}
}

task startingboss(name){
	if(name == "parsee"){
		ObjRender_SetAlpha(sprite, 255);
		ObjRender_SetAlpha(sprite2, 0);
		setauracolor("green");
	}
	if(name == "kisume"){
		ObjRender_SetAlpha(sprite2, 255);
		ObjRender_SetAlpha(sprite, 0);
		setauracolor("darkred");
	}
}

task bossanimation2(sprite2){
	let imgboss = GetCurrentScriptDirectory~"./img/boss2.png"; 
	
	ObjPrim_SetTexture(sprite2, imgboss); 
	ObjSprite2D_SetSourceRect(sprite2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(sprite2);
	Obj_SetRenderPriorityI(sprite2, Obj_GetRenderPriorityI(objBoss));
	let x = 0;
	let x2 = x + 64;
	let y = 0;
	let y2 = y + 64;
	let posneg = 1;
	let n = 0;
	let cm = 0;
	while(!Obj_IsDeleted(sprite2)){
		if(n  < 4){
			x = n * 64;
			x2 = x + 64;
			y = 0;
			y2 = y + 64;
		}
		else{
			x = (n-4) * 64;
			x2 = x + 64;
			y = 64;
			y2 = y + 64;
		}
		ObjSprite2D_SetSourceRect(sprite2, x, y, x2, y2);
		n += 1 * posneg;
		wait(8);
		if(n == 8){n = 0; }
	}
}
task bossanimation1(sprite){
	let imgboss = GetCurrentScriptDirectory~"./img/boss1.png"; 
	
	ObjPrim_SetTexture(sprite, imgboss); 
	ObjSprite2D_SetSourceRect(sprite, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(sprite);
	Obj_SetRenderPriorityI(sprite, Obj_GetRenderPriorityI(objBoss));
	let l = 0;
	let x = 0;
	let x2 = x + 96;
	let y = 0;
	let y2 = y + 10;
	let n = 0;
	let posneg = -1;
	let c = 0;
	let destx = 0;
	
	while(!Obj_IsDeleted(sprite)){
		n = 0;
		posneg = 1;
		while(move == 0){
			ObjSprite2D_SetSourceRect(sprite, 0, 0, 64, 64);
			while(move == 0){wait(1);}
		}
		n = 0;
		posneg = 1;
		while(move == 1 || move == 2){
			let cm = move;
			x = n * 64;
			x2 = x + 64;
			y = 65;
			y2 = y + 63; 
			if(move == 1){ObjSprite2D_SetSourceRect(sprite, x, y, x2, y2);}
			else{ObjSprite2D_SetSourceRect(sprite, x2, y, x, y2);}
			n += 1;
			loop(5){if(move == 1 || move == 2){wait(1);}}
			if(n == 4){
				while(move == 1 || move == 2){wait(1);}
				loop(4){
					n -= 1;
					x = n * 64;
					x2 = x + 64;
					if(cm == 1){ObjSprite2D_SetSourceRect(sprite, x, y, x2, y2);}
					else{ObjSprite2D_SetSourceRect(sprite, x2, y, x, y2);}
					wait(5);
				}
			}
		}
		n = 0;
		posneg = 1;
		while(move == 3){
			x = n * 64;
			x2 = x + 64;
			y = 0;
			y2 = y + 64; 
			ObjSprite2D_SetSourceRect(sprite, x, y, x2, y2);
			n += 1;
			loop(5){if(move == 3){wait(1);}}
			if(n == 4){
				while(move == 3){wait(1);}
				loop(4){
					n -= 1;
					x = n * 64;
					x2 = x + 64;
					ObjSprite2D_SetSourceRect(sprite, x, y, x2, y2);
					wait(5);
				}
			}
		}
		n = 0;
		posneg = 1;
		wait(1);
	}
}

function lifecheck(n){
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && objScene != ID_INVALID && pdcount == n){
		return true;
	}
	else{return false;}
}

function lifecheck2{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && objScene != ID_INVALID){
		return true;
	}
	else{return false;}
}

let pd = 0; //is pointdevice enabled?
let pdcount = 0;
let pdcount2 = 0;
let pdbossx = 0;
let pdbossy = 0;
let pdplayerx = 0;
let pdplayery = 0;
let pdspell = 0;
let pdscore = 0;

task pdreset(time,life){
	if(pd == 1){return}
	let ct = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) + 1;
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	SetForbidPlayerShot(true);
    SetForbidPlayerSpell(true);
	loop(15){
		ObjEnemyBossScene_SetSpellTimer(objScene, ct*60);
		SetPlayerSpell(pdspell);
		yield;
	}
	let ready = GetCurrentScriptDirectory() ~ "./img/pdtext.png";
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(text,ready);
    Obj_SetRenderPriorityI(text,80);
    ObjRender_SetBlendType(text,BLEND_ALPHA);
    ObjSprite2D_SetSourceRect(text,0,146,256,193);
    ObjSprite2D_SetDestCenter(text);
    ObjRender_SetPosition(text, GetCenterX*rez, GetCenterY*rez, 1);
    textscale(text);

    task textscale(text){
    	let alpha = 0;
    	let s = 2.5;
    	let t = 25;
    	let sp = (s-0.5)/t;
    	let ap = 255/t;
    	loop(t){
    		alpha += ap;
    		s -= sp;
    		ObjRender_SetAlpha(text, alpha);
    		ObjRender_SetScaleXYZ(text, s*rez, 0.5*rez, 1);
    		wait(1);
    	}
    	wait(50);
    	let t2 = 20;
    	ap = 255/t2;
    	sp = 0.5/t2;
    	loop(t2){
    		alpha -= ap;
    		s -= sp;
    		ObjRender_SetAlpha(text, alpha);
    		ObjRender_SetScaleXYZ(text, s*rez, s*rez, 1);
    		wait(1);
    	}
    	Obj_Delete(text);
    }


	let img = GetCurrentScriptDirectory() ~ "./img/pdbg.png";
	let neg = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(neg,img);
    Obj_SetRenderPriorityI(neg,79);
    ObjRender_SetBlendType(neg,BLEND_ALPHA);
    ObjSprite2D_SetSourceRect(neg,0,0,1024,1024);
    ObjSprite2D_SetDestCenter(neg);
    ObjRender_SetPosition(neg, GetCenterX*rez, GetCenterY*rez, 1);
    let scale = 0.55;
    ObjRender_SetScaleXYZ(neg, scale*rez, scale*rez, 1);
    let c = 0;
    let pdwait = 20;
    let cp = 255/pdwait;
    let f = 0;
    let s = -1.3;
    loop(pdwait){
    	c += cp;
    	ObjSprite2D_SetSourceRect(neg, 0 + (f*s) % 1024, 0 + (f*s) % 1024, 1024 + (f*s) % 1024, 1024 + (f*s) % 1024);
    	ObjRender_SetAlpha(neg, c);
    	f += 1;
    	wait(1);
    }
    pdcount += 1;
    
    
    loop(50){
    	ObjSprite2D_SetSourceRect(neg, 0 + (f*s) % 1024, 0 + (f*s) % 1024, 1024 + (f*s) % 1024, 1024 + (f*s) % 1024);
    	f += 1;
    	wait(1);
    }
    let t = 15;
    let cm = 255/t;
    deletebullets;
    ObjMove_AddPatternA2(objBoss, 0, 0, 0, 0, 0, 0);
    SetForbidPlayerShot(false);
    SetForbidPlayerSpell(false);
    ObjEnemyBossScene_SetSpellTimer(objScene, time*60);
    ObjEnemy_SetLife(objBoss, life);
    AddScore(pdscore-GetScore());
    ObjMove_SetPosition(objBoss,pdbossx, pdbossy);
    ObjMove_SetPosition(GetPlayerObjectID(),pdplayerx, pdplayery);
    SetPlayerInvincibilityFrame(0);
    move = 0;
    movei = 0;
	movei2 = 0;
	movec = 0;
	movec2 = 0;
	SetPlayerLife(GetPlayerLife()+1);
    startattacking;
    loop(t){
    	c -= cm;
    	ObjRender_SetAlpha(neg, c);
    	ObjSprite2D_SetSourceRect(neg, 0 + (f*s) % 1024, 0 + (f*s) % 1024, 1024 + (f*s) % 1024, 1024 + (f*s) % 1024);
    	f += 1;
    	wait(1);
    }
    Obj_Delete(neg);
   	
}

task deletebullets{
	let endbullet = GetShotIdInCircleA2(0, 0, 99999, TARGET_ENEMY);
	let n = length(endbullet);
	ascent(i in 0..n){
		Obj_Delete(endbullet[i]);
	}
}

task startingposition(x,y){
	if(BossX == 0 && BossY == 0){
		ObjMove_SetPosition(objBoss, x, y);
		SetPlayerLife(9);
		let dir = GetCurrentScriptDirectory();
		soloscript = 1;
		TMode();
		TBossName();
		let musel =  GetAreaCommonData("mode", "music", 0);
    	let bgobj = ObjSound_Create();
    	if(musel == 0){
	    	ObjSound_Load(bgobj, dir ~ "./se/bgm.ogg");
	    	ObjSound_SetLoopEnable(bgobj, true);
	    	ObjSound_SetLoopTime(bgobj, 0, 999999999);
		}
		if(musel == 1){
			ObjSound_Load(bgobj, dir ~ "./se/bgm.ogg");
	    	ObjSound_SetLoopEnable(bgobj, true);
	    	ObjSound_SetLoopTime(bgobj, 0, 999999999);
		}
		ObjSound_SetVolumeRate(bgobj, 85);
		ObjSound_Play(bgobj);
	}

}

task pointdevicepositions{
	pdbossx = BossX;
	pdbossy = BossY;
	pdplayerx = PlayerX;
	pdplayery = PlayerY;
	pdspell = GetPlayerSpell();
	pdscore = GetScore();
}

function spellcutin(name, ver){
	effect;
	spellbackground(ver);
	task effect{
		chargeeffect2("blue");
		chargerelease("pink");
		wait(20);
		chargeeffect2("red");

	}
	let path = GetCurrentScriptDirectory~"img/cutin.png";
	
	let cutin = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(cutin, BLEND_ALPHA);
	ObjPrim_SetTexture(cutin, path);
	Obj_SetRenderPriorityI(cutin,27);
	ObjSprite2D_SetSourceRect(cutin, 0, 0, 372, 441);
	ObjSprite2D_SetDestCenter(cutin);
	cutintaskjunko(cutin, -1);

	let text = ObjText_Create;
	InstallFont(GetCurrentScriptDirectory()~"Meiryo.ttc");
	ObjText_SetText(text, name);
	ObjText_SetFontType(text, "Meiryo");
	ObjText_SetFontSize(text,16*rez);
	ObjText_SetFontBorderType(text,BORDER_FULL);
	ObjText_SetFontBorderColor(text,32,32,32);
	ObjText_SetFontBorderWidth(text,1*rez);
	ObjText_SetHorizontalAlignment(text, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(text, GetStgFrameWidth-24);
	Obj_SetRenderPriority(text, 0.78);
	textask(text, -385, 362, 1, pdcount, 1);
		
	let path2 = GetCurrentScriptDirectory~"img/SpellAttack.png";
	let bg = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bg, path2);
	ObjRender_SetBlendType(bg, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(bg, 0, 0, 255, 27);
	ObjSprite2D_SetDestCenter(bg);
	let c2 = 80;
	ObjRender_SetColor(bg, 255, c2, c2);
	Obj_SetRenderPriority(bg, 0.77);
	textask(bg, -140, 363, 1.1, pdcount, rez);

	task textask(obj, x, y, ss, pdc, rez2){
		let alpha = 0;
		let scale = 2.3;
		let t1 = 30;
		let ap = 255/t1;
		let sp = (scale-ss)/t1;
		let xp = 400/t1;
		loop(t1){
			if(pdc == pdcount){
				alpha += ap;
				scale -= sp;
				x += xp;
				ObjRender_SetAlpha(obj, alpha);
				ObjRender_SetScaleXYZ(obj, scale*rez2, scale*rez2, 1);
				ObjRender_SetPosition(obj, x*rez2, y*rez, 0);
				wait(1);
			}

		}
		wait(40);
		let t2 = 35;
		let yp = 360/t2;
		loop(t2){
			if(pdc == pdcount){
				y -= yp;
				ObjRender_SetPosition(obj, x*rez2, y*rez, 0);
				wait(1);
			}
		}
		while(lifecheck(pdc) == true){yield;}
		Obj_Delete(obj);
	}

	task cutintaskjunko(cutin, posneg){
		let alpha = 0;
		let y = 224 - 20+ 126*posneg;
		ObjRender_SetPosition(cutin, GetCenterX, y, 1);
		ObjRender_SetAlpha(cutin, alpha);
		let t1 = 30;
		let ap = (255-alpha)/t1;
		let s = 8;
		let dacc = (s-0.4)/t1;
		let t2 = 60;
		loop(t1){
			alpha += ap;
			ObjRender_SetAlpha(cutin, alpha);
			y -= s * posneg;
			s -= dacc;
			ObjRender_SetPosition(cutin, GetCenterX+15, y, 1);
			wait(1);
		}
		loop(t2){
			y -= s * posneg;
			ObjRender_SetPosition(cutin, GetCenterX+15, y, 1);
			wait(1);
		}
		loop(t1){
			alpha -= ap;
			ObjRender_SetAlpha(cutin, alpha);
			y -= s * posneg;
			s += dacc;
			ObjRender_SetPosition(cutin, GetCenterX+15, y, 1);
			wait(1);
		}
		Obj_Delete(cutin);
	}

	task cutinflip(cutin){
		let alpha = 0;
		let posneg = 1;
		let y = 370;
		ObjRender_SetPosition(cutin, GetCenterX, y, 1);
		ObjRender_SetAlpha(cutin, alpha);
		let t1 = 30;
		let ap = (255-alpha)/t1;
		let s = 8;
		let dacc = (s-0.4)/t1;
		let t2 = 60;
		loop(t1){
			alpha += ap;
			ObjRender_SetAlpha(cutin, alpha);
			y -= s;
			s -= dacc;
			ObjRender_SetPosition(cutin, GetCenterX, y, 1);
			wait(1);
		}
		loop(t2/3){
			y -= s;
			ObjRender_SetPosition(cutin, GetCenterX, y, 1);
			wait(1);
		}
		loop(t2/3){
			y -= s;
			ObjRender_SetPosition(cutin, GetCenterX, y, 1);
			ObjRender_SetAngleY(cutin, ObjRender_GetAngleY(cutin)-180/t2*3);
			wait(1);
		}
		loop(t2/3){
			y -= s;
			ObjRender_SetPosition(cutin, GetCenterX, y, 1);
			wait(1);
		}
		loop(t1){
			alpha -= ap;
			ObjRender_SetAlpha(cutin, alpha);
			y -= s;
			s += dacc;
			ObjRender_SetPosition(cutin, GetCenterX, y, 1);
			wait(1);
		}
		Obj_Delete(cutin);
	}

	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL) == false){ObjEnemyBossScene_StartSpell(objScene); }
	SE_Play(spelldeclare, 80);
	wait(100);
}

let bg1 = ObjPrim_Create(OBJ_SPRITE_2D);
let bg2 = ObjPrim_Create(OBJ_SPRITE_2D);

task spellbackground(ver){
	let path1 = GetCurrentScriptDirectory() ~ "img/spellbg1.png";
	let path2 = GetCurrentScriptDirectory() ~ "img/spellbg2.png";
	let path3 = GetCurrentScriptDirectory() ~ "img/spellbg3.png";
	let path4 = GetCurrentScriptDirectory() ~ "img/spellbg4.png";
	let pdc = pdcount;
	let sc = 2.3;
    bg1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(bg1, BLEND_ALPHA);
    Obj_SetRenderPriorityI(bg1, 22);
    ObjPrim_SetTexture(bg1, path2);
    ObjSprite2D_SetSourceRect(bg1, 0, 0, 256, 256);
    ObjSprite2D_SetDestCenter(bg1);
    ObjRender_SetPosition(bg1, 384/2, 448/2, 1);
   	ObjRender_SetScaleXYZ(bg1, sc, sc, 1);
    ObjRender_SetAlpha(bg1,255);

    
    bg2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(bg2, BLEND_ADD_ARGB);
    Obj_SetRenderPriorityI(bg2, 23);
    ObjPrim_SetTexture(bg2, path1);
    ObjSprite2D_SetSourceRect(bg2, 0, 0, 256, 256);
    let d = 2;
    ObjSprite2D_SetDestCenter(bg2);
    ObjRender_SetPosition(bg2, 384/2, 448/2, 1);
    let c = 255;
    ObjRender_SetColor(bg2, c, 255, c);
    ObjRender_SetAlpha(bg2,235);
    
    ObjRender_SetScaleXYZ(bg2, sc, sc, 1);

    let bg3 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(bg3, BLEND_ALPHA);
    Obj_SetRenderPriorityI(bg3, 22);
    ObjPrim_SetTexture(bg3, path4);
    ObjSprite2D_SetSourceRect(bg3, 0, 0, 256, 256);
    ObjSprite2D_SetDestCenter(bg3);
    ObjRender_SetPosition(bg3, 384/2, 448/2, 1);
    ObjRender_SetColor(bg3, 255, 0, 0);
    ObjRender_SetAlpha(bg3, 185);
    ObjRender_SetScaleXYZ(bg3, 2,2, 1);



    let frame = 0;
    let alpha = 0;
    let fn = -0.35;
    let fn2 = 0.7;
    while(pdc == pdcount && !Obj_IsDeleted(bg1)) {
        ObjRender_SetAngleZ(bg1, ObjRender_GetAngleZ(bg1) + fn);
        ObjRender_SetAngleZ(bg2, ObjRender_GetAngleZ(bg2) + fn2);
        frame++;
        yield;
    }

    Obj_Delete(bg1);
    Obj_Delete(bg2);
    Obj_Delete(bg3);

}

task flipbg(angle, t){
	let ap = angle/t;
	loop(t){
		//ObjRender_SetAngleZ(bg1, ObjRender_GetAngleZ(bg1) + ap);
		ObjRender_SetAngleZ(bg2, ObjRender_GetAngleZ(bg2) + ap);
		wait(1);

	}
}

task floating{
	let n = 3.3;
	let fp = 0.06;
	let posneg = 1;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && objScene != ID_INVALID){
		while(-n < floaty && floaty < n){
			floaty += fp * posneg;
			wait(1);
		}
		posneg *= -1;
		floaty += fp * posneg;
		wait(15);
	}
}

LoadTexture(GetCurrentScriptDirectory~"./img/eff_aura.dds");
LoadTexture(GetCurrentScriptDirectory~"./img/whitecircle.png");
LoadTexture(GetCurrentScriptDirectory~"./img/maple.png");

task bossaura(obj){
	//bossauraeffect1;
	bossauraeffect2;

	task bossauraeffect1{
		while(!Obj_IsDeleted(obj)){
			loop{
				let path = GetCurrentScriptDirectory~"./img/eff_aura.dds";
				let aura1 = ObjPrim_Create(OBJ_SPRITE_2D);
				let aura2 = ObjPrim_Create(OBJ_SPRITE_2D);
				let angle = rand(-4,4);
				auradetail(aura1,path, angle, 1);
				auradetail(aura2,path, angle, 2);
				bossmoveaura(aura1,1);
				bossmoveaura(aura2,2);
				
				wait(10);
			}

			task bossmoveaura(aura1,an){
				if(an == 2){wait(2);}
				let n = 30;
				let yscale = 1/n;
				let salpha = 600;
				if(an == 2){salpha = 600;}
				let alpha = salpha;
				let alpham = salpha/n;
				ObjRender_SetScaleX(aura1, 1);
				ObjRender_SetScaleY(aura1, 0.6);
				loop(n){
					if(!Obj_IsDeleted(obj)){Obj_Delete(aura1); return;}
					ObjRender_SetPosition(aura1, ObjMove_GetX(obj), ObjMove_GetY(obj), 1);
					ObjRender_SetScaleY(aura1, ObjRender_GetScaleY(aura1)+yscale);
					ObjRender_SetAlpha(aura1, alpha);
					alpha -= alpham;
					wait(1);
				}
				Obj_Delete(aura1);
			}

			task auradetail(aura1,path, angle, an){
				if(an == 2){wait(9999);}
				ObjRender_SetBlendType(aura1, BLEND_ADD_ARGB);
				ObjPrim_SetTexture(aura1, path);
				Obj_SetRenderPriorityI(aura1,27);
				ObjSprite2D_SetSourceRect(aura1, 8, 5, 42, 45);
				ObjSprite2D_SetDestRect(aura1, -25, -50, 25, 0);
				ObjRender_SetPosition(aura1, ObjMove_GetX(obj), ObjMove_GetY(obj), 1);
				let perc = 1;
				ObjRender_SetColor(aura1, 255* perc, 0*perc, 0*perc);
				//ObjRender_SetAngleZ(aura1, angle);
			}
		}
	}
	task bossauraeffect2{
		while(!Obj_IsDeleted(obj)){
			loop{
				let path = GetCurrentScriptDirectory~"./img/eff_aura.dds";
				let aura3 = ObjPrim_Create(OBJ_SPRITE_2D);
				bossauradetail2(aura3,path);
				let angle = rand(0,360);
				ObjRender_SetAngleZ(aura3, angle);
				bossmoveaura2(aura3);
				
				wait(3);
			}

			task bossmoveaura2(aura1){
				let n = 45;
				let salpha = 255;
				let alpha = salpha;
				let alpham = salpha/n;
				let scale = 0;
				let scalem = (3-scale)/n;
				loop(n){
					if(Obj_IsDeleted(obj)){Obj_Delete(aura1); return;}
					ObjRender_SetPosition(aura1, ObjMove_GetX(obj), ObjMove_GetY(obj)+floaty, 1);
					ObjRender_SetScaleXYZ(aura1, scale,scale, 1);
					ObjRender_SetAlpha(aura1, alpha);
					alpha -= alpham;
					scale += scalem;
					wait(1);
				}
				Obj_Delete(aura1);
			}

			task bossauradetail2(aura1,path){
				ObjRender_SetBlendType(aura1, BLEND_ADD_ARGB);
				ObjPrim_SetTexture(aura1, path);
				Obj_SetRenderPriorityI(aura1,27);
				ObjSprite2D_SetSourceRect(aura1, 56, 8, 90, 41);
				ObjSprite2D_SetDestCenter(aura1);
				ObjRender_SetPosition(aura1, ObjMove_GetX(obj), ObjMove_GetY(obj)+20+floaty, 1);
				ObjRender_SetColor(aura1, auracolor2[0], auracolor2[1], auracolor2[2]);
				ObjRender_SetScaleXYZ(aura1, 0, 0, 1);
			}
		}
	}

}

let auracolor2 = [0, 0, 0];

task setauracolor(color){
	if(color == "red"){
		auracolor2 = [255, 40, 40];
	}
	if(color == "darkred"){
		auracolor2 = [255, 25, 25];
	}
	if(color == "blue"){
		auracolor2 = [40, 40, 255];
	}
	if(color == "green"){
		auracolor2 = [0, 255, 40];
	}
}

let dsfx = 10;

task damagesfx{
	let life = ObjEnemy_GetInfo(objBoss, INFO_LIFE);
	let llife = ObjEnemy_GetInfo(objBoss, INFO_LIFE);
	let v = 75;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && objScene != ID_INVALID){
		let clife = ObjEnemy_GetInfo(objBoss, INFO_LIFE);
		if(clife < life){
			SE_Play(shotdamage1, v);
			AddScore(1);
		}
		if(clife < life && clife < llife/dsfx){
			SE_Play(shotdamage2, v+5);

		}
		life = ObjEnemy_GetInfo(objBoss, INFO_LIFE);
		wait(5);
	}
}

task changedsfx{
	dsfx = 20;
}

task timersound{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && objScene != ID_INVALID){
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) < 10.1){
			SE_Play(timeout1, 100);
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) < 6){SE_Play(timeout2,50);}
			wait(59);
		}
		wait(1);
	}
}




task enddelete(obj){
	//while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && !Obj_IsDeleted(obj)){wait(3);}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 0){
		Obj_Delete(obj);
	}
}

task enddelete2(obj){
	//while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && !Obj_IsDeleted(obj)){wait(3);}
	while(lifecheck2 == true && !Obj_IsDeleted(obj)){
		yield;
	}
	Obj_Delete(obj);
}

task bulletclear(death){
	let array = GetShotIdInCircleA2(0, 0, 99999, TARGET_ENEMY);
	let l = length(array);
	let array2 = [];
	ascent(i in 0..l){
		if(!Obj_IsDeleted(array[i])){
			if(death == 0 || ObjShot_IsSpellResist(array[i]) != true){
				let bulletx = ObjMove_GetX(array[i]);
				let bullety = ObjMove_GetY(array[i]);
				let graphic = ObjShot_GetImageID(array[i]);
				let color = GetShotDataInfoA1(graphic, "./ZUNShot_Const.txt", INFO_DELAY_COLOR);
				Obj_Delete(array[i]);
				let cancel = CreateShotA1(bulletx, bullety, 0, 0, 1100, 0);
				ObjRender_SetBlendType(cancel, BLEND_ADD_ARGB);
				ObjMove_AddPatternA2(cancel, 0, 0, rand(0,360), 0, -4, 0);
				ObjShot_SetGraphic(cancel, 1101);
				ObjRender_SetScaleXYZ(cancel, 0.9, 0.9, 1);
				ObjRender_SetColor(cancel, color[0], color[1], color[2]);
				ObjShot_SetSpellResist(cancel, true);
				ObjShot_SetAutoDelete(cancel, false);
				array2 = array2 ~ [cancel];
			}
		}
	}
	loop(31){
		yield;
	}
	l = length(array2);
	ascent(i in 0..l){
		Obj_Delete(array2[i]);
	}
}

task chargeeffect{
	if(lifecheck2 == true){
		chargeorb("blue");
		SE_Play(charge2,80);
	}
	wait(45);
	if(lifecheck2 == true){
		chargerelease("pink");
		SE_Play(chargefinish,60);
	}
}

task chargeeffect2(color){
	if(lifecheck2 == true){
		chargeorb(color);
		SE_Play(charge2,80);
	}
}

task chargeorb(color){
	let r = 0;
	let b = 0;
	let g = 0;
	if(color == "red"){
		let c2 = 90;
		r = 235;
		b = c2;
		g = c2;
	}
	if(color == "blue"){
		let c2 = 80;
		r = c2;
		b = 255;
		g = c2;
	}
	if(color == "pink"){
		let c3 = 235;
		r = c3;
		b = c3;
		g = 50;
	}
	if(color == "green"){
		let c2 = 100;
		r = c2;
		b = c2;
		g = 235;
	}
	let path = GetCurrentScriptDirectory~"./img/whitecircle.png";
	let charge = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(charge, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(charge, path);
	ObjSprite2D_SetSourceRect(charge, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(charge);
	Obj_SetRenderPriorityI(charge,24);
	ObjRender_SetColor(charge, r, g, b);
	let n = 47;
	let alpha = 0;
	let endalpha = 180;
	let scale = 0.7;
	let am = (endalpha-alpha)/n * 2;
	let sm = scale/n;
	enddelete2(charge);
	chargeparticle;
	loop(n){
		if(ObjMove_GetX(objBoss) == 0 && ObjMove_GetY(objBoss) == 0){Obj_Delete(charge); return;}
		ObjRender_SetAlpha(charge,alpha);
		ObjRender_SetScaleXYZ(charge, scale, scale, 1);
		ObjRender_SetPosition(charge, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss),1);
		alpha += am;
		if(alpha > endalpha){alpha = endalpha;}
		scale -= sm;
		wait(1);
	}
	Obj_Delete(charge);
}

task chargeparticle{
	let scale = 2.5;
	let angle = rand(0,360);
	let cosin = 150;
	let n = 3;
	let n2 = 7;
	loop(n2){
		loop(n){
			let path = GetCurrentScriptDirectory~"./img/maple.png";
			let leaf = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(leaf, BLEND_ADD_ARGB);
			ObjPrim_SetTexture(leaf, path);
			ObjSprite2D_SetSourceRect(leaf, 32, 0, 64, 32);
			ObjSprite2D_SetDestCenter(leaf);
			Obj_SetRenderPriorityI(leaf,24);
			ObjRender_SetAlpha(leaf, 120);
			ObjRender_SetScaleXYZ(leaf, scale, scale, 1);
			enddelete2(leaf);
			let angle2 = rand(0,360);
			ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
			ObjRender_SetPosition(leaf, ObjMove_GetX(objBoss) + cosin * cos(angle), ObjMove_GetY(objBoss) + cosin * sin(angle),1);
			moveparticle(leaf,cosin,angle,angle2);
			angle += 360/n;
		}
		angle = rand(0,360);
		wait(4);
	}
}

task moveparticle(leaf,cosin,angle,angle2){
	let n = 25;
	let cosinm = cosin/n;
	let sm = 2;
	loop(n){
		ObjRender_SetPosition(leaf, ObjMove_GetX(objBoss) + cosin * cos(angle), ObjMove_GetY(objBoss) + cosin * sin(angle),1);
		ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
		angle2 += sm;
		cosin -= cosinm;
		wait(1)
	}
	Obj_Delete(leaf);
}

task chargerelease(color){
	SE_Play(chargefinish,60);
	let r = 0;
	let b = 0;
	let g = 0;
	if(color == "red"){
		let c2 = 90;
		r = 235;
		b = c2;
		g = c2;
	}
	if(color == "blue"){
		let c2 = 80;
		r = c2;
		b = 255;
		g = c2;
	}
	if(color == "pink"){
		let c3 = 235;
		r = c3;
		b = c3;
		g = 50;
	}
	if(color == "green"){
		let c2 = 100;
		r = c2;
		b = c2;
		g = 235;
	}
	let path = GetCurrentScriptDirectory~"./img/whitecircle.png";
	let charge = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(charge, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(charge, path);
	ObjSprite2D_SetSourceRect(charge, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(charge);
	Obj_SetRenderPriorityI(charge,24);
	ObjRender_SetColor(charge, r, g, b);
	let n = 35;
	let alpha = 180;
	let endalpha = 0;
	let scale = 0;
	let mscale = 1.5;
	let am = (alpha-endalpha)/n;
	let sm = mscale/n;
	releaseparticles;
	enddelete2(charge);
	loop(n){
		ObjRender_SetAlpha(charge,alpha);
		ObjRender_SetScaleXYZ(charge, scale, scale, 1);
		ObjRender_SetPosition(charge, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss),1);
		alpha -= am;
		scale += sm;
		wait(1);
	}
	Obj_Delete(charge);
}

task releaseparticles{
	let scale = 3.5;
	let angle = rand(0,360);
	let cosin = 0;
	let n = 8;
	let n2 = 3;
	let alpha = 170;
	loop(n2){
		loop(n){
			let path = GetCurrentScriptDirectory~"./img/maple.png";
			let leaf = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(leaf, BLEND_ADD_ARGB);
			ObjPrim_SetTexture(leaf, path);
			ObjSprite2D_SetSourceRect(leaf, 32, 0, 64, 32);
			ObjSprite2D_SetDestCenter(leaf);
			Obj_SetRenderPriorityI(leaf,24);
			ObjRender_SetAlpha(leaf, alpha);
			ObjRender_SetScaleXYZ(leaf, scale, scale, 1);
			enddelete2(leaf);
			let angle2 = rand(0,360);
			ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
			ObjRender_SetPosition(leaf, ObjMove_GetX(objBoss) + cosin * cos(angle), ObjMove_GetY(objBoss) + cosin * sin(angle),1);
			moveparticle2(leaf,cosin,angle,alpha);
			angle += 360/n;
		}
		angle = rand(0,360);
		wait(7);
	}
}

task moveparticle2(leaf,cosin,angle,alpha){
	let n = 35;
	let cosinmax = 300;
	let cosinm = cosinmax/n;
	let sm = 3;
	let am = alpha/n;
	let smm = sm/n/3;
	let angle2 = rand(0,360);
	loop(n){
		ObjRender_SetPosition(leaf, ObjMove_GetX(objBoss) + cosin * cos(angle), ObjMove_GetY(objBoss) + cosin * sin(angle),1);
		ObjRender_SetAngleXYZ(leaf, angle2,angle2,angle2);
		ObjRender_SetAlpha(leaf, alpha);
		alpha += -am;
		angle2 += sm;
		sm -= smm;
		cosin += cosinm;
		wait(1)
	}
	Obj_Delete(leaf);
}


task EnemyMarker{
	let marker = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(marker, 0.1);
	ObjPrim_SetTexture(marker,GetCurrentScriptDirectory() ~ "default_system/img/font1.png");	
	ObjSprite2D_SetSourceRect(marker, 932, 992, 1020, 1024);
	ObjRender_SetAlpha(marker, 255);
	ObjSprite2D_SetDestCenter(marker);
	ObjRender_SetScaleX(marker, 1*rez);
	ObjRender_SetScaleY(marker, 0.5*rez);
	//markeralpha(marker);
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetPosition(marker, (ObjMove_GetX(objBoss)+32)*rez, 472*rez, 1);
		wait(1);
	}
	Obj_Delete(marker);
}

task markeralpha(marker){
	let alpha = 255;
	let blink = 1.2;
	let c = 0;
	while(!Obj_IsDeleted(marker)){
		if(c == 0){blink = 1; c+=1;}
		else if(c == 1){blink = 1.04; c = 0;}
		alpha = GetDistance(GetPlayerX(),0,ObjMove_GetX(objBoss),0)*4;
		if(alpha < 255){alpha = 255;}
		if(alpha > 255){alpha = 255;}
		ObjRender_SetAlpha(marker, alpha/blink);
		wait(5);
	}
}

task SpellDamage{
	ObjEnemy_SetDamageRate(objBoss,0,0);
}

let idle = 0;

task bmovementgraphic(i, f, pdc){
	if(i == -1){move = 2}
	else{move = 1;}
	loop(f){
		if(pdc == pdcount){yield;}
	}
	move = 0;
}

let movei = 0;
let movei2 = 0;
let movec = 0;
let movec2 = 0;

task bmovement(x1, x2, y1, y2, f){
	let x = round(rand(x1,x2));
	let y = round(rand(y1,y2));
	movei = round(rand(0, 1000));
	movei2 = round(rand(0, 1000));
	if(movei > 500){movei = 1;}
	else{movei = -1;}
	if(movec == 2){movei = -1;}
	if(movec == -2){movei = 1;}
	if(movei2 > 500){movei2 = 1;}
	else{movei2 = -1;}
	if(movec2 == 1){movei2 = -1;}
	if(movec2 == -1){movei2 = 1;}
	if(ObjMove_GetX(objBoss) + movei*x > GetCenterX+100 || ObjMove_GetX(objBoss) + movei*x < GetCenterX-100){
		movei = movei * -1;
	}
	if(ObjMove_GetY(objBoss) + movei2*y > (GetCenterY-224)+160 || ObjMove_GetY(objBoss) + movei2*y < (GetCenterY-224)+30){
		movei2 = movei2 * -1;
	}
	if(spriteflip == 1){
		bmovementgraphic(movei * -1 ,f, pdcount);
	}
	else{
		bmovementgraphic(movei ,f, pdcount);
	}
	wait(2);
	ObjMove_SetDestAtFrame(objBoss, BossX + movei*x, BossY + movei2*y, f);
	movec += movei;
	movec2 += movei2;
	
}

task bmoveto(x, y, f){
	if(BossX == x && BossY == y){return;}
	let i = 1;
	if(BossX > x){i = -1;}
	bmovementgraphic(i,f, pdcount);
	wait(2);
	ObjMove_SetDestAtFrame(objBoss, x, y, f);
}



task MagicCircle{
	//if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){
		let path = GetCurrentScriptDirectory~"./img/mcircle.png";
		let mc = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(mc, path);
		ObjSprite2D_SetSourceRect(mc, 0, 0, 256, 256);
		ObjSprite2D_SetDestCenter(mc);
		ObjRender_SetBlendType(mc, BLEND_ADD_ARGB);
		
		Obj_SetRenderPriorityI(mc,25);
		ObjRender_SetScaleXYZ(mc, 1, 1, 1);
		ObjRender_SetColor(mc, 255, 200, 200);
		let n = 0;
		let n2 = 80;
		let s = 0;
		let sc = 0.0025;
		let ap = 40/n2;
		let alpha = 100;
		ObjRender_SetAlpha(mc, alpha);
		
		while(!Obj_IsDeleted(objBoss)){
			ObjRender_SetAlpha(mc, alpha);
			ObjRender_SetPosition(mc, ObjRender_GetX(sprite), ObjRender_GetY(sprite), 1);
			ObjRender_SetAngleZ(mc, ObjRender_GetAngleZ(mc)-4.5);
			if(n != n2 && s == 0){
				ObjRender_SetScaleX(mc, ObjRender_GetScaleX(mc)-sc);
				ObjRender_SetScaleY(mc, ObjRender_GetScaleY(mc)-sc);
				n += 1;
				alpha += ap;
				if(n == n2){s = 1; n = 0;}
			}
			if(n != n2 && s == 1){
				ObjRender_SetScaleX(mc, ObjRender_GetScaleX(mc)+sc);
				ObjRender_SetScaleY(mc, ObjRender_GetScaleY(mc)+sc);
				n += 1;
				alpha -= ap;
				if(n == n2){s = 0; n = 0;}
			}

			wait(1);
		}
		Obj_Delete(mc);
	//}
}


task auraposition(){
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetPosition(sprite, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)+floaty, 1);
		ObjRender_SetPosition(sprite2, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss)+floaty, 1);
		wait(1);
	}
	Obj_Delete(sprite);
	Obj_Delete(sprite2);
}


task TBossName(){
	let img = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(img, 0.75);
	ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/ename.png");	
	ObjSprite2D_SetSourceRect(img, 0, 222, 160, 244);
	ObjRender_SetScaleXYZ(img, 0.6*rez, 0.6*rez, 1);
	ObjSprite2D_SetDestCenter(img);
	ObjRender_SetPosition(img, 50*rez, 11*rez, 1); 
	ObjRender_SetAlpha(img, 0);
	yield;
	let objScene = GetEnemyBossSceneObjectID();
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 999){yield;}
	ObjRender_SetAlpha(img, 255);
}

task TMode(){
	let img = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(img, 0.1);
	ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/font1.png");	
	ObjSprite2D_SetSourceRect(img, 528, 642, 659, 673);
	if(diff == 1){
		ObjSprite2D_SetSourceRect(img, 528, 674, 659, 705);
	}
	if(diff == 2){
		ObjSprite2D_SetSourceRect(img, 528, 706, 659, 737);
	}
	ObjRender_SetScaleXYZ(img, 0.75*rez, 0.75*rez, 1);
	ObjSprite2D_SetDestCenter(img);
	ObjRender_SetPosition(img, 535*rez, 33*rez, 1); 
	//834-960
	//30-75
}
