#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Dross S3"]
#Text[""]
#System[".\system\Default_System.txt"]

#include"./lib/AllStarShot_Const.dnh"
#include"./lib/TheFunctions.txt"
#include"./BGTask.txt"
#include"./Animation_lib_satori.txt"

let Enemy = ID_INVALID;
let EnemyX = 0;
let EnemyY = 0;
//those Min and Max x y are taken from Shadow's Raimoo, Edited for full screen by Me(i am too honest :))
//if(ObjEnemy_GetInfo(Enemy, INFO_LIFE) == 0){break;}  my god "function"
let MinX = 0;
let MaxX = 640;
let MinY = 0;
let MaxY = GetStgFrameHeight*1.8;
let cutinImg = DrossCutin;
let Diff = ["none","Easy","Normal","Hard","Abnormal"];
let Spellname = ["Number 2 -Recollection of Terror Histories-","Shape Magic -Magical Oval-"];

let Mouv = 0;
let Mouv2 = 0;
let rango = 180;

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_REQUEST_LIFE){SetScriptResult(1800);}
	case(EV_REQUEST_TIMER){SetScriptResult(56);}

}

@Initialize{

	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Enemy);
SetShotAutoDeleteClip(50,50,50,50);
SetAutoDeleteObject(true);
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
SetDamageRateAtFrame(Enemy,25,10,120);
name("Dross Rotzank",0,10);
//LifeCircleBar_A(Enemy,0.2,0.0,false);
//LifeCircleBar_B(Enemy,0.2,0.0,false);
//LifeCircleBar_Marker(Enemy,0.8);
//LifeCircleBar_MarkerEx(Enemy,0.8,1200);
//LifeStock(1,1);
TWaveCircle_Shadertask(Enemy,0);
TAura(Enemy,0);
LoadSounds;
PlaySFX(spell);
EnemyMarker(Enemy,Enemy);
SpellBGoverlay;
st3SpellBG2;	
st3SpellBG1;
st3SpellBG3;			
ObjCutin_SetSpellcardS4(Spellname[GetLanguage], cutinImg, NAZRIN, 104, 104, 240);
ObjCutin_LaunchS3(BYAKUREN, cutinImg, Diff[GetDifficulty]);
InitializeAction(Enemy,DrossDot);
DrawBoss_Task(Enemy);
SetAction(Enemy,ACT_MOVE,70);
	ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 110, 20,4);
	BossActions;
}

@MainLoop{

	yield;
}



task BossActions{

	ShootBullets;
//Texto;
	while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 0){
		EnemyX = ObjMove_GetX(Enemy);
		EnemyY = ObjMove_GetY(Enemy);
		ObjEnemy_SetIntersectionCircleToShot(Enemy, EnemyX, EnemyY, 40);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, EnemyX, EnemyY, 30);
		yield;
	}
DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	Obj_Delete(Enemy);
	//loop(60){yield;}
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_PLAYER_SPELL_COUNT) == 0)
		{AddScore(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID,INFO_SPELL_SCORE));}
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_PLAYER_SHOOTDOWN_COUNT) == 0 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_PLAYER_SPELL_COUNT) == 0 ){
	SetCommonData("Perfects",GetCommonData("Perfects",perfects)+1);
	}
	if(GetCommonData("Perfects",perfects) == 5){SetPlayerLife(GetPlayerLife+1); SetCommonData("Perfects",0);NotifyEventAll(EV_USER+999,0)}
		loop(60){yield;}
	CloseScript(GetOwnScriptID);
}
function wait(t){loop(t){yield;}}

task ShootBullets{
let angul = rand(0,360);
let angul2 = 0;
wait(150);
PlaySFX(D2);
let Count1 = [0,17,23,28,32];
let Count2 = [0,3,4,5,5];
let as = 0;
let taim = 120;
let posx = 0;
let posy = 0;
summonWings;
ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 100, 20,4);
wait(60);
//attack2;
movingLasers(EnemyX+100*cos(0),EnemyY+100*sin(0));
movingLasers2(EnemyX+100*cos(180),EnemyY+100*sin(180));
attack3;
ControlMove;
loop{
if(Obj_IsDeleted(Enemy)){break;}
PlaySFX(tan00);	
ascent(i in 0..10){
laserbullets(EnemyX+100*cos(0),EnemyY+100*sin(0),0+i*360/10,LARGE_RED,0);
laserbullets2(EnemyX+100*cos(180),EnemyY+100*sin(180),0+i*360/10,LARGE_RED,0);
}
//ShootShapeC(EnemyX, EnemyY, 3, getAtanAngle(Enemy,Player), 3, 7, SMALL_RED, 0);

 wait(30);
  yield;
  }
}

function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}


task movement{
loop{
wait(180);
ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2+rand(100,-100), 110+rand(0,-60), 20,4);
}
}

task summonWings{

TailWing;
CenterWing;
FaceWing(100,0);
FaceWing(100,180);
FaceWing(100,-45);
FaceWing(100,225);
}

task CenterWing{

 let sbobj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = Ala1;
     
    let sbalpha = 0;
    let sbscroll = 0;
	let huehuehue = 0;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj2,sbimg);
	 ObjSprite2D_SetSourceRect(sbobj2,0,0,512,512);
    ObjSprite2D_SetDestCenter(sbobj2);
    //ObjSprite2D_SetDestRect(sbobj2,-256,-256-100,256,256+100);
    Obj_SetRenderPriorityI(sbobj2,39);
    //ObjRender_SetPosition(sbobj2,cx,cy,0);
    
	
	while(!Obj_IsDeleted(Enemy)){
	if(sbalpha<255){sbalpha+=6;}
if(sbalpha>255){sbalpha=255;}
if(sbscroll<0.7){sbscroll+=0.02;}
if(sbscroll>0.7){sbscroll=0.7;}
ObjRender_SetScaleXYZ(sbobj2,sbscroll,sbscroll,sbscroll);
ObjRender_SetAlpha(sbobj2,sbalpha);
    ObjRender_SetPosition(sbobj2,ObjMove_GetX(Enemy),ObjMove_GetY(Enemy),0);
	yield;
	}
	Obj_Delete(sbobj2);
}

task TailWing{

 let sbobj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = Ala2;
     
    let sbalpha = 0;
    let sbscroll = 0;
	let huehuehue = 0;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj2,sbimg);
	 ObjSprite2D_SetSourceRect(sbobj2,0,0,512,512);
    //ObjSprite2D_SetDestCenter(sbobj2);
    ObjSprite2D_SetDestRect(sbobj2,-256,0,256,512);
	
    Obj_SetRenderPriorityI(sbobj2,39);
    //ObjRender_SetPosition(sbobj2,cx,cy,0);
    
	
	while(!Obj_IsDeleted(Enemy)){
	if(sbalpha<255){sbalpha+=6;}
if(sbalpha>255){sbalpha=255;}
if(sbscroll<0.5){sbscroll+=0.02;}
if(sbscroll>0.5){sbscroll=0.5;}
ObjRender_SetScaleXYZ(sbobj2,sbscroll,sbscroll,sbscroll);
ObjRender_SetAlpha(sbobj2,sbalpha);
    ObjRender_SetPosition(sbobj2,ObjMove_GetX(Enemy),ObjMove_GetY(Enemy),0);
	ObjRender_SetAngleZ(sbobj2,0+sin(huehuehue)*30);
	huehuehue+= 1;
	yield;
	}
	Obj_Delete(sbobj2);
}

task FaceWing(range,angle){

 let sbobj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = Ala3;
     
    let sbalpha = 0;
    let sbscroll = 0;
	let huehuehue = 0;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj2,sbimg);
	 ObjSprite2D_SetSourceRect(sbobj2,0,0,256,256);
    ObjSprite2D_SetDestCenter(sbobj2);
    //ObjSprite2D_SetDestRect(sbobj2,-256,-256-100,256,256+100);
    Obj_SetRenderPriorityI(sbobj2,39);
    //ObjRender_SetPosition(sbobj2,cx,cy,0);
    
	
	while(!Obj_IsDeleted(Enemy)){
	if(sbalpha<255){sbalpha+=6;}
if(sbalpha>255){sbalpha=255;}
if(sbscroll<0.5){sbscroll+=0.02;}
if(sbscroll>0.5){sbscroll=0.5;}
ObjRender_SetScaleXYZ(sbobj2,sbscroll,sbscroll,sbscroll);
ObjRender_SetAlpha(sbobj2,sbalpha);
    ObjRender_SetPosition(sbobj2,ObjMove_GetX(Enemy)+range*cos(angle),ObjMove_GetY(Enemy)+range*sin(angle),0);
	yield;
	}
	Obj_Delete(sbobj2);
}


task movingLasers(x,y){
let radius = 50;
let angle = 0;
ascent(i in 0..10){
let lazor = CreateStraightLaserA1(x,y,0+i*(360/10),500,20,60,ST_LASER_RED,700000000);
RotateLaser(lazor,0+i*(360/10));
angle += 360/10;
}
}

task RotateLaser(obj,angle){
loop{
ObjStLaser_SetAngle(obj,angle+sin(Mouv)*rango);
yield;
}
}

task movingLasers2(x,y){
let radius = 50;
let angle = 0;
ascent(i in 0..10){
let lazor = CreateStraightLaserA1(x,y,0+i*(360/10),500,20,60,ST_LASER_RED,700000000);
RotateLaser2(lazor,0+i*(360/10));
angle += 360/10;
}
}

task RotateLaser2(obj,angle){
loop{
ObjStLaser_SetAngle(obj,angle-sin(Mouv2)*rango);
yield;
}
}

task laserbullets(x,y,angle,graphic,anv){
let radius = 0;
let bullet = CreateShotA1(x,y,8,angle,graphic,0);
while(!Obj_IsDeleted(bullet)){
ObjMove_SetPosition(bullet,x+radius*cos(angle+sin(Mouv)*rango),y+radius*sin(angle+sin(Mouv)*rango));

radius+=4;
yield;
}
}

task laserbullets2(x,y,angle,graphic,anv){
let radius = 0;
let bullet = CreateShotA1(x,y,8,angle,graphic,0);
while(!Obj_IsDeleted(bullet)){
ObjMove_SetPosition(bullet,x+radius*cos(angle-sin(Mouv2)*rango),y+radius*sin(angle-sin(Mouv2)*rango));

radius+=4;
yield;
}
}

task ControlMove{
let C1 = [0,0.09,0.12,0.15];
while(!Obj_IsDeleted(Enemy)){
Mouv += C1[GetDifficulty];
Mouv2 += C1[GetDifficulty];
yield;
}
}

task attack3{
let C2 = [0,2,2,2];
while(!Obj_IsDeleted(Enemy)){
Pilar(0,90,-10,3,C2[GetDifficulty],AMULET_RED,AMULET_BLUE);
Pilar(GetStgFrameWidth,90,190,3,C2[GetDifficulty],AMULET_RED,AMULET_BLUE);

wait(240);
}
}

task Pilar(x,y,angle,speed,waita,graphic,graphic2){
let a=CreateShotA1(x,y,speed,angle,0,1);
PlaySFX(tan04);	
ObjShot_SetSpellResist(a,true);
let angol = angle+90;
let angol2 = angle-90;
let asd = 0;
let asd2 = 0;
let radius= 0;
while(!Obj_IsDeleted(a) && !Obj_IsDeleted(Enemy)){
 let t =CreateShotA2(ObjMove_GetX(a)+radius*cos(angol),ObjMove_GetY(a)+radius*sin(angol),0,(angle+90)+asd,0.01,3.5,graphic,10);
 ObjShot_SetGraphicAtFrame(t,graphic2,30);
 let t2 =CreateShotA2(ObjMove_GetX(a)+radius*cos(angol2),ObjMove_GetY(a)+radius*sin(angol2),0,(angle-90)+asd2,0.01,3.5,graphic,10);
 ObjShot_SetGraphicAtFrame(t2,graphic2,30);
 PlaySFX(tan05);	
 SEatFrame(kira00,30);
 radius = 0+sin(asd)*40;
 asd+= 10;
 asd2-= 10;
 wait(waita);
 }
}

task ObjShot_SetGraphicAtFrame(obj,graphic,frame){
wait(frame);
ObjShot_SetGraphic(obj,graphic);
}

task SEatFrame(se,frame){
wait(frame);
PlaySFX(se);	
}