--Sawblade lua
--by Callmore

if (sawblade_initialized) then return end -- Check if the script has already been run
freeslot("MT_PENGUINATOR", -- Object
		 "S_PENGUINATOR", -- State
		 "SPR_PENG", -- Sprite
		 "SFX_PENTOR") -- Sound effect

-- State and sprite should be updated to match whatever was freeslotted
-- More states can be added since it is just used for animation
states[S_PENGUINATOR] = {SPR_PENG, A, -1, nil, 1, 2, S_PENGUINATOR}

mobjinfo[MT_PENGUINATOR] = {
	doomednum = 4073, -- Change this if you modify to a number below 4095 to avoid conflicts
	spawnstate = S_PENGUINATOR, -- Should match the state that was freeslotted
	spawnhealth = 1000, -- Should be 1000 to prevent the enemy dying
	radius = 32*FRACUNIT, -- Diameter is 2*radius
	height = 64*FRACUNIT,
	speed = 8, -- Speed of object in fracunits per frame
	flags = MF_SPECIAL|MF_ENEMY,
}

local function penguinThink(mo)
	if not mo.sb_init then
		mo.sb_init = true
		mo.sb_currentmovement = false
		mo.sb_startangle = mo.angle
		mo.scale = $*2
	end
	
	local xdir = FixedMul(cos(mo.sb_startangle), mapobjectscale)
	local ydir = FixedMul(sin(mo.sb_startangle), mapobjectscale)
	
	local didMove
	if mo.sb_currentmovement then
		didMove = P_TryMove(mo, mo.x - (xdir * mo.info.speed), mo.y - (ydir * mo.info.speed))
	else
		didMove = P_TryMove(mo, mo.x + (xdir * mo.info.speed), mo.y + (ydir * mo.info.speed))
	end
	if not didMove then
		mo.sb_currentmovement = not $
		mo.angle = mo.sb_startangle + (mo.sb_currentmovement and ANGLE_180 or 0)
	end
	
	-- This will make the a sound play every second from the sawblade
	-- Change the sfx_ to match the sound that you freeslotted
	--   or whatever sound you want to play
	if (leveltime % 35) == 0 then
		S_StartSound(mo, sfx_pentor)
	end
end
addHook("MobjThinker", penguinThink, MT_PENGUINATOR)

rawset(_G, "penguinator_initialized", true) -- The script ran, don't run it again