TTT-2 / TTT2

Trouble in Terrorist Town 2 for Garry's Mod (gmod)
https://steamcommunity.com/sharedfiles/filedetails/?id=1357204556
177 stars 72 forks source link

Better Flashlight / Third Person Flashlight #1486

Open sirzento opened 6 months ago

sirzento commented 6 months ago

Is your feature request related to a problem? Please describe.

Adding a third person flashlight would be greate because I have seen it so many times that someone tryes to hide in the dark and thinks that they are pretty hidden but for another person who has the flashlight on the whole room might be lit up but the person hiding doesn't see this at all. If the flashlight would also light up the room for everyone like the person with the flashlight sees it, the person hiding will see that he isn't in the dark anymore. (And shadows casted from a flashlight do also look pretty cool)

Describe the solution you'd like

A flashlight that lights up the world not only client side

Describe alternatives you've considered

Looking at videos and the workshop I found the Dynamic Flashlight mod that pretty much does all that. The problem is that this mod had a bug with turning off the flashlight but a user in the comments did fix this but there where still other bugs around like:

Additional context

I tryed to fix most of the bugs this mod has.

So overall, the flashlight works flawless in first person but the third person view still has some problems

Open problems

Other possible upgrades

Code

It's very much the same original code, just with my fixes.

if CLIENT then
    local cache = {}
    local function UpdateCache(entity, state)
        if not entity:IsPlayer() then return end

        if state then
            table.insert(cache, entity)
        else
            for i = 1, #cache do
                if cache[i] == entity then
                    table.remove(cache, i)
                end
            end
        end
    end

    hook.Add("NotifyShouldTransmit", "DynamicFlashlight.PVS_Cache", function(entity, state)
        UpdateCache(entity, state)
    end)

    hook.Add("EntityRemoved", "DynamicFlashlight.PVS_Cache", function(entity)
        UpdateCache(entity, false)
    end)

    hook.Add("Think", "DynamicFlashlight.Rendering", function()
        for i = 1, #cache do
            local target = cache[i]

            if target:GetNWBool("DynamicFlashlight") then
                if target.DynamicFlashlight then
                    local position = target:EyePos()
                    local viewPos = target:GetHeightVector().z

                    target.DynamicFlashlight:SetPos(Vector(position[1], position[2], position[3]) + target:GetForward())
                    target.DynamicFlashlight:SetAngles(target:EyeAngles())
                    target.DynamicFlashlight:Update()
                else
                    target.DynamicFlashlight = ProjectedTexture()
                    target.DynamicFlashlight:SetTexture("effects/flashlight001")
                    target.DynamicFlashlight:SetFarZ(900)
                    target.DynamicFlashlight:SetNearZ(20)
                    target.DynamicFlashlight:SetFOV(70)
                end
            else
                if target.DynamicFlashlight then
                    target.DynamicFlashlight:Remove()
                    target.DynamicFlashlight = nil
                end
            end
        end
    end)
else
    hook.Add("PlayerSwitchFlashlight", "DynamicFlashlight.Switch", function(ply, state)
        local isAlive = ply:Alive() and not ply:IsSpec()

        if isAlive then
            ply:EmitSound("HL2Player.FlashLightOn")
        end

        ply:SetNWBool("DynamicFlashlight", isAlive and (not ply:GetNWBool("DynamicFlashlight")) or false)

        return false
    end)
end
TimGoll commented 6 months ago

You still can't see your own shadow if another player shines a light at you. This is because you own player model doesn't get rendered for your client and with no model there is no shadow. You can still the the shadow from other people since you also can see the model of them. This problem isn't that bad and can be ignored I guess.

I wonder how this interacts with GMod Legs

TimGoll commented 6 months ago

Maybe it is also possible to add a player model for the local player, that only interacts with the light, but is not rendered

TimGoll commented 6 months ago

Also I'm wondering if we could create a basic custom animation library that is integrated into TTT2. Similar how we did with it with the view and world models. Adding more animations and sounds is something I really want to focus on to give the game a more polished feel.

Our version of that library should support world model animations though, as it would be nice to see these animations on any player.

We'd probably also need a flashlight model that is attached to the shoulder or so

sirzento commented 6 months ago

I wonder how this interacts with GMod Legs

I have the feeling that you will just have a shadow from your lower body

Also I'm wondering if we could create a basic custom animation library that is integrated into TTT2. Similar how we did with it with the view and world models. Adding more animations and sounds is something I really want to focus on to give the game a more polished feel.

Our version of that library should support world model animations though, as it would be nice to see these animations on any player.

Would be nice. VManip already shows us what animation you guys could add. I like that shoulder tap to turn on the flashlight, the "grab to pick up" and "stretch hand out to interact" is also very nice and immersive. That one addon also adds things like "putting hand infront of the face" when the player did get explosion damage.

We'd probably also need a flashlight model that is attached to the shoulder or so

Isn't that pretty hard since there are any servers that are using custom models? I don't mind seeing no flashlight on the model and if it's turned on you can't see it anyways because of the light.

TimGoll commented 6 months ago

Isn't that pretty hard since there are any servers that are using custom models? I don't mind seeing no flashlight on the model and if it's turned on you can't see it anyways because of the light.

There are attachment points on models, like the hands where the weapons are rendered. It is basically an extension of the custom world model stuff I already did