#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["JeremyPlayer"]
#Title["Jeremy"]
#Text["A cartoon superhero who can [r] do nothing but jump."]
#Image["./Jeremyimg.png"]
#ReplayName["Jeremy"]

let GCSD = GetCurrentScriptDirectory;
let objPlayer = GetPlayerObjectID();
let effectimg = GCSD~"Effects.png";
let pX = 0;
let pY = 0;
let isplayerdead=0;
let canIshoot=0;

//SOUNDS---------
let death = GCSD~"./se/pldead.wav";
let shotsound = GCSD~"./se/plshot.wav";
let explosion = GCSD~"./se/flare.wav";
let grazesound = GCSD~"./se/se_graze.wav";
let bombsound = GCSD~"./se/se_nep00.wav";

@Initialize{
	
	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 1, 20);
	LoadPlayerShotData(GCSD~"JeremyShot.txt");
	SetPlayerDownStateFrame(30);
	addkeys;
	animate;
	loadsounds;
	FControl;
	focuscheck;
	
}

@MainLoop{
	
	pX = GetPlayerX;
	pY = GetPlayerY;
	yield;
	
}

@Event{
	
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		let s = GetPlayerSpell;
		if(s >= 1){
			SetScriptResult(true);
			SetPlayerSpell(s-1);
			Bomb;
			PlaySE(bombsound);
		}
		else{SetScriptResult(false);}
	}

	case(EV_PLAYER_SHOOTDOWN)
	{
		deathEffect(0);
		isplayerdead=1;
		canIshoot=1;
	}

	case(EV_HIT)
	{
		deathEffect(1);
		PlaySE(death);
	}

	case(EV_PLAYER_REBIRTH)
	{
		SetPlayerInvincibilityFrame(120);
		invframecolor(120);
		isplayerdead=0;
		canIshoot=0;
		SetPlayerSpell(3);
	}
	case(EV_GRAZE){
		PlaySE(grazesound);
	}
	
}

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

task addkeys {
	AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
}

task loadsounds{
	LoadSound(death);
	LoadSound(shotsound);
	LoadSound(explosion);
	LoadSound(grazesound);
	LoadSound(bombsound);
}

task animate{

	let frame = 0;
	
	ObjPrim_SetTexture(objPlayer, GetCurrentScriptDirectory~"JeremySprite.png");
	ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 80, 80);
	ObjSprite2D_SetDestCenter(objPlayer);
	ObjRender_SetScaleXYZ(objPlayer,0.8,0.8,0);
	Obj_SetRenderPriorityI(objPlayer,GetPlayerRenderPriorityI+10);
	
	loop{
		if(frame >= 0 && frame < 10){ObjSprite2D_SetSourceRect(objPlayer, 0, 0, 80, 80);}
		if(frame >= 10 && frame < 20){ObjSprite2D_SetSourceRect(objPlayer, 80, 0, 80*2, 80);}
		if(frame >= 20 && frame < 30){ObjSprite2D_SetSourceRect(objPlayer, 80*2, 0, 80*3, 80);}
		if(frame >= 30 && frame < 40){ObjSprite2D_SetSourceRect(objPlayer, 80*3, 0, 80*4, 80);}
		frame++;
		if(frame == 40){frame=0;}
		yield;
	}
	
}

task FControl{

	let Firehmm = -1;
	loop{
		
		if(GetVirtualKeyState(VK_SHOT) == KEY_HOLD && Firehmm == -1) {
			Firehmm = 0;
		}
		if(Firehmm%5==0 && canIshoot == 0){fire; if(GetCommonData("DialogueLock",1) == 1){PlaySE(shotsound);}}
		if(Firehmm >= 0) {
			Firehmm++;
		}
		if(Firehmm >= 30) {
			Firehmm = -1;
		}
		yield;
		
	}
	
}

task fire{
	
	if(GetVirtualKeyState(VK_SLOWMOVE) != KEY_HOLD){
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,290,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,285,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,275,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,270,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,265,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,255,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,250,3,1,1);
	}

	else if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,280,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,275,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,273,4,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,270,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,267,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,265,3,1,1);
		CreatePlayerShotA1(GetPlayerX,GetPlayerY,15,260,3,1,1);
	}
}

task Bomb{
	
	let objManage = GetSpellManageObject;
	InvFrame;
	
	let chairimg = ObjPrim_Create(OBJ_SPRITE_2D);
	let chair = ObjSpell_Create;
	let ischairboom = 0;
	ObjSpell_Regist(chair);
	ObjSpell_SetDamage(chair,10);
	ObjPrim_SetTexture(chairimg,GetCurrentScriptDirectory~"Chair.png");
	ObjSprite2D_SetSourceRect(chairimg,0,0,41,65);
	ObjSprite2D_SetDestCenter(chairimg);
	
	ObjRender_SetPosition(chair,GetPlayerX,GetPlayerY,0);

	let chairx = ObjRender_GetX(chair);
	let chairy = ObjRender_GetY(chair);
	let chairpos = 50;
	let chairdist = 30;
	chairthings;

	loop(60*5)
	{
		let allEnemy = GetAllEnemyID;
		
		ascent(i in 0..length(allEnemy))
		{
			if(Distance(chairx,chairy,ObjMove_GetX(allEnemy[i]),ObjMove_GetY(allEnemy[i]))<=40)
			{
				explodechair;
				return; // stop here and kill the task
			}
		}
		ObjRender_SetPosition(chair,GetPlayerX+chairdist*cos(chairpos),GetPlayerY+chairdist*sin(chairpos),0);
		chairpos+=3;
		if(chairdist < 90){chairdist++;}
		yield;
	}

	//if no contact was made continue on to throw the chair in at a specific angle
	explodechair;


	function Distance(x1,y1,x2,y2){return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5;}

	task explodechair{
		
		ischairboom=1;
		PlaySE(explosion);
		ObjSprite2D_SetSourceRect(chairimg,42,0,182,108);
		ObjRender_SetBlendType(chairimg,BLEND_ALPHA);
		ObjRender_SetAngleZ(chairimg,0);
		fade;
		ascent(i in 0..60){
			ObjRender_SetScaleXYZ(chairimg,1+(i/40)*2.3,1+i/40,0);
			ObjSpell_SetIntersectionCircle(chair,chairx,chairy,50+i);
			yield;
		}
		Obj_Delete(chair);
		Obj_Delete(chairimg);
		Obj_Delete(objManage);

		task fade{
			descent(f in 0..60){
				ObjRender_SetAlpha(chairimg,255/60*f);
				yield;
			}
		}
		
	}

	task InvFrame{
	
		let c=3;
		while(!Obj_IsDeleted(objManage)){
			SetPlayerInvincibilityFrame(1);
			if(c < 3){ObjRender_SetColor(objPlayer,255,255,255); c+=1;}
			if(c == 3){ObjRender_SetColor(objPlayer,0,0,255); c=0;}
			yield;
		}
		ObjRender_SetColor(objPlayer,255,255,255);
	
	}

	task chairthings{

		let chairang = 120;

		while(ischairboom == 0){
			chairx = ObjRender_GetX(chair);
			chairy = ObjRender_GetY(chair);
			ObjSpell_SetIntersectionCircle(chair,chairx,chairy,50);
			ObjRender_SetPosition(chairimg,chairx,chairy,0);
			ObjRender_SetAngleZ(chairimg,chairang);
			chairang+=3;
			yield;
		}

	}
	
}

task deathEffect(type){

	let eff = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetPosition(eff,pX,pY,0);
	ObjRender_SetBlendType(eff,BLEND_ALPHA);
	ObjPrim_SetTexture(eff,effectimg);
	ObjSprite2D_SetSourceRect(eff,200,5,262,67);
	ObjSprite2D_SetDestCenter(eff);
	ObjRender_SetAlpha(eff,0);
	if(type == 0){shrink;}
	else if(type == 1){grow;}
	descent(i in 0..30){
		ObjRender_SetAlpha(eff,255/30*i);
		yield;
	}
	Obj_Delete(eff);
	
	task shrink{ascent(s in 0..30){ObjRender_SetScaleXYZ(eff,3/30*s,3/30*s,0); yield;}}
	task grow{descent(s in 0..30){ObjRender_SetScaleXYZ(eff,3/30*s,3/30*s,0); yield;}}

}

task invframecolor(n){
	let c=3;
	loop(n){
		if(c < 3){ObjRender_SetColor(objPlayer,255,255,255); c+=1;}
		if(c == 3){ObjRender_SetColor(objPlayer,0,0,255); c=0;}
		yield;
	}
	ObjRender_SetColor(objPlayer,255,255,255);
}

task focuscheck{

	while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD){yield;}
	while(GetVirtualKeyState(VK_SLOWMOVE) != KEY_HOLD){yield;}
	focus;
	focuscheck;

}

task focus{
	
	let eff = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(eff,effectimg);
	ObjSprite2D_SetSourceRect(eff,65,0,126,57);
	ObjSprite2D_SetDestCenter(eff);
	ObjRender_SetScaleXYZ(eff,0.8,0.8,0);
	ObjRender_SetBlendType(eff,BLEND_ALPHA);
	let a = 0;
	while(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD && isplayerdead == 0){
		ObjRender_SetPosition(eff,GetPlayerX,GetPlayerY,0);
		ObjRender_SetAngleZ(eff,ObjRender_GetAngleZ(eff)+2);
		ObjRender_SetAlpha(eff,a);
		if(a < 255){a+=5;}
		yield;
	}
	Obj_Delete(eff);
	
}