Skip to content

Commit

Permalink
-add utils, changed modules , changed factory , added tuning vehicle …
Browse files Browse the repository at this point in the history
…methods
  • Loading branch information
Kaiwoknats committed Nov 20, 2023
1 parent 0b26d95 commit 3bc6e6f
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/AltV.Atlas.Vehicles.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
66 changes: 64 additions & 2 deletions src/Factories/Entities/AtlasTuningVehicle.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
using AltV.Net;
using System.Drawing;
using AltV.Atlas.Vehicles.Enums;
using AltV.Atlas.Vehicles.Models;
using AltV.Atlas.Vehicles.Utils;
using AltV.Net;
using AltV.Net.Data;
using AltV.Net.Enums;
namespace AltV.Atlas.Vehicles.Factories.Entities;

public class AtlasTuningVehicle : AtlasVehicle
{
public List<VehicleMod> InstalledMods { get; } = new( );
public EColorMaterial PrimaryColorMaterial { get; private set; } = EColorMaterial.DefaultAlloy;
public EColorMaterial SecondaryColorMaterial { get; private set; } = EColorMaterial.DefaultAlloy;

public AtlasTuningVehicle( ICore core, IntPtr nativePointer, uint id ) : base( core, nativePointer, id )
{
}
public AtlasTuningVehicle( uint vehicleId, ICore core, IntPtr nativePointer, uint id ) : base( vehicleId, core, nativePointer, id )

public void SetPrimaryMaterial( EColorMaterial material )
{
var oldColor = PrimaryColorRgb;
PrimaryColor = ( byte ) material;
PrimaryColorRgb = oldColor;
}

public void SetSecondaryMaterial( EColorMaterial material )
{
var oldColor = PrimaryColorRgb;
PrimaryColor = ( byte ) material;
PrimaryColorRgb = oldColor;
}

public void SetPrimaryRgb( string rgb )
{
if( !rgb.TryParseRgb( out var color ) )
return;

PrimaryColorRgb = new Rgba( color.R, color.G, color.B, color.A );
}

public void SetSecondaryRgb( string rgb )
{
if( !rgb.TryParseRgb( out var color ) )
return;

SecondaryColorRgb = new Rgba( color.R, color.G, color.B, color.A );
}

public bool InstallMod( VehicleMod vehicleMod, bool force = false )
{
var maxIndex = GetModsCount( ( byte ) vehicleMod.ModType );

if( vehicleMod.Index > maxIndex && !force )
{
return false;
}

var result = SetMod( ( byte ) vehicleMod.ModType, ( byte ) vehicleMod.Index );
InstalledMods.Add( vehicleMod );
return result;
}

public IEnumerable<( VehicleMod mod, bool result )> InstallMods( IEnumerable<VehicleMod> vehicleMods )
{
return ( from vehicleMod in vehicleMods let result = InstallMod( vehicleMod ) select ( vehicleMod, result ) ).ToList( );
}

public bool RemoveMod( VehicleModType vehicleModType )
{
var result = SetMod( ( byte ) vehicleModType, 0 );
InstalledMods.RemoveAll( x => x.ModType == vehicleModType );
return result;
}
}
13 changes: 4 additions & 9 deletions src/Factories/Entities/AtlasVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@
using AltV.Net.Elements.Entities;
namespace AltV.Atlas.Vehicles.Factories.Entities;

public class AtlasVehicle : AsyncVehicle , IAtlasVehicle
public class AtlasVehicle : AsyncVehicle, IAtlasVehicle
{
public uint Fuel => CalculateFuel( );
public uint VehicleId { get; private set; }
public uint VehicleId { get; set; }

public AtlasVehicle( ICore core, IntPtr nativePointer, uint id ) : base( core, nativePointer, id )
{
}

public AtlasVehicle( uint vehicleId, ICore core, IntPtr nativePointer, uint id ) : base( core, nativePointer, id )
{
VehicleId = vehicleId;
}

public void SpawnVehicle( uint model, Position pos, Rotation rot )
{
throw new NotImplementedException( );
}

public void WarpOutOfVehicle( IPlayer player )
{
var isInVehicle = Passengers.Any( s => s.Player == player );
Expand All @@ -42,7 +37,7 @@ public bool LockVehicle( IPlayer player )
{
return false;
}

private uint CalculateFuel( )
{
return 1;
Expand Down
30 changes: 26 additions & 4 deletions src/Factories/EntityFactories/AtlasVehicleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,36 @@ public AtlasVehicleFactory( ILogger<AtlasVehicleFactory> logger )
/// <summary>
/// Create a new vehicle of type T
/// </summary>
/// <param name="vehicleId"></param>
/// <param name="model">The model of the vehicle</param>
/// <param name="position">The position to spawn the vehicle at</param>
/// <param name="rotation">The rotation to spawn the vehicle at</param>
/// <typeparam name="T">Type of the ped, by default can be IAtlasVehicle</typeparam>
/// <returns>A new vehicle of type T</returns>
public async Task<T> CreateVehicleAsync<T>( uint model, Position position, Rotation rotation ) where T : IAtlasVehicle
public async Task<T> CreateVehicleAsync<T>( uint vehicleId, uint model, Position position, Rotation rotation ) where T : IAtlasVehicle
{
return ( T ) await AltAsync.CreateVehicle( model, position, rotation );
var veh = await AltAsync.CreateVehicle( model, position, rotation );

var vehicle = ( T ) veh;
vehicle.VehicleId = vehicleId;

return vehicle;
}

public async Task<T> CreateVehicleAsync<T>( VehicleModel model, Position position, Rotation rotation ) where T : IAtlasVehicle
public async Task<T> CreateVehicleAsync<T>( uint vehicleId, string model, Position position, Rotation rotation ) where T : IAtlasVehicle
{
return ( T ) await AltAsync.CreateVehicle( model, position, rotation );
var vehicle = ( T ) await AltAsync.CreateVehicle( model, position, rotation );
vehicle.VehicleId = vehicleId;

return vehicle;
}

public async Task<T> CreateVehicleAsync<T>( uint vehicleId, VehicleModel model, Position position, Rotation rotation ) where T : IAtlasVehicle
{
var vehicle = ( T ) await AltAsync.CreateVehicle( model, position, rotation );
vehicle.VehicleId = vehicleId;

return vehicle;
}

/// <summary>
Expand All @@ -51,6 +68,11 @@ public async Task<IAtlasVehicle> CreateVehicleAsync( uint model, Position positi
return ( IAtlasVehicle ) await AltAsync.CreateVehicle( model, position, rotation );
}

public async Task<IAtlasVehicle> CreateVehicleAsync( string model, Position position, Rotation rotation )
{
return ( IAtlasVehicle ) await AltAsync.CreateVehicle( model, position, rotation );
}

public async Task<IAtlasVehicle> CreateVehicleAsync( VehicleModel model, Position position, Rotation rotation )
{
return ( IAtlasVehicle ) await AltAsync.CreateVehicle( model, position, rotation );
Expand Down
3 changes: 1 addition & 2 deletions src/Interfaces/IAtlasVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace AltV.Atlas.Vehicles.Interfaces;
public interface IAtlasVehicle : IVehicle
{
uint Fuel { get; }
uint VehicleId { get; }

uint VehicleId { get; set; }
void SpawnVehicle( uint model, Position pos, Rotation rot );
void WarpOutOfVehicle( IPlayer player );
bool LockVehicle( object item );
Expand Down
25 changes: 25 additions & 0 deletions src/Models/VehicleMod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using AltV.Net.Enums;
namespace AltV.Atlas.Vehicles.Models;

public class VehicleMod
{
/// <summary>
/// Constructor to create a VehicleMod
/// </summary>
/// <param name="vehicleModType">The type of the mod</param>
/// <param name="index">The index of the mod</param>
public VehicleMod( VehicleModType vehicleModType, uint index )
{
ModType = vehicleModType;
Index = index;
}
/// <summary>
/// Type of the vehicle mod
/// </summary>
public VehicleModType ModType { get; set; }

/// <summary>
/// Index of the vehicle mod
/// </summary>
public uint Index { get; set; }
}
27 changes: 27 additions & 0 deletions src/Utils/Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Drawing;
namespace AltV.Atlas.Vehicles.Utils;

public static class Utility
{
/// <summary>
/// Tries to parse a string to a rgb color
/// </summary>
/// <param name="rgb">The rgb value</param>
/// <param name="color">The color value</param>
/// <returns>Returns if the parsing was successful</returns>
public static bool TryParseRgb( this string rgb, out Color color )
{
color = Color.Empty;

try
{
color = ColorTranslator.FromHtml( rgb );
return true;
}
catch( Exception ex )
{
Console.WriteLine( $"Error parsing color string: {ex.Message}" );
return false;
}
}
}
16 changes: 16 additions & 0 deletions src/VehicleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public static IServiceCollection RegisterVehicleModule( this IServiceCollection

return serviceCollection;
}

/// <summary>
/// Registers the vehicle module and it's classes/interfaces with a specific AtlasVehicleFactory Type T
/// </summary>
/// <param name="serviceCollection">A service collection</param>
/// <returns>The service collection</returns>
public static IServiceCollection RegisterVehicleModule<T>( this IServiceCollection serviceCollection ) where T : AtlasVehicleFactory
{
serviceCollection.AddTransient<IAtlasVehicle, AtlasVehicle>( );
serviceCollection.AddTransient<T>( );
serviceCollection.AddTransient<IVehicle, AsyncVehicle>( );

serviceCollection.AddTransient<IEntityFactory<IVehicle>, AltVehicleFactory>( );

return serviceCollection;
}
}

0 comments on commit 3bc6e6f

Please sign in to comment.