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

#include"./lib/WormShot_Const.dnh"
#include"./lib/TheFunctions.txt"
#include"./BGTask.txt"
#include"./Animation_lib_Kaori.txt"

let Enemy = ID_INVALID;
let EnemyX = 0;
let EnemyY = 0;
let MinX = 0;
let MaxX = 640;
let MinY = 0;
let MaxY = GetStgFrameHeight*1.8;
let cutinImg = KaoriCutin;
let Diff = ["none","Easy","Normal","Hard","Abnormal"];
let Spellname = ["no","Flower Sign \"Shape Seeds\""];

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_REQUEST_LIFE){SetScriptResult(1500);}
	case(EV_REQUEST_TIMER){SetScriptResult(67);}

}

@Initialize{

	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Enemy);
SetShotAutoDeleteClip(300,300 ,300 ,300 );
SetAutoDeleteObject(true);
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
SetDamageRateAtFrame(Enemy,35,20,160);
 name("Kaori Mystique",0,10);
 BossImage;
  //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(2,1);
 TWaveCircle_Shadertask(Enemy,6);
 TAura(Enemy,6);
 LoadSounds;
PlaySFX(spell);
EnemyMarker(Enemy,Enemy);
SpellBGoverlay;
st1SpellBG2;	
st3SpellBG1;
//st1SpellBG3;			
ObjCutin_SetSpellcardS4(Spellname[GetLanguage], cutinImg, NAZRIN, 240, 104, 240);
ObjCutin_LaunchS3(YABUSAME, cutinImg, Diff[GetDifficulty]);
//InitializeAction(Enemy,MichaelDot);
//DrawBoss_Task(Enemy);
//SetAction(Enemy,ACT_MOVE,70);
	ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 100, 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; DeleteShotAll(TYPE_ALL,TYPE_ITEM);}
	CloseScript(GetOwnScriptID);
}
function wait(t){loop(t){yield;}}

task ShootBullets{
let angul = 0;
let angul2 = 0;
wait(150);
//Concentration10(Enemy,120,90,50,90,200,2,10);
let Count1 = [0,10,12,14,16];
let Count2 = [0,3,4,5,6];
let Color = [SMALL_BLUE,SMALL_RED,SMALL_YELLOW];
let Color2 = [PELLET_BLUE,PELLET_RED,PELLET_YELLOW];
let as = 0;
loop{
angul = rand(0,360);
if(Obj_IsDeleted(Enemy)){break;}
PlaySFX(tan00);
ascent(i in 0..5){
GrowFlower(EnemyX,EnemyY,9,rand(45,135),Count2[GetDifficulty],5);
}
wait(200);
as =0;
  }
}
function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}

task GrowFlower(x,y,speed,angle,shape,count){
	let lelb = CreateShotA2(x,y ,speed ,angle ,-0.1 ,0 ,LARGE_RED ,0 ); //Creates the bullet
	ObjShot_SetSpellResist(lelb,true );
	ObjMove_AddPatternA2(lelb,90,4 ,angle+180 ,0 ,rand(0.5,-0.5) ,0 ); //after 90 frames (1.5 secs) the bullet comes back to the enemy with slight angular velocity
	ObjShot_SetIntersectionEnable(lelb,false); //Bullet can't harm the player
	ObjRender_SetAlpha(lelb,128 ); //trans parent
	wait(90); //wait for the AddPatternA2 to happen
	ObjShot_SetIntersectionEnable(lelb,true); //Enables Collision
	ObjRender_SetAlpha(lelb,255); //not more transparent!
	loop(40){ 
		if(Obj_IsDeleted(Enemy)){break;}
		PlaySFX(tan05);
		let shota = CreateShotA1(ObjMove_GetX(lelb),ObjMove_GetY(lelb) ,0 ,ObjMove_GetAngle(lelb) ,OVAL_GREEN ,0 );
		ObjShot_SetDeleteFrame(shota,120 );
		wait(3);
	}
	ObjMove_SetSpeed(lelb,0 ); //STOP!
	wait(30); //get ready b/c this is going to be harDDCore
	if(!Obj_IsDeleted(lelb)){ShootShapeC(ObjMove_GetX(lelb), ObjMove_GetY(lelb), 2, rand(0,360), shape, count, SMALL_RED, 10); PlaySFX(kira00);}
	Obj_Delete(lelb);

}

function ShootShapeC(spawnX, spawnY, v, angle, num, bside, graphic, delay) {
	ascent(i in 0..num) {
		let sx = spawnX+cos(angle+i*360/num); let sy = spawnY+sin(angle+i*360/num); let sxN = spawnX+cos(angle+(i+1)*360/num); let syN = spawnY+sin(angle+(i+1)*360/num);
		GravityShot(spawnX, spawnY, v*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5), atan2(sy-spawnY,sx-spawnX),0.03,90, graphic, delay);
		ascent(j in 0..bside) {
			let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
			GravityShot(spawnX, spawnY, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5), atan2(sy2-spawnY,sx2-spawnX),0.03,90, graphic, delay);
		}
	}
}

function GravityShot(x,y,speed,angle,grvSpeed,grvAngle,graphic,delay){
	let xSpeed = speed * cos( angle );

	let ySpeed = speed * sin( angle );

	let accXSpeed = grvSpeed * cos( grvAngle );

	let accYSpeed = grvSpeed * sin( grvAngle );

	let maxXSpeed = [ 99, - 99 ][ accXSpeed < 0 ];

	let maxYSpeed = [ 99, - 99 ][ accYSpeed < 0 ];

	let gs=CreateShotB2(x,y,xSpeed,ySpeed,accXSpeed,accYSpeed,maxXSpeed,maxYSpeed,graphic,delay);
return gs;
}

