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

local sound_dropship = Sound( "npc/combine_gunship/dropship_engine_near_loop1.wav" ) -- DEFAULT GUNSHIP SOUND IN MOVEMENT

function ENT:SpawnFunction( ply, tr )

	local SpawnPos = tr.HitPos + tr.HitNormal * 100
	local ent = ents.Create( "sent_tacfighter" ) -- NAME OF THE FOLDER
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
	
end

function ENT:Initialize()
	
	if (!self.Sound) then
 		self.Sound = CreateSound( self.Entity, sound_dropship )
 	end
	
	self.Hover = true;
	
	self.Entity:SetUseType( SIMPLE_USE )
	self.Pilot = nil -- Pilot player
	
	self.LastAttack1 = 0;
	self.LastAttack2 = 0;
	
	self.Entity:SetModel( "models/hawx/planes/fa-18e_superhornet.mdl" ) --- THIS IS THE MODEL 
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(10000) -- wow ace this is heavy like a nigger
	end
	
	self.Entity:StartMotionController()
	self.Accel=0

end

function ENT:OnTakeDamage( dmginfo )
	 if self.Pilot then
		self.Pilot:TakeDamage(dmginfo:GetDamage()/3, dmginfo:GetAttacker())
	end
end

function ENT:OnRemove()
	
	if self.Pilot then
		self:UndoPilot();
	end
	
end

function ENT:Think()
	
	if( not ValidEntity( self.Pilot ) ) then
		
		self.Pilot = nil;
		
	end
	
	if self.Pilot then
		
		if( self.Pilot:KeyDown( IN_ATTACK ) and CurTime() - self.LastAttack1 >= 0.1 ) then
			
			local dummy1 = ents.Create( "prop_physics" );
			dummy1:SetModel( "models/props_junk/PopCan01a.mdl" );
			dummy1:SetPos( self.Entity:GetPos() + self.Entity:GetForward() * -50 + self.Entity:GetRight() * -250 + self.Entity:GetUp() * 70 );
			dummy1:SetColor( 255, 255, 255, 0 );
			dummy1:Spawn();
			
			local dummy2 = ents.Create( "prop_physics" );
			dummy2:SetModel( "models/props_junk/PopCan01a.mdl" );
			dummy2:SetPos( self.Entity:GetPos() + self.Entity:GetForward() * -50 + self.Entity:GetRight() * 250 + self.Entity:GetUp() * 70 );
			dummy2:SetColor( 255, 255, 255, 0 );
			dummy2:Spawn();
			
			local shootAngles = self.Entity:GetAngles()
			local shootDir = shootAngles:Forward()
			
			local bullet = {}
				bullet.Num 			= 1;
				bullet.Src 			= self.Entity:GetPos() + self.Entity:GetForward() * -50 + self.Entity:GetRight() * -250 + self.Entity:GetUp() * 70;
				bullet.Dir 			= shootDir;
				bullet.Spread 		= 0.1;
				bullet.Tracer		= 1;
				bullet.TracerName 	= "Tracer";
				bullet.Force		= 50;
				bullet.Damage		= 50;
				bullet.Attacker 	= self.Pilot;	
			dummy1:FireBullets( bullet );
			
			local bullet = {}
				bullet.Num 			= 1;
				bullet.Src 			= self.Entity:GetPos() + self.Entity:GetForward() * -50 + self.Entity:GetRight() * 250 + self.Entity:GetUp() * 70;
				bullet.Dir 			= shootDir;
				bullet.Spread 		= 0.1;
				bullet.Tracer		= 1;
				bullet.TracerName 	= "Tracer";
				bullet.Force		= 50;
				bullet.Damage		= 50;
				bullet.Attacker 	= self.Pilot;	
			dummy2:FireBullets( bullet );
			
			dummy1:Remove();
			dummy2:Remove();
			
			self.Entity:EmitSound( Sound( "Weapon_M249.Single" ) );
			
			self.LastAttack1 = CurTime();
			
		end
		
		if( self.Pilot:KeyDown( IN_ATTACK2 ) and CurTime() - self.LastAttack2 >= 0.4 ) then
			
			local pos = self.Entity:GetPos() + self.Entity:GetUp() * -100;
			local bomb = ents.Create( "sent_tacfighterbomb" );
				bomb:SetPos( pos );
				bomb:Spawn();
				local phy = bomb:GetPhysicsObject();
				if( phy:IsValid() ) then
					
					phy:SetVelocity( self.Entity:GetVelocity() * 0.75 );
					
				end
				
			self.LastAttack2 = CurTime();
			
		end
		
		self.Pilot:SetPos( self.Entity:GetPos() );
		
		if( self.Pilot:KeyDown( IN_USE ) ) then
			
			self:UndoPilot();
			
		end
		
		if( self.Pilot ) then
		
			if( not self.Pilot:Alive() ) then
				
				self:UndoPilot();
				
			end
			
		end
		
		self.Entity:NextThink( CurTime() );
		
	else
	
		self.Entity:NextThink( CurTime() + 1 );
		
	end	
	
	return true
	
end

function ENT:UndoPilot()
	
	self.Pilot:UnSpectate()
	self.Pilot:Spawn();
	self.Pilot:DrawViewModel(true)
	self.Pilot:DrawWorldModel(true)
	self.Entity:SetOwner(nil)
	self.Pilot:SetPos( self.Entity:GetPos() + self.Entity:GetForward() * 175 + self.Entity:GetUp() * 175 );
	self.Pilot:SetNetworkedBool("isDriveJumper",false)

	self.Accel = 0
	
	self.Entity:SetLocalVelocity(Vector(0,0,0))
	self.Pilot = nil
	
	self.Sound:Stop()
	
end

function ENT:SetPilot( ply )
	
	-- Default dropship sound start when go when into the ship
	self.Sound:Play()
	
	ply:Spectate( OBS_MODE_CHASE )
	ply:SpectateEntity(self.Entity)
	ply:SetNetworkedBool("isDriveJumper",true)
	ply:SetNetworkedEntity("Jumper",self.Entity)
	self.Entity:GetPhysicsObject():Wake()
	self.Entity:GetPhysicsObject():EnableMotion(true)
	self.Entity:SetOwner(ply)
	ply:DrawViewModel(false)
	ply:DrawWorldModel(false)
	self.Pilot = ply;
	
end

function ENT:Use(ply,caller)

	if( ply:GetActiveWeapon() ) then
		
		ply:GetActiveWeapon():SetNoDraw( true );
		
	end

	if( not self.Pilot ) then
		
		ply:SelectWeapon( "ts_hands" );
		self:SetPilot( ply );
		
	end
	
end

function ENT:PhysicsSimulate( phys, deltatime )

	if( self.Pilot ) then
		
		local num = 0
		if self.Pilot:KeyDown(IN_FORWARD) then
            num = 1500
        elseif self.Pilot:KeyDown(IN_BACK) then
            num = -1500
        end
		
		phys:Wake();
		self.Accel=math.Approach( self.Accel, num, 10 );
		
		if not self.Hover then
		
            if self.Accel > -200 and self.Accel < 200 then return end --with out this you float
			
        end
		
		local move = { };
		move.secondstoarrive = 1
		move.pos = self.Entity:GetPos()+self.Entity:GetForward()*self.Accel
		if self.Pilot:KeyDown( IN_DUCK ) then
            move.pos = move.pos+self.Entity:GetUp()*-200
        elseif self.Pilot:KeyDown( IN_JUMP ) then
            move.pos = move.pos+self.Entity:GetUp()*300
		end

        if self.Pilot:KeyDown( IN_MOVERIGHT ) then
			move.pos = move.pos+self.Entity:GetRight()*200
		elseif self.Pilot:KeyDown( IN_MOVELEFT ) then
			move.pos = move.pos+self.Entity:GetRight()*-200
		end
			
			
		move.maxangular		= 5000
		move.maxangulardamp	= 10000
		move.maxspeed			= 1000000
		move.maxspeeddamp		= 10000
		move.dampfactor		= 0.8
		move.teleportdistance	= 5000
		local ang = self.Pilot:GetAimVector():Angle();
		if( self.Pilot:KeyDown( IN_SPEED ) ) then
			ang = self:GetAngles();
		end
		move.angle			= ang
		move.deltatime		= deltatime
		phys:ComputeShadowControl(move)
		
	end
	
end
