Skip to content

Instantly share code, notes, and snippets.

@jocafa
Created August 15, 2021 05:13
Show Gist options
  • Save jocafa/edb51dcd34ae0a823c1208e553daa59d to your computer and use it in GitHub Desktop.
Save jocafa/edb51dcd34ae0a823c1208e553daa59d to your computer and use it in GitHub Desktop.
"Burnless" explosion gun
local cfg = {
holeSize = 4,
impulseDistance = 5,
impulseMagnitude = 500
}
local events = {
--[[
(time) = {
center = {x, y , z},
bodies = {
(id) = true ...
}
}
]]
}
local maxImpulse = 0
function init()
RegisterTool('burnless','Burnless','MOD/mini_axis.vox')
SetBool('game.tool.burnless.enabled', true)
SetFloat('game.tool.burnless.ammo', 1000)
SetString('game.tool.burnless.ammo.display', '')
end
function tick(dt)
if GetString('game.player.tool') == 'burnless' and GetBool('game.player.canusetool') then
local now = GetTime()
local body = GetToolBody()
if body ~= 0 then
SetToolTransform(Transform(Vec(0, 0, 1)))
end
if InputPressed('usetool') or InputDown('grab') then
local camera = GetPlayerCameraTransform()
local origin = camera.pos
local direction = TransformToParentVec(camera, Vec(0, 0, -1))
local hit, distance, normal, shape = QueryRaycast(origin, direction, 1000)
if hit and shape then
local hitpoint = VecAdd(origin, VecScale(direction, distance))
local boxCorner = VecScale(Vec(1, 1, 1), cfg.holeSize)
local aabbBodies = QueryAabbBodies(VecSub(hitpoint, boxCorner), VecAdd(hitpoint, boxCorner))
events[now] = { center = hitpoint, bodies = {} }
for i=1, #aabbBodies do
events[now].bodies[aabbBodies[i]] = true
end
MakeHole(hitpoint, cfg.holeSize, cfg.holeSize, cfg.holeSize, true)
end
end
end
end
function update(dt)
ParticleReset()
ParticleTile(15)
ParticleType('plain')
ParticleColor(0, 1, 1, 1, 0, 1)
ParticleAlpha(0.5, 0.0, 'easeout')
ParticleEmissive(10, 0, 'easeout')
ParticleRadius(0, cfg.holeSize, 'easeout')
ParticleCollide(0)
ParticleDrag(0)
ParticleGravity(0)
for _, event in pairs(events) do
QueryRequire('physical dynamic')
local boxCorner = VecScale(Vec(1, 1, 1), cfg.impulseDistance)
local aabbBodies = QueryAabbBodies(VecSub(event.center, boxCorner), VecAdd(event.center, boxCorner))
for i=1, #aabbBodies do
local handle = aabbBodies[i]
local com = GetBodyCenterOfMass(handle)
local mass = GetBodyMass(handle)
local worldPoint = TransformToParentPoint(GetBodyTransform(handle), com)
local distance = VecLength(VecSub(worldPoint, event.center))
local speed = cfg.impulseMagnitude * (1/math.pow(distance+1, 2.0)) * mass
local velocity = VecScale(VecNormalize(VecSub(worldPoint, event.center)), speed)
ApplyBodyImpulse(handle, event.center, velocity)
if speed > maxImpulse then maxImpulse = speed end
end
SpawnParticle(event.center, Vec(0, 0, 0), 0.5)
end
events = {}
--DebugWatch('maxImpulse', maxImpulse)
end
function draw(dt)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment