Guest User

Untitled

a guest
Dec 31st, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. using System;
  2. using GTA;
  3. using GTA.Native;
  4. using GTA.Math;
  5.  
  6. namespace Afterburners
  7. {
  8. public class Afterburners : Script
  9. {
  10. #region Constant Vars
  11. /// <summary>
  12. /// Scale of afterburners.
  13. /// </summary>
  14. public static readonly float Scale = 0.1f;
  15.  
  16. /// <summary>
  17. /// Left afterburner relative offset.
  18. /// </summary>
  19. public static readonly Vector3 LeftOffset = new Vector3(-0.0f, -0f, 0.0f);
  20.  
  21. /// <summary>
  22. /// Right afterburner relative offset.
  23. /// </summary>
  24. public static readonly Vector3 RightOffset = new Vector3(0.0f, -0f, 0.0f);
  25.  
  26. /// <summary>
  27. /// Vehicles with afterburner support.
  28. /// </summary>
  29. public readonly VehicleHash[] Vehicles = new VehicleHash[] {
  30. (VehicleHash)0xF93D230C
  31. };
  32.  
  33. #endregion
  34.  
  35. /// <summary>
  36. /// Player vehicle
  37. /// </summary>
  38. private PlayerVehicle mainVehicle;
  39.  
  40. /// <summary>
  41. /// Initialize the script
  42. /// </summary>
  43. public Afterburners()
  44. {
  45. Tick += OnTick;
  46. }
  47.  
  48. /// <summary>
  49. /// Tick event
  50. /// </summary>
  51. private void OnTick(object sender, EventArgs e)
  52. {
  53. var player = Game.Player.Character;
  54.  
  55. if (mainVehicle != null && !player.IsInVehicle(mainVehicle.Vehicle))
  56. {
  57. mainVehicle.KillAfterburner();
  58. mainVehicle = default(PlayerVehicle);
  59. }
  60.  
  61. if (player.IsInVehicle())
  62. {
  63. if (mainVehicle != null && player.CurrentVehicle.IsDriveable)
  64. mainVehicle.Update();
  65.  
  66. foreach (var vehicle in Vehicles)
  67. {
  68. if (player.CurrentVehicle.Model == vehicle && (mainVehicle == null || mainVehicle.Handle != player.CurrentVehicle.Handle))
  69. {
  70. mainVehicle = new PlayerVehicle(player.CurrentVehicle);
  71. mainVehicle.InitializeAfterburner();
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. public class PlayerVehicle : Entity
  79. {
  80. private readonly Vehicle _vehicle;
  81. private LoopedPTFX afterburnerL = new LoopedPTFX("core", "veh_exhaust_afterburner");
  82. private LoopedPTFX afterburnerR = new LoopedPTFX("core", "veh_exhaust_afterburner");
  83.  
  84. public Vehicle Vehicle { get { return _vehicle; } }
  85.  
  86. /// <summary>
  87. /// Initialize the class
  88. /// </summary>
  89. /// <param name="vehicle"></param>
  90. public PlayerVehicle(Vehicle vehicle) : base(vehicle.Handle)
  91. {
  92. _vehicle = vehicle;
  93. }
  94.  
  95. /// <summary>
  96. /// Initialize afterburners on entitys exhaust dummies
  97. /// </summary>
  98. public void InitializeAfterburner()
  99. {
  100. if (!afterburnerL.IsLoaded)
  101. afterburnerL.Load();
  102.  
  103. afterburnerL.Start(_vehicle, Afterburners.Scale, Afterburners.LeftOffset, Vector3.Zero, (Bone)Function.Call<int>(Hash._GET_ENTITY_BONE_INDEX, _vehicle, "exhaust"));
  104. afterburnerR.Start(_vehicle, Afterburners.Scale, Afterburners.RightOffset, Vector3.Zero, (Bone)Function.Call<int>(Hash._GET_ENTITY_BONE_INDEX, _vehicle, "exhaust_2"));
  105. Function.Call((Hash)0xDCB194B85EF7B541, afterburnerL.Handle, 3000.0f);
  106. Function.Call((Hash)0xDCB194B85EF7B541, afterburnerR.Handle, 3000.0f);
  107. }
  108.  
  109. /// <summary>
  110. /// Kill the afterburner FX
  111. /// </summary>
  112. public void KillAfterburner()
  113. {
  114. afterburnerL.Remove();
  115. afterburnerR.Remove();
  116. }
  117.  
  118. /// <summary>
  119. /// Update FX based on acceleration
  120. /// </summary>
  121. public void Update()
  122. {
  123. Function.Call(Hash.SET_PARTICLE_FX_LOOPED_EVOLUTION, afterburnerL.Handle, "throttle", _vehicle.Acceleration, 0);
  124. Function.Call(Hash.SET_PARTICLE_FX_LOOPED_EVOLUTION, afterburnerR.Handle, "throttle", _vehicle.Acceleration, 0);
  125. }
  126. }
  127.  
  128. public class LoopedPTFX
  129. {
  130. public int Handle { get; private set; }
  131. public string AssetName { get; private set; }
  132. public string FXName { get; private set; }
  133.  
  134. /// <summary>
  135. /// If the particle FX is spawned.
  136. /// </summary>
  137. public bool Exists { get { if (Handle == -1) return false; return Function.Call<bool>(Hash.DOES_PARTICLE_FX_LOOPED_EXIST, Handle); } }
  138.  
  139. /// <summary>
  140. /// If the particle FX asset is loaded.
  141. /// </summary>
  142. public bool IsLoaded { get { return Function.Call<bool>(Hash.HAS_NAMED_PTFX_ASSET_LOADED, AssetName); } }
  143.  
  144. /// <summary>
  145. /// Initialize the class
  146. /// </summary>
  147. /// <param name="assetName"></param>
  148. /// <param name="fxName"></param>
  149. public LoopedPTFX(string assetName, string fxName)
  150. {
  151. this.Handle = -1;
  152. this.AssetName = assetName;
  153. this.FXName = fxName;
  154. }
  155.  
  156. /// <summary>
  157. /// Load the particle FX asset
  158. /// </summary>
  159. public void Load()
  160. {
  161. Function.Call(Hash.REQUEST_NAMED_PTFX_ASSET, AssetName);
  162. while (!Function.Call<bool>(Hash.HAS_NAMED_PTFX_ASSET_LOADED))
  163. Script.Wait(0);
  164. }
  165.  
  166. /// <summary>
  167. /// Start particle FX on the specified entity.
  168. /// </summary>
  169. /// <param name="entity">Entity to attach to.</param>
  170. /// <param name="scale">Scale of the fx.</param>
  171. /// <param name="offset">Optional offset.</param>
  172. /// <param name="rotation">Optional rotation.</param>
  173. /// <param name="bone">Entity bone.</param>
  174. public void Start(Entity entity, float scale, Vector3 offset, Vector3 rotation, Bone? bone)
  175. {
  176. if (Handle != -1) return;
  177.  
  178. Function.Call(Hash._SET_PTFX_ASSET_NEXT_CALL, AssetName);
  179.  
  180. Handle = bone == null ?
  181. Function.Call<int>(Hash.START_PARTICLE_FX_LOOPED_ON_ENTITY, FXName,
  182. entity, offset.X, offset.Y, offset.Z, rotation.X, rotation.Y, rotation.Z, scale, 0, 0, 1) :
  183. Function.Call<int>(Hash._START_PARTICLE_FX_LOOPED_ON_ENTITY_BONE, FXName,
  184. entity, offset.X, offset.Y, offset.Z, rotation.X, rotation.Y, rotation.Z, (int)bone, scale, 0, 0, 0);
  185. }
  186.  
  187. /// <summary>
  188. /// Start particle FX on the specified entity.
  189. /// </summary>
  190. /// <param name="entity">Entity to attach to.</param>
  191. /// <param name="scale">Scale of the fx.</param>
  192. public void Start(Entity entity, float scale)
  193. {
  194. Start(entity, scale, Vector3.Zero, Vector3.Zero, null);
  195. }
  196.  
  197. /// <summary>
  198. /// Start particle FX at the specified position.
  199. /// </summary>
  200. /// <param name="position">Position in world space.</param>
  201. /// <param name="scale">Scale of the fx.</param>
  202. /// <param name="rotation">Optional rotation.</param>
  203. public void Start(Vector3 position, float scale, Vector3 rotation)
  204. {
  205. if (Handle != -1) return;
  206.  
  207. Function.Call(Hash._SET_PTFX_ASSET_NEXT_CALL, AssetName);
  208.  
  209. Handle = Function.Call<int>(Hash.START_PARTICLE_FX_LOOPED_AT_COORD, FXName,
  210. position.X, position.Y, position.Z, rotation.X, rotation.Y, rotation.Z, scale, 0, 0, 0, 0);
  211. }
  212.  
  213. /// <summary>
  214. /// Start particle FX at the specified position.
  215. /// </summary>
  216. /// <param name="position">Position in world space.</param>
  217. /// <param name="scale">Scale of the fx.</param>
  218. public void Start(Vector3 position, float scale)
  219. {
  220. Start(position, scale, Vector3.Zero);
  221. }
  222.  
  223. /// <summary>
  224. /// Remove the particle FX
  225. /// </summary>
  226. public void Remove()
  227. {
  228. if (Handle == -1) return;
  229.  
  230. Function.Call(Hash.REMOVE_PARTICLE_FX, Handle, 0);
  231. Handle = -1;
  232. }
  233.  
  234. /// <summary>
  235. /// Remove the particle FX in range
  236. /// </summary>
  237. public void Remove(Vector3 position, float radius)
  238. {
  239. if (Handle == -1) return;
  240.  
  241. Function.Call(Hash.REMOVE_PARTICLE_FX_IN_RANGE, position.X, position.Y, position.Z, radius);
  242. Handle = -1;
  243. }
  244.  
  245. /// <summary>
  246. /// Unload the loaded particle FX asset
  247. /// </summary>
  248. public void Unload()
  249. {
  250. if (IsLoaded)
  251. Function.Call((Hash)0x5F61EBBE1A00F96D, AssetName);
  252. }
  253. }
  254. }
Add Comment
Please, Sign In to add comment