#TouhouDanmakufu[Player]
#ScriptVersion[3]
#ID["MARISS"]
#Title["JDude's Marisa"]
#Text["A Marisa I made, because I could"]
#Image["./Marisa.png"]
#ReplayName["MARISS"]

#include "./EZ.dnh"

let objPlayer = GetPlayerObjectID();
let isFocus = false;
let ded = false;

let cookie = 360/4;

let dir = GetCurrentScriptDirectory();
let pSprite = dir ~ "player.png";

@Initialize{
	SetPlayerSpeed(4, 1.75);
	let shotsheet = dir ~ "JShot.dnh";
	LoadPlayerShotData(shotsheet);
	
	let stgw = GetStgFrameWidth(); let stgh = GetStgFrameHeight();
	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 2, 12);
	SetPlayerClip(0+6, 0+24, stgw-6, stgh-16);
	SetPlayerDownStateFrame(60);
	SetPlayerRebirthFrame(30);
	SetPlayerAutoItemCollectLine(100);
	
	renderPlayer(); renderHitbox(); 
	ShotA1(); Unfuckous();
	
	let op_spin = [1, 5];
	let nof = [60, 60]; let f = [40, 20];
	renderOptions(4, nof, f, op_spin, 0);
}

@Event{
	alternative(GetEventType())
	
	case(EV_REQUEST_SPELL){
	
		let spell = GetPlayerSpell();
		if(spell > 0){
			SetScriptResult(true);
			SetPlayerSpell(spell - 1);

			squared(cookie); cookie *= -1;
		}
		else{
			SetScriptResult(false);
		}
	}
	
	case(EV_HIT){
		byereimoo();
	}
	
	case(EV_PLAYER_SHOOTDOWN){
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		
		ded = true;
		ObjRender_SetAlpha(objPlayer, 0);
		SetPlayerSpell(3);
	}
	
	case(EV_PLAYER_REBIRTH){
		SetForbidPlayerShot(false);
		SetForbidPlayerSpell(false);
		
		ded = false; 
		ObjRender_SetAlpha(objPlayer, 255);
		SetPlayerInvincibilityFrame(60*3);
		
		Blink();
	}
}

@MainLoop{
	let F = GetVirtualKeyState(VK_SLOWMOVE);
	if(F == KEY_PUSH || F == KEY_HOLD){isFocus = true;}
	if(F == KEY_FREE){isFocus = false;}
	yield;
}

task Blink{
	ObjMove_SetY(objPlayer, GetStgFrameHeight + 30);
	ObjRender_SetDestAtFrame(objPlayer, GetPlayerX, GetPlayerY-63, 50);
	//SetPlayerSpeed(0, 0);
	while(GetPlayerInvincibilityFrame() > 0){
		ObjRender_SetColor(objPlayer, 51, 51, 255);
		holdup(2);
		ObjRender_SetColor(objPlayer, 222, 222, 255);
		holdup(2);
	}
	
	heyreimoo();
	ObjRender_SetColor(objPlayer, 255, 255, 255);
	
	task heyreimoo{
		let scale = 1; let n = 10;

		let path = dir ~ "etama2.png";

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, path);
		//ObjRender_SetBlendType(obj, BLEND_INV_DESTRGB);
		ObjSprite2D_SetSourceRect(obj, 193, 17, 254, 78);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
		
		descent(i in 0..255/n){
			ObjRender_SetAlpha(obj, i*n);
			ObjRender_SetScaleXYZ(obj, scale, scale, 1);
			
			scale += 0.2;
			yield;
		}

		Obj_Delete(obj);
	}
}

task renderPlayer{
	ObjPrim_SetTexture(objPlayer, pSprite);

	let frame = 0; let time = 3.5; 
	while(!Obj_IsDeleted(objPlayer)){
		
		ascent(i in 0..8){
			ObjSprite2D_SetSourceRect(objPlayer, 32*i, 0, 32*(i+1), 48);
			ObjSprite2D_SetDestCenter(objPlayer);
			if(GetVirtualKeyState(VK_LEFT) == KEY_FREE && GetVirtualKeyState(VK_RIGHT) == KEY_FREE){
				holdup(time);
			}
		}
		
		if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			ascent(i in 0..5){
				let e = i + 3;
				if(GetVirtualKeyState(VK_LEFT) == KEY_FREE && GetVirtualKeyState(VK_RIGHT) == KEY_FREE){
					ObjSprite2D_SetSourceRect(objPlayer, 32*e, 0, 32*(e+1), 48);
					ObjSprite2D_SetDestCenter(objPlayer);
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
					ObjSprite2D_SetSourceRect(objPlayer, 32*e, 48, 32*(e+1), 48*2);
					ObjSprite2D_SetDestCenter(objPlayer);
				}
				if(GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
					ObjSprite2D_SetSourceRect(objPlayer, 32*e, 48*2, 32*(e+1), 48*3);
					ObjSprite2D_SetDestCenter(objPlayer);
				}
				holdup(time);
			}
		}
		yield;
	}
}

task renderOptions(num, nf, f, spin, an){
	let obj = [];
	let a; let r = an; let sp = 0;
	
	ascent(i in 0..num){
		obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
	}
	
	let path = dir ~ "shot.png";
	ascent(i in 0..num){
		a = r + 360/num*i;
		
		ObjPrim_SetTexture(obj[i], path);
		Obj_SetRenderPriorityI(obj[i], 51);
		ObjRender_SetBlendType(obj[i], BLEND_ALPHA);
		ObjSprite2D_SetSourceRect(obj[i], 0, 169, 16, 185);
		ObjSprite2D_SetDestCenter(obj[i]);
		ObjRender_SetScaleXYZ(obj[i], 1, 1, 1);
		ObjRender_SetPosition(obj[i], GetPlayerX, GetPlayerY, 0);
		Needles(obj[i]);
	}
	
	//50,10 e 25,5
	let tox; let toy;
	let dist;
	let ato;

	let vel = spin[0];
	let di; let alp = 255;
	while(!Obj_IsDeleted(objPlayer)){
		if(!isFocus){di = nf; vel = spin[0];}
		else{di = f; vel = spin[1];}
		
		if(ded){alp -= 50;}
		else{alp += 50;}
		
		if(alp > 255){alp = 255;}
		if(alp < 0){alp = 0;}
		
		ascent(i in 0..length(obj)){
			a = r + 360/num*i;
			
			ObjRender_SetAlpha(obj[i], alp);
			ObjRender_SetAngleZ(obj[0], +sp);
			ObjRender_SetAngleZ(obj[1], -sp);
			ObjRender_SetScaleXYZ(obj[i], 1, 1, 1);
			
			tox = GetPlayerX + di[0]*cos(a);
			toy = GetPlayerY + di[1]*sin(a);
			let opX = ObjRender_GetX(obj[i]);
			let opY = ObjRender_GetY(obj[i]);
			dist = GetDist(opX, opY, tox, toy);
			ato = GetAngle(opX, opY, tox, toy);
			opX += 0.25 * dist * cos(ato);
			opY += 0.25 * dist * sin(ato);
			
			ObjRender_SetPosition(obj[i], opX, opY, 0);
		}
		sp += 4;
		r += vel;
		yield;
	}
} 

task byereimoo{
	let scale = 1; let n = 8;

	let path = dir ~ "etama2.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(obj, 129, 17, 190, 78);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
	
	descent(i in 0..255/n){
		ObjRender_SetAlpha(obj, i*n);
		ObjRender_SetScaleXYZ(obj, scale, scale, 1);
		
		scale += 0.2;
		yield;
	}

	Obj_Delete(obj);
}

task renderHitbox{
	let obj = [ObjPrim_Create(OBJ_SPRITE_2D), ObjPrim_Create(OBJ_SPRITE_2D)];
	let spin = 0;
	let alpha = 0;

	let path = dir ~ "etama2.png";

	let scale = 3;
	ascent(i in 0..length(obj)){
		ObjPrim_SetTexture(obj[i], path);
		Obj_SetRenderPriorityI(obj[i], 51);
		ObjRender_SetBlendType(obj[i], BLEND_ALPHA);
		ObjSprite2D_SetSourceRect(obj[i], 0, 18, 63, 78);
		ObjSprite2D_SetDestCenter(obj[i]);
		ObjRender_SetScaleXYZ(obj[i], 1, 1, 1);
		ObjRender_SetPosition(obj[i], GetPlayerX, GetPlayerY, 0);
	}
	
	while(!Obj_IsDeleted(objPlayer)){
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD && GetPlayerState() == STATE_NORMAL){
			alpha += 36;
			scale -= 0.05;
		}else{
			alpha -= 36;
			scale += 0.1;
		}
		if(scale < 1){
			scale = 1;
			spin += 2;
		}
		if(scale > 1){spin += 6;}
		if(scale > 2){scale = 2;}
		
		if(alpha > 255){alpha = 255;}
		if(alpha < 0){alpha = 0;}
		
		ascent(i in 0..length(obj)){
			ObjRender_SetAlpha(obj[i], alpha);
			ObjRender_SetAngleZ(obj[0], +spin);
			ObjRender_SetAngleZ(obj[1], -spin);
			ObjRender_SetScaleXYZ(obj[i], scale, scale, scale);
			ObjRender_SetPosition(obj[i], GetPlayerX(), GetPlayerY(), 0);
		}
		
		yield;
	}
}

task ObjRender_SetDestAtFrame(obj, x, y, frames){
    let ox = ObjRender_GetX(obj);
    let oy = ObjRender_GetY(obj);
    let xc = (x-ox)/frames;
    let yc = (y-oy)/frames;
    loop(frames){
        ox += xc;
        oy += yc;
        ObjRender_SetX(obj, ox);
        ObjRender_SetY(obj, oy);
        yield;
    }
}

function holdup(n){loop(n){yield;}}
function PolygonPShot(x, y, speed, angle, nSide, bpSide, special, graphic, dmg, pen){
    let bpos_x = [];
    let bpos_y = [];
    ascent(i in 0..nSide){
        let tmp_angle = 360 / nSide * i;
        bpos_x = bpos_x ~ [cos(tmp_angle)];
        bpos_y = bpos_y ~ [sin(tmp_angle)];
    }

	let B = []; let z = 0;
    ascent(i in 0..nSide){
        let from_x = x + bpos_x[i % nSide];
        let from_y = y + bpos_y[i % nSide];
        let to_x = x + bpos_x[(i + special) % nSide];
        let to_y = y + bpos_y[(i + special) % nSide];

        ascent(j in 0..bpSide){
            let tmp = (j + 1) / bpSide;

            let vx = GetLinePosition(from_x, to_x, tmp);
            let vy = GetLinePosition(from_y, to_y, tmp);
            let dx = vx - x;
            let dy = vy - y;
            let va = atan2(dy, dx) + angle;
            let vs = (dx * dx + dy * dy) ^ 0.5;

           //CreateShotA1(x, y, vs * speed, va, graphic, delay);
			B = B ~ [CreatePlayerShotA1(x, y, vs*speed, va, dmg, pen, graphic)];
			Obj_SetValue(B[z], "sv", vs);
			Obj_SetValue(B[z], "av", va);
			z++;
        }
    }
	
	return B;
    function GetLinePosition(from_pos, to_pos, pos_t){
        return (from_pos + (to_pos - from_pos) * pos_t);
    }
}
function GetDist(x1,y1,x2,y2){return ((x2-x1)^2+(y2-y1)^2)^0.5;}
function GetAngle(x1,y1,x2,y2){return atan2(y2-y1,x2-x1);}
function Getx(obj){return ObjMove_GetX(obj);}
function Gety(obj){return ObjMove_GetY(obj);}