Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. if (SERVER) then
  2. AddCSLuaFile();
  3. end;
  4.  
  5. local material = Material("effects/com_shield003a");
  6. local material2 = Material("effects/com_shield004a");
  7.  
  8. ENT.Type = "anim";
  9. ENT.Base = "base_anim";
  10. ENT.PrintName = "Forcefield";
  11. ENT.Category = "Forcefields";
  12. ENT.Spawnable = true;
  13. ENT.AdminOnly = true;
  14. ENT.RenderGroup = RENDERGROUP_BOTH;
  15. ENT.PhysgunDisabled = true;
  16.  
  17. if (SERVER) then
  18.  
  19. function ENT:SpawnFunction(player, trace)
  20. if !(trace.Hit) then return; end;
  21. local entity = ents.Create("z_forcefield");
  22.  
  23. entity:SetPos(trace.HitPos + Vector(0, 0, 40));
  24. entity:SetAngles(Angle(0, trace.HitNormal:Angle().y - 90, 0));
  25. entity:Spawn();
  26. entity.Owner = player;
  27.  
  28. return entity;
  29. end;
  30.  
  31. function ENT:SetupDataTables()
  32. self:DTVar("Bool", 0, "Enabled");
  33. self:DTVar("Bool", 1, "Alt");
  34. self:DTVar("Entity", 0, "Dummy");
  35. end;
  36.  
  37. function ENT:Initialize()
  38. self:SetModel("models/props_combine/combine_fence01b.mdl");
  39. self:SetSolid(SOLID_VPHYSICS);
  40. self:SetUseType(SIMPLE_USE);
  41. self:PhysicsInit(SOLID_VPHYSICS);
  42. self:DrawShadow(false);
  43. self:SetDTBool(0, true);
  44.  
  45. if (!self.noCorrect) then
  46. local data = {};
  47. data.start = self:GetPos();
  48. data.endpos = self:GetPos() - Vector(0, 0, 300);
  49. data.filter = self;
  50. local trace = util.TraceLine(data);
  51.  
  52. if trace.Hit and util.IsInWorld(trace.HitPos) and self:IsInWorld() then
  53. self:SetPos(trace.HitPos + Vector(0, 0, 39.9));
  54. end;
  55.  
  56. data = {};
  57. data.start = self:GetPos();
  58. data.endpos = self:GetPos() + Vector(0, 0, 150);
  59. data.filter = self;
  60. trace = util.TraceLine(data);
  61.  
  62. if (trace.Hit) then
  63. self:SetPos(self:GetPos() - Vector(0, 0, trace.HitPos:Distance(self:GetPos() + Vector(0, 0, 151))));
  64. end;
  65. end;
  66.  
  67. data = {};
  68. data.start = self:GetPos() + Vector(0, 0, 50) + self:GetRight() * -16;
  69. data.endpos = self:GetPos() + Vector(0, 0, 50) + self:GetRight() * -600;
  70. data.filter = self;
  71. trace = util.TraceLine(data);
  72.  
  73. self.post = ents.Create("prop_physics")
  74. self.post:SetModel("models/props_combine/combine_fence01a.mdl")
  75. self.post:SetPos(self.forcePos or trace.HitPos - Vector(0, 0, 50))
  76. self.post:SetAngles(Angle(0, self:GetAngles().y, 0));
  77. self.post:Spawn();
  78. self.post:PhysicsDestroy()
  79. self.post:SetCollisionGroup(COLLISION_GROUP_WORLD);
  80. self.post:DrawShadow(false);
  81. self.post:DeleteOnRemove(self);
  82. self:DeleteOnRemove(self.post);
  83.  
  84. local verts = {
  85. {pos = Vector(0, 0, -35)},
  86. {pos = Vector(0, 0, 150)},
  87. {pos = self:WorldToLocal(self.post:GetPos()) + Vector(0, 0, 150)},
  88. {pos = self:WorldToLocal(self.post:GetPos()) + Vector(0, 0, 150)},
  89. {pos = self:WorldToLocal(self.post:GetPos()) - Vector(0, 0, 35)},
  90. {pos = Vector(0, 0, -35)},
  91. }
  92.  
  93. self:PhysicsFromMesh(verts);
  94.  
  95. local physObj = self:GetPhysicsObject();
  96.  
  97. if (IsValid(physObj)) then
  98. physObj:SetMaterial("default_silent");
  99. physObj:EnableMotion(false);
  100. end;
  101.  
  102. self:SetCustomCollisionCheck(true);
  103. self:EnableCustomCollisions(true);
  104.  
  105. physObj = self.post:GetPhysicsObject();
  106.  
  107. if (IsValid(physObj)) then
  108. physObj:EnableMotion(false);
  109. end;
  110.  
  111. self:SetDTEntity(0, self.post);
  112.  
  113. self.ShieldLoop = CreateSound(self, "ambient/machines/combine_shield_loop3.wav");
  114. self.AllowedTeams = {};
  115. self.AllowedPlayers = {};
  116. self.Contributors = {};
  117. end;
  118.  
  119. function ENT:StartTouch(ent)
  120. if (!self:GetDTBool(0)) then return; end;
  121.  
  122. if (ent:IsPlayer()) then
  123. if (self:ShouldCollide(ent)) then
  124. if (!ent.ShieldTouch) then
  125. ent.ShieldTouch = CreateSound(ent, "ambient/machines/combine_shield_touch_loop1.wav");
  126. ent.ShieldTouch:Play();
  127. ent.ShieldTouch:ChangeVolume(0.25, 0);
  128. else
  129. ent.ShieldTouch:Play();
  130. ent.ShieldTouch:ChangeVolume(0.25, 0.5);
  131. end;
  132. end;
  133. end;
  134. end;
  135.  
  136. function ENT:Touch(ent)
  137. if (!self:GetDTBool(0)) then return; end;
  138.  
  139. if (ent:IsPlayer()) then
  140. if (self:ShouldCollide(ent)) then
  141. if ent.ShieldTouch then
  142. ent.ShieldTouch:ChangeVolume(0.3, 0);
  143. end;
  144. end;
  145. end;
  146. end;
  147.  
  148. function ENT:EndTouch(ent)
  149. if (!self:GetDTBool(0)) then return; end;
  150.  
  151. if (ent:IsPlayer()) then
  152. if (self:ShouldCollide(ent)) then
  153. if (ent.ShieldTouch) then
  154. ent.ShieldTouch:FadeOut(0.5);
  155. end;
  156. end;
  157. end;
  158. end;
  159.  
  160. function ENT:Think()
  161. if (IsValid(self) and self:GetDTBool(0)) then
  162. self.ShieldLoop:Play();
  163. self.ShieldLoop:ChangeVolume(0.4, 0);
  164. else
  165. self.ShieldLoop:Stop();
  166. end;
  167.  
  168. if (IsValid(self:GetPhysicsObject())) then
  169. self:GetPhysicsObject():EnableMotion(false);
  170. end;
  171. end;
  172.  
  173. function ENT:OnRemove()
  174. if (self.ShieldLoop) then
  175. self.ShieldLoop:Stop();
  176. end;
  177. end;
  178.  
  179. function ENT:Use(act, call, type, val)
  180. if ((self.nextUse or 0) < CurTime()) then
  181. self.nextUse = CurTime() + 1;
  182. else
  183. return;
  184. end;
  185.  
  186. if (self.Owner == act or self.Contributors[act]) then
  187. if (act:KeyDown(IN_WALK)) then
  188. netstream.Start(act, "forcefieldMenu", self);
  189.  
  190. act:SetNWEntity("ffTarget", self);
  191.  
  192. return;
  193. end;
  194.  
  195. self:SetDTBool(0, !self:GetDTBool(0));
  196.  
  197. if (!self:GetDTBool(0)) then
  198. self:SetSkin(1);
  199. self.post:SetSkin(1);
  200. self:EmitSound("shield/deactivate.wav");
  201. self:SetCollisionGroup(COLLISION_GROUP_WORLD);
  202. else
  203. self:SetSkin(0);
  204. self.post:SetSkin(0);
  205. self:EmitSound("shield/activate.wav");
  206. self:SetCollisionGroup(COLLISION_GROUP_NONE);
  207. end;
  208.  
  209. self:EmitSound("buttons/combine_button5.wav", 110, 90);
  210. end;
  211. end;
  212.  
  213. function ENT:OnRemove()
  214. if (self.ShieldLoop) then
  215. self.ShieldLoop:Stop();
  216. self.ShieldLoop = nil;
  217. end;
  218.  
  219. if (self.ShieldTouch) then
  220. self.ShieldTouch:Stop();
  221. self.ShieldTouch = nil;
  222. end;
  223. end;
  224.  
  225. function ENT:AddPlayer(player)
  226. self.AllowedPlayers[player] = true;
  227. netstream.Start(nil, "forcefieldUpdate", "addPlayer", {self, player});
  228. end;
  229.  
  230. function ENT:RemovePlayer(player)
  231. self.AllowedPlayers[player] = nil;
  232. netstream.Start(nil, "forcefieldUpdate", "removePlayer", {self, player});
  233. end;
  234.  
  235. function ENT:AddTeam(team)
  236. self.AllowedTeams[team] = true;
  237. netstream.Start(nil, "forcefieldUpdate", "addTeam", {self, team});
  238. end;
  239.  
  240. function ENT:RemoveTeam(team)
  241. self.AllowedTeams[team] = nil;
  242. netstream.Start(nil, "forcefieldUpdate", "removeTeam", {self, team});
  243. end;
  244.  
  245. function ENT:AddContributor(player)
  246. self.Contributors[player] = true;
  247. netstream.Start(nil, "forcefieldUpdate", "addContributor", {self, player});
  248. end;
  249.  
  250. function ENT:RemoveContributor(player)
  251. self.Contributors[player] = nil;
  252. netstream.Start(nil, "forcefieldUpdate", "removeContributor", {self, player});
  253. end;
  254.  
  255. function ENT:SendFullUpdate(player)
  256. if (IsValid(player)) then
  257. netstream.Start(player, "forcefieldUpdate", "fullUpdate", {self, self.AllowedPlayers, self.AllowedTeams, self.Contributors});
  258. elseif (player == false) then
  259. netstream.Start(nil, "forcefieldUpdate", "fullUpdate", {self, self.AllowedPlayers, self.AllowedTeams, self.Contributors});
  260. end;
  261. end;
  262. end;
  263.  
  264. if (CLIENT) then
  265.  
  266. function ENT:Initialize()
  267. local data = {};
  268. data.start = self:GetPos() + Vector(0, 0, 50) + self:GetRight() * -16;
  269. data.endpos = self:GetPos() + Vector(0, 0, 50) + self:GetRight() * -600;
  270. data.filter = self;
  271. local trace = util.TraceLine(data);
  272.  
  273. local verts = {
  274. {pos = Vector(0, 0, -35)},
  275. {pos = Vector(0, 0, 150)},
  276. {pos = self:WorldToLocal(trace.HitPos - Vector(0, 0, 50)) + Vector(0, 0, 150)},
  277. {pos = self:WorldToLocal(trace.HitPos - Vector(0, 0, 50)) + Vector(0, 0, 150)},
  278. {pos = self:WorldToLocal(trace.HitPos - Vector(0, 0, 50)) - Vector(0, 0, 35)},
  279. {pos = Vector(0, 0, -35)},
  280. };
  281.  
  282. self:PhysicsFromMesh(verts);
  283. self:EnableCustomCollisions(true);
  284.  
  285. self.AllowedTeams = {};
  286. self.AllowedPlayers = {};
  287. self.Contributors = {};
  288. end;
  289.  
  290. function ENT:Draw()
  291. local post = self:GetDTEntity(0);
  292. local angles = self:GetAngles();
  293. local matrix = Matrix();
  294.  
  295. self:DrawModel();
  296. matrix:Translate(self:GetPos() + self:GetUp() * -40 + self:GetForward() * -2);
  297. matrix:Rotate(angles);
  298.  
  299. render.SetMaterial(self:GetDTBool(1) and material2 or material);
  300.  
  301. if (IsValid(post)) then
  302. local vertex = self:WorldToLocal(post:GetPos());
  303. self:SetRenderBounds(vector_origin - Vector(0, 0, 40), vertex + self:GetUp() * 150);
  304.  
  305. cam.PushModelMatrix(matrix);
  306. self:DrawShield(vertex);
  307. cam.PopModelMatrix();
  308.  
  309. matrix:Translate(vertex);
  310. matrix:Rotate(Angle(0, 180, 0));
  311.  
  312. cam.PushModelMatrix(matrix);
  313. self:DrawShield(vertex);
  314. cam.PopModelMatrix();
  315. end;
  316. end;
  317.  
  318. -- I took a peek at how Chessnut drew his forcefields.
  319. function ENT:DrawShield(vertex)
  320. if (self:GetDTBool(0)) then
  321. local dist = self:GetDTEntity(0):GetPos():Distance(self:GetPos());
  322. local useAlt = self:GetDTBool(1);
  323. local matFac = useAlt and 70 or 45;
  324. local height = useAlt and 3 or 5;
  325. local frac = dist / matFac;
  326. mesh.Begin(MATERIAL_QUADS, 1);
  327. mesh.Position(vector_origin);
  328. mesh.TexCoord(0, 0, 0);
  329. mesh.AdvanceVertex();
  330. mesh.Position(self:GetUp() * 190);
  331. mesh.TexCoord(0, 0, height);
  332. mesh.AdvanceVertex();
  333. mesh.Position(vertex + self:GetUp() * 190);
  334. mesh.TexCoord(0, frac, height);
  335. mesh.AdvanceVertex();
  336. mesh.Position(vertex);
  337. mesh.TexCoord(0, frac, 0);
  338. mesh.AdvanceVertex();
  339. mesh.End();
  340. end;
  341. end;
  342. end;
  343.  
  344. function ENT:ShouldCollide(ent)
  345. if (!self:GetDTBool(0)) then return false; end;
  346.  
  347. if (ent:IsPlayer()) then
  348. if (self.AllowedPlayers and self.AllowedPlayers[ent]) then
  349. return false;
  350. elseif (self.AllowedTeams and self.AllowedTeams[ent:Team()]) then
  351. return false;
  352. else
  353. return true;
  354. end;
  355. else
  356. return true;
  357. end;
  358. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement