Skip to content

Commit 2a079e9

Browse files
authored
Merge pull request #249 from AllanOnAir/main
Change the decorate system.
2 parents effb02c + bead34a commit 2a079e9

File tree

2 files changed

+113
-105
lines changed

2 files changed

+113
-105
lines changed

client/decorate.lua

Lines changed: 110 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ ObjectList = {}
22
local DecoMode = false
33
local MainCamera = nil
44
local curPos
5-
local speeds = { 0.01, 0.05, 0.1, 0.2, 0.4, 0.5 }
6-
local curSpeed = 1
5+
local speeds = 0.01
6+
local rotationSpeed = 0.1
77
local cursorEnabled = false
88
local SelectedObj = nil
99
local SelObjHash = {}
@@ -15,7 +15,6 @@ local rotateActive = false
1515
local peanut = false
1616
local previewObj = nil
1717

18-
-- Functions
1918

2019
local function openDecorateUI()
2120
SetNuiFocus(true, true)
@@ -35,22 +34,12 @@ local function closeDecorateUI()
3534
})
3635
end
3736

38-
local function CreateEditCamera()
39-
local rot = GetEntityRotation(PlayerPedId())
40-
local pos = GetEntityCoords(PlayerPedId(), true)
41-
MainCamera = CreateCamWithParams('DEFAULT_SCRIPTED_CAMERA', pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, 60.00, false, 0)
42-
SetCamActive(MainCamera, true)
43-
RenderScriptCams(true, false, 1, true, true)
44-
end
45-
4637
local function EnableEditMode()
4738
local pos = GetEntityCoords(PlayerPedId(), true)
4839
curPos = { x = pos.x, y = pos.y, z = pos.z }
49-
SetEntityVisible(PlayerPedId(), false)
50-
FreezeEntityPosition(PlayerPedId(), true)
51-
SetEntityCollision(PlayerPedId(), false, false)
52-
CreateEditCamera()
5340
DecoMode = true
41+
exports['qb-target']:AllowTargeting(false)
42+
LocalPlayer.state:set('inv_busy', true, true)
5443
TriggerEvent('qb-anticheat:client:ToggleDecorate', true)
5544
end
5645

@@ -75,31 +64,22 @@ local function SaveDecorations()
7564
end
7665
end
7766

78-
local function SetDefaultCamera()
79-
RenderScriptCams(false, true, 500, true, true)
80-
SetCamActive(MainCamera, false)
81-
DestroyCam(MainCamera, true)
82-
DestroyAllCams(true)
83-
end
84-
8567
local function DisableEditMode()
8668
SaveDecorations()
87-
SetEntityVisible(PlayerPedId(), true)
88-
FreezeEntityPosition(PlayerPedId(), false)
89-
SetEntityCollision(PlayerPedId(), true, true)
90-
SetDefaultCamera()
9169
EnableAllControlActions(0)
9270
ObjectList = nil
9371
SelectedObj = nil
9472
peanut = false
9573
DecoMode = false
74+
exports['qb-target']:AllowTargeting(true)
75+
LocalPlayer.state:set('inv_busy', false, true)
9676
TriggerEvent('qb-anticheat:client:ToggleDecorate', false)
9777
end
9878

9979
local function CheckObjMovementInput()
100-
local xVect = speeds[curSpeed]
101-
local yVect = speeds[curSpeed]
102-
local zVect = speeds[curSpeed]
80+
local xVect = speeds
81+
local yVect = speeds
82+
local zVect = speeds
10383

10484
if IsControlPressed(1, 27) or IsDisabledControlPressed(1, 27) then -- Up Arrow
10585
SelObjPos = GetOffsetFromEntityInWorldCoords(SelectedObj, 0, -yVect, 0)
@@ -129,9 +109,9 @@ local function CheckObjMovementInput()
129109
end
130110

131111
local function CheckObjRotationInput()
132-
local xVect = speeds[curSpeed] * 5.5
133-
local yVect = speeds[curSpeed] * 5.5
134-
local zVect = speeds[curSpeed] * 5.5
112+
local xVect = rotationSpeed * 5.5
113+
local yVect = rotationSpeed * 5.5
114+
local zVect = rotationSpeed * 5.5
135115

136116
if IsControlPressed(1, 27) or IsDisabledControlPressed(1, 27) then -- Up Arrow
137117
SelObjRot.x = SelObjRot.x + xVect
@@ -160,16 +140,16 @@ local function CheckObjRotationInput()
160140
SetEntityRotation(SelectedObj, SelObjRot.x, SelObjRot.y, SelObjRot.z)
161141
end
162142

163-
local function CheckRotationInput()
164-
local rightAxisX = GetDisabledControlNormal(0, 220)
165-
local rightAxisY = GetDisabledControlNormal(0, 221)
166-
local rotation = GetCamRot(MainCamera, 2)
167-
if rightAxisX ~= 0.0 or rightAxisY ~= 0.0 then
168-
local new_z = rotation.z + rightAxisX * -1.0 * (2.0) * (4.0 + 0.1)
169-
local new_x = math.max(math.min(20.0, rotation.x + rightAxisY * -1.0 * (2.0) * (4.0 + 0.1)), -20.5)
170-
SetCamRot(MainCamera, new_x, 0.0, new_z, 2)
171-
end
172-
end
143+
--local function CheckRotationInput()
144+
-- local rightAxisX = GetDisabledControlNormal(0, 220)
145+
-- local rightAxisY = GetDisabledControlNormal(0, 221)
146+
-- local rotation = GetCamRot(MainCamera, 2)
147+
-- if rightAxisX ~= 0.0 or rightAxisY ~= 0.0 then
148+
-- local new_z = rotation.z + rightAxisX * -1.0 * (2.0) * (4.0 + 0.1)
149+
-- local new_x = math.max(math.min(20.0, rotation.x + rightAxisY * -1.0 * (2.0) * (4.0 + 0.1)), -20.5)
150+
-- SetCamRot(MainCamera, new_x, 0.0, new_z, 2)
151+
-- end
152+
--end
173153

174154
local function getTableLength(T)
175155
local count = 0
@@ -183,35 +163,54 @@ local function degToRad(degs)
183163
return degs * 3.141592653589793 / 180
184164
end
185165

186-
local function CheckMovementInput()
187-
local rotation = GetCamRot(MainCamera, 2)
188-
189-
if IsControlJustReleased(0, 21) then -- Left Shift
190-
curSpeed = curSpeed + 1
191-
if curSpeed > getTableLength(speeds) then
192-
curSpeed = 1
193-
end
194-
QBCore.Functions.Notify(Lang:t('info.speed') .. tostring(speeds[curSpeed]))
195-
end
196166

197-
local xVect = speeds[curSpeed] * math.sin(degToRad(rotation.z)) * -1.0
198-
local yVect = speeds[curSpeed] * math.cos(degToRad(rotation.z))
199-
local zVect = speeds[curSpeed] * math.tan(degToRad(rotation.x) - degToRad(rotation.y))
167+
-- Raycast function
168+
169+
local function RayCastGamePlayCamera(distance, ignoreEntity)
170+
local cameraRotation = GetGameplayCamRot()
171+
local cameraCoord = GetGameplayCamCoord()
172+
local direction = vector3(
173+
math.sin(degToRad(cameraRotation.z)) * -1.0,
174+
math.cos(degToRad(cameraRotation.z)),
175+
math.tan(degToRad(cameraRotation.x))
176+
)
177+
local destination = cameraCoord + direction * distance
178+
local rayHandle = StartShapeTestRay(cameraCoord.x, cameraCoord.y, cameraCoord.z, destination.x, destination.y, destination.z, 17, ignoreEntity, 0)
179+
local _, hit, endCoords, surfaceNormal, entityHit = GetShapeTestResult(rayHandle)
180+
return hit, endCoords, surfaceNormal, entityHit
181+
end
200182

201-
if IsControlPressed(1, 32) or IsDisabledControlPressed(1, 32) then -- W
202-
curPos.x = curPos.x + xVect
203-
curPos.y = curPos.y + yVect
204-
curPos.z = curPos.z + zVect
205-
end
206183

207-
if IsControlPressed(1, 33) or IsDisabledControlPressed(1, 33) then -- S
208-
curPos.x = curPos.x - xVect
209-
curPos.y = curPos.y - yVect
210-
curPos.z = curPos.z - zVect
211-
end
212184

213-
SetCamCoord(MainCamera, curPos.x, curPos.y, curPos.z)
214-
end
185+
--local function CheckMovementInput()
186+
-- local rotation = GetCamRot(MainCamera, 2)
187+
--
188+
-- if IsControlJustReleased(0, 21) then -- Left Shift
189+
-- curSpeed = curSpeed + 1
190+
-- if curSpeed > getTableLength(speeds) then
191+
-- curSpeed = 1
192+
-- end
193+
-- QBCore.Functions.Notify(Lang:t('info.speed') .. tostring(speeds[curSpeed]))
194+
-- end
195+
--
196+
-- local xVect = speeds[curSpeed] * math.sin(degToRad(rotation.z)) * -1.0
197+
-- local yVect = speeds[curSpeed] * math.cos(degToRad(rotation.z))
198+
-- local zVect = speeds[curSpeed] * math.tan(degToRad(rotation.x) - degToRad(rotation.y))
199+
--
200+
-- if IsControlPressed(1, 32) or IsDisabledControlPressed(1, 32) then -- W
201+
-- curPos.x = curPos.x + xVect
202+
-- curPos.y = curPos.y + yVect
203+
-- curPos.z = curPos.z + zVect
204+
-- end
205+
--
206+
-- if IsControlPressed(1, 33) or IsDisabledControlPressed(1, 33) then -- S
207+
-- curPos.x = curPos.x - xVect
208+
-- curPos.y = curPos.y - yVect
209+
-- curPos.z = curPos.z - zVect
210+
-- end
211+
--
212+
-- SetCamCoord(MainCamera, curPos.x, curPos.y, curPos.z)
213+
--end
215214

216215
-- Events
217216

@@ -372,17 +371,18 @@ RegisterNUICallback('spawnobject', function(data, cb)
372371
while not HasModelLoaded(modelHash) do
373372
Wait(1000)
374373
end
375-
local rotation = GetCamRot(MainCamera, 2)
376-
local xVect = 2.5 * math.sin(degToRad(rotation.z)) * -1.0
377-
local yVect = 2.5 * math.cos(degToRad(rotation.z))
378-
SelectedObj = CreateObject(modelHash, curPos.x + xVect, curPos.y + yVect, curPos.z, false, false, false)
374+
local rotation = 90
375+
local hit, castPos, _, _ = RayCastGamePlayCamera(50.0, PlayerPedId())
376+
curPos = castPos
377+
378+
SelectedObj = CreateObject(modelHash, curPos.x, curPos.y, curPos.z, false, false, false)
379379
local pos = GetEntityCoords(SelectedObj, true)
380380
local rot = GetEntityRotation(SelectedObj)
381381
SelObjRot = { x = rot.x, y = rot.y, z = rot.z }
382382
SelObjPos = { x = pos.x, y = pos.y, z = pos.z }
383383
SelObjHash = data.object
384384
PlaceObjectOnGroundProperly(SelectedObj)
385-
SetEntityCompletelyDisableCollision(SelectedObj, true) -- Prevents crazy physics when collidin with other entitys
385+
SetEntityCompletelyDisableCollision(SelectedObj, true, false) -- Prevents crazy physics when collidin with other entitys
386386
peanut = true
387387
cb('ok')
388388
end)
@@ -409,10 +409,10 @@ RegisterNUICallback('chooseobject', function(data, cb)
409409
type = 'objectLoaded',
410410
})
411411

412-
local rotation = GetCamRot(MainCamera, 2)
413-
local xVect = 2.5 * math.sin(degToRad(rotation.z)) * -1.0
414-
local yVect = 2.5 * math.cos(degToRad(rotation.z))
415-
previewObj = CreateObject(modelHash, curPos.x + xVect, curPos.y + yVect, curPos.z, false, false, false)
412+
local rotation = 90 -- rotation
413+
local hit, castPos, _, _ = RayCastGamePlayCamera(50.0, PlayerPedId())
414+
curPos = castPos
415+
previewObj = CreateObject(modelHash, curPos.x, curPos.y, curPos.z, false, false, false)
416416
PlaceObjectOnGroundProperly(previewObj)
417417
cb('ok')
418418
end)
@@ -423,7 +423,9 @@ CreateThread(function()
423423
while true do
424424
Wait(7)
425425
if DecoMode then
426-
DisableAllControlActions(0)
426+
427+
428+
--DisableAllControlActions(0)
427429
EnableControlAction(0, 32, true) -- W
428430
EnableControlAction(0, 33, true) -- S
429431
EnableControlAction(0, 245, true) -- T
@@ -440,29 +442,40 @@ CreateThread(function()
440442
EnableControlAction(0, 10, true) -- Page Up
441443
EnableControlAction(0, 11, true) -- Page Down
442444
EnableControlAction(0, 194, true) -- Backspace
443-
444445
DisplayRadar(false)
445446

446-
CheckRotationInput()
447-
CheckMovementInput()
447+
if IsControlJustReleased(0, 166) then -- F5
448+
449+
if not cursorEnabled then
450+
SetNuiFocus(true, true)
451+
cursorEnabled = true
452+
else
453+
SetNuiFocus(false, false)
454+
cursorEnabled = false
455+
end
456+
457+
end
458+
459+
448460

449461
if SelectedObj and peanut then
450-
SetEntityDrawOutline(SelectedObj)
451-
SetEntityDrawOutlineColor(116, 189, 252, 100)
452-
DrawMarker(21, SelObjPos.x, SelObjPos.y, SelObjPos.z + 1.28, 0.0, 0.0, 0.0, 180.0, 0.0, 0.0, 0.6, 0.6, 0.6, 28, 149, 255, 100, true, true, 2, false, false, false, false)
453-
if rotateActive then
462+
FreezeEntityPosition(SelectedObj, true)
463+
SetEntityDrawOutline(SelectedObj, true)
464+
SetEntityDrawOutlineColor(255, 150, 150, 200)
465+
466+
if IsControlPressed(0, 21) then --left shft
454467
CheckObjRotationInput()
455468
else
456469
CheckObjMovementInput()
457470
end
458-
if IsControlJustReleased(0, 170) then -- F3
459-
rotateActive = not rotateActive
460-
end
461-
if IsControlJustReleased(0, 19) then -- Left Alt
462-
PlaceObjectOnGroundProperly(SelectedObj)
463-
local groundPos = GetEntityCoords(SelectedObj)
464-
SelObjPos = groundPos
471+
if IsControlPressed(0, 19) then -- Left Alt
472+
local hit, pos, _, _ = RayCastGamePlayCamera(50.0, SelectedObj)
473+
if hit then
474+
SetEntityCoords(SelectedObj, pos.x, pos.y, pos.z)
475+
SelObjPos = pos
476+
end
465477
end
478+
466479
if IsControlJustReleased(0, 191) then -- Enter
467480
SetNuiFocus(true, true)
468481
cursorEnabled = true
@@ -478,16 +491,13 @@ CreateThread(function()
478491
SelObjId = 0
479492
peanut = false
480493
isEdit = false
481-
end
482-
end
483-
else
484-
if IsControlJustPressed(0, 166) then -- F5
485-
if not cursorEnabled then
486-
SetNuiFocus(true, true)
487-
cursorEnabled = true
494+
SetEntityDrawOutline(SelectedObj, false)
488495
end
489496
end
490497
end
498+
499+
500+
491501
end
492502
end
493503
end)
@@ -497,8 +507,8 @@ CreateThread(function()
497507
while true do
498508
Wait(7)
499509
if DecoMode then
500-
local camPos = GetCamCoord(MainCamera)
501-
local dist = #(vector3(camPos.x, camPos.y, camPos.z) - vector3(Config.Houses[ClosestHouse].coords.enter.x, Config.Houses[ClosestHouse].coords.enter.y, Config.Houses[ClosestHouse].coords.enter.z))
510+
local Pos = GetEntityCoords(PlayerPedId())
511+
local dist = #(vector3(Pos.x, Pos.y, Pos.z) - vector3(Config.Houses[ClosestHouse].coords.enter.x, Config.Houses[ClosestHouse].coords.enter.y, Config.Houses[ClosestHouse].coords.enter.z))
502512
if dist > 50.0 then
503513
DisableEditMode()
504514
closeDecorateUI()

html/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,11 @@
8989
</div>
9090
<div class="decorate-keys">
9191
<ul id="decorate-keys" style="list-style-type:disc;">
92-
<li>WASD/Mouse to move the camera.</li>
9392
<li>Arrow Keys to Move Object.</li>
94-
<li>PAGE UP/DOWN To move object up and down.</li>
95-
<li>LEFTALT To place object on the ground></object>.</li>
96-
<li>F3 To switch to turn and move.</li>
93+
<li>HOLD Left Shift to Rotate to rotate</li>
94+
<li>LEFTALT To place object where you'r looking at.</li>
9795
<li>ENTER to buy and place the object.</li>
98-
<li>F5 to turn mouse on / off to turn mouse on / off to turn mouse on / off.</li>
96+
<li>F5 switch between menu and placement mode</li>
9997
<li>ESC to disable Decoration mode.</li>
10098
</ul>
10199
</div>

0 commit comments

Comments
 (0)