let objDeletes;

let particles = [];
let colorsall = [
	[255, 0, 0],
	[255, 128, 0],
	[255, 255, 0],
	[128, 255, 0],
	[0, 255, 0],
	[0, 255, 128],
	[0, 255, 255],
	[0, 128, 255],
	[0, 0, 255],
	[128, 0, 255],
	[255, 0, 255],
	[255, 0, 128],
	[160, 160, 160],
	[160, 160, 160]
];

@Initialize {
	SetShotDeleteEventEnable(EV_DELETE_SHOT_IMMEDIATE, true);
	SetShotDeleteEventEnable(EV_DELETE_SHOT_TO_ITEM, true);
	SetShotDeleteEventEnable(EV_DELETE_SHOT_FADE, false);
	
	objDeletes = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	Obj_SetRenderPriorityI(objDeletes, 50);
	ObjRender_SetBlendType(objDeletes, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(objDeletes, GetCurrentScriptDirectory ~ "./img/bulletbreak.png");
}

@MainLoop {
	let particlesNew = [];
	ObjSpriteList2D_ClearVertexCount(objDeletes);
	ascent(i in 0..length(particles)) {
		let thisParticle = particles[i];
		
		let c = floor(thisParticle[4] / 2);
		ObjSpriteList2D_SetSourceRect(objDeletes, 0+(c%8)*64, 0+floor(c/8)*64, 64+(c%8)*64, 64+floor(c/8)*64);
		ObjSpriteList2D_SetDestRect(objDeletes, -32, -32, 32, 32);
		ObjRender_SetScaleXYZ(objDeletes, thisParticle[3], thisParticle[3], 1);
		
		ObjRender_SetX(objDeletes, thisParticle[0]);
		ObjRender_SetY(objDeletes, thisParticle[1]);
		let colorthis = colorsall[thisParticle[2]];
		ObjRender_SetColor(objDeletes, colorthis[0], colorthis[1], colorthis[2]);
		
		if(thisParticle[4] < 20) {
			thisParticle[4] = thisParticle[4]+1;
			particlesNew = particlesNew ~ [thisParticle];
		}
		
		ObjSpriteList2D_AddVertex(objDeletes);
	}
	ObjSpriteList2D_CloseVertex(objDeletes);
	particles = particlesNew;
	yield;
}

@Event {
	alternative(GetEventType())
	case(EV_DELETE_SHOT_IMMEDIATE, EV_DELETE_SHOT_TO_ITEM) {
		let objShot = GetEventArgument(0);
		let objPos = GetEventArgument(1);

		let color = (ObjShot_GetImageID(objShot) - 1) % 14;
		let size = 0;
		let thisbasegraphic = (ObjShot_GetImageID(objShot)-1) % 560;
		if(thisbasegraphic < 252) { size = 0.5; }
		else if(thisbasegraphic >= 252 && thisbasegraphic < 434) {
			size = 1;
		}
		else if(thisbasegraphic >= 434 && thisbasegraphic < 462) {
			size = 0.5;
		}
		else if(thisbasegraphic >= 462 && thisbasegraphic < 490) {
			size = 0.33;
		}
		else if(thisbasegraphic >= 490 && thisbasegraphic < 546) {
			size = 1.75;
		}
		else {
			size = 0.5;
		}
		particles = particles ~ [[ObjMove_GetX(objShot), ObjMove_GetY(objShot), color, size, 0]];
	}
}