#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID[Reisen]
#Title["Reisen Udongein Inaba"]
#Text["hot gun action"]
#Image["./img/cutin.png"]
#ReplayName["Reisen"]

#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 PlayerSprite = CSD ~ "./img/reisensprite.png";
let HitboxSprite = CSD ~ "./img/hitbox.png";
let PlayerShot = CSD ~ "./p_shot.dnh";
let ExpSprite = CSD ~ "./img/Explosion.png";
let SplSprite = CSD ~ "./img/bomb.png";

let spin = 0;
let spl = 0;

let ammo = 6;
let delay = 0;
let ammodl = 0;

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL){
		if(GetPlayerSpell > 0){
			SetScriptResult(true);
			SetPlayerSpell(GetPlayerSpell-1);
			Bomb;
		}
		else{
			SetScriptResult(false);
		}
	}
	case(EV_HIT){
		PlaySE(Pichuun);
		Explosion;
		SetCommonData("PlayerPosX", GetPlayerX);
		SetCommonData("PlayerPosY", GetPlayerY);
	}
	case(EV_PLAYER_SHOOTDOWN){
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		ObjRender_SetAlpha(PlayerObj,0);
		ammo = 0;
		ammodl = 0;
	}
	case(EV_PLAYER_REBIRTH){
		SetForbidPlayerShot(false);
		//PlaySE(Pichuun2);
		SetPlayerInvincibilityFrame(120);
		//ObjMove_SetPosition(PlayerID, GetCommonData("PlayerPosX", NULL), GetCommonData("PlayerPosY", NULL));
		BlinkEffect(120);
		ammodl = 0;
		SetPlayerSpell(3);
	}
	case(EV_GRAZE){
		AddScore(100);
		PlaySE(GrazeSE);
	}
}

@Initialize{
	LoadPlayerShotData(PlayerShot);
	SetPlayerSpeed(3.5,1.5);
	ObjPlayer_AddIntersectionCircleA1(PlayerID,0,0,1,24);
	//SetPlayerDownStateFrame(2);
	SetPlayerDownStateFrame(60);
	SetPlayerRebirthFrame(30);
	SetPlayerAutoItemCollectLine(200);
	
	RenderPlayer;
	RenderHitbox(GetPlayerX,GetPlayerY);
	
	SetPlayerLife(6);
	SetPlayerSpell(3);

	AmmoCount;
	ShotCheck;
}

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

	spl++;
	if(GetCommonData("isShotRecently", false) == false && GetPlayerState != EV_PLAYER_SHOOTDOWN){
		if(isFocus){ ammodl += 2; }
		else if(!isFocus){ ammodl++; }
	}
	if(ammodl >= 30){
		if(ammo < 6){ ammo++; PlaySE(ReloadSE); }
		ammodl = 0;
	}

	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,50);
	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,50,32,50*2); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,50,32*2,50*2); }
			if(moveFrame >= 30 && moveFrame < 50) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,50,32*3,50*2); }
			if(moveFrame >= 45 && moveFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,50,32*4,50*2); }
			if(moveFrame >= 60 && moveFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,50,32*3,50*2); }
			if(moveFrame >= 75 && moveFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32,50,32*2,50*2); }
			if(moveFrame >= 90) { ObjSprite2D_SetSourceRect(PlayerObj,0,50,32,50*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,50,32,50*2); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,50,32*2,50*2); }
			if(moveFrame >= 30 && moveFrame < 50) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,50,32*3,50*2); }
			if(moveFrame >= 45 && moveFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,50,32*4,50*2); }
			if(moveFrame >= 60 && moveFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,50,32*3,50*2); }
			if(moveFrame >= 75 && moveFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32,50,32*2,50*2); }
			if(moveFrame >= 90) { ObjSprite2D_SetSourceRect(PlayerObj,0,50,32,50*2); }
			idleFrame = 0;
		}
		else{
			if(idleFrame < 15) { ObjSprite2D_SetSourceRect(PlayerObj,0,0,32,50); }
			if(idleFrame >= 15 && idleFrame < 30) { ObjSprite2D_SetSourceRect(PlayerObj,32,0,32*2,50); }
			if(idleFrame >= 30 && idleFrame < 45) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,0,32*3,50); }
			if(idleFrame >= 45 && idleFrame < 60) { ObjSprite2D_SetSourceRect(PlayerObj,32*3,0,32*4,50); }
			if(idleFrame >= 60 && idleFrame < 75) { ObjSprite2D_SetSourceRect(PlayerObj,32*2,0,32*3,50); }
			if(idleFrame >= 75 && idleFrame < 90) { ObjSprite2D_SetSourceRect(PlayerObj,32,0,32*2,50); }
			if(idleFrame >= 90) { ObjSprite2D_SetSourceRect(PlayerObj,0,0,32,50); }
			moveFrame = 0;
		}
		
		idleFrame = idleFrame+2;
		if(idleFrame > 90){idleFrame = 0;}
		
		moveFrame = moveFrame+2;
		if(moveFrame > 90){moveFrame = 60;}
		
		yield;
	}
}

task RenderHitbox(x,y){
	let alpha = 0;
	let spin = 0;
	let scale = 0;
	
	ObjPrim_SetTexture(HitboxObj, HitboxSprite);
	Obj_SetRenderPriorityI(HitboxObj, 32);
	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_SetPosition(HitboxObj,GetPlayerX,GetPlayerY,0);
		ObjRender_SetScaleXYZ(HitboxObj, scale, scale, 0);
		ObjRender_SetAngleZ(HitboxObj,spin);
		ObjRender_SetAlpha(HitboxObj, alpha);
		
		if(isFocus){
			if(alpha < 225){ alpha += 15; }
			if(scale < 1){ scale += 0.2; }
		}
		else{
			if(alpha > 0){ alpha -= 15; }
			if(scale > 0){ scale -= 0.2; }
		}
		
		yield;
	}
}

task Shot{
	if(delay > 0){ delay--; }

	if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH){                    
		if(IsPermitPlayerShot && ammo > 0 && delay == 0){
			BulletFire;
			SetCommonData("Shot", true);
			ammo = ammo-1;
			ammodl = 0;
			delay = 0;
		}
	}
}
task BulletFire{
	loop(1){
		let shot = CreatePlayerShotA1(GetPlayerX,GetPlayerY,50,270,100,1,1);
		//EffectTrail1(shot);
		//wait(3); 
	}
	PlaySE(GunSE);
}
task EffectTrail1(shot){
	while(!Obj_IsDeleted(shot)){
		CreateEffTrail1(shot);
		wait(1);
	}
}
task CreateEffTrail1(shot){
	let eff = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(eff, GetCurrentScriptDirectory ~ "./img/bullet.png");
	ObjSprite2D_SetSourceRect(eff, 0,0,16,42);
	ObjSprite2D_SetDestCenter(eff);
	ObjRender_SetPosition(eff, ObjMove_GetX(shot), ObjMove_GetY(shot), 0);
	ObjRender_SetAngleZ(eff, ObjMove_GetAngle(shot)+90);
	ObjRender_SetBlendType(eff, BLEND_ALPHA);
	//Obj_SetRenderPriorityI(eff, 75);
	ObjRender_SetScaleXYZ(eff, 1, 1, 1);

	let alpha = 100;
	while(!Obj_IsDeleted(eff)){
		ObjRender_SetAlpha(eff, alpha);
		//ObjRender_SetAngleZ(eff, ObjMove_GetAngle(shot)+90);
		alpha-=5;
		if(alpha <= 0){
			Obj_Delete(eff);
		}
		yield;
	}
	Obj_Delete(eff);
}


task ShotCheck{
	loop{
		if(GetCommonData("Shot", false) == true){
			SetCommonData("isShotRecently", true);
			SetCommonData("Shot", false);
			wait(60);
			SetCommonData("isShotRecently", false);
		}
		yield;
	}
}

task Bomb{
	SetCommonData("isSpell", true);

	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);

	SetPlayerInvincibilityFrame(128+120);
	BlinkEffect(128+120);

	SetPlayerSpeed(0.5,0.5);

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

	PlaySE(BombSE);
	PlaySE(SlashSE);

	CreateSpellWaves;
	ScreenShake(128, 8);
	loop(128){
		ObjSpell_SetIntersectionCircle(objSpell, GetStgFrameWidth/2, GetStgFrameHeight/2, 620);
		ObjSpell_SetDamage(objSpell, 10);
		//SetCommonData("alpha", GetCommonData("alpha", 0)+4);
		yield;
	}

	PlaySE(SlashSE);
	SetForbidPlayerShot(false);
	SetPlayerSpeed(3.5,1.5);
	Obj_Delete(GetSpellManageObject);
	wait(120);
	SetCommonData("isSpell", false);
}

task CreateSpellWaves{
	loop(128/15/2){
		CreateSpellWave(1);
		wait(15);
		CreateSpellWave(2);
		wait(15);
	}
}

task CreateSpellWave(type){
	let scl = 0;

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, SplSprite);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 30);
	ObjRender_SetX(obj, GetPlayerX);
	ObjRender_SetY(obj, GetPlayerY);
	ObjRender_SetScaleXYZ(obj, 0, 0, 0);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

	while(GetCommonData("isSpell", false) == true){
		ObjRender_SetScaleXYZ(obj, scl, scl, 0);
		if(type == 1){ ObjRender_SetAngleZ(obj, scl*100); }
		else if(type == 2){ ObjRender_SetAngleZ(obj, -scl*100); }
		ObjRender_SetAlpha(obj, GetCommonData("alpha", 0));
		scl += 0.1;
		yield;
	}
	Obj_Delete(obj);
}

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);
	PlaySE(Pichuun2);
	SetForbidPlayerSpell(false);
}

task AmmoCount{
	let objText = ObjText_Create();
	ObjText_SetText(objText, IntToString(ammo));
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 24);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriorityI(objText, 75);
	ObjRender_SetX(objText, GetStgFrameWidth-24);
	ObjRender_SetY(objText, GetStgFrameHeight-24);
	//ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	//ObjText_SetMaxWidth(objText, 24);

	let objText2 = ObjText_Create();
	ObjText_SetText(objText2, IntToString(ammo));
	ObjText_SetFontType(objText2, "Russell Square Regular");
	ObjText_SetFontSize(objText2, 12);
	ObjText_SetFontBold(objText2, true);
	ObjText_SetFontColorTop(objText2, 255, 255, 255);
	ObjText_SetFontColorBottom(objText2, 64, 64, 64);
	ObjText_SetFontBorderType(objText2, BORDER_FULL);
	ObjText_SetFontBorderColor(objText2,0, 0, 0);
	ObjText_SetFontBorderWidth(objText2, 1);
	Obj_SetRenderPriorityI(objText2, 75);
	ObjRender_SetX(objText2, GetPlayerX+5);
	ObjRender_SetY(objText2, GetPlayerY-25);
	//ObjText_SetHorizontalAlignment(objText2, ALIGNMENT_RIGHT);
	//ObjText_SetMaxWidth(objText2, 24);

	CheckVisible(objText2);
	loop{
		ObjText_SetText(objText, IntToString(ammo));
		ObjText_SetText(objText2, IntToString(ammo));
		ObjRender_SetX(objText2, GetPlayerX+5);
		ObjRender_SetY(objText2, GetPlayerY-25);
		yield;
	}
}
task CheckVisible(objText){
	let alpha = 255;

	loop{
		if(GetCommonData("isShotRecently", false) == false && ammo == 6){ if(alpha > 0){ alpha -= 15; } }
		else{ alpha = 255; }
		ObjRender_SetAlpha(objText, alpha);
		yield;
	}
}