|
| 1 | +AddCSLuaFile() |
| 2 | + |
| 3 | +ENT.Base = "base_wire_entity" |
| 4 | +ENT.PrintName = "Wire Balloon Deployer" |
| 5 | +ENT.RenderGroup = RENDERGROUP_BOTH |
| 6 | +ENT.WireDebugName = "Balloon Deployer" |
| 7 | + |
| 8 | +if CLIENT then return end |
| 9 | + |
| 10 | +function ENT:Initialize() |
| 11 | + self:PhysicsInit(SOLID_VPHYSICS) |
| 12 | + WireLib.CreateInputs(self, { "Force", "Length", "Weld?", "Popable?", "BalloonType", "Deploy" }) |
| 13 | + WireLib.CreateOutputs(self, { "Deployed", "BalloonEntity [ENTITY]" }) |
| 14 | +end |
| 15 | + |
| 16 | +function ENT:Setup(force, length, weld, popable, balloontype) |
| 17 | + self.Force = force and math.Clamp(force, -1E34, 1E34) or 500 |
| 18 | + self.Length = length or 64 |
| 19 | + self.Weld = weld |
| 20 | + self.Popable = popable |
| 21 | + self.BalloonType = balloontype |
| 22 | + WireLib.TriggerOutput(self, "Deployed", 0) |
| 23 | +end |
| 24 | + |
| 25 | +WireLib.AddInputAlias("Lenght", "Length") |
| 26 | + |
| 27 | +function ENT:TriggerInput(iname, value) |
| 28 | + if iname == "Deploy" then |
| 29 | + if value ~= 0 then |
| 30 | + if not self.Deployed and (self.NextDeploy or 0) <= CurTime() then |
| 31 | + self:DeployBalloon() |
| 32 | + self.NextDeploy = CurTime() + 0.5 |
| 33 | + self.Deployed = true |
| 34 | + end |
| 35 | + |
| 36 | + WireLib.TriggerOutput(self, "Deployed", 1) |
| 37 | + else |
| 38 | + if self.Deployed then |
| 39 | + self:RetractBalloon() |
| 40 | + self.Deployed = false |
| 41 | + end |
| 42 | + |
| 43 | + WireLib.TriggerOutput(self, "Deployed", 0) |
| 44 | + end |
| 45 | + elseif iname == "Force" then |
| 46 | + self.Force = math.Clamp(value, -1E34, 1E34) |
| 47 | + |
| 48 | + if IsValid(self.Balloon) then |
| 49 | + self.Balloon:SetForce(self.Force) |
| 50 | + end |
| 51 | + elseif iname == "Length" then |
| 52 | + self.Length = value |
| 53 | + elseif iname == "Weld?" then |
| 54 | + self.Weld = value ~= 0 |
| 55 | + elseif iname == "Popable?" then |
| 56 | + self.Popable = value ~= 0 |
| 57 | + |
| 58 | + if IsValid(self.Balloon) then |
| 59 | + self.Balloon.Indestructible = not self.Popable |
| 60 | + end |
| 61 | + elseif iname == "BalloonType" then |
| 62 | + self.BalloonType = value + 1 |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +local balloon_types = { |
| 67 | + "models/maxofs2d/balloon_classic.mdl", |
| 68 | + "models/balloons/balloon_classicheart.mdl", |
| 69 | + "models/balloons/balloon_dog.mdl", |
| 70 | + "models/balloons/balloon_star.mdl", |
| 71 | + "models/maxofs2d/balloon_gman.mdl", |
| 72 | + "models/maxofs2d/balloon_mossman.mdl" |
| 73 | +} |
| 74 | + |
| 75 | +function ENT:DeployBalloon() |
| 76 | + local balloon = ents.Create("gmod_balloon") |
| 77 | + |
| 78 | + local model = balloon_types[self.BalloonType] or balloon_types[1] |
| 79 | + balloon:SetModel(model) |
| 80 | + balloon:Spawn() |
| 81 | + |
| 82 | + local pos = self:GetPos() + self:GetUp() * 25 |
| 83 | + balloon:SetPos(pos) |
| 84 | + |
| 85 | + balloon:SetPlayer(self:GetPlayer()) |
| 86 | + balloon:SetColor(ColorRand()) |
| 87 | + balloon:SetForce(self.Force) |
| 88 | + balloon.Indestructible = not self.Popable |
| 89 | + |
| 90 | + local hit_entity = self |
| 91 | + local hit_pos = self:LocalToWorld(Vector(0, 0, self:OBBMaxs().z)) |
| 92 | + |
| 93 | + local trace = util.TraceLine({ |
| 94 | + start = hit_pos, |
| 95 | + endpos = pos, |
| 96 | + filter = balloon |
| 97 | + }) |
| 98 | + |
| 99 | + if constraint.CanConstrain(trace.Entity, trace.PhysicsBone) then |
| 100 | + local phys = trace.Entity:GetPhysicsObjectNum(trace.PhysicsBone) |
| 101 | + |
| 102 | + if phys:IsValid() then |
| 103 | + hit_entity = trace.Entity |
| 104 | + hit_pos = trace.HitPos |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + if self.Weld then |
| 109 | + local const = constraint.Weld(balloon, hit_entity, 0, trace.PhysicsBone, 0) |
| 110 | + |
| 111 | + if const then |
| 112 | + balloon:DeleteOnRemove(const) |
| 113 | + end |
| 114 | + else |
| 115 | + local const, rope = constraint.Rope(balloon, hit_entity, 0, trace.PhysicsBone, balloon:WorldToLocal(pos), hit_entity:WorldToLocal(hit_pos), 0, math.Clamp(self.Length, 0, 1024), 0, 1.5, "cable/rope") |
| 116 | + |
| 117 | + if const then |
| 118 | + balloon:DeleteOnRemove(const) |
| 119 | + balloon:DeleteOnRemove(rope) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + self:DeleteOnRemove(balloon) |
| 124 | + self.Balloon = balloon |
| 125 | + |
| 126 | + WireLib.TriggerOutput(self, "BalloonEntity", balloon) |
| 127 | +end |
| 128 | + |
| 129 | +function ENT:RetractBalloon() |
| 130 | + if self.Balloon:IsValid() then |
| 131 | + local effectdata = EffectData() |
| 132 | + effectdata:SetOrigin(self.Balloon:GetPos()) |
| 133 | + |
| 134 | + local color = self.Balloon:GetColor() |
| 135 | + effectdata:SetStart(Vector(color.r, color.g, color.b)) |
| 136 | + util.Effect("balloon_pop", effectdata) |
| 137 | + |
| 138 | + self.Balloon:Remove() |
| 139 | + else |
| 140 | + self.Balloon = nil |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +function ENT:PrepareOverlayData() |
| 145 | + self:SetOverlayText("Deployed = " .. (self.Deployed and "yes" or "no")) |
| 146 | +end |
| 147 | + |
| 148 | +duplicator.RegisterEntityClass("gmod_wire_balloondeployer", WireLib.MakeWireEnt, "Data", "Force", "Length", "Weld", "Popable", "BallonType") |
| 149 | +scripted_ents.Alias("sent_deployableballoons", "gmod_wire_balloondeployer") |
0 commit comments