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

ENT.model = "models/buildables/sentry3_rockets.mdl"
ENT.AutomaticFrameAdvance = true 

function ENT:Initialize()
	self:SetModel( self.model )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_CUSTOM )
	self:SetHealth(1)
	self:PhysicsInitSphere( 12 )
	self:SetCollisionBounds(Vector(-12,-12,-12),Vector(12,12,12)) 
	self:SetMoveCollide( 3 )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(1)
		phys:EnableGravity( false )
		phys:EnableDrag( false )
		phys:ApplyForceCenter( self:GetForward() *1500 )
	end
	
	self.ai_sound = ents.Create( "ai_sound" )
	self.ai_sound:SetPos( self:GetPos() )
	self.ai_sound:SetKeyValue( "volume", "80" )
	self.ai_sound:SetKeyValue( "duration", "8" )
	self.ai_sound:SetKeyValue( "soundtype", "8" )
	self.ai_sound:SetParent( self )
	self.ai_sound:Spawn()
	self.ai_sound:Activate()
	self.ai_sound:Fire( "EmitAISound", "", 0 )
	
	self:ResetSequence( self:LookupSequence( "idle" ) )
	self:SetPlaybackRate( 1 )
end

function ENT:OnRemove()
	self.ai_sound:Remove()
end

function ENT:Think()
	if self.enemy and ValidEntity(self.enemy) then
		local angle = (self.enemy:GetPosCenter() -self:GetPos()):Angle()
		if angle.y >= 160 and angle.y <= 200 and (angle.p <= 20 or angle.p >= 340) then
			self:SetAngles(angle)

			local phys = self:GetPhysicsObject()
			if (phys:IsValid()) then
				phys:ApplyForceCenter( self:GetForward() *1500 )
			end
		end
	end
	//self:NextThink( CurTime() +0.1 )
	//return true
end

function ENT:Explode()
	local vPoint = self:GetPos()
	local effectdata = EffectData()
	effectdata:SetStart( vPoint )
	effectdata:SetOrigin( vPoint )
	effectdata:SetScale( 1 )
	self:EmitSound( "weapons/explode" .. math.random(1,3) .. ".wav", 100, 100 )
	local explosion = ents.Create( "info_particle_system" )
	explosion:SetKeyValue( "effect_name", "ExplosionCore_MidAir" )
	explosion:SetPos( self:GetPos()	) 
	explosion:SetAngles( self:GetAngles() )
	explosion:Spawn()
	explosion:Activate() 
	explosion:Fire( "Start", "", 0 )
	explosion:Fire( "Kill", "", 0.1 )
	
	if self.sentryowner and ValidEntity( self.sentryowner ) then
		util.BlastDamage( self, self.sentryowner, self:GetPos(), 180, 85 )
	elseif self.owner and ValidEntity(self.owner) then
		util.BlastDamage( self, self.owner, self:GetPos(), 180, 85 )
	else
		util.BlastDamage( self, self, self:GetPos(), 180, 85 )
	end
	
	self:Remove()
end

function ENT:PhysicsCollide( data, physobj )
	self:Explode()
	return true
end

