/// Ability: Wall-Jump
// allows you to cling to walls, and leap off of them

// CUSTOMIZE SECTION START
local skin = "sparrow" // your character here
local thebutton = BT_SPIN // the button needed to do the wall jump
local wallkickheight = 20 // the upwards thrust of the walljump
local wallkickthrust = 15 // the thrust of the wall jump
local sound = sfx_cdfm02 // sound of the wall jump
// CUSTOMIZE SECTION END PROCCED BELOW IF YOU KNOW WHAT YOUR DOING

addHook("PlayerThink", function(player)
	if player.mo.walljump == nil
		player.mo.walljump = false
	end
	if player.mo.collidewall == nil
		player.mo.collidewall = 0
	end
	if player.mo.collidewall > 0
		player.mo.collidewall = $ - 1
	end
	if player.mo and player.mo.skin == skin
	and (player.cmd.buttons & thebutton)
	and not P_IsObjectOnGround(player.mo)
	and player.mo.collidewall
	and player.mo.walljump == false
		player.mo.walljump = true
		player.mo.state = S_PLAY_CLING
		player.pflags = $|PF_FULLSTASIS
		P_SpawnSkidDust(player, player.mo.radius, true)
		P_SetObjectMomZ(player.mo, 0)
	end
	if player.mo.walljump == true
		player.mo.state = S_PLAY_CLING
		player.pflags = $|PF_FULLSTASIS
		player.pflags = $ & ~PF_GLIDING
		P_SpawnSkidDust(player, player.mo.radius, true)
		P_SetObjectMomZ(player.mo, 0)
	end
	if player.mo and player.mo.skin == skin
	and not(player.cmd.buttons & thebutton)
	and not P_IsObjectOnGround(player.mo)
	and player.mo.walljump == true
		player.mo.walljump = false
		player.mo.collidewall = 0
		player.mo.angle = $ + ANGLE_180
		player.drawangle = player.mo.angle
		P_InstaThrust(player.mo, player.mo.angle, wallkickthrust*FRACUNIT)
		P_SetObjectMomZ(player.mo, wallkickheight*FRACUNIT)
		S_StartSound(player.mo, sound)
		player.mo.state = S_PLAY_ROLL
		player.pflags = $ |PF_JUMPED
		player.pflags = $ & ~PF_THOKKED
	end
	if player.mo.walljump == true
	and player.mo.collidewall == 0
	and not (player.cmd.buttons & thebutton)
	and not P_IsObjectOnGround(player.mo)
		player.mo.walljump = false
		player.mo.state = S_PLAY_FALL
		player.pflags = $1 & ~PF_FULLSTASIS
		player.pflags = $ & ~PF_THOKKED
	end
end)

addHook("MobjMoveBlocked", function(mo)
	if mo.skin == skin and not P_IsObjectOnGround(mo)
		if not (mo.player.pflags & PF_ANALOGMODE)
		and mo.player.cmd.forwardmove > 0
			mo.collidewall = 10
		end
		if not (mo.player.cmd.forwardmove == 0)
		or not (mo.player.cmd.sidemove == 0)
			mo.collidewall = 10
		end
	end
end, MT_PLAYER)