let csd = GetCurrentScriptDirectory;
let imgdir = csd~"Images/";
let sounddir = csd~"Sounds/";
#include_function ".\System\soundlist.dnh"
#include_function ".\System\imagelist.dnh"

//////////////////////////////

let time = 70;
let MaxLife = 0;
let currenttime = 70;
let animcount = 0;
let yoffset = 0;
let shooting = false;
let warping = false;
let xscale = 1;
let yscale = 1;

let petalcolour = [RED21, BLUE21, PURPLE21, YELLOW21, ORANGE21, AQUA21];
let centercolour = [YELLOW04, ORANGE04, RED04];

/////

let delaydecrease = 0;
let maxdecreasecount = 6;
let decreasestep = 20;
let decreasecount = 0;

//////////////////////////////

if(GetCommonDataDefault("difficulty", -1) == -1){
	SetCommonData("difficulty", 1);
}

//////////////////////////////
SetRateScoreSystemEnable(false);
function Init
{
	SetTimer(time);
	LoadGraphic(imgWhite);
	LoadGraphic(imgBlack);
	LoadGraphic(imgleaf);
	LoadGraphic(CutinImg);

	LoadSE(time1);
	LoadSE(time2);
	LoadSE(hit1);
	LoadSE(hit2);
	LoadSE(wave);
	LoadSE(wave2);
	LoadSE(die);
	LoadSE(charge);
	LoadSE(bonus);
}

function Final
{
	DeleteGraphic(imgWhite);
	DeleteGraphic(imgBlack);
	DeleteGraphic(imgleaf);
	DeleteGraphic(CutinImg);
	DeleteSE(time1);
	DeleteSE(time2);
	DeleteSE(hit1);
	DeleteSE(hit2);
	DeleteSE(wave);
	DeleteSE(wave2);
	DeleteSE(die);
	DeleteSE(charge);
	DeleteSE(bonus);
}

function CheckForDamage{
	if(GetHitCount > 0){
		PlayDamageSound;
	}
}

function PlayDamageSound
{
	if(GetLife() <= MaxLife/8){
		PlaySE(hit2);
	} else
	{
		PlaySE(hit1);
	}
}

function PlayTimerSound
{
	if(GetTimer != currenttime)
	{
		if(GetTimer <= 4)
		{
			PlaySE(time2);
		} else if(GetTimer <= 10)
		{
			PlaySE(time1);
		}
		currenttime = GetTimer;
	}
}

function DrawLilyWhite
{
	SetTexture(imgWhite);
	if(GetSpeedY > -0.1)
	{
		yoffset = 65;
	} else if(!shooting)
	{
		yoffset = 0;
	} else
	{
		yoffset = 130;
	}
	SetGraphicRect(floor(animcount%4)*64 + 1, yoffset, floor(animcount%4)*64 + 64, yoffset + 63);
	if(!warping){
		if(xscale < 1){
			xscale += 0.1;
		}
		if(yscale > 1){
			yscale --;
		}
	} else {
		if(xscale > 0.1){
			xscale -= 0.1;
		}
		if(yscale < 9){
			yscale ++;
		}
	}
	SetGraphicScale(xscale, yscale);
	if(xscale > 0.1){
		DrawGraphic(GetX, GetY);
	}
	animcount += 0.1;
}

function DrawLilyBlack
{
	SetTexture(imgBlack);
	if(GetSpeedY > -0.1)
	{
		yoffset = 65;
	} else if(!shooting)
	{
		yoffset = 0;
	} else
	{
		yoffset = 130;
	}
	SetGraphicRect(floor(animcount%4)*64 + 1, yoffset, floor(animcount%4)*64 + 64, yoffset + 63);
	if(!warping){
		if(xscale < 1){
			xscale += 0.1;
		}
		if(yscale > 1){
			yscale --;
		}
	} else {
		if(xscale > 0.1){
			xscale -= 0.1;
		}
		if(yscale < 9){
			yscale ++;
		}
	}
	SetGraphicScale(xscale, yscale);
	if(xscale > 0.1){
		DrawGraphic(GetX, GetY);
	}
	animcount += 0.1;
}

function leafSplode
{
	let ang = rand(0, 360);
	let randnum = rand(20, 25);
	loop(randnum)
	{
		objectLeaf(ang);
		ang += 360/randnum;
	}
}

task objectLeaf(let angle)
{
	let obj = Obj_Create(OBJ_EFFECT);
	Obj_SetPosition(obj, GetX, GetY);
	ObjEffect_SetLayer(obj, 2);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_SetRenderState(obj,ADD);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetTexture(obj, imgleaf);
	ObjEffect_SetVertexUV(obj, 0, 0, 0);
	ObjEffect_SetVertexUV(obj, 1, 32, 0);
	ObjEffect_SetVertexUV(obj, 2, 0, 34);
	ObjEffect_SetVertexUV(obj, 3, 32, 34);
	ObjEffect_SetVertexXY(obj, 0, -16, -17);
	ObjEffect_SetVertexXY(obj, 1, 16, -17);
	ObjEffect_SetVertexXY(obj, 2, -16, 17);
	ObjEffect_SetVertexXY(obj, 3, 16, 17);
	
	let alpha = 255;
	let randspeed = rand(0.5, 6);
	let rotateang = rand(2, 7);
	let totalrotation = rand(0, 360);
	let rValue = rand(100, 255);
	let gValue = rand(100, 255);
	let bValue = rand(100, 255);
	let randscale = rand(1, 3);
	
	ObjEffect_SetVertexColor(obj, 0, alpha, rValue, gValue, bValue);
	ObjEffect_SetVertexColor(obj, 1, alpha, rValue, gValue, bValue);
	ObjEffect_SetVertexColor(obj, 2, alpha, rValue, gValue, bValue);
	ObjEffect_SetVertexColor(obj, 3, alpha, rValue, gValue, bValue);
	ObjEffect_SetScale(obj, randscale, randscale);
	loop(150)
	{
		Obj_SetPosition(obj, Obj_GetX(obj) + randspeed * cos(angle), Obj_GetY(obj) + randspeed * sin(angle));
		ObjEffect_SetAngle(obj, 0, 0, totalrotation);
		totalrotation += rotateang;
		yield;
	}
	loop(52){
		Obj_SetPosition(obj, Obj_GetX(obj) + randspeed / 2 * cos(angle), Obj_GetY(obj) + randspeed / 2 * sin(angle));
		ObjEffect_SetAngle(obj, 0, 0, totalrotation);
		totalrotation += rotateang;
	
		ObjEffect_SetVertexColor(obj, 0, alpha, rValue, gValue, bValue);
		ObjEffect_SetVertexColor(obj, 1, alpha, rValue, gValue, bValue);
		ObjEffect_SetVertexColor(obj, 2, alpha, rValue, gValue, bValue);
		ObjEffect_SetVertexColor(obj, 3, alpha, rValue, gValue, bValue);
		alpha -= 255 / 51;
		yield;
	}
}

task Flower(let X, let Y, let n)
{
	let angle = rand(0, 360);
	let randcol = rand_int(0, 5);
	PlaySE(shot1);
	PlaySE(wave);
	let center = Obj_Create(OBJ_SHOT);
	ObjShot_SetDelay(center, 30);
	Obj_SetPosition(center, X + rand(-12, 12), Y + rand(-12, 12));
	Obj_SetAngle(center, 0);
	ObjShot_SetGraphic(center, centercolour[rand_int(0, 2)]);
	ObjShot_SetBombResist(center, false);
	loop(60)
	{
		yield;
	}
	loop(n)
	{
		Petal(Obj_GetX(center), Obj_GetY(center), angle, petalcolour[randcol]);
		angle += 360/n;
	}
	let count = 0;
		task Petal(let Xpos, let Ypos, let PetalAngle, let col)
		{
			let pet = Obj_Create(OBJ_SHOT);
			Obj_SetPosition(pet, Xpos, Ypos);
			ObjShot_SetGraphic(pet, col);
			Obj_SetAngle(pet, PetalAngle);
			Obj_SetSpeed(pet, 0.5);
			ObjShot_SetBombResist(pet, false);
			loop(20)
			{
				yield;
			}
			Obj_SetSpeed(pet, 0);
			while(!Obj_BeDeleted(center))
			{
				yield;
			}
			PlaySE(wave);
			Obj_SetAngle(pet, 180 + Obj_GetAngle(pet));
			while(Obj_GetSpeed(pet) < 2)
			{
				Obj_SetSpeed(pet, Obj_GetSpeed(pet) + 0.06);
				yield;
			}
		}
		
	while(count < 100)
	{
		count ++;
		yield;
	}
	Obj_Delete(center);
}