Advertisement
FusionLord

custom_zone.lua

Sep 15th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.81 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. DEFINE_BASECLASS( "base_anim" )
  4.  
  5. ENT.PrintName = "Custom Zone"
  6. ENT.Author = "FusionLord"
  7. ENT.Information = "A configurable zone."
  8. ENT.Category = "temp"
  9.  
  10. ENT.Spawnable = true
  11. ENT.AdminOnly = false
  12. ENT.RenderGroup = RENDERGROUP_TRANSLUCENT
  13.  
  14. function ENT:Initialize()
  15.  
  16.     if SERVER then
  17.         self:SetTrigger(true);
  18.     end
  19.    
  20.     self:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
  21.     self:SetMoveType(MOVETYPE_NONE)
  22.     self:SetSolid(SOLID_VPHYSICS);
  23.     self:DrawShadow(false);
  24. end
  25.  
  26. function ENT:SetZoneData(data)
  27.     self:SetNWInt("type", data.typeID);
  28.  
  29.     self:SetPos(data.pos);
  30.     self:SetAngles(data.angles);
  31.  
  32.     self:UpdateBounds(data.mins, data.maxs);
  33. end
  34.  
  35. function ENT:UpdateBounds(mins, maxs)
  36.     net.Start("cz_zone_bounds_update");
  37.     net.WriteEntity(self);
  38.     net.WriteVector(mins);
  39.     net.WriteVector(maxs);
  40.     net.Broadcast()
  41. end
  42.  
  43. if SERVER then
  44.     util.AddNetworkString("cz_zone_bounds_update");
  45.  
  46.     function ENT:StartTouch(entity)
  47.         if entity:GetClass() ~= "player" then return end
  48.         local zone = customzones:GetZoneTypeFromInt(self:GetNWInt("type"));
  49.         if zone.entering then
  50.             zone.entering(entity, self);
  51.         end
  52.     end
  53.  
  54.     function ENT:Touch(entity)
  55.         if entity:GetClass() ~= "player" then
  56.             return;
  57.         end
  58.         local zone = customzones:GetZoneTypeFromInt(self:GetNWInt("type"));
  59.         if zone.inside then
  60.             zone.inside(entity, self);
  61.         end
  62.     end
  63.  
  64.     function ENT:EndTouch(entity)
  65.         if entity:GetClass() ~= "player" then
  66.             return;
  67.         end
  68.         local zone = customzones:GetZoneTypeFromInt(self:GetNWInt("type"));
  69.         if zone.exiting then
  70.             zone.exiting(entity, self);
  71.         end
  72.     end
  73.  
  74.     function ENT:Freeze()
  75.         local phys = self:GetPhysicsObject();
  76.         if IsValid(phys) then
  77.             phys:EnableMotion( false );
  78.         end
  79.     end
  80. elseif CLIENT then
  81.     net.Receive("cz_zone_bounds_update", function(len, ply)
  82.         local entity = net.ReadEntity();
  83.         local mins = net.ReadVector();
  84.         local maxs = net.ReadVector();
  85.  
  86.         entity:PhysicsInitConvex({ -- This is where the entity is null.
  87.                 Vector( mins.x, mins.y, mins.z ),
  88.                 Vector( mins.x, mins.y, maxs.z ),
  89.                 Vector( mins.x, maxs.y, mins.z ),
  90.                 Vector( mins.x, maxs.y, maxs.z ),
  91.                 Vector( maxs.x, mins.y, mins.z ),
  92.                 Vector( maxs.x, mins.y, maxs.z ),
  93.                 Vector( maxs.x, maxs.y, mins.z ),
  94.                 Vector( maxs.x, maxs.y, maxs.z ),
  95.         });
  96.         entity:EnableCustomCollisions(true);
  97.         entity:SetCollisionBounds(maxs, mins);
  98.         entity:PhysicsInit(entity:GetSolid());
  99.         entity:Freeze();
  100.         UTIL:Log("Updated physics!");
  101.     end);
  102.  
  103.     function ENT:Draw()
  104.         local phys = self:GetPhysicsObject();
  105.         if IsValid(phys) then
  106.             local mins, maxs = phys:GetAABB();
  107.             render.DrawWireframeBox(self:GetPos(), self:GetAngles(), mins, maxs, Color(0, 0, 255), true);
  108.         else
  109.             render.DrawWireframeSphere(self:GetPos(),16,16,16,Color(255, 0, 0),true);
  110.         end
  111.     end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement