#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID[Sanae]
#Title["Sanae Kochiya"]
#Text["by Awalys"]
#Image["./img/sanae.png"]
#ReplayName["Sanae"]

#include "./p_se_lib.dnh"

let CSD = GetCurrentScriptDirectory;
let PlayerID = GetPlayerObjectID;
let PlayerObj = ObjPrim_Create(OBJ_SPRITE_2D);
let HitboxObj = ObjPrim_Create(OBJ_SPRITE_2D);
let count = -1;
let isFocus = false;

let alpha = 0;

let PlayerSprite = CSD ~ "./img/sanaesprite.png";
let HitboxSprite = CSD ~ "./img/hitbox.png";
let PlayerShot = CSD ~ "./p_shot.dnh";
let ShotSprite = CSD ~ "./img/sanaeshot2.png";
let ExpSprite = CSD ~ "./img/Explosion.png";
let SplSprite = CSD ~ "./img/sanaebomb.png";

let fam1 = CreateFamiliar(1);
let fam2 = CreateFamiliar(1);
let fam3 = CreateFamiliar(2);
let fam4 = CreateFamiliar(2);

let fam1b = CreateFamiliar2(1);
let fam2b = CreateFamiliar2(1);
let fam3b = CreateFamiliar2(2);
let fam4b = CreateFamiliar2(2);

let fams = [fam1, fam2, fam3, fam4];
let famsb = [fam1b, fam2b, fam3b, fam4b];

let spin = 0;
let spl = 0;
let offset = 0;

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL){
		if(GetPlayerSpell > 0){
			SetScriptResult(true);
			SetPlayerSpell(GetPlayerSpell-1);
			Bomb;
		}
		else{
			SetScriptResult(false);
		}
	}
	case(EV_HIT){
		Play(Pichuun, 25);
		Explosion;
		SetCommonData("PlayerPosX", GetPlayerX);
		SetCommonData("PlayerPosY", GetPlayerY);
	}
	case(EV_PLAYER_SHOOTDOWN){
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		ObjRender_SetAlpha(PlayerObj, 0);
		ascent(i in 0..4){
			Obj_SetVisible(fams[i], 0);
			Obj_SetVisible(famsb[i], 0);
		}
		Obj_SetVisible(HitboxObj, 0);
		alpha = 0;
	}
	case(EV_PLAYER_REBIRTH){
		SetForbidPlayerShot(false);
		ascent(i in 0..4){
			Obj_SetVisible(fams[i], 1);
			Obj_SetVisible(famsb[i], 1);
		}
		Obj_SetVisible(HitboxObj, 1);
		SetPlayerInvincibilityFrame(120);
		//ObjMove_SetPosition(PlayerID, GetCommonData("PlayerPosX", NULL), GetCommonData("PlayerPosY", NULL));
		BlinkEffect(120);
		alpha = 225;
		SetPlayerSpell(3);
	}
	case(EV_GRAZE){
		AddScore(100);
		Play(GrazeSE, 25);
	}
}

@Initialize{
	LoadPlayerShotData(PlayerShot);
	SetPlayerSpeed(4.25,2);
	ObjPlayer_AddIntersectionCircleA1(PlayerID,0,0,1,24);
	//SetPlayerDownStateFrame(2);
	SetPlayerDownStateFrame(60);
	SetPlayerRebirthFrame(25);
	SetPlayerAutoItemCollectLine(200);
	SetPlayerClip(5, 5, GetStgFrameWidth-5, GetStgFrameHeight-5);
	
	RenderPlayer;
	RenderHitbox(GetPlayerX,GetPlayerY);
	
	// No cool health mechanic :C
	SetPlayerLife(7);
	SetPlayerSpell(3);
}

@MainLoop{
	if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
		isFocus = true;
	}
	if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PULL){
		isFocus = false;
	}
	Shot;
	FamiliarCommands;

	yield;
}

@Finalize{
	Obj_Delete(HitboxObj);
}

task RenderPlayer{
	let idleFrame = 0;
	let moveFrame = 0;
	
	ObjPrim_SetTexture(PlayerObj, PlayerSprite);
	Obj_SetRenderPriorityI(PlayerObj, 31);
	ObjRender_SetAlpha(PlayerObj, 255);
	ObjSprite2D_SetSourceRect(PlayerObj,0,0,32,46);
	ObjSprite2D_SetDestCenter(PlayerObj);
	ObjRender_SetPosition(PlayerObj,GetPlayerX,GetPlayerY,0);
	ObjRender_SetBlendType(PlayerObj, BLEND_ALPHA);
	
	while(!Obj_IsDeleted(PlayerObj)){
		ObjRender_SetPosition(PlayerObj,GetPlayerX,GetPlayerY,0);
		
		let dir = ObjMove_GetAngle(PlayerObj);
		let speed = ObjMove_GetSpeed(PlayerObj);
		
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			ObjRender_SetAngleY(PlayerObj,0);
			if(moveFrame < 15) { ObjSprite2D_SetSourceRect(PlayerObj,0,46,32,46*2); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,46,32*2,46*2); }
			if(moveFrame >= 30 && moveFrame < 46) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,46,32*3,46*2); }
			if(moveFrame >= 45 && moveFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,46,32*4,46*2); }
			if(moveFrame >= 60 && moveFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*4,46,32*5,46*2); }
			if(moveFrame >= 75 && moveFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32*5,46,32*6,46*2); }
			if(moveFrame >= 90 && moveFrame < 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*6,46,32*7,46*2); }
			if(moveFrame >= 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*7,46,32*8,46*2); }
			idleFrame = 0;
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			ObjRender_SetAngleY(PlayerObj,180);
			if(moveFrame < 15) { ObjSprite2D_SetSourceRect(PlayerObj,0,46,32,46*2); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,46,32*2,46*2); }
			if(moveFrame >= 30 && moveFrame < 46) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,46,32*3,46*2); }
			if(moveFrame >= 45 && moveFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,46,32*4,46*2); }
			if(moveFrame >= 60 && moveFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*4,46,32*5,46*2); }
			if(moveFrame >= 75 && moveFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32*5,46,32*6,46*2); }
			if(moveFrame >= 90 && moveFrame < 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*6,46,32*7,46*2); }
			if(moveFrame >= 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*7,46,32*8,46*2); }
			idleFrame = 0;
		}
		else{
			if(idleFrame < 15) { ObjSprite2D_SetSourceRect(PlayerObj,0,0,32,46); }
			if(idleFrame >= 15 && idleFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,0,32*2,46); }
			if(idleFrame >= 30 && idleFrame < 45) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,0,32*3,46); }
			if(idleFrame >= 45 && idleFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,0,32*4,46); }
			if(idleFrame >= 60 && idleFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*4,0,32*5,46); }
			if(idleFrame >= 75 && idleFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32*5,0,32*6,46); }
			if(idleFrame >= 90 && idleFrame < 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*6,0,32*7,46); }
			if(idleFrame >= 105) { ObjSprite2D_SetSourceRect(PlayerObj,32*7,0,32*8,46); }
			moveFrame = 0;
		}
		
		idleFrame = idleFrame+2;
		if(idleFrame > 105){idleFrame = 0;}
		
		moveFrame = moveFrame+2;
		if(moveFrame > 105){moveFrame = 60;}
		
		yield;
	}
}

task RenderHitbox(x,y){
	let spin = 0;
	
	ObjPrim_SetTexture(HitboxObj, HitboxSprite);
	Obj_SetRenderPriorityI(HitboxObj, 60);
	ObjRender_SetAlpha(HitboxObj, 225);
	ObjRender_SetScaleXYZ(HitboxObj, 1,1,0);
	ObjSprite2D_SetSourceRect(HitboxObj,0,0,63,63);
	ObjSprite2D_SetDestCenter(HitboxObj);
	ObjRender_SetPosition(HitboxObj,GetPlayerX,GetPlayerY,0);
	
	while(!Obj_IsDeleted(HitboxObj)){
		spin++;
		
		ObjRender_SetAlpha(HitboxObj, alpha);
		
		if(isFocus){
			ObjRender_SetPosition(HitboxObj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetAngleZ(HitboxObj,spin);
			alpha = 225;
		}
		else{
			alpha = 0;
		}
		
		yield;
	}
}

function CreateFamiliar(type){
	let objFam = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objFam, ShotSprite);
	Obj_SetRenderPriorityI(objFam, 48);

	if(type == 1){
		ObjSprite2D_SetSourceRect(objFam, 66, 15, 3*16+1, 0);
		ObjSprite2D_SetDestCenter(objFam);

	}
	else if(type == 2){
		ObjSprite2D_SetSourceRect(objFam, 81, 15, 4*16+2, 0);
		ObjSprite2D_SetDestCenter(objFam);

	}
	return objFam;
}

function CreateFamiliar2(type){
	let objFam2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objFam2, ShotSprite);
	ObjRender_SetScaleXYZ(objFam2, 1.35, 1.35, 0);
	ObjRender_SetAlpha(objFam2, 175);
	Obj_SetRenderPriorityI(objFam2, 47);

	if(type == 1){
		ObjSprite2D_SetSourceRect(objFam2, 66, 15, 3*16+1, 0);
		ObjSprite2D_SetDestCenter(objFam2);
	}
	else if(type == 2){
		ObjSprite2D_SetSourceRect(objFam2, 81, 15, 4*16+2, 0);
		ObjSprite2D_SetDestCenter(objFam2);
	}
	return objFam2;
}

task FamiliarCommands{
	ObjRender_SetPosition(fam1, GetPlayerX-30+offset, GetPlayerY-10, 0);
	ObjRender_SetPosition(fam2, GetPlayerX+30-offset, GetPlayerY-10, 0);
	ObjRender_SetPosition(fam3, GetPlayerX-60+offset*2, GetPlayerY+10, 0);
	ObjRender_SetPosition(fam4, GetPlayerX+60-offset*2, GetPlayerY+10, 0);

	ObjRender_SetPosition(fam1b, ObjRender_GetX(fam1), ObjRender_GetY(fam1), 0);
	ObjRender_SetPosition(fam2b, ObjRender_GetX(fam2), ObjRender_GetY(fam2), 0);
	ObjRender_SetPosition(fam3b, ObjRender_GetX(fam3), ObjRender_GetY(fam3), 0);
	ObjRender_SetPosition(fam4b, ObjRender_GetX(fam4), ObjRender_GetY(fam4), 0);

	if(isFocus == true){
		if(offset < 10){ offset += 2; }
	}
	else{
		if(offset > 0){ offset -= 2; }
	}
	
	spin += 5;
	ObjRender_SetAngleZ(fam1, spin);
	ObjRender_SetAngleZ(fam2, -spin);
	ObjRender_SetAngleZ(fam3, -spin);
	ObjRender_SetAngleZ(fam4, spin);

	ObjRender_SetAngleZ(fam1b, -spin);
	ObjRender_SetAngleZ(fam2b, spin);
	ObjRender_SetAngleZ(fam3b, spin);
	ObjRender_SetAngleZ(fam4b, -spin);

	/*if(isFocus == true){
		if(posX1 != GetPlayerX-20){ObjRender_SetPosition(fam1, posX1-1, GetPlayerY-10, 0);}
		if(posX2 != GetPlayerX+20){ObjRender_SetPosition(fam2, posX2+1, GetPlayerY-10, 0);}

		if(posX3 != GetPlayerX-40){ObjRender_SetPosition(fam3, posX3-1, GetPlayerY+10, 0);}
		if(posX4 != GetPlayerX+40){ObjRender_SetPosition(fam4, posX4+1, GetPlayerY+10, 0);}
	}
	else{
		if(posX1 != GetPlayerX-30){ObjRender_SetPosition(fam1, posX1-1, GetPlayerY-10, 0);}
		if(posX2 != GetPlayerX+30){ObjRender_SetPosition(fam2, posX2+1, GetPlayerY-10, 0);}

		if(posX3 != GetPlayerX-60){ObjRender_SetPosition(fam3, posX3-1, GetPlayerY+10, 0);}
		if(posX4 != GetPlayerX+60){ObjRender_SetPosition(fam4, posX4+1, GetPlayerY+10, 0);}
	}*/
}

task Shot{
		if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){
			count++;
		}
		if(GetVirtualKeyState(VK_SHOT) == KEY_PULL || !IsPermitPlayerShot){
			count = -1;
		}
		if(count >= 5){
			count = 0;
		}
		
		if(isFocus){
			if(count == 3){
				CreatePlayerShotA1(GetPlayerX,GetPlayerY,10,270,2,1,2);
				ascent(i in 1..3){
					CreatePlayerShotA1(GetPlayerX-7.5*i,GetPlayerY,10,270,2,1,2);
					CreatePlayerShotA1(GetPlayerX+7.5*i,GetPlayerY,10,270,2,1,2);
				}

				ascent(i in 0..2){
					let Sshot1 = CreatePlayerShotA1(ObjRender_GetX(fams[i]),ObjRender_GetY(fams[i]),10,270,3,1,1);
					let Sshot2 = CreatePlayerShotA1(ObjRender_GetX(fams[i+2]),ObjRender_GetY(fams[i+2]),10,285-30*i,3,1,1);
					ObjRender_SetColorHSV(Sshot1, 80, 100, 255); ObjRender_SetAngleXYZ(Sshot1,0,0,270);
					ObjRender_SetColorHSV(Sshot2, 80, 100, 255); ObjRender_SetAngleXYZ(Sshot2,0,0,270);
				}

				let shot1 = CreatePlayerShotA1(GetPlayerX-10,GetPlayerY,15,270,3,1,3);
				let shot2 = CreatePlayerShotA1(GetPlayerX+10,GetPlayerY,15,270,3,1,3);
				ObjRender_SetAngleXYZ(shot1,0,0,90);
				ObjRender_SetAngleXYZ(shot2,0,0,90);
				Play(ShotSE, 25);
			}
		}
		else{
			if(count == 3){
				ascent(i in 0..5){
					let shot = CreatePlayerShotA1(GetPlayerX,GetPlayerY,10,250+i*10,2.5,1,2);
					ObjRender_SetColorHSV(shot, 25, 150, 200);
				}

				ascent(i in 0..4){
					let Sshot = CreatePlayerShotA1(ObjRender_GetX(fams[i]),ObjRender_GetY(fams[i]),10,270,3,1,1);
					ObjRender_SetColorHSV(Sshot, 80, 100, 255);
					ObjRender_SetAngleXYZ(Sshot,0,0,270);
				}

				let shot1 = CreatePlayerShotA1(GetPlayerX-10,GetPlayerY,15,270,3,1,3);
				let shot2 = CreatePlayerShotA1(GetPlayerX+10,GetPlayerY,15,270,3,1,3);
				ObjRender_SetAngleXYZ(shot1,0,0,90);
				ObjRender_SetAngleXYZ(shot2,0,0,90);
				Play(ShotSE, 25);
			}
		}
}

task Bomb{
	SetCommonData("alpha", 0);

	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);

	SetPlayerInvincibilityFrame(256+120);
	BlinkEffect(256+120);

	SetPlayerSpeed(1.5,1);

	let objSpell = ObjSpell_Create;
	ObjSpell_Regist(objSpell);
	ObjSpell_SetEraseShot(objSpell, true);

	Play(BombSE, 30);
	Play(SlashSE, 45);

	CreateSpellSnakes;
	ScreenShake(256, 8);
	loop(200){
		ObjSpell_SetIntersectionCircle(objSpell, GetStgFrameWidth/2, GetStgFrameHeight/2, 620);
		ObjSpell_SetDamage(objSpell, 4);
		SetCommonData("alpha", GetCommonData("alpha", 0)+4);
		yield;
	}
	loop(56){
		ObjSpell_SetIntersectionCircle(objSpell, GetStgFrameWidth/2, GetStgFrameHeight/2, 620);
		ObjSpell_SetDamage(objSpell, 2);
		SetCommonData("alpha", GetCommonData("alpha", 0)-30);
		yield;
	}

	Play(SlashSE, 45);
	SetForbidPlayerShot(false);
	SetPlayerSpeed(4.25,2);
	Obj_Delete(GetSpellManageObject);
}

task CreateSpellSnakes{
	ascent(i in 0..14){
		CreateSpellSnake(30, i);
		wait(256/7-20);
	}
}

task CreateSpellSnake(spd, ang){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, SplSprite);
	ObjSprite2D_SetSourceRect(obj, 256,256,0,0);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 30);
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, GetStgFrameHeight+321);
	ObjRender_SetScaleXYZ(obj, 2, 2, 0);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	//ObjRender_SetAngleX(obj, 180*ang);

	loop{
		ObjRender_SetY(obj, ObjRender_GetY(obj)-spd);
		ObjRender_SetAlpha(obj, GetCommonData("alpha", 0));
		yield;
	}
}

function wait(n){loop(n){yield;}}

function ObjRender_SetDestAtSpeed(obj, x, y, speed){
	let posX = ObjRender_GetX(obj)+speed;
	let posY = ObjRender_GetY(obj)+speed;

	while(posX != x){
		ObjRender_SetPosition(obj, posX, ObjRender_GetY(obj), 0);
	}
	while(posY != y){
		ObjRender_SetPosition(obj, posX, ObjRender_GetY(obj), 0);
	}
}

task ScreenShake(frames, inten){
	loop(frames) {
		Set2DCameraFocusX(GetStgFrameWidth/2+rand(-inten,inten));
		Set2DCameraFocusY(GetStgFrameHeight/2+rand(-inten,inten));
		yield;
	}
	Reset2DCamera;
}

task Explosion{
	let animFrame = 0;

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, ExpSprite);
	ObjSprite2D_SetSourceRect(obj,0,0,32,34);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
	//ObjRender_SetScaleXYZ(obj, 1.25, 1.25, 0);
	ObjRender_SetAlpha(obj, 155);

	loop{
		if(animFrame < 10) { ObjSprite2D_SetSourceRect(obj,0,0,32,34); }
		if(animFrame >= 20 && animFrame < 30) { ObjSprite2D_SetSourceRect(obj,32,0,32*2,34); }
		if(animFrame >= 30 && animFrame < 40) { ObjSprite2D_SetSourceRect(obj,32*2,0,32*3,34); }
		if(animFrame >= 40 && animFrame < 50) { ObjSprite2D_SetSourceRect(obj,32*3,0,32*4,34); }
		if(animFrame >= 50) { Obj_Delete(obj); }

		animFrame += 2;
		ObjRender_SetScaleXYZ(obj, 1+animFrame*0.02, 1+animFrame*0.02, 0);
		yield;
	}
}

task BlinkEffect(frames){
	let blink = 0;
	loop(frames){
		blink++;

		if(blink == 2){
			ObjRender_SetAlpha(PlayerObj, 0);
		}
		if(blink == 4){
			ObjRender_SetAlpha(PlayerObj, 255);
			blink = 0;
		}
		yield;
	}
	ObjRender_SetAlpha(PlayerObj, 255);
	Play(Pichuun2, 25);
	SetForbidPlayerSpell(false);
}

function Play(sfx, vol){
	let se = ObjSound_Create;
	ObjSound_Load(se, sfx);
	ObjSound_SetSoundDivision(se, SOUND_SE);
	ObjSound_SetVolumeRate(se, vol);
	ObjSound_Play(se);
	Delet(se);
	return se;
}
task Delet(se){
	wait(60*9);
	Obj_Delete(se);
}