Skip to content

Commit

Permalink
Shell manager (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImXirvin authored Jul 31, 2023
1 parent a5547f1 commit 9e87da0
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 13 deletions.
18 changes: 6 additions & 12 deletions client/cl_property.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Property = {
property_id = nil,
propertyData = nil,

shell = nil,
shellData = nil,
inProperty = false,
shellObj = nil,
Expand Down Expand Up @@ -75,13 +76,10 @@ end
function Property:CreateShell()
local coords = self:GetDoorCoords()

local shellHash = self.shellData.hash
lib.requestModel(shellHash)
coords = vec3(coords.x, coords.y, coords.z - 25.0)
self.shell = Shell:CreatePropertyShell(self.propertyData.shell, coords, self.shellData.doorOffset.h)

self.shellObj = CreateObjectNoOffset(shellHash, coords.x, coords.y, coords.z - 25.0, false, false, false)

SetModelAsNoLongerNeeded(shellHash)
FreezeEntityPosition(self.shellObj, true)
self.shellObj = self.shell.entity

local doorOffset = self.shellData.doorOffset
local offset = GetOffsetFromEntityInWorldCoords(self.shellObj, doorOffset.x, doorOffset.y, doorOffset.z)
Expand Down Expand Up @@ -231,12 +229,8 @@ function Property:LeaveShell()
self:UnloadFurnitures()
self.propertyData.furnitures = {}


if self.shellObj then
DeleteEntity(self.shellObj)
self.shellObj = nil
end

self.shell:DespawnShell()
self.shell = nil
if self.exitTarget then
Framework[Config.Target].RemoveTargetZone(self.exitTarget)
self.exitTarget = nil
Expand Down
115 changes: 115 additions & 0 deletions client/shell.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
Shells = {}


Shell = {
entity = nil,
hash = nil,
position = nil,
rotation = nil,
shellData = nil,
oldCoord = nil,

exitTarget = nil,
}
Shell.__index = Shell


function Shell:SpawnShell(shellHash, position, rotation)
lib.requestModel(shellHash)

local entity = CreateObjectNoOffset(shellHash, position.x, position.y, position.z, false, false, false)
FreezeEntityPosition(entity, true)

SetEntityRotation(entity, rotation, 2, true)

SetModelAsNoLongerNeeded(shellHash)

return entity
end

function Shell:DespawnShell()
if DoesEntityExist(self.entity) then
DeleteEntity(self.entity)
end

if self.exitTarget then
Framework[Config.Target].RemoveTargetZone(self.exitTarget)
end
self = nil
end


function Shell:CreatePropertyShell(shellName, position, rotation)
local self = setmetatable({}, Shell)

self.shellData = Config.Shells[shellName]
self.hash = self.shellData.hash
self.position = position
self.rotation = rotation or vector3(0.0, 0.0, 0.0)

self.entity = self:SpawnShell(self.hash, self.position, self.rotation)


return self
end


-- example of how to use
-- exports["ps-housing"]:CreateTempShell("Modern Hotel", GetEntityCoords(PlayerPedId()), GetEntityRotation(PlayerPedId()), function()
-- Framework[Config.Notify].Notify("You left the shell", "error")
-- local coords = GetEntityCoords(PlayerPedId())
-- SetEntityCoordsNoOffset(PlayerPedId(), coords.x, coords.y, coords.z + 50.0, false, false, true)
-- end)
-- this is used as a constructor for third party scripts
function Shell:CreateTempShell(shellName, position, rotation, leaveCb)
local self = setmetatable({}, Shell)
self.shellData = Config.Shells[shellName]
self.hash = self.shellData.hash
self.position = position
self.rotation = rotation

self.oldCoord = GetEntityCoords(PlayerPedId())

self.entity = self:SpawnShell(self.hash, self.position, self.rotation)


local doorOffset = self.shellData.doorOffset
local offset = GetOffsetFromEntityInWorldCoords(self.entity, doorOffset.x, doorOffset.y, doorOffset.z)

SetEntityCoordsNoOffset(cache.ped, offset.x, offset.y, offset.z, false, false, true)
SetEntityHeading(cache.ped, self.shellData.doorOffset.h)

local coords = offset
local size = vector3(1.0, self.shellData.doorOffset.width, 3.0)
local heading = self.shellData.doorOffset.h

local function leave()
if leaveCb then
leaveCb()
else
SetEntityCoordsNoOffset(PlayerPedId(), self.oldCoord.x, self.oldCoord.y, self.oldCoord.z, false, false, true)
end

self:DespawnShell()
end

self.exitTarget = Framework[Config.Target].AddDoorZoneInsideTempShell(coords, size, heading, leave)

Shells[self.entity] = self

return self.entity
end

exports('CreateTempShell', function(shellName, position, rotation, leaveCb)
return Shell:CreateTempShell(shellName, position, rotation, leaveCb)
end)

exports("GetShellData", function (shellName)
return Config.Shells[shellName]
end)

exports("DespawnTempShell", function (shellEntity)
if Shells[shellEntity] then
Shells[shellEntity]:DespawnShell()
end
end)
1 change: 1 addition & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ shared_script {
}

client_script {
'client/shell.lua',
'client/apartment.lua',
'client/cl_property.lua',
'client/client.lua',
Expand Down
2 changes: 1 addition & 1 deletion shared/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config = {}
Config.Target = "ox" -- "ox" or "qb"
Config.Notify = "ox" -- "ox" or "qb"
Config.Radial = "ox" -- "ox" or "qb"
Config.Inventory = "ox" -- "ox" or "qb"
Config.Inventory = "qb" -- "ox" or "qb"

-- Anyone provided with keys to a property has the ability to modify its furnishings.
Config.AccessCanEditFurniture = true
Expand Down
46 changes: 46 additions & 0 deletions shared/framework.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ Framework.qb = {
return "shellExit"
end,

AddDoorZoneInsideTempShell = function(coords, size, heading, leave)
exports["qb-target"]:AddBoxZone(
"shellExit",
vector3(coords.x, coords.y, coords.z),
size.x,
size.y,
{
name = "shellExit",
heading = heading,
debugPoly = Config.DebugMode,
minZ = coords.z - 2.0,
maxZ = coords.z + 1.0,
},
{
options = {
{
label = "Leave",
action = leave,
icon = "fas fa-right-from-bracket",
},
},
}
)

return "shellExit"
end,

RemoveTargetZone = function(targetName)
exports["qb-target"]:RemoveZone(targetName)
end,
Expand Down Expand Up @@ -395,6 +422,25 @@ Framework.ox = {
return handler
end,

AddDoorZoneInsideTempShell = function (coords, size, heading, leave)
local handler = exports.ox_target:addBoxZone({
coords = vector3(coords.x, coords.y, coords.z), --z = 3.0
size = vector3(size.y, size.x, size.z),
rotation = heading,
debug = Config.DebugMode,
options = {
{
name = "leave",
label = "Leave",
onSelect = leave,
icon = "fas fa-right-from-bracket",
},
},
})
print("made")
return handler
end,

RemoveTargetZone = function (handler)
exports.ox_target:removeZone(handler)
end,
Expand Down

0 comments on commit 9e87da0

Please sign in to comment.