--------------
--MAURICE!----
--------------

-- For ever sprite, sound, object and state, you need to use a freeslot!
freeslot("MT_MAURICEBUMPER","MT_PATROLMAURICEBUMPER", "S_MAURICEBUMPER", "S_MAURICEBUMPERHIT", "SPR_NTVM")

--This is used for how strong the bumper will bounce the player
local DEFAULTSTRENGTH = 32

--IMPORTANT
--We're starting LUA set objects from 30001, to avoid conflicts with SOC declarated objects

-- Here's where Maurice's object is created
mobjinfo[MT_MAURICEBUMPER] = {
	spawnstate = S_MAURICEBUMPER,
	radius = 48*FRACUNIT,
	height = 64*FRACUNIT,
	doomednum = 30001,
	dispoffset = 0,
	deathstate = S_NULL,
	deathsound = sfx_s1b4,
	flags = MF_SPECIAL|MF_NOGRAVITY|MF_DONTENCOREMAP
}

--This changes Maurice size on startup
local function MakeMauriceGreat(mo)
	mo.scale = 4*$/3
end
addHook("MobjSpawn", MakeMauriceGreat, MT_MAURICEBUMPER)

--Setting the states----
--Arguments: Sprite, Frame (+ FF & TR constants), tics, action (affects var1 & 2), var1, var2,next state 
states[S_MAURICEBUMPER] = {SPR_NTVM, FF_FULLBRIGHT|A, -1, nil, 0, 0, S_MAURICEBUMPER}
states[S_MAURICEBUMPERHIT] = {SPR_NTVM, FF_FULLBRIGHT|B, 12, nil, 1, 3, S_MAURICEBUMPER}

--Taken from Spring Yard Zone, but will be kept for customization and when devs hardcode this function
local function sybumpPlayerMaurice(sybumpermo, playermo)
	--Determine the angle the player hit the bumper from.
	local horizontalAngle = R_PointToAngle2(sybumpermo.x,sybumpermo.y,playermo.x,playermo.y)
	local verticalAngle = R_PointToAngle2(FixedHypot(sybumpermo.x,sybumpermo.y),sybumpermo.z+(sybumpermo.height/2),FixedHypot(playermo.x,playermo.y),playermo.z+(playermo.height/2))
	
	-- If the bumper was created by another object or script, the minimum strength will be determined by the DEFAULTSTRENGTH value at the top of this script.	
	local sybumperStrength = DEFAULTSTRENGTH
	
	-- If the bumper was placed on the map in ZoneBuilder, the angle will determine the bumper's minimum strength.
	if sybumpermo.spawnpoint.valid and sybumpermo.spawnpoint.angle > 0
		sybumperStrength = sybumpermo.spawnpoint.angle/10
	end
	
	-- Bounce the player back slightly slower than the speed at which they hit the bumper.
	local playerVelocity = FixedHypot(playermo.player.speed, playermo.momz)
	if sybumperStrength < (9*playerVelocity) / (10*FRACUNIT)
		sybumperStrength = (9*playerVelocity) / (10*FRACUNIT)
	end
	
	-- Bounce the player back at the angle they hit the bumper from.
	P_SetObjectMomZ(playermo, sybumperStrength*sin(verticalAngle), false)
	P_InstaThrust(playermo, horizontalAngle, sybumperStrength*abs(cos(verticalAngle)))
	
	-- Show the "hit" frames and play the bumper sound (but only if the bumper is not already being hit).
	if sybumpermo.state == S_MAURICEBUMPER
		S_StartSound(sybumpermo, sybumpermo.info.deathsound)
		sybumpermo.state = S_MAURICEBUMPERHIT
	end	
		
			-- If "Ambush" flag is checked, the bumper will disappear after being hit.
	if not (sybumpermo.spawnpoint.options & MTF_AMBUSH)
		return true
	end
end

--Hooks the function to the Bumper object. Notice the Bumper object has MFSPECIAL, so we use TouchSpecial
addHook("TouchSpecial",sybumpPlayerMaurice,MT_MAURICEBUMPER)