local function SpawnHyperSparkles(player)
	local hypersparkle = P_SpawnMobjFromMobj(player.mo, 0, 0, 20*FRACUNIT, MT_JTHYPERSPARKLES)
	hypersparkle.momx = P_RandomRange(-5, 5) * FRACUNIT + FixedMul(-12, 12, player.mo.scale)
	hypersparkle.momy = P_RandomRange(-5, 5) * FRACUNIT + FixedMul(-12, 12, player.mo.scale)
	hypersparkle.momz = P_RandomRange(-5, 5) * FRACUNIT + FixedMul(-12, 12, player.mo.scale)

	hypersparkle.scale = player.mo.scale * 20/22
	hypersparkle.source = player.mo
	if hypersparkle.source.player == displayplayer
		local chasecam = CV_FindVar("chasecam")
		if chasecam and chasecam.value == 1 then
			hypersparkle.flags2 = hypersparkle.flags2 & ~MF2_DONTDRAW
		elseif chasecam and chasecam.value == 0 then
			hypersparkle.flags2 = hypersparkle.flags2 | MF2_DONTDRAW
		end
	end
	return hypersparkle
end

local colorTable = {
	SKINCOLOR_HYPER_ORANGE,
	SKINCOLOR_HYPER_RED,
	SKINCOLOR_HYPER_PURPLE,
	SKINCOLOR_HYPER_LIGHTBLUEORCYAN,
	SKINCOLOR_HYPER_GREEN,
	SKINCOLOR_HYPER_YELLOW,
	SKINCOLOR_HYPER_WHITE
}

local iter = 1
addHook("ThinkFrame", do
	if leveltime % 3 == 0 then
		iter = $+1
		if iter > #colorTable then
			iter = 1
		end
	end
	skincolors[SKINCOLOR_HYPERJAYT].ramp = skincolors[colorTable[iter]].ramp
end)


addHook("PlayerThink", function(player)
	if player.mo and player.mo.skin == "jayt" then
		-- if your Capable of Going in Hyper form!
		if not player.powers[pw_super] and player.rings > 99 then
			player.hyper.capable = true
		end
		
		--If you didn't collected enough rings, i'm sorry, your not going hyper.
		if player.rings < 100 and not player.powers[pw_super] then
			player.hyper.capable = false
		end
	end
end)

addHook("PlayerThink", function(player)
	if player.mo and player.mo.skin == "jayt" then
		if player.hyper.capable and player.powers[pw_super]
			player.mo.color = SKINCOLOR_HYPERJAYT
			SpawnHyperSparkles(player)
			
			if (player.cmd.buttons & BT_CUSTOM1) and not player.pressedcustom1
				P_SpawnPlayerMissile(player.mo, MT_KNIF)
				player.pressedcustom1 = true
			else
				player.pressedcustom1 = false
			end
			
			--like some characters in SRB2, Going hyper can cause the ring to drain fast! gotta re-get rings fast!
			if (leveltime % 35 == 18) and (player.hyper.transformed)
				P_GivePlayerRings(player, -1)
			end

			--Hyper forms doesnt have to breathe.
			if (player.mo.eflags & MFE_UNDERWATER)
				player.powers[pw_underwater] = 0
			end
			if P_InSpaceSector(player.mo)
				player.powers[pw_spacetime] = 0
			end
		end
	end
end)

addHook("PlayerThink", function(player)
	if player.mo and player.mo.skin == "jayt" then
		if player.powers[pw_super]
			if (player.cmd.buttons & BT_CUSTOM3) and not player.hyper.capable and player.rings > 99
				player.hyper.capable = true
				P_DoSuperTransformation(player)
			end
		end
	end
end)

local function SpinAbility(player)
    if player.mo.skin ~= "jayt" then
		return
	end
		
	if player.hyper.capable and player.powers[pw_super] and not ((player.mo.state >= S_PLAY_HYPERTRNS_1) and (player.mo.state <= S_PLAY_HYPERTRNS_6))
		if (player.cmd.buttons & BT_SPIN) then
			if not player.pressedspin
				if player.pflags & PF_THOKKED then
					return true
				end
				player.jaytfly = true
				player.pflags = $ | PF_THOKKED
				
				player.pressedspin = true
			else
				player.pressedspin = false
			end
		end
    end
end

addHook("PlayerThink", SpinAbility)

addHook("ThinkFrame", do
	for player in players.iterate
		if player.mo
			if player.mo.skin == "jayt"
				if player.hyper.capable and player.powers[pw_super]
					if player.jaytfly == true
						P_SetObjectMomZ(player.mo, 0*FRACUNIT, false)
						if player.mo.state == S_PLAY_ROLL or player.mo.state == S_PLAY_SPRING or player.mo.state == S_PLAY_JUMP or player.mo.state == S_PLAY_TRICKDOWN or player.mo.state == S_PLAY_TRICKUP or player.mo.state == S_PLAY_FALL
							player.mo.state = S_PLAY_FLOAT
						end
						
						if (player.cmd.buttons & BT_JUMP)
							P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
						elseif (player.cmd.buttons & BT_SPIN)
							P_SetObjectMomZ(player.mo, -10*FRACUNIT, false)
						end
					end
					
					if P_IsObjectOnGround(player.mo)
						player.jaytfly = false
					end
				end
			end
		end
	end
end)

addHook("AbilitySpecial", function(player)
	if player.mo.skin == "jayt"
		if player.hyper.capable
			if player.powers[pw_super]
				if player.jaytfly
					if (player.pflags & PF_THOKKED) and ((player.cmd.buttons & BT_JUMP) and (player.cmd.buttons & BT_SPIN)) then
						S_StartSound(player.mo, sfx_zoom)
						S_StartSound(player.mo, sfx_kc5b)
						player.mo.state = S_PLAY_ROLL
						player.pflags = $|PF_THOKKED
						P_Thrust(player.mo, player.mo.angle, 40*FRACUNIT)
					end
				end
			end
		end
	end
end)