RopeCreator.cs 6.73 KB
Newer Older
Jitnaught's avatar
init  
Jitnaught committed
1
2
3
4
5
6
7
8
9
using GTA;
using GTA.Math;
using System;
using System.Windows.Forms;

namespace RopeCreator
{
	public class RopeCreator : Script
	{
10
		internal static RopeGroup[] ropeGroups = { new RopeGroup(), new RopeGroup(), new RopeGroup(), new RopeGroup(), new RopeGroup() };
Jitnaught's avatar
init  
Jitnaught committed
11

Jitnaught's avatar
Jitnaught committed
12
		int nextDeleteBadRopes = 0, nextReattachRopes = 0;
Jitnaught's avatar
init  
Jitnaught committed
13
14
		Vector3 firstPos = Vector3.Zero, firstOffset = Vector3.Zero;
		Entity firstEntity = null;
15
		bool firstAttachBone = false;
Jitnaught's avatar
init  
Jitnaught committed
16
17
18
19
20
21
22
23
24
25

		public RopeCreator()
		{
			INI.GetSettings(Settings);

			Aborted += RopeCreator_Aborted;
			KeyDown += RopeCreator_KeyDown;
			Interval = 1;
			Tick += RopeCreator_Tick;
		}
26

Jitnaught's avatar
init  
Jitnaught committed
27
28
		internal static void DeleteAllRopes()
		{
29
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
30
			{
31
				group.DeleteRopes();
Jitnaught's avatar
init  
Jitnaught committed
32
33
34
35
36
			}
		}

		internal static void DeleteLastRope(bool showSubtitle = false)
		{
37
38
39
			var group = ropeGroups[Menu.liGroupIndex.SelectedIndex];

			if (group.ropes.Count > 0)
Jitnaught's avatar
init  
Jitnaught committed
40
			{
41
42
43
				int lastIndex = group.ropes.Count - 1;
				group.ropes[lastIndex].Delete();
				group.ropes.RemoveAt(lastIndex);
Jitnaught's avatar
init  
Jitnaught committed
44
45
46
47
48
49
50

				UI.ShowSubtitle("Deleted last rope");
			}
		}

		internal static bool AreAllRopesWinding()
		{
51
52
53
			bool oneNotEmpty = false;

			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
54
			{
55
56
57
58
59
				if (group.ropes.Count == 0) continue; //skip empty groups

				oneNotEmpty = true;

				if (!group.AreAllRopesWinding()) return false;
Jitnaught's avatar
init  
Jitnaught committed
60
61
			}

62
63
			//only return true if there is at least one rope
			return oneNotEmpty;
Jitnaught's avatar
init  
Jitnaught committed
64
65
66
67
		}

		internal static bool AreAllRopesUnwinding()
		{
68
			bool oneNotEmpty = false;
Jitnaught's avatar
init  
Jitnaught committed
69

70
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
71
			{
72
73
74
75
76
				if (group.ropes.Count == 0) continue; //skip empty groups

				oneNotEmpty = true;

				if (!group.AreAllRopesUnwinding()) return false;
Jitnaught's avatar
init  
Jitnaught committed
77
78
			}

79
80
			//only return true if there is at least one rope
			return oneNotEmpty;
Jitnaught's avatar
init  
Jitnaught committed
81
82
83
84
		}

		internal static void StartWindAllRopes()
		{
85
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
86
			{
87
				group.StartWindRopes();
Jitnaught's avatar
init  
Jitnaught committed
88
89
90
91
92
			}
		}

		internal static void StartUnwindAllRopes()
		{
93
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
94
			{
95
				group.StartUnwindRopes();
Jitnaught's avatar
init  
Jitnaught committed
96
97
98
99
100
			}
		}

		internal static void StopWindAllRopes()
		{
101
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
102
			{
103
				group.StopWindRopes();
Jitnaught's avatar
init  
Jitnaught committed
104
105
106
107
108
			}
		}

		internal static void StopUnwindAllRopes()
		{
109
			foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
110
			{
111
				group.StopUnwindRopes();
Jitnaught's avatar
init  
Jitnaught committed
112
113
114
115
116
			}
		}

		private void DeleteRopesWithBadEntity()
		{
Jitnaught's avatar
Jitnaught committed
117
			if (Game.GameTime >= nextDeleteBadRopes)
Jitnaught's avatar
init  
Jitnaught committed
118
			{
119
120
				bool deletedOne = false;

121
				foreach (var group in ropeGroups)
Jitnaught's avatar
init  
Jitnaught committed
122
				{
123
124
125
126
					if (group.DeleteRopesWithBadEntity())
					{
						deletedOne = true;
					}
Jitnaught's avatar
Jitnaught committed
127

128
					Yield();
Jitnaught's avatar
init  
Jitnaught committed
129
130
				}

131
132
				if (deletedOne) Menu.ReloadRopeIndices();

Jitnaught's avatar
init  
Jitnaught committed
133
134
135
136
				nextDeleteBadRopes = Game.GameTime + 1000;
			}
		}

Jitnaught's avatar
Jitnaught committed
137
138
139
140
		private void ReattachRagdollPeds()
		{
			if (Game.GameTime >= nextReattachRopes)
			{
141
				foreach (var group in ropeGroups)
Jitnaught's avatar
Jitnaught committed
142
				{
143
					group.ReattachRagdollPeds();
Jitnaught's avatar
Jitnaught committed
144
145
146
147
148
149
				}

				nextReattachRopes = Game.GameTime + 500;
			}
		}

Jitnaught's avatar
init  
Jitnaught committed
150
151
152
153
154
155
		private void AttachRope()
		{
			if (!INI.onlyCreateRopeWhenAiming || Helper.IsAiming())
			{
				var ray = Helper.CreateRaycastFromCam();

Jitnaught's avatar
Jitnaught committed
156
				if (Menu.attachNothing || ray.DitHitAnything)
Jitnaught's avatar
init  
Jitnaught committed
157
158
159
				{
					if (firstPos == Vector3.Zero)
					{
Jitnaught's avatar
Jitnaught committed
160
161
162
						if (!ray.DitHitAnything) //attach to nothing
						{
							Camera cam = World.RenderingCamera;
Jitnaught's avatar
init  
Jitnaught committed
163

Jitnaught's avatar
Jitnaught committed
164
165
166
167
							if (cam != null && cam.Exists()) firstPos = cam.GetOffsetInWorldCoords(INI.camOffset);
							else firstPos = GameplayCamera.GetOffsetInWorldCoords(INI.camOffset);
						}
						else
Jitnaught's avatar
init  
Jitnaught committed
168
						{
Jitnaught's avatar
Jitnaught committed
169
170
171
172
173
174
175
176
							firstPos = ray.HitCoords;

							if (ray.DitHitEntity && ray.HitEntity != null && ray.HitEntity.Exists())
							{
								firstEntity = ray.HitEntity;
								firstOffset = firstEntity.GetOffsetFromWorldCoords(firstPos);
								firstAttachBone = !firstEntity.Model.IsVehicle && ((Menu.attachPedBone && firstEntity.Model.IsPed) || (Menu.attachObjBone && !firstEntity.Model.IsPed));
							}
Jitnaught's avatar
init  
Jitnaught committed
177
178
179
180
181
182
						}

						UI.ShowSubtitle("First position");
					}
					else
					{
Jitnaught's avatar
Jitnaught committed
183
						Vector3 secondPos;
Jitnaught's avatar
init  
Jitnaught committed
184
						Entity secondEntity = null;
185
186
						bool secondAttachBone = false;

Jitnaught's avatar
Jitnaught committed
187
						if (!ray.DitHitAnything) //attach to nothing
188
						{
Jitnaught's avatar
Jitnaught committed
189
							Camera cam = World.RenderingCamera;
190

Jitnaught's avatar
Jitnaught committed
191
192
193
194
195
196
197
198
							if (cam != null && cam.Exists()) secondPos = cam.GetOffsetInWorldCoords(INI.camOffset);
							else secondPos = GameplayCamera.GetOffsetInWorldCoords(INI.camOffset);
						}
						else
						{
							secondPos = ray.HitCoords;

							if (ray.DitHitEntity)
199
							{
Jitnaught's avatar
Jitnaught committed
200
201
202
203
204
205
								secondEntity = ray.HitEntity;

								if (secondEntity != null && secondEntity.Exists())
								{
									secondAttachBone = !secondEntity.Model.IsVehicle && ((Menu.attachPedBone && secondEntity.Model.IsPed) || (Menu.attachObjBone && !secondEntity.Model.IsPed));
								}
206
207
							}
						}
Jitnaught's avatar
init  
Jitnaught committed
208
209
210
211
212
213

						if (firstEntity != null && firstEntity.Exists())
						{
							firstPos = firstEntity.GetOffsetInWorldCoords(firstOffset);
						}

214
						var rope = new AttachedRope(firstEntity, firstPos, firstAttachBone, secondEntity, secondPos, secondAttachBone);
Jitnaught's avatar
init  
Jitnaught committed
215

216
						var group = ropeGroups[Menu.liGroupIndex.SelectedIndex];
Jitnaught's avatar
init  
Jitnaught committed
217

218
219
220
						group.ropes.Add(rope);

						if (group.ropes.Count > INI.maxRopes)
Jitnaught's avatar
init  
Jitnaught committed
221
						{
222
223
							group.ropes[0].Delete();
							group.ropes.RemoveAt(0);
Jitnaught's avatar
init  
Jitnaught committed
224
225
						}

226
						Menu.ReloadRopeIndices(true); //true = select last item
Jitnaught's avatar
init  
Jitnaught committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

						firstPos = Vector3.Zero;
						firstEntity = null;

						UI.ShowSubtitle("Created rope");
					}
				}
				//else UI.ShowSubtitle("Didn't hit anything");
			}
		}

		private void HandleControls()
		{
			if (Game.CurrentInputMode == InputMode.GamePad)
			{
				if (Helper.AreControlsJustPressed(INI.menuToggleControls))
				{
					Menu.Toggle();
				}

				if (Menu.modEnabled)
				{
					if (Helper.AreControlsJustPressed(INI.removeLastControls))
					{
251
252
						var group = ropeGroups[Menu.liGroupIndex.SelectedIndex];

Jitnaught's avatar
init  
Jitnaught committed
253
						DeleteLastRope(true);
254
						Menu.ReloadRopeIndices();
Jitnaught's avatar
init  
Jitnaught committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
					}
					else if (Game.IsControlJustPressed(0, INI.attachControl))
					{
						AttachRope();
					}
				}
			}
		}

		private void RopeCreator_Aborted(object sender, EventArgs e)
		{
			DeleteAllRopes();
		}

		private void RopeCreator_Tick(object sender, EventArgs e)
		{
			DeleteRopesWithBadEntity();
Jitnaught's avatar
Jitnaught committed
272
			ReattachRagdollPeds();
Jitnaught's avatar
init  
Jitnaught committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
			HandleControls();
		}

		private void RopeCreator_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == INI.menuToggleKey)
			{
				Menu.Toggle();
			}

			if (Menu.modEnabled)
			{
				if (e.KeyCode == INI.attachKey)
				{
					AttachRope();
				}
				else if (e.KeyCode == INI.removeLastKey)
				{
291
292
					var group = ropeGroups[Menu.liGroupIndex.SelectedIndex];

Jitnaught's avatar
init  
Jitnaught committed
293
					DeleteLastRope(true);
294
					Menu.ReloadRopeIndices();
Jitnaught's avatar
init  
Jitnaught committed
295
296
297
298
299
				}
			}
		}
	}
}