--Code by GenericHeroGuy, Thanks!
--might as well add the credits here, sprites etc by Spee
--thank you achiidev for the bigger kart template (mitsuos-big-brother-kart) //slightly edited by me, Spee
--thanks to xyzzy with helping me as well with the code
local FRAME = T

function A_RatgodWings(actor, var1, var2)
	if not actor.target then
		if actor.hnext then P_RemoveMobj(actor.hnext) end
		P_RemoveMobj(actor)
		return
	end

	local pmo = actor.target
	if not actor.hnext then
		local wing = P_SpawnMobj(pmo.x, pmo.y, pmo.z, MT_THOK)
		wing.sprite = SPR_PLAY
		wing.skin = "ratgod"
		wing.frame = FRAME+1 | FF_PAPERSPRITE
		wing.flags = $ | MF_NOTHINK
		wing.dispoffset = 1
		actor.hnext = wing

		actor.skin = "ratgod"
		actor.dispoffset = 1
	end

	local actor2 = actor.hnext
	local x = pmo.x - P_ReturnThrustX(pmo.player.frameangle, 18*pmo.scale/2)
	local y = pmo.y - P_ReturnThrustY(pmo.player.frameangle, 18*pmo.scale/2)
	local amplitude = 3700
	local rotationAngle = sin(leveltime*ANG10)*amplitude

	for _, v in pairs{actor, actor2} do
		P_MoveOrigin(v, x, y, pmo.z)
		v.scale = pmo.scale
		v.colorized = pmo.colorized
		v.color = pmo.color
	end

	actor.angle = pmo.player.frameangle + ANGLE_67h - rotationAngle
	actor2.angle = pmo.player.frameangle + ANGLE_112h + rotationAngle

	if pmo.rollangle ~= nil and pmo.spritexscale ~= nil and pmo.spriteyscale ~= nil and pmo.spritexoffset ~= nil and pmo.spriteyoffset ~= nil then 
			actor.rollangle = (pmo.rollsum ~= nil and pmo.rollsum) or pmo.sloperoll
			actor.spritexscale = pmo.spritexscale
			actor.spriteyscale = pmo.spriteyscale
			actor.spritexoffset = pmo.spritexoffset
			actor.spriteyoffset = pmo.spriteyoffset
			actor2.rollangle = (pmo.rollsum ~= nil and pmo.rollsum) or pmo.sloperoll
			actor2.spritexscale = pmo.spritexscale
			actor2.spriteyscale = pmo.spriteyscale
			actor2.spritexoffset = pmo.spritexoffset
			actor2.spriteyoffset = pmo.spriteyoffset
	end
end

freeslot("S_RATGODWINGS")
states[S_RATGODWINGS] = {
	tics = 1,
	sprite = SPR_PLAY,
	frame = FRAME | FF_PAPERSPRITE,
	nextstate = S_RATGODWINGS,
	action = A_RatgodWings,
}

addHook("PlayerThink", function(p)
	if p.spectator or p.ratgodwingsc then return end
	local pmo = p.mo
	if pmo.skin == "ratgod" and p.playerstate ~= PST_DEAD then
		if not (pmo.ratgodwings and pmo.ratgodwings.valid) then
			pmo.ratgodwings = P_SpawnMobj(pmo.x, pmo.y, pmo.z, MT_THOK)
			pmo.ratgodwings.target = pmo
			pmo.ratgodwings.state = S_RATGODWINGS
		end
	elseif pmo.ratgodwings then
		if pmo.ratgodwings.hnext then P_RemoveMobj(pmo.ratgodwings.hnext) end
		if pmo.ratgodwings.valid then P_RemoveMobj(pmo.ratgodwings) end
		pmo.ratgodwings = nil
	end
end)

COM_AddCommand("rgwings", function(p, target)
	if target == "on" then
	CONS_Printf(p, "\134Turned on wings for rat god")
	p.ratgodwingsc = false
	elseif target == "off" then
	CONS_Printf(p, "\134Turned off wings for rat god, this will apply next round or once you die")
	p.ratgodwingsc = true
	else
	CONS_Printf(p, "\134Commands available: \135on \134or \135off")
	end
end)