--Sawblade lua, motobugs, flying lightbulbs, who knows anymore
--by Callmore
--also rai
--ash helped maybe

freeslot("MT_MCTR", "S_MCTR", "SPR_MCTRA")--, "S_MCTR")
freeslot("MT_MCTB", "S_MCTB", "SPR_MCTBA")--, "S_MCTR")

states[S_MCTR] = {SPR_MCTR, FF_FULLBRIGHT|A, -1, nil, 0, 2, S_MCTR}
states[S_MCTB] = {SPR_MCTB, FF_FULLBRIGHT|A, -1, nil, 0, 2, S_MCTB}

--Variables for whole script


local function mmSawbladeSpawn(mo)
	mo.scale = $*3
	mo.moveLimit = 10
	mo.currentTime = 0
	mo.canMove = true
	
	
end

addHook("MobjSpawn", mmSawbladeSpawn, MT_MCTR)
addHook("MobjSpawn", mmSawbladeSpawn, MT_MCTB)


local function mmCtrklrThink(mo)
	
	if not mo.init then
	
	  
		if mo.spawnpoint.options & MTF_AMBUSH then 
			mo.moveup = true
			--print("move up")
		else 
			mo.moveup = false
			--print("move down")
		end
		mo.init = true
	end
	
	local xdir = sin(mo.angle - ANGLE_90)
    local ydir = cos(mo.angle + ANGLE_90)
    
    local didMove

    if mo.canMove then    --allows movement script to run when true
        if mo.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
		
		--print("moveup "+mo.moveup) 
		
		if mo.moveup then 
			mo.z = $+FRACUNIT*2 
		else 
			mo.z = $-FRACUNIT*2 
		end
		
        if not didMove then
            --print("Flipping sawblade")
            --mo.currentmovement = not $
            mo.angle = $+ANGLE_180
        end
    end

    if mo.currentTime > mo.moveLimit then  --Resets the timer and toggles movement
        mo.canMove = not mo.canMove
		mo.currentTime = 0
		if mo.canMove then
			 mo.moveup = not mo.moveup
			
		end
    end

    mo.currentTime = $ + 1  --Increases timer value
    --print(tostring(mo.currentTime))
end

addHook("MobjThinker", mmCtrklrThink, MT_MCTR)
addHook("MobjThinker", mmCtrklrThink, MT_MCTB)