#TouhouDanmakufu[Player]
#ScriptVersion[2]
#Menu[                                Type A]
#Image[.\selectA.png]
#ReplayName[ReimuA]

script_player_main {
	let GCSD = GetCurrentScriptDirectory;
	let imgPlayer = GCSD ~ "pl00.png";
	let dx = 0; let dy = 0; let didle = 0; let dmovf = 0; let col = 0; let colF = 0;
	let objopts = [];
	let isFocusing = false;
	let deathtime = 0; let dying = false;
	function AsKoishi { return GetCommonDataDefault("Being Koishi", false); }
	#include_function ".\hitbox.txt"
	@Initialize {
		LoadGraphic(imgPlayer);
		LoadGraphic(objHitboxGraphic);
		LoadPlayerShotData(GCSD~"shotsP.txt");
		SetRebirthFrame(10); SetRebirthFrameReduction(1);
		SetInitialBombCount(2); SetAutoEffect(false);
		SetPlayerRect; SetPlayerRectIdle; SetPlayerRectMoving;
		CreateHitbox; ShotsAll;
		SetCommonData("JustDied", false);
		SetPlayerLifeImage(imgPlayer, 0, 0, 32, 48);
	}
	@Finalize {
		DeleteGraphic(imgPlayer);
	}
	@DrawLoop {
		SetTexture(imgPlayer);
		SetGraphicRect(dx*32, dy*48, (dx+1)*32, (dy+1)*48);
		SetGraphicScale(1, 1);
		SetGraphicAngle(0, 0, 0);
		SetRenderState(ALPHA);
		SetAlpha(255);
		SetColor(128+127*((-1)^col), 128+127*((-1)^col), 255);
		DrawGraphic(GetPlayerX, GetPlayerY);
		DrawHitbox;
	}
	@MainLoop {
		yield;
		isFocusing = GetKeyState(VK_SLOWMOVE)!=KEY_FREE;
		if(!GetCommonDataDefault("Event Active?", false)) {
			SetIntersectionCircle([GetPlayerX,GetCommonDataDefault("X Koishi", 0)][AsKoishi], [GetPlayerY,GetCommonDataDefault("Y Koishi", 0)][AsKoishi], 0);
		}
		if(GetTimeOfPlayerInvincibility > 0) { if(colF==0) { col++; col%=2; } colF++; colF%=2; } else { col = 0; colF = 0; }
		SetSpeed(4*!AsKoishi, 3/2*!AsKoishi);
		deathtime = 0;
		dying = false;
	}
	@Missed {
		DrawHitbox;
		if(AsKoishi) { deathtime++; }
		if(deathtime == GetRebirthFrame-1 && GetPlayerLife > 1) { dying = true; AddBomb(1); CallSpellCard; }
		yield;
	}
	@SpellCard {
		if(dying) {
			UseSpellCard("Die", NULL);
			dying = false;
		} else {
			if(AsKoishi) {
				UseSpellCard("Ancestors", NULL);
			} else {
				UseSpellCard("FantasySeal", NULL);
			}
		}
	}
	task ShotsAll {
		while(!GetCommonDataDefault("Chosen Difficulty", true)) { yield; }
		PlayerShot; PlayerShot2; ascent(i in 0..4) { PlayerOption(i); }
	}
	task SetPlayerRect {
		loop {
			if(dmovf == 0) {
				dx = didle; dy = 0;
			} else if(dmovf < 0) {
				dx = -dmovf-1; dy = 1;
			} else if(dmovf > 0) {
				dx = dmovf-1; dy = 2;
			}
			yield;
		}
	}
	task SetPlayerRectIdle {
		loop {
			didle+=1; didle%=8;
			loop(5) { yield; }
		}
	}
	task SetPlayerRectMoving {
		loop {
			if(GetKeyState(VK_RIGHT)!=KEY_FREE) {
				dmovf++;
			} else if(GetKeyState(VK_LEFT)!=KEY_FREE) {
				dmovf--;
			} else {
				if(dmovf > 1) { dmovf-=2; } else if(dmovf < -1) { dmovf+=2; } else { dmovf = 0; }
			}
			if(dmovf > 8) { dmovf = 8; } else if(dmovf < -8) { dmovf = -8; }
			if(AsKoishi) { dmovf = 0; }
			loop(1) { yield; }
		}
	}
	task PlayerShot {
		loop(5) { yield; }
		task CreatePlayerShot01C (x, y) {
			let obj = Obj_Create(OBJ_SHOT);
			Obj_SetPosition(obj, x, y);
			Obj_SetSpeed(obj, 17); Obj_SetAngle(obj, -90);
			ObjShot_SetDamage(obj, 4); ObjShot_SetPenetration(obj, 1);
			ObjShot_SetGraphic(obj, 1);
			Obj_SetAlpha(obj, 160);
			let gx = x; let gy = y;
			while(!Obj_BeDeleted(obj)) { gx = Obj_GetX(obj); gy = Obj_GetY(obj); yield; }
			if(!(gx == x && gy == y)) {
				let objg = Obj_Create(OBJ_EFFECT);
				Obj_SetPosition(objg, gx, gy-24);
				ObjEffect_SetTexture(objg, imgPlayer);
				ObjEffect_SetPrimitiveType(objg, PRIMITIVE_TRIANGLEFAN);
				ObjEffect_CreateVertex(objg, 4);
				ObjEffect_SetLayer(objg, 6);
				ObjEffect_SetRenderState(objg, ADD);
				ascent(i in 0..4) {
					ObjEffect_SetVertexUV(objg, 0, 128, 144+i*16);
					ObjEffect_SetVertexUV(objg, 1, 128+16, 144+i*16);
					ObjEffect_SetVertexUV(objg, 2, 128+16, 144+i*16+16);
					ObjEffect_SetVertexUV(objg, 3, 128, 144+i*16+16);
					ObjEffect_SetVertexXY(objg, 0, -8, -8);
					ObjEffect_SetVertexXY(objg, 1, 8, -8);
					ObjEffect_SetVertexXY(objg, 2, 8, 8);
					ObjEffect_SetVertexXY(objg, 3, -8, 8);
					ObjEffect_SetScale(objg, 1+i/6, 1+i/6);
					loop(2) { yield; }
				}
				Obj_Delete(objg);
			}
		}
		while(!GetCommonDataDefault("Ending", false)) {
			if(GetKeyState(VK_SHOT)!=KEY_FREE && !AsKoishi && !GetCommonDataDefault("Event Active?", false)) {
				PlaySE(GCSD~"se_playershot.wav");
				CreatePlayerShot01C(GetPlayerX-8, GetPlayerY);
				CreatePlayerShot01C(GetPlayerX+8, GetPlayerY);
			}
			loop(3) { while(OnMissed) { yield; } yield; }
		}
	}
	task PlayerShot2 {
		loop(5) { yield; }
		task HomingShot (x, y, v, ang) {
			let obj = Obj_Create(OBJ_SHOT);
			Obj_SetPosition(obj, x, y);
			Obj_SetSpeed(obj, 16+v); Obj_SetAngle(obj, ang);
			ObjShot_SetDamage(obj, 1); ObjShot_SetPenetration(obj, 1);
			ObjShot_SetGraphic(obj, 2);
			Obj_SetAlpha(obj, 160);
			let gx = x; let gy = y; let ga = ang; let t = 10; let time = 180; let doeff = true;
			while(!Obj_BeDeleted(obj)) {
				if(GetEnemyNum > 0) {
					let eid = 0;
					let edist = 1000;
					ascent(enemy in EnumEnemyBegin..EnumEnemyEnd) {
						let enemyID = EnumEnemyGetID(enemy);
						let enemyX = GetEnemyInfo(enemyID, ENEMY_X);
						let enemyY = GetEnemyInfo(enemyID, ENEMY_Y);
						let enemyDist = ((enemyX-gx)^2+(enemyY-gy)^2)^0.5;
						if(enemyDist < edist) { eid = enemyID; edist = enemyDist; }
					}
					let ex = GetEnemyInfo(eid, ENEMY_X);
					let ey = GetEnemyInfo(eid, ENEMY_Y);
					let ang = Obj_GetAngle(obj); ang = atan2(sin(ang), cos(ang));
					if((|ex-GetCenterX|) < 224 && (|ey-GetCenterY|) < 256) {
						let angt = atan2(ey-Obj_GetY(obj),ex-Obj_GetX(obj));
						if((|angt|) >= 90) {
							while(ang < 0) { ang+=360; } ang%=360;
							while(angt < 0) { angt+=360; } angt%=360;
						}
						if(((ex-Obj_GetX(obj))^2+(ey-Obj_GetY(obj))^2)^0.5 < 10) { time = 0; doeff = false; }
						Obj_SetAngle(obj, ang+(angt-ang)/t);
					}
					if(t > 1) { t-=1/2; }
				}
				time--; if(time < 0 || AsKoishi) { Obj_Delete(obj); }
			gx = Obj_GetX(obj); gy = Obj_GetY(obj); ga = Obj_GetAngle(obj); yield; }
			if(!(gx == x && gy == y) && doeff) {
				let objg = Obj_Create(OBJ_EFFECT);
				Obj_SetPosition(objg, gx, gy);
				ObjEffect_SetAngle(objg, 0, 0, ga-90);
				ObjEffect_SetTexture(objg, imgPlayer);
				ObjEffect_SetPrimitiveType(objg, PRIMITIVE_TRIANGLEFAN);
				ObjEffect_CreateVertex(objg, 4);
				ObjEffect_SetLayer(objg, 6);
				ObjEffect_SetRenderState(objg, ADD);
				ascent(i in 0..4) {
					ObjEffect_SetVertexUV(objg, 0, 128, 144+i*16);
					ObjEffect_SetVertexUV(objg, 1, 128+16, 144+i*16);
					ObjEffect_SetVertexUV(objg, 2, 128+16, 144+i*16+16);
					ObjEffect_SetVertexUV(objg, 3, 128, 144+i*16+16);
					ObjEffect_SetVertexXY(objg, 0, -8, -8);
					ObjEffect_SetVertexXY(objg, 1, 8, -8);
					ObjEffect_SetVertexXY(objg, 2, 8, 8);
					ObjEffect_SetVertexXY(objg, 3, -8, 8);
					ObjEffect_SetScale(objg, 1+i/6, 1+i/6);
					loop(2) { yield; }
				}
				Obj_Delete(objg);
			}
		}
		while(!GetCommonDataDefault("Ending", false)) {
			if(GetKeyState(VK_SHOT)!=KEY_FREE && !AsKoishi && !GetCommonDataDefault("Event Active?", false)) {
				HomingShot(Obj_GetX(objopts[0]), Obj_GetY(objopts[0]), 0, -90-10*[4,2][isFocusing]);
				HomingShot(Obj_GetX(objopts[1]), Obj_GetY(objopts[1]), 0, -90+10*[4,2][isFocusing]);
				HomingShot(Obj_GetX(objopts[2]), Obj_GetY(objopts[2]), 2, -90-10*[2,1][isFocusing]);
				HomingShot(Obj_GetX(objopts[3]), Obj_GetY(objopts[3]), 2, -90+10*[2,1][isFocusing]);
			}
			loop(3) { while(OnMissed) { yield; } yield; }
		}
	}
	task PlayerOption ( let opt ) {
		yield;
		objopts = objopts ~ [Obj_Create(OBJ_EFFECT)];
		let objg = objopts[opt];
		ObjEffect_SetTexture(objg, imgPlayer);
		ObjEffect_SetPrimitiveType(objg, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objg, 4);
		ObjEffect_SetVertexUV(objg, 0, 64, 192);
		ObjEffect_SetVertexUV(objg, 1, 64+16, 192);
		ObjEffect_SetVertexUV(objg, 2, 64+16, 192+16);
		ObjEffect_SetVertexUV(objg, 3, 64, 192+16);
		ObjEffect_SetVertexXY(objg, 0, -8, -8);
		ObjEffect_SetVertexXY(objg, 1, 8, -8);
		ObjEffect_SetVertexXY(objg, 2, 8, 8);
		ObjEffect_SetVertexXY(objg, 3, -8, 8);
		let x = GetPlayerX; let y = GetPlayerY; let z = 0; let s = 0;
		loop {
			ObjEffect_SetLayer(objg, [3,2][AsKoishi]);
			ObjEffect_SetScale(objg, s, s);
			Obj_SetPosition(objg, x, y);
			let xt = [[-32, 32, -16, 16],[-24, 24, -8, 8]][isFocusing];
			let yt = [[12, 12, -32, -32],[-16, -16, -28, -28]][isFocusing];
			let t = [2,3][isFocusing];
			x+=(GetPlayerX+xt[opt]*[1,0][AsKoishi]-x)/t; y+=(GetPlayerY+yt[opt]*[1,0][AsKoishi]-y)/t;
			s+=([1,0][AsKoishi]-s)/8;
			ObjEffect_SetAngle(objg, 0, 0, z); z+=2; z%=360;
			yield;
		}
	}
}

#include_script ".\playerspell.txt"