#include "script/common.lua" function init() --Register tool and enable it RegisterTool("camera", "loc@TOOL_CAMERA", "") SetBool("game.tool.camera.enabled", true) inShot = false focusTimer = 0 focusSound = LoadLoop("camera-focus.ogg") shotSound = LoadSound("camera-shot.ogg") errorSound = LoadSound("error.ogg") currentDist = 100 targetInFocus = {} targets = FindBodies("target", true) focusDist = 0 end function tick(dt) SetBool("level.cameraclick", false) if GetTime() < 0.5 then --Make sure camera is selected when entering level SetString("game.player.tool", "camera") end if GetString("game.player.tool") == "camera" then SetString("game.tool.camera.ammo.display", "") local ct = GetPlayerCameraTransform() local fwd = TransformToParentVec(ct, Vec(0, 0, -1)) local hit, dist = QueryRaycast(ct.pos, fwd, 100) if hit then currentDist = dist else currentDist = 100 end if GetBool("game.player.canusetool") then if InputPressed("usetool") then inShot = true focusTimer = 0 focusDist = currentDist targetInFocus = {} end if inShot then if InputDown("usetool") then focusTimer = focusTimer + dt SetCameraDof(clamp(focusTimer, 0.0, 1.0) * focusDist) if focusTimer < 1.0 then PlayLoop(focusSound) else PlaySound(shotSound) PointLight(TransformToParentPoint(GetPlayerCameraTransform(), Vec(0, 0, -1)), 1, 1, 1, 30) SetBool("level.cameraclick", true) for i=1, #targets do local target = targets[i] if targetInFocus[target] then SetTag(target, "target", "cleared") end end targetInFocus = {} inShot = false focusTimer = 0 end else focusTimer = 0 inShot = false end end else inShot = false end end end function draw() if GetString("level.state")=="" and GetString("game.player.tool") == "camera" and GetFloat("game.map.fade") < 0.1 then UiPush() UiTranslate(100, 100) UiAlign("top left") UiImage("camera-ul.png") UiPop() UiPush() UiTranslate(UiWidth()-100, 100) UiAlign("top right") UiImage("camera-ur.png") UiPop() UiPush() UiTranslate(100, UiHeight()-100) UiAlign("bottom left") UiImage("camera-ll.png") UiPop() UiPush() UiTranslate(UiWidth()-100, UiHeight()-100) UiAlign("bottom right") UiImage("camera-lr.png") UiPop() UiPush() UiTranslate(UiCenter(), UiHeight()-65) UiAlign("center middle") UiImage("camera-lc.png") UiPop() local hasTarget = false local focusSize = 106 - 53*focusTimer for i=1, #targets do local target = targets[i] local inFocus = false if HasTag(target, "target") then local t = GetBodyTransform(target) local pos = TransformToParentPoint(t, Vec(0, 0.5, 0)) local player = GetPlayerCameraTransform().pos local toPos = VecSub(pos, player) local dist = VecLength(toPos) toPos = VecScale(toPos, 1.0/dist) local bodies = GetJointedBodies(target) for j=1,#bodies do QueryRejectBody(bodies[j]) end local hit = QueryRaycast(player, toPos, dist) if not hit then local x, y, distPx = UiWorldToPixel(pos) if distPx > 0 and distPx < 8 then UiPush() local alpha = 1.0 alpha = clamp((distPx - 1.0), 0.0, alpha) alpha = clamp((8.0 - distPx), 0.0, alpha) UiTranslate(x, y) UiAlign("center middle") local inside = true if math.abs(x-UiCenter()) > focusSize then inside = false end if math.abs(y-UiMiddle()) > focusSize then inside = false end if inShot and targetInFocus[target] == false then inside = false end if alpha > 0 then if inside then UiColor(0.7,1,0.7,alpha) UiScale(1.1) UiImage("camera-icon.png") inFocus = true else UiColor(1,1,1,alpha) UiScale(0.9) UiImage("camera-icon-false.png") end end UiPop() end end end if inFocus then hasTarget = true if targetInFocus[target] == nil then targetInFocus[target] = true end else if targetInFocus[target] == true then inShot = false PlaySound(errorSound) end targetInFocus[target] = false end end if inShot or not InputDown("usetool") then UiPush() UiTranslate(UiCenter(), UiMiddle()) UiAlign("center middle") if hasTarget then UiColor(0.7,1,0.7) end UiScale(1-focusTimer*0.5) UiImage("camera-mc.png") UiPop() end end end