1
0
Fork 0
forked from Simnation/Main

Update client.lua

This commit is contained in:
Nordi98 2025-08-11 18:44:12 +02:00
parent 33075c6a84
commit 22e4719062

View file

@ -9,15 +9,27 @@ local trainAtStation = {}
local cinemaCam = nil local cinemaCam = nil
local waitingForTrain = false local waitingForTrain = false
-- Beim Laden des Scripts
CreateThread(function()
Wait(1000) -- Kurz warten bis alles geladen ist
CreateStationBlips()
print("^2[TRAIN] Blips erstellt^7")
end)
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
PlayerData = QBCore.Functions.GetPlayerData() PlayerData = QBCore.Functions.GetPlayerData()
Wait(2000)
CreateStationBlips() CreateStationBlips()
CreateStationInteractionPoints() print("^2[TRAIN] Player loaded - Blips erstellt^7")
end) end)
-- Bahnhof Blips erstellen -- Bahnhof Blips erstellen
function CreateStationBlips() function CreateStationBlips()
for _, station in pairs(Config.TrainStations) do print("^3[TRAIN] Erstelle Blips für " .. #Config.TrainStations .. " Bahnhöfe^7")
for i, station in pairs(Config.TrainStations) do
print("^3[TRAIN] Erstelle Blip für: " .. station.name .. " bei " .. station.coords.x .. ", " .. station.coords.y .. "^7")
local blip = AddBlipForCoord(station.coords.x, station.coords.y, station.coords.z) local blip = AddBlipForCoord(station.coords.x, station.coords.y, station.coords.z)
SetBlipSprite(blip, station.blip.sprite) SetBlipSprite(blip, station.blip.sprite)
SetBlipDisplay(blip, 4) SetBlipDisplay(blip, 4)
@ -26,30 +38,42 @@ function CreateStationBlips()
BeginTextCommandSetBlipName("STRING") BeginTextCommandSetBlipName("STRING")
AddTextComponentString("🚂 " .. station.name) AddTextComponentString("🚂 " .. station.name)
EndTextCommandSetBlipName(blip) EndTextCommandSetBlipName(blip)
print("^2[TRAIN] Blip erstellt für: " .. station.name .. "^7")
end end
end end
-- Bahnhof Interaktionspunkte erstellen -- Bahnhof Interaktionen - Haupt-Loop
function CreateStationInteractionPoints() CreateThread(function()
CreateThread(function() print("^2[TRAIN] Interaktions-Loop gestartet^7")
while true do while true do
local sleep = 1000 local sleep = 1000
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed) local playerCoords = GetEntityCoords(playerPed)
if not isRiding then if not isRiding then
for stationId, station in pairs(Config.TrainStations) do for i, station in pairs(Config.TrainStations) do
local distance = #(playerCoords - station.interactionPoint) local interactionPoint = station.interactionPoint or vector3(station.coords.x, station.coords.y, station.coords.z)
local distance = #(playerCoords - interactionPoint)
if distance <= Config.StationInteractionDistance then if distance <= Config.StationInteractionDistance then
sleep = 0 sleep = 0
-- DrawText anzeigen -- DrawText anzeigen
DrawText3D(station.interactionPoint.x, station.interactionPoint.y, station.interactionPoint.z + 1.0, DrawText3D(interactionPoint.x, interactionPoint.y, interactionPoint.z + 1.0,
Config.DrawText.stationText) Config.DrawText.stationText)
-- Debug Info
if Config.Debug then
DrawText3D(interactionPoint.x, interactionPoint.y, interactionPoint.z + 2.0,
"Station: " .. station.name .. " | Dist: " .. math.floor(distance))
end
-- Interaktion -- Interaktion
if IsControlJustPressed(0, 38) then -- E if IsControlJustPressed(0, 38) then -- E
print("^3[TRAIN] E gedrückt bei Station: " .. station.name .. "^7")
if not waitingForTrain then if not waitingForTrain then
OpenStationMenu(station) OpenStationMenu(station)
else else
@ -64,14 +88,19 @@ function CreateStationInteractionPoints()
-- Prüfen ob Zug am Bahnhof wartet -- Prüfen ob Zug am Bahnhof wartet
if trainAtStation[station.id] then if trainAtStation[station.id] then
local trainDistance = #(playerCoords - GetEntityCoords(trainAtStation[station.id])) local train = trainAtStation[station.id]
if DoesEntityExist(train) then
local trainCoords = GetEntityCoords(train)
local trainDistance = #(playerCoords - trainCoords)
if trainDistance <= 8.0 then if trainDistance <= 8.0 then
sleep = 0 sleep = 0
DrawText3D(GetEntityCoords(trainAtStation[station.id]), Config.DrawText.boardText) DrawText3D(trainCoords.x, trainCoords.y, trainCoords.z + 2.0, Config.DrawText.boardText)
if IsControlJustPressed(0, 38) then -- E if IsControlJustPressed(0, 38) then -- E
-- Hier würde das Zielmenü für die Fahrt geöffnet print("^3[TRAIN] Einsteigen in Zug^7")
OpenDestinationMenu(trainAtStation[station.id], station) BoardTrain(train, station)
end
end end
end end
end end
@ -80,14 +109,16 @@ function CreateStationInteractionPoints()
Wait(sleep) Wait(sleep)
end end
end) end)
end
-- Bahnhof Menü öffnen -- Bahnhof Menü öffnen
function OpenStationMenu(station) function OpenStationMenu(station)
print("^3[TRAIN] Öffne Menü für Station: " .. station.name .. "^7")
local options = {} local options = {}
-- Verfügbare Ziele anzeigen -- Verfügbare Ziele anzeigen
if station.destinations then
for _, destinationId in pairs(station.destinations) do for _, destinationId in pairs(station.destinations) do
local destination = GetStationById(destinationId) local destination = GetStationById(destinationId)
if destination then if destination then
@ -99,6 +130,7 @@ function OpenStationMenu(station)
description = destination.description .. " - Preis: $" .. price, description = destination.description .. " - Preis: $" .. price,
icon = 'train', icon = 'train',
onSelect = function() onSelect = function()
print("^3[TRAIN] Ziel gewählt: " .. destination.name .. "^7")
CallTrainToStation(station, destination, price) CallTrainToStation(station, destination, price)
end, end,
metadata = { metadata = {
@ -108,6 +140,7 @@ function OpenStationMenu(station)
}) })
end end
end end
end
if #options == 0 then if #options == 0 then
table.insert(options, { table.insert(options, {
@ -135,6 +168,8 @@ end
-- Zug zum Bahnhof rufen -- Zug zum Bahnhof rufen
function CallTrainToStation(station, destination, price) function CallTrainToStation(station, destination, price)
print("^3[TRAIN] Rufe Zug für " .. station.name .. " -> " .. destination.name .. "^7")
-- Geld prüfen -- Geld prüfen
QBCore.Functions.TriggerCallback('train:server:canAfford', function(canAfford) QBCore.Functions.TriggerCallback('train:server:canAfford', function(canAfford)
if canAfford then if canAfford then
@ -163,8 +198,13 @@ end
-- Zug spawnen und zum Bahnhof fahren lassen -- Zug spawnen und zum Bahnhof fahren lassen
function SpawnAndMoveTrainToStation(station, destination) function SpawnAndMoveTrainToStation(station, destination)
CreateThread(function() CreateThread(function()
print("^3[TRAIN] Spawne Zug für Station: " .. station.name .. "^7")
-- Spawn-Punkt bestimmen
local spawnPoint = station.trainSpawnPoint or vector4(station.coords.x - 500, station.coords.y, station.coords.z, station.coords.w)
-- Zug am Spawn-Punkt erstellen -- Zug am Spawn-Punkt erstellen
local train = SpawnTrainAtLocation(station.trainSpawnPoint) local train = SpawnTrainAtLocation(spawnPoint)
if not train then if not train then
waitingForTrain = false waitingForTrain = false
@ -173,9 +213,12 @@ function SpawnAndMoveTrainToStation(station, destination)
description = 'Zug konnte nicht gespawnt werden', description = 'Zug konnte nicht gespawnt werden',
type = 'error' type = 'error'
}) })
print("^1[TRAIN] Fehler beim Spawnen des Zugs^7")
return return
end end
print("^2[TRAIN] Zug gespawnt, ID: " .. train .. "^7")
-- Zug zum Bahnhof fahren lassen -- Zug zum Bahnhof fahren lassen
local targetCoords = vector3(station.coords.x, station.coords.y, station.coords.z) local targetCoords = vector3(station.coords.x, station.coords.y, station.coords.z)
local arrived = false local arrived = false
@ -196,6 +239,10 @@ function SpawnAndMoveTrainToStation(station, destination)
local trainCoords = GetEntityCoords(train) local trainCoords = GetEntityCoords(train)
local distance = #(trainCoords - targetCoords) local distance = #(trainCoords - targetCoords)
if Config.Debug then
print("^3[TRAIN] Zug Entfernung zum Bahnhof: " .. math.floor(distance) .. "m^7")
end
if distance < 100 then if distance < 100 then
-- Langsamer werden -- Langsamer werden
SetTrainSpeed(train, Config.TrainArrival.arrivalSpeed) SetTrainSpeed(train, Config.TrainArrival.arrivalSpeed)
@ -208,6 +255,8 @@ function SpawnAndMoveTrainToStation(station, destination)
SetTrainCruiseSpeed(train, 0) SetTrainCruiseSpeed(train, 0)
arrived = true arrived = true
print("^2[TRAIN] Zug angekommen am Bahnhof: " .. station.name .. "^7")
-- Zug am Bahnhof registrieren -- Zug am Bahnhof registrieren
trainAtStation[station.id] = train trainAtStation[station.id] = train
waitingForTrain = false waitingForTrain = false
@ -223,6 +272,8 @@ function SpawnAndMoveTrainToStation(station, destination)
if Config.TrainArrival.despawnAfterWait then if Config.TrainArrival.despawnAfterWait then
SetTimeout(Config.TrainArrival.waitTime, function() SetTimeout(Config.TrainArrival.waitTime, function()
if trainAtStation[station.id] == train and not isRiding then if trainAtStation[station.id] == train and not isRiding then
print("^3[TRAIN] Zug fährt ab - keine Passagiere^7")
lib:notify({ lib:notify({
title = '🚂 Zug fährt ab', title = '🚂 Zug fährt ab',
description = 'Der Zug verlässt den Bahnhof', description = 'Der Zug verlässt den Bahnhof',
@ -251,11 +302,12 @@ function SpawnAndMoveTrainToStation(station, destination)
end) end)
end end
-- Zielmenü für Fahrt öffnen -- In Zug einsteigen
function OpenDestinationMenu(train, currentStation) function BoardTrain(train, station)
-- Hier das normale Zielmenü wie vorher, aber mit dem wartenden Zug
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
SetPedIntoVehicle(playerPed, train, 1) SetPedIntoVehicle(playerPed, train, 1)
isRiding = true
currentTrain = train
lib:notify({ lib:notify({
title = '🚂 Willkommen an Bord', title = '🚂 Willkommen an Bord',
@ -265,10 +317,30 @@ function OpenDestinationMenu(train, currentStation)
}) })
-- Hier würde die normale Zugfahrt-Logik weitergehen -- Hier würde die normale Zugfahrt-Logik weitergehen
-- StartTrainJourney(train, destination) etc. -- Für jetzt einfach nach 10 Sekunden wieder aussteigen lassen
SetTimeout(10000, function()
TaskLeaveVehicle(playerPed, train, 0)
isRiding = false
currentTrain = nil
trainAtStation[station.id] = nil
lib:notify({
title = '🚂 Ankunft',
description = 'Sie sind angekommen',
type = 'success'
})
-- Zug wegfahren lassen
SetTrainSpeed(train, 15.0)
SetTimeout(5000, function()
if DoesEntityExist(train) then
DeleteMissionTrain(train)
end
end)
end)
end end
-- Hilfsfunktionen -- Zug spawnen
function SpawnTrainAtLocation(spawnPoint) function SpawnTrainAtLocation(spawnPoint)
local model = GetHashKey(Config.TrainCars.main) local model = GetHashKey(Config.TrainCars.main)
@ -285,6 +357,7 @@ function SpawnTrainAtLocation(spawnPoint)
SetTrainCruiseSpeed(train, 0.0) SetTrainCruiseSpeed(train, 0.0)
-- Waggons hinzufügen -- Waggons hinzufügen
CreateThread(function()
Wait(1000) Wait(1000)
for _, carModel in pairs(Config.TrainCars.cars) do for _, carModel in pairs(Config.TrainCars.cars) do
local carHash = GetHashKey(carModel) local carHash = GetHashKey(carModel)
@ -294,12 +367,14 @@ function SpawnTrainAtLocation(spawnPoint)
end end
CreateMissionTrainCar(train, carHash, false, false, false) CreateMissionTrainCar(train, carHash, false, false, false)
end end
end)
return train return train
end end
return nil return nil
end end
-- Hilfsfunktionen
function GetStationById(id) function GetStationById(id)
for _, station in pairs(Config.TrainStations) do for _, station in pairs(Config.TrainStations) do
if station.id == id then if station.id == id then
@ -367,6 +442,22 @@ function DrawText3D(x, y, z, text)
end end
end end
-- Debug Commands
RegisterCommand('trainblips', function()
CreateStationBlips()
print("^2[TRAIN] Blips neu erstellt^7")
end)
RegisterCommand('traintest', function()
local playerCoords = GetEntityCoords(PlayerPedId())
print("^3[TRAIN] Player Position: " .. playerCoords.x .. ", " .. playerCoords.y .. ", " .. playerCoords.z .. "^7")
for i, station in pairs(Config.TrainStations) do
local distance = #(playerCoords - vector3(station.coords.x, station.coords.y, station.coords.z))
print("^3[TRAIN] " .. station.name .. " - Entfernung: " .. math.floor(distance) .. "m^7")
end
end)
-- Cleanup -- Cleanup
AddEventHandler('onResourceStop', function(resourceName) AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() == resourceName then if GetCurrentResourceName() == resourceName then