let EventObjList_Left = [];
let EventObjAlpha_Left = [];
let EventCharTalking_Left = [];
let EventCharPresent_Left = [];
let EventObjList_Right = [];
let EventObjAlpha_Right = [];
let EventCharTalking_Right = [];
let EventCharPresent_Right = [];
let EventRTN = "Event Render Target Name";
let EventCDFlag = "Event Active?";
let EventStep = "Event Step";

function InitializeEvent(let numLeft, let numRight) {
	ForbidShot(true); ForbidBomb(true);
	SetCommonData(EventCDFlag, true);
	SetCommonData(EventStep, 0);
	CreateRenderTarget(EventRTN, 512, 128);
	loop(numLeft) {
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetLayer(obj, 8);
		ObjEffect_SetTexture(obj, talk);
		ObjEffect_CreateVertex(obj, 4);
		Obj_SetPosition(obj, 320-256-192, GetClipMaxY+32);
		EventObjList_Left = EventObjList_Left ~ [ obj ];
		EventCharTalking_Left = EventCharTalking_Left ~ [ false ];
		EventCharPresent_Left = EventCharPresent_Left ~ [ false ];
		EventObjAlpha_Left = EventObjAlpha_Left ~ [ 0 ];
	}
	loop(numRight) {
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetLayer(obj, 8);
		ObjEffect_SetTexture(obj, talk);
		ObjEffect_CreateVertex(obj, 4);
		Obj_SetPosition(obj, 320+256+192, GetClipMaxY+32);
		EventObjList_Right = EventObjList_Right ~ [ obj ];
		EventCharTalking_Right = EventCharTalking_Right ~ [ false ];
		EventCharPresent_Right = EventCharPresent_Right ~ [ false ];
		EventObjAlpha_Right = EventObjAlpha_Right ~ [ 0 ];
	}
	HandleEventLogic;
}
function FinalizeEvent {
	ascent(i in 0..length(EventObjList_Left)) {
		Obj_Delete(EventObjList_Left[i]);
	}
	EventObjList_Left = []; EventCharTalking_Left = []; EventCharPresent_Left = [];
	ascent(i in 0..length(EventObjList_Right)) {
		Obj_Delete(EventObjList_Right[i]);
	}
	EventObjList_Right = []; EventCharTalking_Right = []; EventCharPresent_Right = [];
	DeleteGraphic(EventRTN);
	SetCommonData(EventCDFlag, false);
	SetCommonData(EventStep, 0);
	ForbidShot(false); ForbidBomb(false);
}
task HandleEventLogic {
	while(OnEventEx) {
		ascent(i in 0..length(EventObjList_Left)) {
			let obj = EventObjList_Left[i];
			let talking = EventCharTalking_Left[i];
			let here = EventCharPresent_Left[i];
			Obj_SetPosition(obj, Obj_GetX(obj)+(320-256+[-192, [0, 64][talking]][here]-Obj_GetX(obj))/4, Obj_GetY(obj)+(GetClipMaxY+32-[0, [48, 96][talking]][here]-Obj_GetY(obj))/4);
			EventObjAlpha_Left[i] = EventObjAlpha_Left[i] + ([0, [128, 255][talking]][here] - EventObjAlpha_Left[i])/4;
			let alpha = EventObjAlpha_Left[i];
			ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, alpha, 255, 255, 255); }
		}
		ascent(i in 0..length(EventObjList_Right)) {
			let obj = EventObjList_Right[i];
			let talking = EventCharTalking_Right[i];
			let here = EventCharPresent_Right[i];
			Obj_SetPosition(obj, Obj_GetX(obj)+(320+256+[192, [0, -64][talking]][here]-Obj_GetX(obj))/4, Obj_GetY(obj)+(GetClipMaxY+32-[0, [48, 96][talking]][here]-Obj_GetY(obj))/4);
			EventObjAlpha_Right[i] = EventObjAlpha_Right[i] + ([0, [128, 255][talking]][here] - EventObjAlpha_Right[i])/4;
			let alpha = EventObjAlpha_Right[i];
			ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, alpha, 255, 255, 255); }
		}
		yield;
	}
}
task SetEventStepEx(let num) {
	SetCommonData(EventStep, num);
}
function GetEventStepEx {
	return GetCommonDataDefault(EventStep, 0);
}
function OnEventEx {
	return GetCommonDataDefault(EventCDFlag, false);
}
function SetCutinGraphic(let LeftRight, let charID, let x, let y) {
	let array = [EventObjList_Left, EventObjList_Right][LeftRight];
	let obj = array[charID];
	ObjEffect_SetTexture(obj, talk);
	ObjEffect_SetVertexUV(obj, 0, x*384, y*512);
	ObjEffect_SetVertexUV(obj, 1, x*384+384, y*512);
	ObjEffect_SetVertexUV(obj, 2, x*384+384, y*512+512);
	ObjEffect_SetVertexUV(obj, 3, x*384, y*512+512);
	ObjEffect_SetVertexXY(obj, 0, -192, -256);
	ObjEffect_SetVertexXY(obj, 1, 192, -256);
	ObjEffect_SetVertexXY(obj, 2, 192, 256);
	ObjEffect_SetVertexXY(obj, 3, -192, 256);
}
function SetCharStatusTalking(let LeftRight, let charID, let talking) {
	alternative(LeftRight)
	case(0) {
		EventCharTalking_Left[charID] = talking;
	}
	case(1) {
		EventCharTalking_Right[charID] = talking;
	}
}
function SetCharStatusPresent(let LeftRight, let charID, let talking) {
	alternative(LeftRight)
	case(0) {
		EventCharPresent_Left[charID] = talking;
	}
	case(1) {
		EventCharPresent_Right[charID] = talking;
	}
}
function SetEventText(let LeftRight, let bubbletype, let linenum, let linelength, let time) {
	SetRenderTarget(EventRTN);
	ClearRenderTarget(EventRTN);
	SetTexture(talk);
	let y = 1536+[0, 64, 128, 256, 336, 416][bubbletype];
	let h = [64,80][bubbletype>=2];
	SetGraphicAngle(0, 0, 0);
	SetGraphicScale(1, 1);
	SetColor(255, 255, 255);
	SetAlpha(255);
	SetRenderState(ALPHA);
	SetGraphicRect(0, y, 48, y+h);
	DrawGraphic(24, h/2);
	SetGraphicRect(48, y, 64, y+h);
	ascent(i in 0..(linelength-16)/16) {
		DrawGraphic(48+8+16*i, h/2);
	}
	SetGraphicRect(64, y, 96, y+h);
	DrawGraphic(linelength+48, h/2);
	SetTexture(talk2);
	SetGraphicScale(1*((-1)^LeftRight), 1);
	SetGraphicRect(0, linenum*40, linelength, linenum*40+40);
	DrawGraphic(linelength/2, 100);
	SetRenderTarget("DEFAULT");
	let obj = Obj_Create(OBJ_EFFECT);
	Obj_SetPosition(obj, 320-(160*((-1)^LeftRight)), GetClipMaxY-160);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetLayer(obj, 8);
	ObjEffect_SetTexture(obj, EventRTN);
	ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 512, 0);
		ObjEffect_SetVertexUV(obj, 2, 512, 80);
		ObjEffect_SetVertexUV(obj, 3, 0, 80);
		ObjEffect_SetVertexXY(obj, 0, -32, -16);
		ObjEffect_SetVertexXY(obj, 1, 480, -16);
		ObjEffect_SetVertexXY(obj, 2, 480, 64);
		ObjEffect_SetVertexXY(obj, 3, -32, 64);
	let obj2 = Obj_Create(OBJ_EFFECT);
	Obj_SetPosition(obj2, 320-(128*((-1)^LeftRight)), GetClipMaxY-140);
	ObjEffect_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetLayer(obj2, 8);
	ObjEffect_SetTexture(obj2, EventRTN);
	ObjEffect_CreateVertex(obj2, 4);
		ObjEffect_SetVertexUV(obj2, 0, 0, 80);
		ObjEffect_SetVertexUV(obj2, 1, linelength, 80);
		ObjEffect_SetVertexUV(obj2, 2, linelength, 128);
		ObjEffect_SetVertexUV(obj2, 3, 0, 128);
		ObjEffect_SetVertexXY(obj2, 0, -32, -16);
		ObjEffect_SetVertexXY(obj2, 1, linelength-32, -16);
		ObjEffect_SetVertexXY(obj2, 2, linelength-32, 32);
		ObjEffect_SetVertexXY(obj2, 3, -32, 32);
	let scale = 15; let fin = true; let t = (|time|);
	while(fin) {
		ObjEffect_SetScale(obj, ((-1)^LeftRight)*cos(scale*90/15), 1);
		ObjEffect_SetScale(obj2, ((-1)^LeftRight)*cos(scale*90/15), 1);
		if(scale > 0) { scale--; }
		if(time == 0) {
			if(scale == 0 && GetKeyState(VK_SHOT)==KEY_PUSH) { fin = false; }
			if(scale < 12 && GetKeyState(VK_SKIP)!=KEY_FREE) { fin= false; }
		} else if(time > 0) {
			if(scale == 0) { t--; if(t == 0) { fin = false; } }
		} else if(time < 0) {
			if(scale == 0) { t--; if(t == 0 && (GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SKIP)!=KEY_FREE)) { fin = false; } }
		}
		yield;
	}
	PlaySE(GCSD ~ "se_playershot.wav");
	Obj_Delete(obj);
	Obj_Delete(obj2);
	yield;
}