//Credit goes to SP Moves Enjoyer for the "shoot" lua that served as a basis for this.

local sound = sfx_corkp
local projectile = MT_FANGBULLET
local autoaim = true
local hasdelay = true
local firedelay = 4


addHook("PlayerThink", function(player)
	if player.mo.shootaim == nil
		player.mo.shootaim = 0
	end
	
	if player.mo.projangle == nil
		player.mo.projangle = 0
	end
	
   if player.mo and player.mo.skin == "fang" and autoaim == true
		and not (player.pflags & PF_SPINNING)
		  player.mo.shootaim = P_LookForEnemies(player, false, true)
   if player.mo.shootaim and player.mo.shootaim.valid and player.mo.shootaim.health
		  player.mo.projangle = R_PointToAngle2(player.mo.x, player.mo.y, player.mo.shootaim.x, player.mo.shootaim.y)
		  P_SpawnLockOn(player, player.mo.shootaim, S_LOCKON1)
	   end
    end
	
	if player.mo and player.mo.skin == "fang" and (player.cmd.buttons & BT_SPIN)
	    and P_IsObjectOnGround(player.mo) and player.playerstate == PST_LIVE
	    and hasdelay == true
	    and not player.weapondelay
		and not player.exiting
		and not (player.lastbuttons & BT_SPIN)
		and not (player.powers[pw_carry] == CR_ZOOMTUBE)
		and not (player.powers[pw_carry] == CR_NIGHTSMODE)
		and not (player.mo.state == S_PLAY_PAIN)
		and not (player.mo.state == S_PLAY_GASP)
		and not (player.mo.state == S_PLAY_FALL)
		and not (player.pflags & PF_SPINNING)
		and not (player.pflags & PF_SLIDING)
		
	if player.mo.shootaim and player.mo.shootaim.valid and autoaim == true
			P_SpawnMissile(player.mo, player.mo.shootaim, projectile)
		else
			P_SpawnPlayerMissile(player.mo,projectile)
	end	
		S_StartSound(player.mo, sound)
		player.weapondelay = TICRATE*firedelay/5
		player.mo.state = S_PLAY_FIRE
	end
end)


addHook("PlayerThink", function(player)
    if player.mo.statetime == nil
		player.mo.statetime = 30
	end  
  
       if player.mo.state == S_PLAY_GUNRUN 
	      or player.mo.state == S_PLAY_GUNRUNM 
		   or player.mo.state == S_PLAY_GUNRUNF
		      player.mo.statetime = $ - 1
	      end
	
       if player.mo.statetime == 0
              player.mo.statetime = 30
	          player.mo.state = S_PLAY_STND
	      end
	
  if player.mo and player.mo.skin == "fang" and P_IsObjectOnGround(player.mo)
   and (player.cmd.buttons & BT_SPIN) and player.speed >= 1*FRACUNIT
	  and not (player.lastbuttons & BT_SPIN)
          player.mo.state = S_PLAY_GUNRUN
      elseif player.speed >= 37*FRACUNIT and player.mo.state == S_PLAY_GUNRUN and not (player.lastbuttons & BT_SPIN)
	      player.mo.state = S_PLAY_GUNRUNM
	  elseif player.speed >= 50*FRACUNIT and player.mo.state == S_PLAY_GUNRUNM and not (player.lastbuttons & BT_SPIN)
	      player.mo.state = S_PLAY_GUNRUNF
	  elseif player.speed == 0*FRACUNIT and player.mo.state == S_PLAY_GUNRUN
	      player.mo.state = S_PLAY_STND
	  elseif player.speed >= 1*FRACUNIT and player.mo.state == S_PLAY_FIRE
	      player.mo.state = S_PLAY_GUNRUN
     end 
end)
