-- Basic Super Peel-out ability by icySndw/IcySandwich

-- Animation states
freeslot("S_PEEL_WALK","S_PEEL_RUN")
states[S_PEEL_WALK] = {
	sprite	= SPR_PLAY,
	frame	= SPR2_WALK,
	next	= S_PEEL_WALK
}
states[S_PEEL_RUN] = {
	sprite	= SPR_PLAY,
	frame	= SPR2_RUN_,
	next	= S_PEEL_RUN
}

local character	= "ultclsonic"		-- The character capable of using the Super Peel-out

local bt		= BT_CUSTOM1	-- This is the button you press to rev up the thing itself
local maxspd	= 85*FU			-- Maximum speed you can charge to with the peelout
local steps		= 2*FU			-- The amount of speed added each frame as the peelout charges up

local statelist	= {				-- A list of animation states the peelout goes through
	S_PEEL_WALK,S_PEEL_RUN,S_PLAY_DASH
}
local afterimg	= true			-- Gives the peelout an afterimages effect
local maxvfx	= true			-- Produces a blink effect on the player when maxspd is reached

local startsfx	= sfx_cdfm11	-- Sound played when you start revving up the peelout
local maxsfx	= sfx_cdfm13	-- Sound played when you reach maxspd charge
local launchsfx	= sfx_cdfm01	-- Sound played when you're launched by the peelout
local cancelsfx	= sfx_cdfm17	-- Sound played when the peelout is interrupted or its speed is lost

-- !! PLEASE, DO NOT TOUCH ANYTHING BELOW THIS, UNLESS YOU KNOW WHAT YOU'RE DOING !!

local function speedToState(x)
	local i = min(x/(maxspd/#statelist)+1, #statelist)
	return statelist[i]
end

addHook("PlayerSpawn", function(player)
	player.normalspeed = skins[player.mo.skin].normalspeed
	player.peelout = 0
	player.peelout_threshold = 0
end)

addHook("SpinSpecial", function(player)
	if player.peelout return true end
end)

addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.valid and player.mo.health) return end
	
	local canPeelout = (
		player.mo.skin == character
		and P_IsObjectOnGround(player.mo)
		and not player.mo.speed
	)
	
	if canPeelout
		if player.cmd.buttons & bt
			if not player.peelout
			and startsfx
				S_StartSound(player.mo, startsfx)
			end
			
			local lastpeel = player.peelout
			player.peelout = min($+steps, maxspd)
			
			if player.peelout == maxspd
			and lastpeel ~= maxspd
				if maxsfx
					S_StartSound(player.mo, maxsfx)
				end
				if maxvfx
					local ghost = P_SpawnGhostMobj(player.mo)
					ghost.color = SKINCOLOR_SUPERSILVER1
					ghost.colorized = true
					ghost.dispoffset = 1
				end
			end
			
			player.mo.state = speedToState(player.peelout)
			player.normalspeed = 0
		
		elseif player.lastbuttons & bt
		and player.peelout
			if launchsfx
				S_StartSound(player.mo, launchsfx)
			end
			
			P_InstaThrust(player.mo, player.mo.angle, FixedMul(player.peelout, player.mo.scale))
			player.mo.state = S_PLAY_RUN
			
			local threshold = player.peelout*4/5
			if threshold > skins[player.mo.skin].normalspeed
				player.normalspeed = player.peelout
				player.peelout_threshold = -threshold
			end
			player.peelout = 0
		end
	
	elseif player.peelout
		if cancelsfx
			S_StartSound(player.mo, cancelsfx)
		end
		
		player.peelout = 0
		
		player.normalspeed = skins[player.mo.skin].normalspeed
	end
	
	if player.peelout_threshold
		if afterimg
		and leveltime%2
			local ghost = P_SpawnGhostMobj(player.mo)
			ghost.colorized = true
		end
		
		if player.peelout_threshold < 0
			player.peelout_threshold = -$
		elseif player.speed < FixedMul(player.peelout_threshold, player.mo.scale)
			player.peelout_threshold = 0
			player.normalspeed = skins[player.mo.skin].normalspeed
		end
	end
end)