freeslot("S_PLAY_SUPERJUMP", "SPR2_SJMP")

states[S_PLAY_SUPERJUMP] = {
	sprite = SPR_PLAY,
	frame = SPR2_SJMP,
	tics = 1,
	nextstate = S_PLAY_SUPERJUMP
}

local holdtime = 10*TICRATE

addHook("PlayerThink", function(p)
	if p.mo.skin ~= "sonic" then return end 
		if p.superjumped == false
			p.cansuperjump = true
		else
			p.cansuperjump = false
		end
		if p.cansuperjump
		and (p.cmd.buttons & BT_CUSTOM2)
		and holdtime
			p.superjumpcharge = true
		else
			p.superjumpcharge = false
		end
		if p.superjumpcharge
		and not p.superjumped
			local superjumpchargeghost = P_SpawnGhostMobj(p.mo)
			superjumpchargeghost.color = SKINCOLOR_LAVENDER
			superjumpchargeghost.momx = p.mo.momx+P_RandomRange(-6, 6)
			superjumpchargeghost.momy = p.mo.momy+P_RandomRange(-6, 6)
			superjumpchargeghost.blendmode = AST_ADD
			superjumpchargeghost.renderflags = superjumpchargeghost.renderflags|FF_FULLBRIGHT
		end
		if p.superjumpcharge
		and (p.cmd.buttons & BT_JUMP)
		and not p.superjumped
			P_SetObjectMomZ(p.mo, 41*FRACUNIT)
			S_StartSound(p.mo, sfx_bkpoof)
			P_NukeEnemies(p.mo, p.mo, 11)
			P_StartQuake(10*FRACUNIT, 11)
			p.mo.state = S_PLAY_SUPERJUMP
			p.superjumpcharge = false
			p.cansuperjump = false
			p.superjumped = true
		end
		if p.mo.state == S_PLAY_SUPERJUMP
		and p.mo.momz <= 0
			p.mo.state = S_PLAY_FALL
		end
		if p.superjumped
			local superjumpghost = P_SpawnGhostMobj(p.mo)
			superjumpghost.color = SKINCOLOR_LAVENDER
			superjumpghost.blendmode = AST_ADD
			superjumpghost.renderflags = superjumpghost.renderflags|FF_FULLBRIGHT
		end
		if P_IsObjectOnGround(p.mo)
			p.superjumped = false
		end
	end)