Update 07.12.2022
This commit is contained in:
@@ -1,38 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CitizenFX.Core;
|
||||
using CitizenFX.Core.Native;
|
||||
using CitizenFX.Core.UI;
|
||||
using Nexd.ESX.Client;
|
||||
using TaxiJob.Client.Extensions;
|
||||
using TaxiJob.Shared;
|
||||
|
||||
namespace TaxiJob.Client.Handler {
|
||||
public static class JobHandler {
|
||||
private static bool _vehicleSpawned;
|
||||
private static Vehicle _vehicle;
|
||||
private static Blip _vehicleBlip;
|
||||
private static Blip _taskBlip;
|
||||
|
||||
private static int _lastJob;
|
||||
private static bool _inJob;
|
||||
private static bool _isNpcJob;
|
||||
private static bool _arrived;
|
||||
private static bool _lookStarted;
|
||||
private static Ped _npc;
|
||||
private static int _nextSpeech;
|
||||
|
||||
private static int _lastUpdate = 0;
|
||||
private static Vector3 _lastPos = Vector3.Zero;
|
||||
private static int _requestTime = 0;
|
||||
|
||||
public static Vehicle Vehicle;
|
||||
public static bool InJob;
|
||||
public static bool PendingJob;
|
||||
public static bool IsNpcJob;
|
||||
public static Vector3 Destination;
|
||||
public static string JobId;
|
||||
|
||||
public static void RequestJob(Vector3 destination, string jobId) {
|
||||
_requestTime = Game.GameTime;
|
||||
Destination = destination;
|
||||
JobId = jobId;
|
||||
PendingJob = true;
|
||||
TaxiJob.SetServiceStatus(false);
|
||||
}
|
||||
|
||||
public static void OnTick() {
|
||||
HandleVehicleSpawner();
|
||||
HandleVehicleDeleter();
|
||||
HandleVehicleBlip();
|
||||
HandleNpcJob();
|
||||
HandleJob();
|
||||
HandleJobAcception();
|
||||
|
||||
if (!_inJob && _vehicleSpawned && _vehicleBlip == null && Game.GameTime - _lastJob >= TaxiJob.Config.TimeToNpcJob) {
|
||||
StartNpcJob();
|
||||
if (!InJob && _vehicleSpawned && _vehicleBlip == null && Game.GameTime - _lastJob >= TaxiJob.Config.TimeToNpcJob && !PendingJob) {
|
||||
IsNpcJob = true;
|
||||
_requestTime = Game.GameTime;
|
||||
PendingJob = true;
|
||||
TaxiJob.SetServiceStatus(false);
|
||||
Game.PlaySound("SELECT", "HUD_FRONTEND_DEFAULT_SOUNDSET");
|
||||
}
|
||||
|
||||
if (Game.PlayerPed.IsDead && _inJob) StopJob();
|
||||
if (Game.PlayerPed.IsDead && InJob) StopJob();
|
||||
}
|
||||
|
||||
private static async void HandleVehicleSpawner() {
|
||||
@@ -45,34 +68,40 @@ namespace TaxiJob.Client.Handler {
|
||||
Screen.DisplayHelpTextThisFrame("Drücke ~INPUT_CONTEXT~ um das Taxi aus der Garage zu holen");
|
||||
|
||||
if (Game.IsControlJustReleased(0, Control.Context)) {
|
||||
if (!await TaxiJob.ServerCallback<bool>("esx_license:hasLicense", "drive")) {
|
||||
Notify.Error("Du hast keinen Führerschein!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ESX.Game.IsSpawnPointClear(TaxiJob.Config.VehicleSpawnPoint.ToVector3(), 5.0)) {
|
||||
Notify.Error("Ein Fahrzeug blockiert den Spawnpunkt!");
|
||||
return;
|
||||
}
|
||||
|
||||
_vehicle = await TaxiJob.SpawnVehicle(API.GetHashKey(TaxiJob.Config.VehicleModel), TaxiJob.Config.VehicleSpawnPoint);
|
||||
_vehicle.SetNumberPlateText(await TaxiJob.GeneratePlate());
|
||||
BaseScript.TriggerEvent("craftix:refuelVehicle", _vehicle.Handle);
|
||||
Game.PlayerPed.SetIntoVehicle(_vehicle, VehicleSeat.Driver);
|
||||
Vehicle = await TaxiJob.SpawnVehicle(API.GetHashKey(TaxiJob.Config.VehicleModel), TaxiJob.Config.VehicleSpawnPoint);
|
||||
Vehicle.Mods.LicensePlate = await TaxiJob.GeneratePlate();
|
||||
BaseScript.TriggerEvent("craftix:refuelVehicle", Vehicle.Handle);
|
||||
Game.PlayerPed.SetIntoVehicle(Vehicle, VehicleSeat.Driver);
|
||||
_lastJob = Game.GameTime;
|
||||
_vehicleSpawned = true;
|
||||
TaxiJob.SetServiceStatus(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleVehicleDeleter() {
|
||||
if (_vehicleSpawned) {
|
||||
if (!_vehicle.Exists() || _vehicle.IsDead) {
|
||||
if (!Vehicle.Exists() || Vehicle.IsDead) {
|
||||
_vehicleBlip?.Delete();
|
||||
StopJob();
|
||||
_vehicleSpawned = false;
|
||||
_vehicleBlip = null;
|
||||
_vehicle = null;
|
||||
Vehicle = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_vehicleSpawned || !Game.PlayerPed.IsInVehicle(_vehicle)) return;
|
||||
if (!_vehicleSpawned || !Game.PlayerPed.IsInVehicle(Vehicle)) return;
|
||||
World.DrawMarker(MarkerType.HorizontalCircleSkinny, TaxiJob.Config.VehicleDeleter, Vector3.Zero, Vector3.Zero, new Vector3(3.7f, 3.7f, 0.2f), TaxiJob.Config.VehicleSpawner.Color);
|
||||
|
||||
var dist = World.GetDistance(Game.PlayerPed.Position, TaxiJob.Config.VehicleDeleter);
|
||||
@@ -80,19 +109,21 @@ namespace TaxiJob.Client.Handler {
|
||||
|
||||
Screen.DisplayHelpTextThisFrame("Drücke ~INPUT_CONTEXT~ um das Auto in die Garage zu stellen");
|
||||
if (Game.IsControlJustReleased(0, Control.Context)) {
|
||||
_vehicle?.Delete();
|
||||
Vehicle?.Delete();
|
||||
_vehicleBlip?.Delete();
|
||||
StopJob();
|
||||
_vehicleSpawned = false;
|
||||
_vehicleBlip = null;
|
||||
_vehicle = null;
|
||||
Vehicle = null;
|
||||
Taximeter.Reset();
|
||||
TaxiJob.SetServiceStatus(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleVehicleBlip() {
|
||||
if (!_vehicleSpawned) return;
|
||||
|
||||
var inVehicle = Game.PlayerPed.IsInVehicle(_vehicle);
|
||||
var inVehicle = Game.PlayerPed.IsInVehicle(Vehicle);
|
||||
if (inVehicle && _vehicleBlip != null) {
|
||||
_lastJob = Game.GameTime;
|
||||
_vehicleBlip?.Delete();
|
||||
@@ -106,25 +137,50 @@ namespace TaxiJob.Client.Handler {
|
||||
_vehicleBlip.Name = "Dienstwagen";
|
||||
API.SetBlipDisplay(_vehicleBlip.Handle, 4);
|
||||
}
|
||||
|
||||
if (inVehicle && Game.IsControlJustReleased(0, Control.SelectCharacterFranklin)) {
|
||||
if (TaxiMenuHandler.MenuOpen) TaxiMenuHandler.CloseMenu();
|
||||
else TaxiMenuHandler.OpenMenu();
|
||||
}
|
||||
|
||||
if (inVehicle) HandleTaximeter();
|
||||
}
|
||||
|
||||
private static void HandleJob() {
|
||||
if (!_inJob || _isNpcJob) return;
|
||||
if (!InJob || IsNpcJob) return;
|
||||
|
||||
if (!_arrived) {
|
||||
if (!_arrived && _taskBlip != null) {
|
||||
if (!Game.PlayerPed.IsInVehicle()) return;
|
||||
var dist = World.GetDistance(Game.PlayerPed.Position, _taskBlip.Position);
|
||||
var dist = World.GetDistance(Game.PlayerPed.CurrentVehicle.Position, _taskBlip.Position);
|
||||
|
||||
if (dist <= 30.0 && Game.PlayerPed.CurrentVehicle.Speed <= 1) {
|
||||
_taskBlip.Delete();
|
||||
_taskBlip = null;
|
||||
_arrived = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_arrived && Game.PlayerPed.CurrentVehicle?.PassengerCount != 0) {
|
||||
_arrived = true;
|
||||
Taximeter.ActivateForClient(true);
|
||||
|
||||
_taskBlip?.Delete();
|
||||
_taskBlip = null;
|
||||
|
||||
Taximeter.Attributes.CurrentFare = "000.00";
|
||||
Taximeter.Attributes.DistanceTraveled = 0;
|
||||
Taximeter.Pause(false);
|
||||
}
|
||||
|
||||
if (_arrived && Game.PlayerPed.CurrentVehicle?.PassengerCount == 0) {
|
||||
var price = (int)Math.Round(Convert.ToSingle(Taximeter.Attributes.CurrentFare) + 0.5f);
|
||||
BaseScript.TriggerServerEvent("taxijob:server:bill", price, Taximeter.CurrentClient);
|
||||
BaseScript.TriggerServerEvent("taxijob:server:payout", (int)Math.Floor(price * 0.9f));
|
||||
StopJob();
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleNpcJob() {
|
||||
if (!_inJob || !_isNpcJob) return;
|
||||
if (!InJob || !IsNpcJob) return;
|
||||
|
||||
|
||||
if (!_arrived) {
|
||||
@@ -172,20 +228,68 @@ namespace TaxiJob.Client.Handler {
|
||||
}
|
||||
|
||||
if (dist <= 30.0 && Game.PlayerPed.CurrentVehicle.Speed <= 1) {
|
||||
_inJob = false;
|
||||
InJob = false;
|
||||
_npc.Task.LeaveVehicle(_npc.CurrentVehicle, true);
|
||||
_npc.PlayAmbientSpeech("GENERIC_THANKS");
|
||||
_npc.MarkAsNoLongerNeeded();
|
||||
_npc = null;
|
||||
|
||||
var price = (int)Math.Floor(Convert.ToSingle(Taximeter.Attributes.CurrentFare) * 0.9f);
|
||||
StopJob();
|
||||
Notify.Success("Auftrag beendet!");
|
||||
BaseScript.TriggerServerEvent("taxijob:server:payout", price);
|
||||
Notify.Success($"Auftrag beendet!\nDu hast ${price} bekommen.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleTaximeter() {
|
||||
if (!InJob || Taximeter.Attributes.MeterPause || Taximeter.Attributes.DistanceTraveled >= 15000) return;
|
||||
var current = Game.GameTime;
|
||||
if (current - _lastUpdate < 2000) return;
|
||||
_lastUpdate = current;
|
||||
var pos = Game.PlayerPed.CurrentVehicle.Position;
|
||||
if (_lastPos == Vector3.Zero) _lastPos = pos;
|
||||
Taximeter.CalculatePrice(_lastPos, pos);
|
||||
_lastPos = pos;
|
||||
}
|
||||
|
||||
private static void HandleJobAcception() {
|
||||
if (!PendingJob) return;
|
||||
Screen.DisplayHelpTextThisFrame("Neuer Auftrag!~n~~INPUT_SELECT_WEAPON_UNARMED~ Annehmen~n~~INPUT_SELECT_WEAPON_MELEE~ Ablehnen");
|
||||
|
||||
if (Game.IsControlJustReleased(0, Control.SelectWeaponUnarmed)) {
|
||||
if (IsNpcJob) StartNpcJob();
|
||||
else {
|
||||
BaseScript.TriggerServerEvent("taxijob:server:accept", true, JobId);
|
||||
StartJob();
|
||||
}
|
||||
}
|
||||
|
||||
if (Game.IsControlJustReleased(0, Control.SelectWeaponMelee) || Game.GameTime - _requestTime > 30000) {
|
||||
BaseScript.TriggerServerEvent("taxijob:server:accept", false, JobId);
|
||||
StopJob();
|
||||
}
|
||||
}
|
||||
|
||||
public static void StartJob() {
|
||||
TaxiMenuHandler.CloseMenu();
|
||||
Taximeter.Reset(Taximeter.Attributes.MeterVisible);
|
||||
var dest = Destination;
|
||||
StopJob(false);
|
||||
TaxiJob.SetServiceStatus(false);
|
||||
|
||||
_taskBlip = new Blip(API.AddBlipForCoord(dest.X, dest.Y, dest.Z));
|
||||
_taskBlip.Name = "Kunde";
|
||||
_taskBlip.ShowRoute = true;
|
||||
|
||||
InJob = true;
|
||||
}
|
||||
|
||||
private static async void StartNpcJob() {
|
||||
Notify.Info("Neuer Auftrag!");
|
||||
StopJob();
|
||||
TaxiMenuHandler.CloseMenu();
|
||||
Taximeter.Reset(Taximeter.Attributes.MeterVisible);
|
||||
StopJob(false);
|
||||
TaxiJob.SetServiceStatus(false);
|
||||
|
||||
var target = TaxiJob.Config.Positions.Random();
|
||||
_taskBlip = new Blip(API.AddBlipForCoord(target.X, target.Y, target.Z));
|
||||
@@ -201,11 +305,13 @@ namespace TaxiJob.Client.Handler {
|
||||
_npc.SetBlockingOfNonTemporaryEvents(true);
|
||||
_npc.IsInvincible = true;
|
||||
_npc.Task.StandStill(int.MaxValue);
|
||||
_isNpcJob = true;
|
||||
_inJob = true;
|
||||
IsNpcJob = true;
|
||||
InJob = true;
|
||||
|
||||
Taximeter.Pause(false);
|
||||
}
|
||||
|
||||
private static void StopJob() {
|
||||
public static void StopJob(bool updateService = true) {
|
||||
_taskBlip?.Delete();
|
||||
_taskBlip = null;
|
||||
|
||||
@@ -213,12 +319,19 @@ namespace TaxiJob.Client.Handler {
|
||||
_npc?.MarkAsNoLongerNeeded();
|
||||
_npc = null;
|
||||
|
||||
_inJob = false;
|
||||
_isNpcJob = false;
|
||||
InJob = false;
|
||||
IsNpcJob = false;
|
||||
_arrived = false;
|
||||
_lookStarted = false;
|
||||
_lastJob = Game.GameTime;
|
||||
PendingJob = false;
|
||||
Destination = Vector3.Zero;
|
||||
JobId = null;
|
||||
|
||||
Taximeter.Pause(true);
|
||||
if (updateService) TaxiJob.SetServiceStatus(true);
|
||||
Taximeter.ActivateForClient(false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user