Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1. using CitizenFX.Core;
  2. using CitizenFX.Core.Native;
  3. using CitizenFX.Core.UI;
  4. using NativeUI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading.Tasks;
  8.  
  9. namespace VehicleControls {
  10.     public class VehicleControls : BaseScript {
  11.         private static string ERROR = "~r~Errore: ";
  12.         private static string ERROR_NOCAR = ERROR + "Non sei su un veicolo oppure non hai un veicolo salvato!";
  13.  
  14.         private Vehicle savedVehicle;
  15.  
  16.         private void AddEngineItem(UIMenu menu) {
  17.             var newItem = new UIMenuItem("Gestisci Motore");
  18.             menu.AddItem(newItem);
  19.  
  20.             menu.OnItemSelect += (sender, item, index) => {
  21.                 if (item != newItem) {
  22.                     return;
  23.                 }
  24.  
  25.                 Vehicle car = LocalPlayer.Character.CurrentVehicle;
  26.  
  27.                 if (car == null && savedVehicle == null) {
  28.                     Screen.ShowNotification(ERROR_NOCAR);
  29.                     return;
  30.                 }
  31.  
  32.                 if (car != null) {
  33.                     ToggleEngine(car);
  34.                 }
  35.                 else if (savedVehicle != null) {
  36.                     ToggleEngine(savedVehicle);
  37.                 }
  38.             };
  39.         }
  40.  
  41.         private void ToggleEngine(Vehicle car) {
  42.             if (car.IsEngineRunning) {
  43.                 Screen.ShowNotification("Il motore è ~r~spento~w~.");
  44.                 car.IsDriveable = false;
  45.                 car.IsEngineRunning = false;
  46.             }
  47.             else {
  48.                 Screen.ShowNotification("Il motore è ~g~acceso~w~.");
  49.                 car.IsDriveable = true;
  50.                 car.IsEngineRunning = true;
  51.             }
  52.         }
  53.  
  54.         private void AddOpenDoorItem(UIMenu menu) {
  55.             List<dynamic> doors = new List<dynamic> {
  56.                 "Avanti Sinistra",
  57.                 "Avanti Destra",
  58.                 "Dietro Sinistra",
  59.                 "Dietro Destra",
  60.                 "Cofano",
  61.                 "Bagagliaio"
  62.             };
  63.             var newItem = new UIMenuListItem("Gestisci Portiera", doors, 0);
  64.             menu.AddItem(newItem);
  65.  
  66.             menu.OnItemSelect += (sender, item, index) => {
  67.                 if (item != newItem) {
  68.                     return;
  69.                 }
  70.  
  71.                 int itemIndex = newItem.Index;
  72.                 string doorName = newItem.IndexToItem(itemIndex);
  73.                 Vehicle car = LocalPlayer.Character.CurrentVehicle;
  74.  
  75.                 if (car == null && savedVehicle == null) {
  76.                     Screen.ShowNotification(ERROR_NOCAR);
  77.                     return;
  78.                 }
  79.  
  80.                 if (car != null) {
  81.                     ToggleDoor(car, itemIndex, doorName);
  82.                 }
  83.                 else {
  84.                     ToggleDoor(savedVehicle, itemIndex, doorName);
  85.                 }
  86.             };
  87.         }
  88.  
  89.         private void ToggleDoor(Vehicle car, int index, string doorName) {
  90.             bool doorBroken = Function.Call<bool>(Hash.IS_VEHICLE_DOOR_DAMAGED, car, index);
  91.             if (doorBroken) {
  92.                 Screen.ShowNotification(ERROR + "Portiera rotta!");
  93.                 return;
  94.             }
  95.  
  96.             float doorAngle = Function.Call<float>(Hash.GET_VEHICLE_DOOR_ANGLE_RATIO, car, index);
  97.             if (doorAngle == 0) // Door chiusa
  98.             {
  99.                 Screen.ShowNotification("La portiera: " + doorName + " è ~g~aperta~w~.");
  100.                 Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, car, index, false, false);
  101.             }
  102.             else {
  103.                 Screen.ShowNotification("La portiera: " + doorName + " è ~r~chiusa~w~.");
  104.                 Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, car, index, false);
  105.             }
  106.         }
  107.  
  108.         private void AddSaveVehicleItem(UIMenu menu) {
  109.             var newItem = new UIMenuItem("Salva Veicolo");
  110.             menu.AddItem(newItem);
  111.  
  112.             menu.OnItemSelect += (sender, item, index) => {
  113.                 if (item != newItem) {
  114.                     return;
  115.                 }
  116.  
  117.                 Vehicle car = LocalPlayer.Character.CurrentVehicle;
  118.  
  119.                 if (car == null) {
  120.                     Screen.ShowNotification(ERROR_NOCAR);
  121.                     return;
  122.                 }
  123.  
  124.                 SaveVehicle(car);
  125.                 Screen.ShowNotification("Veicolo Salvato.");
  126.             };
  127.         }
  128.  
  129.         private void AddCredits(UIMenu menu) {
  130.             var newItem = new UIMenuItem("Crediti");
  131.             menu.AddItem(newItem);
  132.  
  133.             menu.OnItemSelect += (sender, item, index) => {
  134.                 if (item != newItem) {
  135.                     return;
  136.                 }
  137.  
  138.                 Screen.ShowNotification("Resource sviluppata da: ~o~Zio_Pagnotta");
  139.             };
  140.  
  141.         }
  142.  
  143.         private void SaveVehicle(Vehicle car) {
  144.             if (savedVehicle != null) {
  145.                 foreach (Blip vehBlip in savedVehicle.AttachedBlips) {
  146.                     vehBlip.Alpha = 0;
  147.                 }
  148.             }
  149.  
  150.             Blip blip = car.AttachBlip();
  151.             blip.Sprite = BlipSprite.PersonalVehicleCar;
  152.  
  153.             savedVehicle = car;
  154.         }
  155.  
  156.         public VehicleControls() {
  157.             MenuPool menuPool = new MenuPool();
  158.  
  159.             UIMenu menu = new UIMenu("TheFinalRoad", "Gestione Veicolo");
  160.             menuPool.Add(menu);
  161.  
  162.             AddEngineItem(menu);
  163.             AddOpenDoorItem(menu);
  164.             AddSaveVehicleItem(menu);
  165.             AddCredits(menu);
  166.  
  167.             menu.RefreshIndex();
  168.  
  169.             Tick += new Func<Task>(async delegate {
  170.                 await Task.FromResult(0);
  171.  
  172.                 menuPool.ProcessMenus();
  173.                 if (Game.IsControlJustReleased(1, Control.InteractionMenu)) {
  174.                     menu.Visible = !menu.Visible;
  175.                 }
  176.             });
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement