Skip to content

Commit 20c8c45

Browse files
authored
Doors: Improved door destruction handling (#1836)
I've made small improvements to door destruction code: - Carry over door's bodygroups to the destruction prop - Carry over ents parented to the door to the destruction prop - Spawn the door's destruction prop as soon as the door is destroyed instead of waiting a frame (areaportal opening still works) - Reuse existing vector globals instead of creating new vectors
1 parent d92d83e commit 20c8c45

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
44

55
## Unreleased
66

7+
### Changed
8+
9+
- Made improvements to door destruction (by @TW1STaL1CKY)
10+
- Bodygroups from destroyed doors are now transferred to their physics prop
11+
- Entities parented to destroyed doors are now transferred to their physics prop
12+
- Door props are now spawned as soon as the door is destroyed (instead of a frame later)
13+
- Reuse existing vector globals instead of creating new vectors
14+
715
### Fixed
816

917
- Fixed markerVision null entity error (by @mexikoedi)

gamemodes/terrortown/gamemode/shared/sh_door.lua

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,34 @@ if SERVER then
362362
doorProp:SetCollisionGroup(COLLISION_GROUP_NONE)
363363
doorProp:SetMoveType(MOVETYPE_VPHYSICS)
364364
doorProp:SetSolid(SOLID_BBOX)
365-
doorProp:SetPos(self:GetPos() + Vector(0, 0, 2))
365+
doorProp:SetPos(self:GetPos())
366366
doorProp:SetAngles(self:GetAngles())
367367
doorProp:SetModel(self:GetModel())
368368
doorProp:SetSkin(self:GetSkin())
369369

370+
local bodygroups = self:GetBodyGroups()
371+
for i = 1, #bodygroups do
372+
local id = bodygroups[i].id
373+
doorProp:SetBodygroup(id, self:GetBodygroup(id))
374+
end
375+
376+
-- if there are entities parented to the door, transfer them over
377+
local children = self:GetChildren()
378+
for i = 1, #children do
379+
local child = children[i]
380+
local pos, ang = child:GetLocalPos(), child:GetLocalAngles()
381+
382+
child:SetParent(doorProp)
383+
child:SetLocalPos(pos)
384+
child:SetLocalAngles(ang)
385+
end
386+
370387
door.HandleDestruction(self)
371388

389+
-- hide away the real door while it is being opened and destroyed
390+
self:SetNoDraw(true)
391+
self:SetSolid(SOLID_NONE)
392+
372393
-- disable the door move sound for the destruction
373394
self:SetKeyValue("soundmoveoverride", "")
374395

@@ -379,6 +400,38 @@ if SERVER then
379400
-- this function is called multiple times for the same door in the same tick
380401
self.isDestroyed = true
381402

403+
if IsValid(ply) and ply:IsPlayer() then
404+
DamageLog(
405+
string.format(
406+
"TTT2Doors: The door with the index %s has been destroyed by %s.",
407+
self:EntIndex(),
408+
ply:Nick()
409+
)
410+
)
411+
else
412+
DamageLog(
413+
string.format(
414+
"TTT2Doors: The door with the index %s has been destroyed.",
415+
self:EntIndex()
416+
)
417+
)
418+
end
419+
420+
doorProp:Spawn()
421+
doorProp:SetHealth(cvDoorPropHealth:GetInt())
422+
423+
doorProp.isDoorProp = true
424+
425+
local physObj = doorProp:GetPhysicsObject()
426+
427+
if IsValid(physObj) then
428+
physObj:ApplyForceCenter(pushForce or vector_origin)
429+
end
430+
431+
---
432+
-- @realm server
433+
hook.Run("TTT2DoorDestroyed", doorProp, ply)
434+
382435
-- if the door is grouped as a pair, call the other one as well
383436
if not surpressPair and IsValid(self.otherPairDoor) then
384437
self.otherPairDoor:SafeDestroyDoor(ply, pushForce, true)
@@ -392,37 +445,6 @@ if SERVER then
392445
-- we have to kill the entity here instead of removing it because this way we
393446
-- have no problems with area portals (invisible rooms after door is destroyed)
394447
self:Fire("Kill", "", 0)
395-
396-
if IsValid(ply) and ply:IsPlayer() then
397-
DamageLog(
398-
"TTT2Doors: The door with the index "
399-
.. self:EntIndex()
400-
.. " has been destroyed by "
401-
.. ply:Nick()
402-
.. "."
403-
)
404-
else
405-
DamageLog(
406-
"TTT2Doors: The door with the index "
407-
.. self:EntIndex()
408-
.. " has been destroyed."
409-
)
410-
end
411-
412-
doorProp:Spawn()
413-
doorProp:SetHealth(cvDoorPropHealth:GetInt())
414-
415-
doorProp.isDoorProp = true
416-
417-
local physObj = doorProp:GetPhysicsObject()
418-
419-
if IsValid(physObj) then
420-
physObj:ApplyForceCenter(pushForce or Vector(0, 0, 0))
421-
end
422-
423-
---
424-
-- @realm server
425-
hook.Run("TTT2DoorDestroyed", doorProp, ply)
426448
end)
427449

428450
return doorProp

0 commit comments

Comments
 (0)