AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include( "shared.lua" )

function ENT:Initialize()
	self.Entity:SetModel( "models/props_c17/TrapPropeller_Lever.mdl" )
	
	self:PhysicsInit(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()
	if IsValid(phys) then
		phys:Wake()
	end
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetCollisionGroup(COLLISION_GROUP_NONE)
	self.StartTime = CurTime()
end

function ENT:Use( activator, caller )
end

function ENT:Touch( ent )
	local phys = self:GetPhysicsObject()
	if IsValid(phys) and phys:GetVelocity():Length() > 100 then
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() )
		effectdata:SetNormal( self:GetPhysicsObject():GetVelocity():GetNormalized() * -1 )
		util.Effect( "Impact", effectdata )
	end
end

function ENT:Think()
	local phys = self:GetPhysicsObject()
	if !self:GetNWBool("Stuck") and self.StartTime + 4 < CurTime() then
		if IsValid(phys) then
			phys:AddVelocity( Vector(0,0,-350) )
		end
	elseif !self:GetNWBool("Stuck") and self.StartTime + 2 < CurTime() then
		if IsValid(phys) then
			phys:EnableGravity(true)
		end
	end
	
	if !self:GetNWBool("Stuck") then return end
	if self:GetNWBool("Useless") then return end
	
	local hookPos = self:GetPos()
	local groundPos = self:GetNWVector("DownHit", hookPos + Vector(0,0,-1000))
	
	for _, ply in ipairs(player.GetAll()) do
		if not IsValid(ply) or not ply:Alive() then continue end
		
		local plyPos = ply:GetPos() + Vector(0, 0, 40)
		local dist = util.DistanceToLine(hookPos, groundPos, plyPos)
		
		if dist < 60 then
			if ply:KeyDown(IN_USE) and (not ply.NextUse or ply.NextUse < CurTime()) then
				ply.NextUse = CurTime() + 0.5
				
				if ply:GetNWEntity("ClimbingEnt") == self and ply:GetMoveType() == MOVETYPE_CUSTOM then
					ply:SetMoveType(MOVETYPE_WALK)
					ply:SetGroundEntity(NULL)
					ply:SetNWEntity("ClimbingEnt", NULL)
					
					local dropPos = ply:GetPos()
					dropPos.x = hookPos.x 
					dropPos.y = hookPos.y
					ply:SetPos(dropPos - self:GetNWVector("HitNormal"):Angle():Forward() * 18)
					
					ply:DrawViewModel(true)
					ply:DrawWorldModel(true)
				elseif not IsValid(ply:GetNWEntity("ClimbingEnt")) then
					ply:SetMoveType(MOVETYPE_CUSTOM)
					ply:SetGroundEntity(NULL)
					ply:SetNWEntity("ClimbingEnt", self)
					
					local attachPos = ply:GetPos()
					attachPos.x = hookPos.x
					attachPos.y = hookPos.y
					
					if self:GetNWBool("MultiAngle") then
						local ang = ply:EyeAngles()
						ang.p = 0
						ang.r = 0
						ply:SetPos(attachPos - ang:Forward() * 14)
						ply:SetNWVector("ClimbNormal", ang:Forward())
					else
						ply:SetPos(attachPos - self:GetNWVector("HitNormal"):Angle():Forward() * 14)
					end
					
					if GetConVar("gk_enableshooting"):GetBool() == false then
						ply:DrawViewModel(false)
						ply:DrawWorldModel(false)
					end
				end
			end
		end
	end
end

function ENT:OnRemove()
	for k,ply in pairs(player.GetAll()) do
		if ply:GetNWEntity("ClimbingEnt") == self then
			ply:SetMoveType(MOVETYPE_WALK)
			ply:SetGroundEntity( NULL )
			ply:SetNWEntity("ClimbingEnt", NULL)
			ply:DrawViewModel(true)
			ply:DrawWorldModel(true)
		end
	end
end

hook.Add("PlayerNoClip", "NoclipRope", function( pl )
	local oldstate = pl:GetMoveType()
	if oldstate == MOVETYPE_CUSTOM and IsValid(pl:GetNWEntity("ClimbingEnt")) then
		local ent = pl:GetNWEntity("ClimbingEnt")
		pl:SetMoveType(MOVETYPE_WALK)
		pl:SetGroundEntity( NULL )
		local pos = pl:GetPos()
		pos.x = ent:GetPos().x
		pos.y = ent:GetPos().y
		pl:SetPos( pos - ent:GetNWVector("HitNormal"):Angle():Forward()*18 )
		pl:SetNWEntity("ClimbingEnt", NULL)
		pl:DrawViewModel(true)
		pl:DrawWorldModel(true)
	end
end)
