-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
292 lines (247 loc) · 10.4 KB
/
client.lua
File metadata and controls
292 lines (247 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
local activeShells = {} -- Table of persistent shell entities spawned locally
local shellDataList = {} -- Metadata for all registered shells
-- Sanity check for Config load
if not Config then
print("^1[Shell Management] ERROR: Config.lua failed to load! Check fxmanifest.lua.^7")
return
end
-- Function to clean up all locally spawned shells
local function ClearLocalShells()
for id, entity in pairs(activeShells) do
if DoesEntityExist(entity) then
DeleteObject(entity)
end
end
activeShells = {}
end
-- Function to spawn a persistent shell
local function SpawnPersistentShell(shell)
local hash = GetHashKey(shell.model)
RequestModel(hash)
local timeout = 0
while not HasModelLoaded(hash) and timeout < 100 do -- 1s timeout
Citizen.Wait(10)
timeout = timeout + 1
end
if HasModelLoaded(hash) then
local obj = CreateObject(hash, shell.pos.x, shell.pos.y, shell.pos.z, false, false, false)
SetEntityRotation(obj, shell.rot.x, shell.rot.y, shell.rot.z, 2, true)
FreezeEntityPosition(obj, true)
SetEntityAsMissionEntity(obj, true, true)
activeShells[shell.id] = obj
end
end
-- Sync event from server
RegisterNetEvent("shellspawner:syncShells", function(shells)
ClearLocalShells()
shellDataList = shells
for _, shell in ipairs(shells) do
SpawnPersistentShell(shell)
end
end)
-- Initialize shell sync on join
Citizen.CreateThread(function()
Citizen.Wait(1000)
TriggerServerEvent("shellspawner:requestShells")
end)
-- Command to open Management Dashboard
RegisterCommand(Config.CommandName, function()
TriggerServerEvent("shellspawner:checkPermission")
end, false)
-- Event to actually open the menu after permission check
RegisterNetEvent("shellspawner:openMenu", function()
SetNuiFocus(true, true)
SendNUIMessage({
action = "openManager",
shells = shellDataList
})
end)
-- NUI Callbacks
RegisterNUICallback("close", function(_, cb)
SetNuiFocus(false, false)
cb("ok")
end)
RegisterNUICallback("teleport", function(data, cb)
local shellId = data.id
local shell = nil
for _, s in ipairs(shellDataList) do
if s.id == shellId then
shell = s
break
end
end
if shell then
SetEntityCoords(PlayerPedId(), shell.pos.x, shell.pos.y, shell.pos.z)
end
cb("ok")
end)
RegisterNUICallback("delete", function(data, cb)
TriggerServerEvent("shellspawner:deleteShell", data.id)
cb("ok")
end)
RegisterNUICallback("updateShell", function(data, cb)
TriggerServerEvent("shellspawner:updateShell", data)
cb("ok")
end)
-- Cleanup on resource stop
AddEventHandler("onResourceStop", function(resourceName)
if GetCurrentResourceName() == resourceName then
ClearLocalShells()
end
end)
-- Placement Mode
local inPlacementMode = false
local previewShell = nil
local function ShowHelpText(text)
SetTextComponentFormat("STRING")
AddTextComponentString(text)
DisplayHelpTextFromStringLabel(0, 0, 1, -1)
end
RegisterNUICallback("startPlacement", function(data, cb)
SetNuiFocus(false, false)
cb("ok")
local modelStr = data.model
local hash = GetHashKey(modelStr)
if not IsModelInCdimage(hash) then
print("^1[Shell Management] Invalid Model: " .. tostring(modelStr) .. "^7")
return
end
RequestModel(hash)
while not HasModelLoaded(hash) do
Citizen.Wait(10)
end
local playerPed = PlayerPedId()
local pCoords = GetEntityCoords(playerPed)
-- Creation of Preview
previewShell = CreateObject(hash, pCoords.x, pCoords.y, pCoords.z, false, false, false)
SetEntityCollision(previewShell, false, false)
SetEntityAlpha(previewShell, 180, false)
inPlacementMode = true
-- Camera Setup
local cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
local camCoords = pCoords + vector3(0.0, 0.0, 10.0)
local camRot = vector3(-20.0, 0.0, GetEntityHeading(playerPed))
SetCamCoord(cam, camCoords.x, camCoords.y, camCoords.z)
SetCamRot(cam, camRot.x, camRot.y, camRot.z, 2)
RenderScriptCams(true, true, 500, true, true)
local shellRot = 0.0
local zoomLevels = {10.0, 20.0, 40.0, 60.0} -- Distances above the shell
local currentZoomIdx = 1
local moveSpeed = 0.5
local snappedRotation = false
local shellCoords = pCoords -- The primary point we move
Citizen.CreateThread(function()
while inPlacementMode do
Citizen.Wait(0)
-- Instruction UI
local snapText = snappedRotation and "90° Snap" or "Free"
ShowHelpText("WASD: Move | Q/E: Rotate | SHIFT/CTRL: Height\nG: Snap ("..snapText..") | Scroll: Speed ("..string.format("%.1f", moveSpeed)..")\nV: Zoom | ~INPUT_FRONTEND_ACCEPT~: Place")
-- Disable Controls
DisableControlAction(0, 0, true) -- V (Next Camera)
DisableControlAction(0, 47, true) -- G (Weapon Tint/Map)
DisableControlAction(0, 14, true) -- Wheel Up
DisableControlAction(0, 15, true) -- Wheel Down
DisableControlAction(0, 30, true) -- Move LR
DisableControlAction(0, 31, true) -- Move UD
DisableControlAction(0, 1, true) -- Look LR
DisableControlAction(0, 2, true) -- Look UD
DisableControlAction(0, 24, true) -- Attack
DisableControlAction(0, 25, true) -- Aim
-- Camera Rotation (Mouse)
local mouseX = GetDisabledControlNormal(0, 1) * -10.0
local mouseY = GetDisabledControlNormal(0, 2) * -10.0
camRot = camRot + vector3(mouseY, 0.0, mouseX)
if camRot.x > 80.0 then camRot = vector3(80.0, camRot.y, camRot.z) end
if camRot.x < -80.0 then camRot = vector3(-80.0, camRot.y, camRot.z) end
SetCamRot(cam, camRot.x, camRot.y, camRot.z, 2)
-- Camera Movement (WASD - Locked to XY Plane)
local forward, right, up, p2 = GetCamMatrix(cam)
local moveForward = vector3(forward.x, forward.y, 0.0)
local moveRight = vector3(right.x, right.y, 0.0)
-- Normalize so movement speed is consistent regardless of pitch
moveForward = #moveForward > 0 and (moveForward / #moveForward) or vector3(0,0,0)
moveRight = #moveRight > 0 and (moveRight / #moveRight) or vector3(0,0,0)
if IsDisabledControlPressed(0, 32) then -- W
shellCoords = shellCoords + (moveForward * moveSpeed)
end
if IsDisabledControlPressed(0, 33) then -- S
shellCoords = shellCoords - (moveForward * moveSpeed)
end
if IsDisabledControlPressed(0, 34) then -- A
shellCoords = shellCoords - (moveRight * moveSpeed)
end
if IsDisabledControlPressed(0, 35) then -- D
shellCoords = shellCoords + (moveRight * moveSpeed)
end
-- Height Adjust (Shift / Ctrl - Moves the SHELL)
if IsControlPressed(0, 21) then -- Shift
shellCoords = shellCoords + vector3(0.0, 0.0, moveSpeed)
end
if IsControlPressed(0, 36) then -- Ctrl
shellCoords = shellCoords - vector3(0.0, 0.0, moveSpeed)
end
-- Update Camera to stay above the shell
local camCoords = shellCoords + vector3(0.0, 0.0, zoomLevels[currentZoomIdx])
SetCamCoord(cam, camCoords.x, camCoords.y, camCoords.z)
-- Rotation Logic
if snappedRotation then
if IsControlJustPressed(0, 44) then -- Q (Snap Left)
shellRot = (shellRot + 90.0) % 360.0
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", false)
end
if IsControlJustPressed(0, 38) then -- E (Snap Right)
shellRot = (shellRot - 90.0) % 360.0
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", false)
end
else
if IsControlPressed(0, 44) then -- Q (Free Rotate Left)
shellRot = (shellRot + 1.5) % 360.0
end
if IsControlPressed(0, 38) then -- E (Free Rotate Right)
shellRot = (shellRot - 1.5) % 360.0
end
end
-- Toggle Snap (G)
if IsDisabledControlJustPressed(0, 47) then
snappedRotation = not snappedRotation
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", false)
end
-- Speed Control (Scroll Wheel)
if IsDisabledControlJustPressed(0, 14) then -- Scroll Up
moveSpeed = math.min(moveSpeed + 0.1, 5.0)
end
if IsDisabledControlJustPressed(0, 15) then -- Scroll Down
moveSpeed = math.max(moveSpeed - 0.1, 0.1)
end
-- Zoom Toggle (V)
if IsDisabledControlJustPressed(0, 0) then
currentZoomIdx = currentZoomIdx + 1
if currentZoomIdx > #zoomLevels then currentZoomIdx = 1 end
PlaySoundFrontend(-1, "SELECT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false)
end
-- Position the shell at its tracked coordinates
SetEntityCoordsNoOffset(previewShell, shellCoords.x, shellCoords.y, shellCoords.z, false, false, false)
SetEntityRotation(previewShell, 0.0, 0.0, shellRot, 2, true)
-- confirmation
if IsControlJustPressed(0, 201) then -- ENTER
local finalPos = GetEntityCoords(previewShell)
TriggerServerEvent("shellspawner:registerShell", {
model = modelStr,
pos = {x = finalPos.x, y = finalPos.y, z = finalPos.z},
rot = {x = 0.0, y = 0.0, z = shellRot}
})
inPlacementMode = false
elseif IsControlJustPressed(0, 202) then -- BACKSPACE
inPlacementMode = false
end
end
-- Cleanup
RenderScriptCams(false, true, 500, true, true)
DestroyCam(cam, false)
if previewShell then
DeleteObject(previewShell)
previewShell = nil
end
SetModelAsNoLongerNeeded(hash)
end)
end)