#include_function ".\variables.txt"
#include_function ".\function_cutin.txt"
let timer = 0; let timerMax = 1; let timerReduce = false;
let sc = false;
let bossSpellBonus = 0;
let bossSpellCanGet = 1;
let nowgraze = GetGraze;

function GetSpellBonus { return floor(bossSpellBonus*bossSpellCanGet); }
@Initialize {
	SetLife(5000);
	SetShotAutoDeleteClip(192, 192, 192, 192);
	SetCommonData("Dead Satori", false);
	SetCommonData("Dead Koishi", false);
	SetCommonData("Life Satori", 0);
	SetCommonData("Life Koishi", 0);
	SetCommonData("Lower Life", 0);
	SetCommonData("Life Master Max", 5000);
	InitMaster;
}
@MainLoop {
	SetCommonData("Boss Timer", timer);
	SetCommonData("Boss Timer Max", timerMax);
	if(timerReduce) { timer-=1; }
	AddLife(-GetCommonData("Lower Life"));
	if(timer <= 0 || GetCommonData("Dead Satori") || GetCommonData("Dead Koishi")) { AddLife(-GetLife); }
	if(GetCommonData("Spell Active")) {
		sc = true;
		if(OnPlayerMissed || OnBomb) { bossSpellCanGet = 0; }
		if(GetGraze > nowgraze) { bossSpellBonus+=(GetGraze-nowgraze)*100; }
	}
	SetCommonData("Lower Life", 0);
	SetCommonData("Life Master", GetLife);
	nowgraze = GetGraze;
	yield;
} 
@Finalize {
	SetCommonData("Dead Satori", true);
	SetCommonData("Dead Koishi", true);
	SetCommonData("Lower Life", 0);
	SetCommonData("Life Master", 0);
	SetCommonData("Boss Timer", 0);
	SetCommonData("Boss Timer Max", 1);
	SetCommonData("Spell Active", false);
	DeleteSE("se\seEnemyExplode01.wav");
	DeleteEnemyShotToItem(ALL);
	if(sc) {
		SetCommonData("Get Spell Card Bonus!", GetSpellBonus);
		AddScore(GetSpellBonus);
		if(GetSpellBonus > 0 && timer > 0) { PlaySE2("cardget"); }
	}
}
function wait ( let frames ) { loop(frames) { yield; } }
function PlaySE2(sfxname) { PlaySE(GCSD ~ "se_" ~ sfxname ~ ".wav"); }

task SpellLine (let spellid, let howmany) {
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, spellback);
	ObjEffect_SetLayer(obj, 5);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetVertexUV(obj, 0, 0, 0);
	ObjEffect_SetVertexUV(obj, 1, 256, 0);
	ObjEffect_SetVertexUV(obj, 2, 256, 36);
	ObjEffect_SetVertexUV(obj, 3, 0, 36);
	ObjEffect_SetVertexXY(obj, 0, -256, -32);
	ObjEffect_SetVertexXY(obj, 1, 0, -32);
	ObjEffect_SetVertexXY(obj, 2, 0, 4);
	ObjEffect_SetVertexXY(obj, 3, -256, 4);
	let obj2 = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj2, spellnames);
	ObjEffect_SetLayer(obj2, 5);
	ObjEffect_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj2, 4);
	ObjEffect_SetVertexUV(obj2, 0, 0, spellid*32);
	ObjEffect_SetVertexUV(obj2, 1, 384, spellid*32);
	ObjEffect_SetVertexUV(obj2, 2, 384, (spellid+1)*32);
	ObjEffect_SetVertexUV(obj2, 3, 0, (spellid+1)*32);
	ObjEffect_SetVertexXY(obj2, 0, -384, -32);
	ObjEffect_SetVertexXY(obj2, 1, 0, -32);
	ObjEffect_SetVertexXY(obj2, 2, 0, 0);
	ObjEffect_SetVertexXY(obj2, 3, -384, 0);
	let y = GetClipMaxY-4;
	let h = 128;
	descent(i in 0..60) {
		Obj_SetPosition(obj, GetClipMaxX-200*(-(cos(i*90/60)-1)), y-h*2);
		Obj_SetPosition(obj2, GetClipMaxX, y-h*2);
		ObjEffect_SetScale(obj2, 1+1*(-(cos(i*90/60)-1)), 1+1*(-(cos(i*90/60)-1)));
		ascent(j in 0..4) {
			ObjEffect_SetVertexColor(obj, j, 255*cos(i*90/60), 255, 255, 255);
			ObjEffect_SetVertexColor(obj2, j, 255*cos(i*90/60), 255, 255, 255);
		}
	yield; }
	descent(i in 0..60) {
		Obj_SetPosition(obj, GetClipMaxX, y-h+h*cos(i*180/60));
		Obj_SetPosition(obj2, GetClipMaxX, y-h+h*cos(i*180/60));
	yield; }
/*
	ObjEffect_SetLayer(obj, 1);
	ObjEffect_SetLayer(obj2, 1);
	descent(i in 0..60) {
		ascent(j in 0..4) {
			ObjEffect_SetVertexColor(obj, j, 255-160*cos(i*90/60), 255, 255, 255);
			ObjEffect_SetVertexColor(obj2, j, 255-160*cos(i*90/60), 255, 255, 255);
		}
	yield; }
*/
}

task ShowBonus(let bonus, let howmany) {
	bossSpellBonus = bonus;
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, numbers);
	ObjEffect_SetLayer(obj, 8);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjEffect_CreateVertex(obj, 3);
	Obj_SetPosition(obj, GetClipMaxX-96, 2);
	function CreateScoreArray {
		let num = floor(GetSpellBonus);
		let array = [];
		while(num >= 1) {
			let temp = truncate(num%10);
			num/=10;
			array = [temp]~array;
		}
		return array;
	}
	let alpha = 0;
	task HandleAlpha {
		wait(90);
		descent(i in 0..30) {
			alpha = 255*cos(i*90/30);
			wait(1);
		}
	}
	HandleAlpha;
	loop {
		if(bossSpellCanGet == 0) {
			ObjEffect_ResizeVertexCount(obj, 12);
			ObjEffect_SetVertexUV(obj, 0, 80, 16);
			ObjEffect_SetVertexUV(obj, 1, 112, 16);
			ObjEffect_SetVertexUV(obj, 2, 80, 32);
			ObjEffect_SetVertexUV(obj, 3, 112, 32);
			ObjEffect_SetVertexUV(obj, 4, 112, 16);
			ObjEffect_SetVertexUV(obj, 5, 80, 32);
			ObjEffect_SetVertexXY(obj, 0, 0, 0);
			ObjEffect_SetVertexXY(obj, 1, 32, 0);
			ObjEffect_SetVertexXY(obj, 2, 0, 16);
			ObjEffect_SetVertexXY(obj, 3, 32, 16);
			ObjEffect_SetVertexXY(obj, 4, 32, 0);
			ObjEffect_SetVertexXY(obj, 5, 0, 16);
			ObjEffect_SetVertexUV(obj, 6, 112, 16);
			ObjEffect_SetVertexUV(obj, 7, 144, 16);
			ObjEffect_SetVertexUV(obj, 8, 112, 32);
			ObjEffect_SetVertexUV(obj, 9, 144, 32);
			ObjEffect_SetVertexUV(obj, 10, 144, 16);
			ObjEffect_SetVertexUV(obj, 11, 112, 32);
			ObjEffect_SetVertexXY(obj, 6, 48+0, 0);
			ObjEffect_SetVertexXY(obj, 7, 48+32, 0);
			ObjEffect_SetVertexXY(obj, 8, 48+0, 16);
			ObjEffect_SetVertexXY(obj, 9, 48+32, 16);
			ObjEffect_SetVertexXY(obj, 10, 48+32, 0);
			ObjEffect_SetVertexXY(obj, 11, 48+0, 16);
			ascent(i in 0..12) {
				ObjEffect_SetVertexColor(obj, i, alpha, 255, 255, 255);
			}
		} else {
			let numarr = CreateScoreArray;
			ObjEffect_ResizeVertexCount(obj, (length(numarr)+2)*6);
			ObjEffect_SetVertexUV(obj, 0, 80, 16);
			ObjEffect_SetVertexUV(obj, 1, 112, 16);
			ObjEffect_SetVertexUV(obj, 2, 80, 32);
			ObjEffect_SetVertexUV(obj, 3, 112, 32);
			ObjEffect_SetVertexUV(obj, 4, 112, 16);
			ObjEffect_SetVertexUV(obj, 5, 80, 32);
			ObjEffect_SetVertexXY(obj, 0, 0, 0);
			ObjEffect_SetVertexXY(obj, 1, 32, 0);
			ObjEffect_SetVertexXY(obj, 2, 0, 16);
			ObjEffect_SetVertexXY(obj, 3, 32, 16);
			ObjEffect_SetVertexXY(obj, 4, 32, 0);
			ObjEffect_SetVertexXY(obj, 5, 0, 16);
			ascent(i in 0..length(numarr)) {
				let place = numarr[i];
				ObjEffect_SetVertexUV(obj, (i+1)*6+0, place*8, 16);
				ObjEffect_SetVertexUV(obj, (i+1)*6+1, (place+1)*8, 16);
				ObjEffect_SetVertexUV(obj, (i+1)*6+2, place*8, 32);
				ObjEffect_SetVertexUV(obj, (i+1)*6+3, (place+1)*8, 32);
				ObjEffect_SetVertexUV(obj, (i+1)*6+4, (place+1)*8, 16);
				ObjEffect_SetVertexUV(obj, (i+1)*6+5, place*8, 32);
				ObjEffect_SetVertexXY(obj, (i+1)*6+0, 48+i*6, 0);
				ObjEffect_SetVertexXY(obj, (i+1)*6+1, 48+i*6+8, 0);
				ObjEffect_SetVertexXY(obj, (i+1)*6+2, 48+i*6, 16);
				ObjEffect_SetVertexXY(obj, (i+1)*6+3, 48+i*6+8, 16);
				ObjEffect_SetVertexXY(obj, (i+1)*6+4, 48+i*6+8, 0);
				ObjEffect_SetVertexXY(obj, (i+1)*6+5, 48+i*6, 16);
			}
			ascent(i in 0..(length(numarr)+2)*6) {
				ObjEffect_SetVertexColor(obj, i, alpha, 255, 255, 255);
			}
		}
		yield;
	}
}