Skip to content

Instantly share code, notes, and snippets.

@trixnz
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trixnz/1b317af374d6b77cef85 to your computer and use it in GitHub Desktop.
Save trixnz/1b317af374d6b77cef85 to your computer and use it in GitHub Desktop.
Ion Cannon
--[[
Note: This script was created for demonstrative purposes only.
]]--
--- SERVER
Network:Subscribe('FireCannon', function(target, sender)
Network:Broadcast('FireCannon', target)
end)
-- CLIENT
local last_fire_timer = Timer()
local effects = {}
local fire_instances = {}
local first_time = true
Events:Subscribe( 'LocalPlayerInput', function(e)
if e.input == Action.FireRight then
if first_time or last_fire_timer:GetSeconds() > 2 then
local target = Physics:Raycast( Camera:GetPosition(), Camera:GetAngle() * Vector3.Forward, 0, 1024 )
Network:Send('FireCannon', target.position)
last_fire_timer:Restart()
first_time = false
end
end
end)
function CleanupEffects()
for k, v in pairs(effects) do
v:Remove()
end
effects = {}
end
function wait(ms)
local t = Timer()
while t:GetMilliseconds() < ms do
coroutine.yield()
end
end
function CannonTick(target)
-- Spawn the laser effects
local radius = 10
for i=1, 32 do
local pos = target + (Vector3(math.cos(i), 0, math.sin(i)) * radius)
local effect = Effect.Create(AssetLocation.Game, {
effect_id = 344,
position = pos,
angle = Angle()
})
table.insert(effects, effect)
wait(35)
radius = math.clamp(radius - .5, 0, 10)
end
wait(100)
-- Spawn the electrical disturbance
local effect = Effect.Create(AssetLocation.Game, {
effect_id = 137,
position = target,
angle = Angle()
})
table.insert(effects, effect)
wait(1000)
-- Spawn the explosion
local effect = Effect.Create(AssetLocation.Game, {
effect_id = 249,
position = target,
angle = Angle()
})
table.insert(effects, effect)
wait(5000)
CleanupEffects()
end
Events:Subscribe('PostTick', function()
for k, v in ipairs(fire_instances) do
if v ~= nil then
if coroutine.status(v) == 'dead' then
table.remove(fire_instances, k)
elseif coroutine.status(v) ~= 'dead' then
coroutine.resume(v)
end
end
end
end)
Events:Subscribe('ModuleUnload', function()
CleanupEffects()
end)
Network:Subscribe('FireCannon', function(target)
local co = coroutine.create(CannonTick)
coroutine.resume(co, target)
table.insert(fire_instances, co)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment