local horseHeads = {} function init() RegisterTool("revolver", "loc@TOOL_NAME_REVOLVER", "MOD/data/vox/tool/revolver.vox", 3) horseHeads = FindBodies("head", true) angle = 0 angVel = 0 coolDown = 0 smoke = 0 shootSnd = {} for i=0, 5 do shootSnd[i] = LoadSound("tools/gun"..i..".ogg") end oldPipePos = Vec() particleTimer = 0 shape = 1 isLeftShot = false shootHaptic = LoadHaptic("MOD/haptic/gun_fire.xml") toolHaptic = LoadHaptic("MOD/haptic/background.xml") SetToolHaptic("revolver", toolHaptic); end function rndVec(length) local v = VecNormalize(Vec(math.random(-100,100), math.random(-100,100), math.random(-100,100))) return VecScale(v, length) end function rnd(mi, ma) return math.random(1000)/1000*(ma-mi) + mi end function tick(dt) if GetString("game.player.tool") == "revolver" then local ct = GetCameraTransform() if GetBool("game.player.canusetool") and InputDown("usetool") and GetInt("game.tool.revolver.ammo") > 0 then if coolDown < 0 then firePos = Vec() if isLeftShot then firePos = TransformToParentPoint(ct, Vec(-0.45, -0.22, -1.2)) else firePos =TransformToParentPoint(ct, Vec(0.45, -0.22, -1.2)) end local forwardPos = TransformToParentPoint(ct, Vec(0, 0, -1000)) local direction = VecSub(forwardPos, ct.pos) local distance = VecLength(direction) direction = VecNormalize(direction) for _, head in pairs(horseHeads) do QueryRejectBody(head) end local hit, hitDistance = QueryRaycast(ct.pos, direction, distance) if hit then forwardPos = TransformToParentPoint(ct, Vec(0, 0, -hitDistance)) distance = hitDistance end local aimDir = VecNormalize(VecSub(forwardPos, firePos)) Shoot(firePos, aimDir, "gun", 1) --Light, particles and sound local lp = TransformToParentPoint(ct, Vec(0.25, -0.25, -2.0)) PointLight(lp, 1, 0.7, 0.5, 3) PlaySound(shootSnd[math.random(0,#shootSnd)]) if not HapticIsPlaying(shootHaptic) then PlayHaptic(shootHaptic, 1) end smoke = math.min(1.0, smoke + 0.2) coolDown = 0.2 particleTimer = -0.5 SetInt("game.tool.revolver.ammo", GetInt("game.tool.revolver.ammo")-1) isLeftShot = not isLeftShot end end local p = firePos--TransformToParentPoint(ct, Vec(0.25, -0.4, -1.6)) --if not InputDown("lmb") then if smoke > 0 then if particleTimer < 0.0 then particleTimer = dt + (1.0-smoke)*0.05 local vel = VecScale(VecSub(p, oldPipePos), 0.5/ dt) vel = VecAdd(vel, Vec(0, rnd(0, 2), 0)) ParticleType("smoke") ParticleRadius(0.08, 0.15) ParticleAlpha(smoke*0.9, 0) ParticleDrag(1.0) SpawnParticle(p, VecAdd(vel, rndVec(0.1)), particleTimer*50) end end --end particleTimer = particleTimer - dt*550 oldPipePos = p coolDown = coolDown - dt angle = angle + angVel*dt --Move tool a bit to the right and recoil local t = Transform() local recoil = math.max(0, coolDown) t.pos = Vec(0.7, -0.7, -1) SetToolTransform(t) local b = GetToolBody() local voxSize = 0.05 local attach = Transform(Vec(0.5*voxSize, -8.5*voxSize, 0), Quat()) if body ~= b then body = b -- Barrel is the second shape in vox file. Remember original position in attachment frame local shapes = GetBodyShapes(b) leftgun = shapes[1] rightgun = shapes[2] leftgunTransform = TransformToLocalTransform(attach, GetShapeLocalTransform(leftgun)) rightgunTransform = TransformToLocalTransform(attach, GetShapeLocalTransform(rightgun)) end attach.pos[3] = recoil attach.rot = QuatEuler(200*recoil, 0, 0) if isLeftShot then t2 = TransformToParentTransform(attach, leftgunTransform) -- t2.rot = QuatEuler(0,1,0) SetShapeLocalTransform(leftgun, t2) else t = TransformToParentTransform(attach, rightgunTransform) -- t.rot = QuatEuler(0,1,0) SetShapeLocalTransform(rightgun, t) end end smoke = math.max(0.0, smoke - dt) end function draw() if GetString("game.player.tool") == "revolver" then cross = UiPixelToWorld(UiCenter(), UiMiddle()) end end