SetCommonData("HB", 0);

let DrawCounter = 0;
let DrawCounterTwo = 0;
let GraphicAngle = 0;

let left = false;
let right = true;
let g = 0;
let exp = 0;
let ghg = 0.1;

let Xcoordinate = 0;
let NotMoving = false;
let MovementDifference = 0;

sub RunEveryFrame{
	DrawCounter++;
	DrawCounterTwo++;
	GraphicAngle+=3;
	MovementDifference = Xcoordinate - GetX;
	Xcoordinate = GetX;
}

function GetVelocityX{
	return MovementDifference;
}

function DrawTokiko{
	if(GetVelocityX>0){
		right = true;
		left = false;
	}
	if(GetVelocityX<0){
		left = true;
		right = false;
	}
	if(left==true){
		g = 0;
	}else{
		g = 64;
	}
	if(GetVelocityX==0){
		NotMoving=true;
	}
	
	//Magic Circle
	if(GetCommonData("HB")>400){
		SetTexture(GetCurrentScriptDirectory~"MagicCircle.png");
		if(exp<10){
			exp++;
		}
		if(GetCommonData("HB")%20<16){
			ghg-=0.005;
		}else{
			ghg+=0.02;
		}
		SetGraphicRect(0, 0, 128, 128);
		SetGraphicScale(exp*0.15 - 0.1*sin(GetCommonData("HB")*3) + ghg, exp*0.15 - 0.1*sin(GetCommonData("HB")*3) + ghg);
		SetRenderState(ALPHA);
		SetGraphicAngle(0, 0, GraphicAngle);
		DrawGraphic(GetX, GetY);
	}
	
	SetGraphicAngle(0, 0, 0);
	SetRenderState(ALPHA);
	SetGraphicScale(1, 1);
	
	SetTexture(GetCurrentScriptDirectory~"tokikosprite.png");
	//wings
	if(DrawCounter%40<=3){
		SetGraphicRect(0, 256 + g, 63, 319 + g);
	}else if(DrawCounter%40<=7){
		SetGraphicRect(64, 256 + g, 127, 319 + g);
	}else if(DrawCounter%40<=11){
		SetGraphicRect(128, 256 + g, 191, 319 + g);
	}else if(DrawCounter%40<=15){
		SetGraphicRect(192, 256 + g, 255, 319 + g);
	}else if(DrawCounter%40<=19){
		SetGraphicRect(256, 256 + g, 319, 319 + g);
	}else if(DrawCounter%40<=23){
		SetGraphicRect(320, 256 + g, 383, 319 + g);
	}else if(DrawCounter%40<=27){
		SetGraphicRect(256, 256 + g, 319, 319 + g);
	}else if(DrawCounter%40<=31){
		SetGraphicRect(192, 256 + g, 255, 319 + g);
	}else if(DrawCounter%40<=35){
		SetGraphicRect(128, 256 + g, 191, 319 + g);
	}else{
		SetGraphicRect(64, 256 + g, 127, 319 + g);
	}
	//wings
	DrawGraphic(GetX, GetY);
	
	//body
	if(GetVelocityX!=0 && NotMoving){
		DrawCounterTwo = 0;
		NotMoving = false;
	}
	if(GetVelocityX==0){
		if(DrawCounterTwo%24<=5){
			SetGraphicRect(0, 0 + g, 63, 63 + g);
		}else if(DrawCounterTwo%24<=11){
			SetGraphicRect(64, 0 + g, 127, 63 + g);
		}else if(DrawCounterTwo%24<=17){
			SetGraphicRect(128, 0 + g, 191, 63 + g);
		}else{
			SetGraphicRect(192, 0 + g, 255, 63 + g);
		}
	}else{
		if(DrawCounterTwo<=5){
			SetGraphicRect(0, 128 + g, 63, 191 + g);
		}else if(DrawCounterTwo<=11){
			SetGraphicRect(64, 128 + g, 127, 191 + g);
		}else if(DrawCounterTwo<=17){
			SetGraphicRect(128, 128 + g, 191, 191 + g);
		}else{
			SetGraphicRect(192, 128 + g, 255, 191 + g);
		}
	}
	//body
	DrawGraphic(GetX, GetY);
	
	//head
	SetGraphicRect(0 + g, 384, 63 + g, 447);
	//head
	if(GetCommonData("HB")>400){
		if(left){
			DrawGraphic(GetX + (GetCommonData("HB")%20)/10, GetY + 1 - (GetCommonData("HB")%20)/15);
		}else{
			DrawGraphic(GetX - (GetCommonData("HB")%20)/10, GetY + 1 - (GetCommonData("HB")%20)/15);
		}
	}else{
		DrawGraphic(GetX, GetY + 1);
	}
	
		
	
}


//Specify beginning point, end point, distance along line towards the end point, distance away from that point -- which is perpendicular to the line (can be negative, these two distances will give you a point of curvature), time it takes to move.
task SetBezierMove01(xxx, yyy, ex, ey, dis, dis2, time){
	let ang = atan2(ey - yyy, ex - xxx);
	let oscx = xxx + dis*cos(ang) + dis2*cos(ang + 90);
	let oscy = yyy + dis*sin(ang) + dis2*sin(ang + 90);
	ascent(counter in 0..(time + 1)){
		let apo = atan2(oscy - yyy, oscx - xxx);
		let aoe = atan2(ey - oscy, ex - oscx);
		let dpo = ((xxx - oscx)^2 + (yyy - oscy)^2)^0.5;
		let doe = ((oscx - ex)^2 + (oscy - ey)^2)^0.5;
		let rdpo = (dpo/time)*counter;
		let rdoe = (doe/time)*counter;
		let lx = xxx + rdpo*cos(apo);
		let ly = yyy + rdpo*sin(apo);
		let kx = oscx + rdoe*cos(aoe);
		let ky = oscy + rdoe*sin(aoe);
		let alk = atan2(ky - ly, kx - lx);
		let dlk = ((kx - lx)^2 + (ky - ly)^2)^0.5;
		let rdlk = (dlk/time)*counter;
		let rx = lx + rdlk*cos(alk);
		let ry = ly + rdlk*sin(alk);
		SetX(rx);
		SetY(ry);
		yield;
	}
}

	task explode(x, y){
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, GetCurrentScriptDirectory~"explode.png");
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 255, 0);
		ObjEffect_SetVertexUV(obj, 2, 255, 255);
		ObjEffect_SetVertexUV(obj, 3, 0, 255);
		let alpha = 255;
		let rad = 10;
		let angle = rand(0, 360);
		loop(17){
			ascent(i in 0..4){
				ObjEffect_SetVertexXY(obj, i, x + rad*cos(angle + i*90), y + rad*sin(angle + i*90));
				ObjEffect_SetVertexColor(obj, i, alpha, 255, 255, 255);
			}
			alpha-=15;
			rad+=8;
			angle = rand(0, 360);
			yield;
		}
		Obj_Delete(obj);
	}
	task halo(x, y, angle, vel){
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, GetCurrentScriptDirectory~"halo.png");
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 75, 0);
		ObjEffect_SetVertexUV(obj, 2, 75, 63);
		ObjEffect_SetVertexUV(obj, 3, 0, 63);
		ObjEffect_SetVertexXY(obj, 0, -38, -32);
		ObjEffect_SetVertexXY(obj, 1, 38, -32);
		ObjEffect_SetVertexXY(obj, 2, 38, 32);
		ObjEffect_SetVertexXY(obj, 3, -38, 32);
		ObjEffect_SetAngle(obj, 0, 0, angle + 90);
		loop(50){
			x+=vel*cos(angle);
			y+=vel*sin(angle);
			Obj_SetPosition(obj, x, y);
			yield;
		}
		Obj_Delete(obj);
	}
