Help me understand this entity stuff

Okay so I’m going to make a SWEP or something that you can create props with, and want to know what’s doing what. I found this in the codebase for the toolgun, but I don’t get all of it

It calls a few functions from other files in here: http://luabin.foszor.com/code/gamemodes/sandbox/entities/weapons/gmod_tool

[lua]function ToolObj:UpdateGhostEntity()
if (self.GhostEntity == nil) then return end
if (!self.GhostEntity:IsValid()) then self.GhostEntity = nil return end --if the entity is junk don’t do anything okay

    local tr = utilx.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() ) --get where the player is looking, or in this case clicking
    local trace = util.TraceLine( tr ) --what the players lookin at
    if (!trace.Hit) then return end --lookin at the sky or some shit, do nothing 

    local Ang1, Ang2 = self:GetNormal(1):Angle(), (trace.HitNormal * -1):Angle() --???
    local TargetAngle = self:GetEnt(1):AlignAngles( Ang1, Ang2 ) --???

    self.GhostEntity:SetPos( self:GetEnt(1):GetPos() ) --move the ghostent to somewhere
    self.GhostEntity:SetAngles( TargetAngle ) 

    local TranslatedPos = self.GhostEntity:LocalToWorld( self:GetLocalPos(1) )
    local TargetPos = trace.HitPos + (self:GetEnt(1):GetPos() - TranslatedPos) + (trace.HitNormal) --whats all this 
    self.GhostEntity:SetPos( TargetPos ) --whyd we move it again

end[/lua]

The bit of code you posted controls the angle and position of the ghost entity (The see-through prop which follows your cursor until you make the actual object).

The two lines you question marked align the angles of the ghost ent with the normal of the surface you’re looking at (so it sits flat against a wall or whatever).