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


/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	// Use the helibomb model just for the shadow (because it's about the same size)
	self.Entity:SetModel( "models/dav0r/hoverball.mdl" )
	
	// Don't use the model's physics object, create a perfect sphere
	self.Entity:PhysicsInitSphere( 8, "metal_bouncy" )
	
	// Wake up our physics object so we don't start asleep
	local phys = self.Entity:GetPhysicsObject()
	if ( phys:IsValid() ) then 
		phys:SetMass( 400 )
		phys:EnableGravity( false )
		phys:Wake() 
	end
	
	// Start with the matrix effect by default
	self:SetEffect( self.EFFECT_MATRIX )	
	
	// Start the motion controller (so PhysicsSimulate gets called)
	self.Entity:StartMotionController()
	
	// Start floating 32 above ground
	self:SetAltitude( 128 )
	self:SetAltitudeVelocity( 0 )
	
	self.Fraction = 0
	
end


/*---------------------------------------------------------
   Name: SetEffect
---------------------------------------------------------*/
function ENT:SetEffect( type )

	self.Effect = type
	self.Entity:SetNetworkedInt( self.IDX_EFFECT, self.Effect )
	
end

/*---------------------------------------------------------
   Sets whether this thruster is constrained by altitude
---------------------------------------------------------*/
function ENT:SetAltitude( alt )

	self.Altitude = alt
	self.Entity:SetNetworkedInt( self.IDX_ALTITUDE, self.Altitude )
	
end

/*---------------------------------------------------------
   Sets whether this thruster is constrained by altitude
---------------------------------------------------------*/
function ENT:SetAltitudeVelocity( num )

	self.AltitudeVelocity = num
	
end


/*---------------------------------------------------------
   Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )

	//self.Entity:TakePhysicsDamage( dmginfo )
	
end


/*---------------------------------------------------------
   Name: Use
---------------------------------------------------------*/
function ENT:Use( activator, caller )

end

function ENT:Think()

	if ( self.AltitudeVelocity != 0 ) then
	
		local Target = self.Altitude + self.AltitudeVelocity * 10
		if ( Target < 8 ) then Target = 8 end
		self:SetAltitude( Target )
		
		local phys = self.Entity:GetPhysicsObject()
		if ( phys:IsValid() ) then 
			phys:Wake() 
		end
			
	end
	
	self.Fraction = self:GetFractionFromHover()
	self.Entity:NextThink( CurTime() + 0.1 )

end

/*---------------------------------------------------------
   Name: Simulate
---------------------------------------------------------*/
function ENT:PhysicsSimulate( phys, deltatime )

	local ForceAngle, ForceLinear = self.ForceAngle, self.ForceLinear
	local CurVel = phys:GetVelocity()

	local Altitude = self.Fraction
	
	if (Altitude > 0 && CurVel.z > 0 || Altitude < 0 && CurVel.z < 0 ) then
	
		// Todo, a better curve for this
		Altitude = Altitude * 2 //(1 + math.abs(CurVel.z)/100)
	
	end

	ForceAngle = Vector( 0, 0, 0 )
	ForceLinear = Vector( 0, 0, -Altitude )
	ForceLinear = ForceLinear * 500
	
	return ForceAngle, ForceLinear, SIM_GLOBAL_ACCELERATION
	
end

/*---------------------------------------------------------
   Returns a value if this thruster is constrained by altitude
---------------------------------------------------------*/
function ENT:GetAltitude()
	return self.altitude
end


/*---------------------------------------------------------
   Numpad control functions
   These are layed out like this so it'll all get saved properly
---------------------------------------------------------*/
local function Up( pl, ent, keydown )

	if (!ent:IsValid()) then return false end
	
	if (keydown) then
		ent:GetTable():SetAltitudeVelocity( 1 )
	else
		ent:GetTable():SetAltitudeVelocity( 0 )
	end
	
	return true
	
end

local function Down( pl, ent, keydown )

	if (!ent:IsValid()) then return false end
	
	if (keydown) then
		ent:GetTable():SetAltitudeVelocity( -1 )
	else
		ent:GetTable():SetAltitudeVelocity( 0 )
	end
	
	return true
	
end

// register numpad functions
numpad.Register( "Hoverball_Up", 	Up )
numpad.Register( "Hoverball_Down", 	Down )

include('shared.lua')