Skip to content

Commit

Permalink
internetradio: add attaching to vehicles (multitheftauto#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds1-e authored Jul 16, 2024
1 parent d39da17 commit 3dd5cbd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion [gameplay]/internetradio/config/SRadioConfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
-- ## Version: 1.0 ##
-- #######################################

RADIO_BOX_MODEL = 2229
RADIO_BOX_MODEL = 2229
RADIO_DESTROY_ON_VEHICLE_EXPLODE = false
RADIO_DESTROY_ON_VEHICLE_DESTROY = false
49 changes: 48 additions & 1 deletion [gameplay]/internetradio/handle_radio/SHandleRadio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function clearPlayerSpeaker(playerOrSpeaker)
local matchingElement = (playerElement == playerOrSpeaker) or (speakerBox == playerOrSpeaker)

if (matchingElement) then
local boxElement = isElement(speakerBox)

if (boxElement) then
destroyElement(speakerBox)
end

playerSpeakers[playerElement] = nil

return true
Expand Down Expand Up @@ -87,6 +93,12 @@ function onServerCreateSpeaker(streamURL)
setElementDimension(boxElement, playerDimension)
setElementCollisionsEnabled(boxElement, false)

local playerVehicle = isPedInVehicle(client) and getPedOccupiedVehicle(client)

if (playerVehicle) then
attachElements(boxElement, playerVehicle, -0.7, -1.5, -0.1, 0, 90, 0)
end

local speakerData = {
speakerBox = boxElement,
speakerStreamURL = streamURL,
Expand Down Expand Up @@ -167,4 +179,39 @@ function clearSpeakersOnDestroyQuit()
clearPlayerSpeaker(source)
end
addEventHandler("onPlayerQuit", root, clearSpeakersOnDestroyQuit)
addEventHandler("onElementDestroy", resourceRoot, clearSpeakersOnDestroyQuit)
addEventHandler("onElementDestroy", resourceRoot, clearSpeakersOnDestroyQuit)

function destroyAttachedRadioOnVehicleExplodeOrDestroy()
local validElement = isElement(source)

if (not validElement) then
return false
end

local elementType = getElementType(source)
local vehicleType = (elementType == "vehicle")

if (not vehicleType) then
return false
end

local attachedElements = getAttachedElements(source)

for attachedID = 1, #attachedElements do
local attachedElement = attachedElements[attachedID]
local attachedElementType = getElementType(attachedElement)
local attachedElementObject = (attachedElementType == "object")

if (attachedElementObject) then
clearPlayerSpeaker(attachedElement)
end
end
end

if (RADIO_DESTROY_ON_VEHICLE_EXPLODE) then
addEventHandler("onVehicleExplode", root, destroyAttachedRadioOnVehicleExplodeOrDestroy)
end

if (RADIO_DESTROY_ON_VEHICLE_DESTROY) then
addEventHandler("onElementDestroy", root, destroyAttachedRadioOnVehicleExplodeOrDestroy)
end

0 comments on commit 3dd5cbd

Please sign in to comment.