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 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()
PlayerData = QBCore.Functions.GetPlayerData()
Wait(2000)
CreateStationBlips()
CreateStationInteractionPoints()
print("^2[TRAIN] Player loaded - Blips erstellt^7")
end)
-- Bahnhof Blips erstellen
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)
SetBlipSprite(blip, station.blip.sprite)
SetBlipDisplay(blip, 4)
@ -26,86 +38,107 @@ function CreateStationBlips()
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("🚂 " .. station.name)
EndTextCommandSetBlipName(blip)
print("^2[TRAIN] Blip erstellt für: " .. station.name .. "^7")
end
end
-- Bahnhof Interaktionspunkte erstellen
function CreateStationInteractionPoints()
CreateThread(function()
while true do
local sleep = 1000
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
if not isRiding then
for stationId, station in pairs(Config.TrainStations) do
local distance = #(playerCoords - station.interactionPoint)
-- Bahnhof Interaktionen - Haupt-Loop
CreateThread(function()
print("^2[TRAIN] Interaktions-Loop gestartet^7")
while true do
local sleep = 1000
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
if not isRiding then
for i, station in pairs(Config.TrainStations) do
local interactionPoint = station.interactionPoint or vector3(station.coords.x, station.coords.y, station.coords.z)
local distance = #(playerCoords - interactionPoint)
if distance <= Config.StationInteractionDistance then
sleep = 0
if distance <= Config.StationInteractionDistance then
sleep = 0
-- DrawText anzeigen
DrawText3D(station.interactionPoint.x, station.interactionPoint.y, station.interactionPoint.z + 1.0,
Config.DrawText.stationText)
-- Interaktion
if IsControlJustPressed(0, 38) then -- E
if not waitingForTrain then
OpenStationMenu(station)
else
lib:notify({
title = 'Bitte warten',
description = 'Ein Zug ist bereits unterwegs',
type = 'warning'
})
end
end
-- DrawText anzeigen
DrawText3D(interactionPoint.x, interactionPoint.y, interactionPoint.z + 1.0,
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
-- Prüfen ob Zug am Bahnhof wartet
if trainAtStation[station.id] then
local trainDistance = #(playerCoords - GetEntityCoords(trainAtStation[station.id]))
-- Interaktion
if IsControlJustPressed(0, 38) then -- E
print("^3[TRAIN] E gedrückt bei Station: " .. station.name .. "^7")
if not waitingForTrain then
OpenStationMenu(station)
else
lib:notify({
title = 'Bitte warten',
description = 'Ein Zug ist bereits unterwegs',
type = 'warning'
})
end
end
end
-- Prüfen ob Zug am Bahnhof wartet
if trainAtStation[station.id] then
local train = trainAtStation[station.id]
if DoesEntityExist(train) then
local trainCoords = GetEntityCoords(train)
local trainDistance = #(playerCoords - trainCoords)
if trainDistance <= 8.0 then
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
-- Hier würde das Zielmenü für die Fahrt geöffnet
OpenDestinationMenu(trainAtStation[station.id], station)
print("^3[TRAIN] Einsteigen in Zug^7")
BoardTrain(train, station)
end
end
end
end
end
Wait(sleep)
end
end)
end
Wait(sleep)
end
end)
-- Bahnhof Menü öffnen
function OpenStationMenu(station)
print("^3[TRAIN] Öffne Menü für Station: " .. station.name .. "^7")
local options = {}
-- Verfügbare Ziele anzeigen
for _, destinationId in pairs(station.destinations) do
local destination = GetStationById(destinationId)
if destination then
local price = CalculatePrice(station, destination)
local icon = GetStationIcon(destination.name)
table.insert(options, {
title = icon .. " " .. destination.name,
description = destination.description .. " - Preis: $" .. price,
icon = 'train',
onSelect = function()
CallTrainToStation(station, destination, price)
end,
metadata = {
{label = "Preis", value = "$" .. price},
{label = "Entfernung", value = math.floor(GetDistanceBetweenStations(station, destination)) .. "m"}
}
})
if station.destinations then
for _, destinationId in pairs(station.destinations) do
local destination = GetStationById(destinationId)
if destination then
local price = CalculatePrice(station, destination)
local icon = GetStationIcon(destination.name)
table.insert(options, {
title = icon .. " " .. destination.name,
description = destination.description .. " - Preis: $" .. price,
icon = 'train',
onSelect = function()
print("^3[TRAIN] Ziel gewählt: " .. destination.name .. "^7")
CallTrainToStation(station, destination, price)
end,
metadata = {
{label = "Preis", value = "$" .. price},
{label = "Entfernung", value = math.floor(GetDistanceBetweenStations(station, destination)) .. "m"}
}
})
end
end
end
@ -135,6 +168,8 @@ end
-- Zug zum Bahnhof rufen
function CallTrainToStation(station, destination, price)
print("^3[TRAIN] Rufe Zug für " .. station.name .. " -> " .. destination.name .. "^7")
-- Geld prüfen
QBCore.Functions.TriggerCallback('train:server:canAfford', function(canAfford)
if canAfford then
@ -163,8 +198,13 @@ end
-- Zug spawnen und zum Bahnhof fahren lassen
function SpawnAndMoveTrainToStation(station, destination)
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
local train = SpawnTrainAtLocation(station.trainSpawnPoint)
local train = SpawnTrainAtLocation(spawnPoint)
if not train then
waitingForTrain = false
@ -173,9 +213,12 @@ function SpawnAndMoveTrainToStation(station, destination)
description = 'Zug konnte nicht gespawnt werden',
type = 'error'
})
print("^1[TRAIN] Fehler beim Spawnen des Zugs^7")
return
end
print("^2[TRAIN] Zug gespawnt, ID: " .. train .. "^7")
-- Zug zum Bahnhof fahren lassen
local targetCoords = vector3(station.coords.x, station.coords.y, station.coords.z)
local arrived = false
@ -196,6 +239,10 @@ function SpawnAndMoveTrainToStation(station, destination)
local trainCoords = GetEntityCoords(train)
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
-- Langsamer werden
SetTrainSpeed(train, Config.TrainArrival.arrivalSpeed)
@ -208,6 +255,8 @@ function SpawnAndMoveTrainToStation(station, destination)
SetTrainCruiseSpeed(train, 0)
arrived = true
print("^2[TRAIN] Zug angekommen am Bahnhof: " .. station.name .. "^7")
-- Zug am Bahnhof registrieren
trainAtStation[station.id] = train
waitingForTrain = false
@ -223,6 +272,8 @@ function SpawnAndMoveTrainToStation(station, destination)
if Config.TrainArrival.despawnAfterWait then
SetTimeout(Config.TrainArrival.waitTime, function()
if trainAtStation[station.id] == train and not isRiding then
print("^3[TRAIN] Zug fährt ab - keine Passagiere^7")
lib:notify({
title = '🚂 Zug fährt ab',
description = 'Der Zug verlässt den Bahnhof',
@ -251,11 +302,12 @@ function SpawnAndMoveTrainToStation(station, destination)
end)
end
-- Zielmenü für Fahrt öffnen
function OpenDestinationMenu(train, currentStation)
-- Hier das normale Zielmenü wie vorher, aber mit dem wartenden Zug
-- In Zug einsteigen
function BoardTrain(train, station)
local playerPed = PlayerPedId()
SetPedIntoVehicle(playerPed, train, 1)
isRiding = true
currentTrain = train
lib:notify({
title = '🚂 Willkommen an Bord',
@ -265,10 +317,30 @@ function OpenDestinationMenu(train, currentStation)
})
-- 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
-- Hilfsfunktionen
-- Zug spawnen
function SpawnTrainAtLocation(spawnPoint)
local model = GetHashKey(Config.TrainCars.main)
@ -285,21 +357,24 @@ function SpawnTrainAtLocation(spawnPoint)
SetTrainCruiseSpeed(train, 0.0)
-- Waggons hinzufügen
Wait(1000)
for _, carModel in pairs(Config.TrainCars.cars) do
local carHash = GetHashKey(carModel)
RequestModel(carHash)
while not HasModelLoaded(carHash) do
Wait(500)
CreateThread(function()
Wait(1000)
for _, carModel in pairs(Config.TrainCars.cars) do
local carHash = GetHashKey(carModel)
RequestModel(carHash)
while not HasModelLoaded(carHash) do
Wait(500)
end
CreateMissionTrainCar(train, carHash, false, false, false)
end
CreateMissionTrainCar(train, carHash, false, false, false)
end
end)
return train
end
return nil
end
-- Hilfsfunktionen
function GetStationById(id)
for _, station in pairs(Config.TrainStations) do
if station.id == id then
@ -367,6 +442,22 @@ function DrawText3D(x, y, z, text)
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
AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() == resourceName then