Skip to content

Commit

Permalink
UI: Grenade Throw Arc (#1319)
Browse files Browse the repository at this point in the history
  • Loading branch information
EntranceJew authored Feb 4, 2024
1 parent 662b93a commit 694d307
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Added `SWEP:ClearHUDHelp()` to allow blanking the help text, for dynamically updating help text on equipment (by @EntranceJew)
- Throwables (grenades) now have a `:GetPullTime()` accessor
- Throwables (grenades) show UI for the amount of time remaining before detonation (fuse time) (by @EntranceJew)
- UI for grenade throw arcs from [colemclaren's TTT fork](https://github.com/colemclaren/ttt/blob/master/addons/moat_addons/lua/weapons/weapon_tttbasegrenade.lua#L293-L353) (integrated by @EntranceJew)

### Changed

Expand Down
161 changes: 143 additions & 18 deletions gamemodes/terrortown/entities/weapons/weapon_tttbasegrenade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ SWEP.IsGrenade = true
SWEP.was_thrown = false
SWEP.detonate_timer = 5
SWEP.DeploySpeed = 1.5
SWEP.throwForce = 1

---
-- @accessor number
Expand Down Expand Up @@ -88,6 +89,121 @@ function SWEP:SecondaryAttack()

end

---
-- @param Vector currentIterationPosition The initial position for this iteration cycle.
-- @param number launchVelocity The launch velocity for this iteration cycle.
-- @param number stepDistance The step size for the current iteration cycle.
-- @return Vector The next location to iterate from.
local function PositionFromPhysicsParams(currentIterationPosition, launchVelocity, stepDistance)
local activeGravity = physenv.GetGravity()

return currentIterationPosition + (launchVelocity * stepDistance + 0.5 * activeGravity * stepDistance ^ 2)
end

if CLIENT then
---
-- @realm client
local cvEnableTrajectoryUI = CreateConVar("ttt2_grenade_trajectory_ui", 0, FCVAR_ARCHIVE)

local function AlphaLerp(from, frac, max)
local fr = frac ^ 0.5
return ColorAlpha(from, Lerp(fr, 0, math.min(max, 255)))
end

---
-- @param Player ply
-- @realm client
function SWEP:DrawDefaultThrowPath(ply)
local stepSize = 0.005

local owner = self:GetOwner()
local currentIterationPosition, _ = self:GetViewModelPosition(owner:EyePos(), owner:EyeAngles())
local launchPosition, launchVelocity = self:GetThrowVelocity()
currentIterationPosition = currentIterationPosition - ply:EyePos() + launchPosition
local previousIterationPosition = PositionFromPhysicsParams(currentIterationPosition, launchVelocity, stepSize)

local fractionalFrameTime = (SysTime() % 1) * 2
local i = fractionalFrameTime > 1 and 1 or 0
fractionalFrameTime = fractionalFrameTime - math.floor(fractionalFrameTime)

local arcColor = appearance.ShouldUseGlobalFocusColor() and appearance.GetFocusColor() or self:GetOwner():GetRoleColor()
local arcAlpha = math.Round(GetConVar("ttt_crosshair_opacity"):GetFloat() * 255)
local arcWidth = 0.6
local arcWidthDiminishOverDistanceFactor = 0.25
local arcSegmentLengthFactor = 0.5
local arcImpactCrossLength = 1

render.SetColorMaterial()
cam.Start3D(EyePos(), EyeAngles())

for stepDistance = stepSize * 2, 1, stepSize do
local drawColor = AlphaLerp(arcColor, stepDistance, arcAlpha)

local pos = PositionFromPhysicsParams(currentIterationPosition, launchVelocity, stepDistance)
local t = util.TraceLine({
start = previousIterationPosition,
endpos = pos,
filter = {ply, self}
})

local from = previousIterationPosition
local to = t.Hit and t.HitPos or pos
local norm = to - from
norm:Normalize()

local denom = (stepDistance / stepSize) ^ arcWidthDiminishOverDistanceFactor
local arcSegmentLength = from:DistToSqr(to) ^ arcSegmentLengthFactor
i = (i + 1) % 2
if i == 0 then
render.DrawBeam(from, from + norm * (fractionalFrameTime * arcSegmentLength), arcWidth * denom, 0, 1, drawColor)
else
render.DrawBeam(to - norm * ((1 - fractionalFrameTime) * arcSegmentLength), to, arcWidth * denom, 0, 1, drawColor)
end

if t.Hit then
local hitPosition = t.HitPos + t.HitNormal * (t.FractionLeftSolid * denom)
local impactCrossSegmentLength = arcImpactCrossLength * denom
local impactCrossVector = Vector(0, 0, impactCrossSegmentLength)

local angleLeft = Angle(t.HitNormal:Angle())
angleLeft:RotateAroundAxis(t.HitNormal, -45)
local left = Vector(impactCrossVector)
left:Rotate(angleLeft)
render.DrawBeam(
hitPosition - (left * impactCrossSegmentLength),
hitPosition + left * impactCrossSegmentLength,
arcWidth * denom, 0, 1, drawColor
)

local angleUp = Angle(t.HitNormal:Angle())
angleUp:RotateAroundAxis(t.HitNormal, 45)
local up = Vector(impactCrossVector)
up:Rotate(angleUp)
render.DrawBeam(
hitPosition - (up * impactCrossSegmentLength),
hitPosition + up * impactCrossSegmentLength,
arcWidth * denom, 0, 1, drawColor
)
break
end
previousIterationPosition = pos
end

cam.End3D()
end

---
-- @param Entity vm
-- @param Weapon weapon
-- @param Player ply
-- @realm client
function SWEP:PostDrawViewModel(vm, weapon, ply)
if cvEnableTrajectoryUI:GetBool() then
self:DrawDefaultThrowPath(ply)
end
end
end

---
-- @ignore
function SWEP:PullPin()
Expand Down Expand Up @@ -168,6 +284,31 @@ function SWEP:StartThrow()
self:SetThrowTime(CurTime() + 0.1)
end

---
-- @return Vector, Vector The point of origin for the thrown projectile, and its force.
-- @realm shared
function SWEP:GetThrowVelocity()
local ply = self:GetOwner()
local ang = ply:EyeAngles()
local src = ply:GetPos() + (ply:Crouching() and ply:GetViewOffsetDucked() or ply:GetViewOffset()) + (ang:Forward() * 8) + (ang:Right() * 10)
local target = ply:GetEyeTraceNoCursor().HitPos
-- A target angle to actually throw the grenade to the crosshair instead of forwards
local tang = (target - src):Angle()
-- Makes the grenade go upwards
if tang.p < 90 then
tang.p = -10 + tang.p * ((90 + 10) / 90)
else
tang.p = 360 - tang.p
tang.p = -10 + tang.p * -((90 + 10) / 90)
end

-- Makes the grenade not go backwards :/
tang.p = math.Clamp(tang.p, -90, 90)
local vel = math.min(800, (90 - tang.p) * 6)
local force = tang:Forward() * vel * self.throwForce + ply:GetVelocity()
return src, force
end

---
-- @ignore
function SWEP:Throw()
Expand All @@ -181,24 +322,8 @@ function SWEP:Throw()

self.was_thrown = true

local ang = ply:EyeAngles()
local src = ply:GetPos() + (ply:Crouching() and ply:GetViewOffsetDucked() or ply:GetViewOffset()) + ang:Forward() * 8 + ang:Right() * 10
local target = ply:GetEyeTraceNoCursor().HitPos
local tang = (target-src):Angle() -- A target angle to actually throw the grenade to the crosshair instead of fowards

-- Makes the grenade go upgwards
if tang.p < 90 then
tang.p = -10 + tang.p * ((90 + 10) / 90)
else
tang.p = 360 - tang.p
tang.p = -10 + tang.p * -((90 + 10) / 90)
end

tang.p = math.Clamp(tang.p,-90,90) -- Makes the grenade not go backwards :/

local vel = math.min(800, (90 - tang.p) * 6)
local thr = tang:Forward() * vel + ply:GetVelocity()
self:CreateGrenade(src, Angle(0,0,0), thr, Vector(600, math.random(-1200, 1200), 0), ply)
local src, force = self:GetThrowVelocity()
self:CreateGrenade(src, Angle(0,0,0), force, Vector(600, math.random(-1200, 1200), 0), ply)

self:SetThrowTime(0)
self:Remove()
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/de.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.target_credits_on_confirm = "Bestätige Toten, um ungenutzte Credits zu erhalt
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
L.header_miscellaneous_settings = "Verschiedene Einstellungen"
L.label_hud_pulsate_health_enable = "Pulsieren der Lebensleiste bei weniger als 25% Gesundheit"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,9 @@ mode 1: This mode increases the importance of public policing roles by limiting
mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
L.header_miscellaneous_settings = "Miscellaneous Settings"
L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/es.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Gracias a tus habilidades de detective, has identificado que la
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/fr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "En utilisant vos compétences de détective, vous avez identifi
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/it.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Usando le tue abilità da detective, hai identificato che l'ult
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/ja.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "こいつが最後の人物は、{player}。こいつは敵か
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/pl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Używając umiejętności detektywa, zidentyfikowałeś ostatni
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/pt_br.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Usando suas técnicas de detetive, você identificou a última
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/ru.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,9 @@ L.search_eyes = "Используя свои детективные навыки
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Genom att använda dina detektivfärdigheter kan du identifiera
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/tr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ mode 1: Bu mod, onay seçeneğini bunlarla sınırlandırarak kamu polisliği ro
mode 2: Bu mod, mod 1'den biraz daha katıdır. Bu modda arama yeteneği normal oyunculardan da kaldırılır. Bu, bir cesedi bir kamu polis oyuncusuna bildirmenin artık cesetlerden herhangi bir bilgi almanın tek yolu olduğu anlamına gelir.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
L.header_miscellaneous_settings = "Çeşitli Ayarlar"
L.label_hud_pulsate_health_enable = "Sağlık %25'in altındayken sağlık göstergesini titret"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/uk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "Використовуючи свої навички детек
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/zh_hans.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.help_inspect_confirm_mode = [[

模式 2: 这种模式比模式 1 更严格一些。在这种模式中,普通玩家的搜索能力也被移除了。这意味着向公共警察玩家报告死尸现在是获取任何尸体信息的唯一方式。]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
L.header_miscellaneous_settings = "其他设置"
L.label_hud_pulsate_health_enable = "当生命值低于 25% 时,生命条会出现脉动"
Expand Down
3 changes: 3 additions & 0 deletions lua/terrortown/lang/zh_tw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ L.search_eyes = "透過你的探查技能,你確信他臨死前見到的最後
--
--mode 2: This mode is yet a bit more strict than mode 1. In this mode the search ability is removed as well from normal players. This means that reporting a dead body to a public policing player is now the only way to get any information from dead bodies.]]

-- 2023-10-19
--L.label_grenade_trajectory_ui = "Grenade trajectory indicator"

-- 2023-10-23
--L.header_miscellaneous_settings = "Miscellaneous Settings"
--L.label_hud_pulsate_health_enable = "Pulsate healthbar when below 25% health"
Expand Down
6 changes: 6 additions & 0 deletions lua/terrortown/menus/gamemode/appearance/crosshair.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ function CLGAMEMODESUBMENU:Populate(parent)
decimal = 1,
master = crossEnb
})

form:MakeCheckBox({
label = "label_grenade_trajectory_ui",
convar = "ttt2_grenade_trajectory_ui",
master = crossEnb
})
end

0 comments on commit 694d307

Please sign in to comment.