using System.Collections;
namespace SAMP.API
{
public class Vehicle : Core.IWorldEntity, Core.ICountable
{
#region Class Variables
private int
_nId = -1,
_nModel,
_nColor1,
_nColor2,
_nRespawn;
private float _fAngle = 0f;
private Vector3 _qPosition = Vector3.Zero;
internal static Hashtable vehicleStore = new Hashtable();
#endregion
#region Constructor
///
/// Creates a new SAMP.API.Vehicle to be spawned with World.SpawnVehicle.
///
/// The model for the vehicle.
/// The primary color ID.
/// The secondary color ID.
/// The delay until the car is respawned without a driver in seconds.
public Vehicle(int modelid, int color1, int color2, int respawn_delay)
{
_nModel = modelid;
_nColor1 = color1;
_nColor2 = color2;
_nRespawn = respawn_delay;
}
#endregion
#region Events, Delegates and Method Calls
#region OnSpawn
public delegate void OnSpawnHandler(object sender, System.EventArgs e);
public static event OnSpawnHandler OnSpawn;
internal void _OnSpawn()
{
try { OnSpawn(this, null); }
catch { }
}
#endregion
#region OnDeath
public delegate void OnDeathHandler(object sender, System.EventArgs e);
public static event OnDeathHandler OnDeath;
internal void _OnDeath(int killerid)
{
EventData.VehicleDeathEventArgs args = new EventData.VehicleDeathEventArgs(Player.Get(killerid));
try { OnDeath(this, args); }
catch { }
}
#endregion
#region OnStreamIn
public delegate void OnStreamInHandler(object sender, EventData.VehicleStreamEventArgs e);
public static event OnStreamInHandler OnStreamIn;
internal void _OnStreamIn(int forplayerid)
{
EventData.VehicleStreamEventArgs args = new EventData.VehicleStreamEventArgs(Player.Get(forplayerid));
try { OnStreamIn(this, args); }
catch { }
}
#endregion
#region OnStreamOut
public delegate void OnStreamOutHandler(object sender, EventData.VehicleStreamEventArgs e);
public static event OnStreamOutHandler OnStreamOut;
internal void _OnStreamOut(int forplayerid)
{
EventData.VehicleStreamEventArgs args = new EventData.VehicleStreamEventArgs(Player.Get(forplayerid));
try { OnStreamOut(this, args); }
catch { }
}
#endregion
#endregion
#region Accessors & Mutators
///
/// Gets the model ID of this SAMP.API.Vehicle.
///
public int Model
{
get
{
if(!this.ID.Equals(-1))
_nModel = Core.Natives.GetVehicleModel(this.ID);
return _nModel;
}
}
///
/// Gets the primary color of this SAMP.API.Vehicle.
///
public int PrimaryColor
{
get { return _nColor1; }
set
{
_nColor1 = value;
if(!this.ID.Equals(-1))
Core.Natives.ChangeVehicleColor(this.ID, value, this.SecondaryColor);
}
}
///
/// Gets the secondary color of this SAMP.API.Vehicle.
///
public int SecondaryColor
{
get { return _nColor2; }
set
{
_nColor2 = value;
if(!this.ID.Equals(-1))
Core.Natives.ChangeVehicleColor(this.ID, this.PrimaryColor, value);
}
}
///
/// Gets the respawn delay of this SAMP.API.Vehicle.
///
public int RespawnDelay { get { return _nRespawn; } }
///
/// Gets the vehicle ID.
///
public int ID { get { return _nId; } internal set { _nId = value; } }
///
/// Gets or sets the vehicle's facing angle.
///
public float Angle
{
get
{
if(!this.ID.Equals(-1))
Core.Natives.GetVehicleZAngle(this.ID, ref _fAngle);
return _fAngle;
}
set
{
_fAngle = value;
if(!this.ID.Equals(-1))
Core.Natives.SetVehicleZAngle(_nId, value);
}
}
///
/// Gets or sets the vehicle's health.
///
public float Health
{
get {
float health = 0f;
if(!this.ID.Equals(-1))
Core.Natives.GetVehicleHealth(this.ID, ref health);
return health; }
set
{
if(!this.ID.Equals(-1))
Core.Natives.SetVehicleHealth(this.ID, value);
}
}
///
/// Gets whether the vehicle is occupied by a player.
///
public bool Occupied
{
get
{
foreach(Player player in Player.All)
{
if(player.IsInVehicle(this))
return true;
}
return false;
}
}
///
/// Gets or sets the vehicle's position.
///
public Vector3 Position
{
get
{
float
x = 0f,
y = 0f,
z = 0f;
if(!this.ID.Equals(-1))
Core.Natives.GetVehiclePos(this.ID, ref x, ref y, ref z);
return new Vector3(x, y, z);
}
set
{
_qPosition = value;
if(!this.ID.Equals(-1))
Core.Natives.SetVehiclePos(this.ID, value.X, value.Y, value.Z);
}
}
///
/// Gets or sets the trailer attatched to this vehicle.
///
public Vehicle Trailer
{
get
{
if(!this.ID.Equals(-1))
return Vehicle.Get(Core.Natives.GetVehicleTrailer(this.ID));
else
return null;
}
set
{
if(!this.ID.Equals(-1))
if(value == null)
Core.Natives.DetachTrailerFromVehicle(this.ID);
else
Core.Natives.AttachTrailerToVehicle(value.ID, this.ID);
}
}
#endregion
#region Public Methods
internal static Vehicle Get(int vehicleid)
{
// Iterate through the vehicle collection store
foreach(DictionaryEntry d in vehicleStore)
{
if(d.Key.Equals(vehicleid))
return (Vehicle)d.Value;
}
// The vehicle doesn't exist.
return null;
}
///
/// Adds a component to the vehicle.
///
/// The componentid that needs to be added to the vehicle.
/// Important Note: Using an invalid componentid crashes the game.
public void AddComponent(int componentid)
{
Core.Natives.AddVehicleComponent(this.ID, componentid);
}
///
/// Destroys the vehicle.
///
public void Destroy()
{
Core.Natives.DestroyVehicle(this.ID);
vehicleStore.Remove(this.ID);
}
///
/// Determines the installed component from a vehicle in a specific slot.
///
/// The component slot to check for components.
/// Returns the ID of the component installed in the specified slot.
public int GetComponentInSlot(int slot)
{
return Core.Natives.GetVehicleComponentInSlot(this.ID, slot);
}
///
/// Determines what type of component (the slot) of a component id Find out what type of component a certain ID is.
///
/// The component ID to check.
/// Returns the component slot of the specified component.
public static ComponentSlot GetComponentType(int component)
{
return (ComponentSlot)Core.Natives.GetVehicleComponentType(component);
}
///
/// Removes a component from the vehicle.
///
/// The componentid that needs to be removed from the vehicle.
public void RemoveComponent(int componentid)
{
Core.Natives.RemoveVehicleComponent(this.ID, componentid);
}
#endregion
};
};