forked from pongo1231/VehicleControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleControls.cs
301 lines (259 loc) · 8.7 KB
/
VehicleControls.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using CitizenFX.Core;
using CitizenFX.Core.Native;
using CitizenFX.Core.UI;
using NativeUI;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace VehicleControls
{
public class VehicleControls : BaseScript
{
private static string ERROR = "~r~錯誤: ";
private static string ERROR_NOCAR = ERROR + "你不在車輛內或者你不擁有這輛車";
private Vehicle savedVehicle;
private void AddEngineItem(UIMenu menu)
{
var newItem = new UIMenuItem("引擎開關");
menu.AddItem(newItem);
menu.OnItemSelect += (sender, item, index) =>
{
if (item != newItem)
{
return;
}
Vehicle car = LocalPlayer.Character.CurrentVehicle;
if (car == null
&& savedVehicle == null)
{
Screen.ShowNotification(ERROR_NOCAR);
return;
}
if (car != null)
{
ToggleEngine(car);
}
else if (savedVehicle != null)
{
ToggleEngine(savedVehicle);
}
};
}
private void ToggleEngine(Vehicle car)
{
if (car.IsEngineRunning)
{
Screen.ShowNotification("引擎 ~r~熄火~w~.");
car.IsDriveable = false;
car.IsEngineRunning = false;
}
else
{
Screen.ShowNotification("引擎 ~g~發動~w~.");
car.IsDriveable = true;
car.IsEngineRunning = true;
}
}
private void AddDoorLockItem(UIMenu menu)
{
var newItem = new UIMenuItem("車門開關", "注意:這項操作將自動儲存此車輛");
menu.AddItem(newItem);
menu.OnItemSelect += (sender, item, index) =>
{
if (item == newItem)
{
Vehicle car = LocalPlayer.Character.CurrentVehicle;
if (car == null
&& savedVehicle == null)
{
Screen.ShowNotification(ERROR_NOCAR);
return;
}
if (car != null)
{
LockDoor(car);
SaveVehicle(car);
}
else
{
LockDoor(savedVehicle);
}
}
};
}
private void LockDoor(Vehicle car)
{
bool doorLocked = (Function.Call<int>(Hash.GET_VEHICLE_DOOR_LOCK_STATUS, car) == 2);
if (doorLocked)
{
Screen.ShowNotification("車門已經 ~g~解鎖了~w~.");
Function.Call(Hash.SET_VEHICLE_DOORS_LOCKED, car, 0);
}
else
{
Screen.ShowNotification("車門已經 ~r~鎖住了~w~.");
Function.Call(Hash.SET_VEHICLE_DOORS_LOCKED, car, 2);
}
}
private void AddOpenDoorItem(UIMenu menu)
{
List<dynamic> doors = new List<dynamic>
{
"左前",
"右前",
"左後",
"右後",
"引擎蓋",
"後車箱"
};
var newItem = new UIMenuListItem("開關車門", doors, 0);
menu.AddItem(newItem);
menu.OnItemSelect += (sender, item, index) =>
{
if (item != newItem)
{
return;
}
int itemIndex = newItem.Index;
string doorName = newItem.IndexToItem(itemIndex);
Vehicle car = LocalPlayer.Character.CurrentVehicle;
if (car == null
&& savedVehicle == null)
{
Screen.ShowNotification(ERROR_NOCAR);
return;
}
if (car != null)
{
ToggleDoor(car, itemIndex, doorName);
}
else
{
ToggleDoor(savedVehicle, itemIndex, doorName);
}
};
}
private void ToggleDoor(Vehicle car, int index, string doorName)
{
bool doorBroken = Function.Call<bool>(Hash.IS_VEHICLE_DOOR_DAMAGED, car, index);
if (doorBroken)
{
Screen.ShowNotification(ERROR + "車門已損毀");
return;
}
float doorAngle = Function.Call<float>(Hash.GET_VEHICLE_DOOR_ANGLE_RATIO, car, index);
if (doorAngle == 0) // Door is closed
{
Screen.ShowNotification(doorName + " 車門已經 ~g~開啟了~w~.");
Function.Call(Hash.SET_VEHICLE_DOOR_OPEN, car, index, false, false);
}
else
{
Screen.ShowNotification(doorName + " 車門已經 ~r~關閉了~w~.");
Function.Call(Hash.SET_VEHICLE_DOOR_SHUT, car, index, false);
}
}
private void AddLockSpeedItem(UIMenu menu)
{
List<dynamic> speeds = new List<dynamic>()
{
"None"
};
for (int i = 30; i < 121; i = i + 10)
{
speeds.Add(i + " KM/H");
}
UIMenuListItem newItem = new UIMenuListItem("鎖定最高時速", speeds, 0);
menu.AddItem(newItem);
menu.OnItemSelect += (sender, item, index) =>
{
if (item != newItem)
{
return;
}
Vehicle car = LocalPlayer.Character.CurrentVehicle;
if (car == null
&& savedVehicle == null)
{
Screen.ShowNotification(ERROR_NOCAR);
return;
}
if (car != null)
{
LockSpeed(car, newItem);
}
else
{
LockSpeed(savedVehicle, newItem);
}
};
}
private void LockSpeed(Vehicle car, UIMenuListItem item)
{
string[] itemName = item.IndexToItem(item.Index).Split(' ');
if (itemName[0] == "None")
{
car.MaxSpeed = int.MaxValue;
Screen.ShowNotification($"車輛限速已被解除");
return;
}
float itemSpeed = float.Parse(itemName[0]) / 3.6f;
car.MaxSpeed = itemSpeed;
Screen.ShowNotification($"車輛已被限速在 {itemName[0]} {itemName[1]}.");
}
private void AddSaveVehicleItem(UIMenu menu)
{
var newItem = new UIMenuItem("儲存車輛");
menu.AddItem(newItem);
menu.OnItemSelect += (sender, item, index) =>
{
if (item != newItem)
{
return;
}
Vehicle car = LocalPlayer.Character.CurrentVehicle;
if (car == null)
{
Screen.ShowNotification(ERROR_NOCAR);
return;
}
SaveVehicle(car);
Screen.ShowNotification("已成功儲存車輛");
};
}
private void SaveVehicle(Vehicle car)
{
if (savedVehicle != null)
{
foreach (Blip vehBlip in savedVehicle.AttachedBlips)
{
vehBlip.Alpha = 0;
}
}
Blip blip = car.AttachBlip();
blip.Sprite = BlipSprite.PersonalVehicleCar;
savedVehicle = car;
}
public VehicleControls()
{
MenuPool menuPool = new MenuPool();
UIMenu menu = new UIMenu("代客修改此", "");
menuPool.Add(menu);
AddEngineItem(menu);
AddDoorLockItem(menu);
AddOpenDoorItem(menu);
AddLockSpeedItem(menu);
AddSaveVehicleItem(menu);
menu.RefreshIndex();
Tick += new Func<Task>(async delegate
{
await Task.FromResult(0);
menuPool.ProcessMenus();
if (Game.IsControlJustReleased(1, Control.InteractionMenu))
{
menu.Visible = !menu.Visible;
}
});
}
}
}