Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/SampSharp.OpenMp.Entities/SAMP/Components/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace SampSharp.Entities.SAMP;
/// </summary>
public class Actor : WorldEntity
{
private readonly IActor _actor;
private readonly IActorsComponent _actors;

/// <summary>
Expand All @@ -17,20 +16,24 @@ public class Actor : WorldEntity
protected Actor(IActorsComponent actors, IActor actor) : base((IEntity)actor)
{
_actors = actors;
_actor = actor;
Resource = actor;
}

/// <summary>
/// Gets a value indicating whether the open.mp entity counterpart has been destroyed.
/// </summary>
protected bool IsOmpEntityDestroyed => _actor.TryGetExtension<ComponentExtension>()?.IsOmpEntityDestroyed ?? true;
private IActor Resource
{
get
{
ObjectDisposedException.ThrowIf(!IsComponentAlive, typeof(Actor));
return field;
}
}

/// <summary>
/// Gets or sets the facing angle of this actor.
/// </summary>
public virtual float Angle
{
get => float.RadiansToDegrees(MathHelper.GetZAngleFromRotationMatrix(Matrix4x4.CreateFromQuaternion(_actor.GetRotation())));
get => float.RadiansToDegrees(MathHelper.GetZAngleFromRotationMatrix(Matrix4x4.CreateFromQuaternion(Resource.GetRotation())));
set => Rotation = Quaternion.CreateFromAxisAngle(GtaVector.Up, float.DegreesToRadians(value));
}

Expand All @@ -39,26 +42,26 @@ public virtual float Angle
/// </summary>
public virtual int Skin
{
get => _actor.GetSkin();
set => _actor.SetSkin(value);
get => Resource.GetSkin();
set => Resource.SetSkin(value);
}

/// <summary>
/// Gets or sets the health of this actor.
/// </summary>
public virtual float Health
{
get => _actor.GetHealth();
set => _actor.SetHealth(value);
get => Resource.GetHealth();
set => Resource.SetHealth(value);
}

/// <summary>
/// Gets or sets a value indicating whether this actor is invulnerable.
/// </summary>
public virtual bool IsInvulnerable
{
get => _actor.IsInvulnerable();
set => _actor.SetInvulnerable(value);
get => Resource.IsInvulnerable();
set => Resource.SetInvulnerable(value);
}

/// <summary>
Expand All @@ -70,7 +73,7 @@ public virtual bool IsStreamedIn(Player player)
{
ArgumentNullException.ThrowIfNull(player);

return _actor.IsStreamedInForPlayer(player);
return Resource.IsStreamedInForPlayer(player);
}

/// <summary>
Expand All @@ -89,7 +92,7 @@ public virtual void ApplyAnimation(string library, string name, float fDelta, bo
{
ArgumentNullException.ThrowIfNull(library);
ArgumentNullException.ThrowIfNull(name);
_actor.ApplyAnimation(new AnimationData(fDelta, loop, lockX, lockY, freeze, (uint)time.TotalMilliseconds, library, name));
Resource.ApplyAnimation(new AnimationData(fDelta, loop, lockX, lockY, freeze, (uint)time.TotalMilliseconds, library, name));
}

/// <inheritdoc cref="ApplyAnimation(string, string, float, bool, bool, bool, bool, TimeSpan)" />
Expand All @@ -102,29 +105,33 @@ public virtual void ApplyAnimation(string library, string name, float fDelta, bo
/// </summary>
public virtual void ClearAnimations()
{
_actor.ClearAnimations();
Resource.ClearAnimations();
}

/// <inheritdoc />
protected override void OnDestroyComponent()
{
if (!IsOmpEntityDestroyed)
if (!Resource.GetExtension<ComponentExtension>().IsOmpEntityDestroyed)
{
_actors.AsPool().Release(Id);
_actors.AsPool().Release(Resource.GetID());
}
}

/// <inheritdoc />
public override string ToString()
{
return $"(Id: {Id})";
if (!IsComponentAlive)
{
return "(Destroyed)";
}
return $"(Id: {Id}, Health: {Health}, Position: {Position})";
}

/// <summary>
/// Performs an implicit conversion from <see cref="Actor" /> to <see cref="IActor" />.
/// </summary>
public static implicit operator IActor(Actor? actor)
{
return actor?._actor ?? default;
return actor?.Resource ?? default;
}
}
33 changes: 20 additions & 13 deletions src/SampSharp.OpenMp.Entities/SAMP/Components/BaseGangZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace SampSharp.Entities.SAMP;
public abstract class BaseGangZone : IdProvider
{
private readonly IOmpEntityProvider _entityProvider;
private readonly IGangZone _gangZone;
private readonly IGangZonesComponent _gangZones;

/// <summary>
Expand All @@ -19,24 +18,28 @@ public abstract class BaseGangZone : IdProvider
protected BaseGangZone(IOmpEntityProvider entityProvider, IGangZonesComponent gangZones, IGangZone gangZone) : base((IIDProvider)gangZone)
{
_entityProvider = entityProvider;
_gangZone = gangZone;
Resource = gangZone;
_gangZones = gangZones;
}

/// <summary>
/// Gets a value indicating whether the open.mp entity counterpart has been destroyed.
/// </summary>
protected bool IsOmpEntityDestroyed => _gangZone.TryGetExtension<ComponentExtension>()?.IsOmpEntityDestroyed ?? true;
private IGangZone Resource
{
get
{
ObjectDisposedException.ThrowIf(!IsComponentAlive, typeof(BaseGangZone));
return field;
}
}

/// <summary>
/// Gets the minimum position of this gang zone as a <see cref="Vector2" />.
/// </summary>
public virtual Vector2 Min => _gangZone.GetPosition().Min;
public virtual Vector2 Min => Resource.GetPosition().Min;

/// <summary>
/// Gets the maximum position of this gang zone as a <see cref="Vector2" />.
/// </summary>
public virtual Vector2 Max => _gangZone.GetPosition().Max;
public virtual Vector2 Max => Resource.GetPosition().Max;

/// <summary>
/// Gets the minimum x coordinate of this gang zone.
Expand Down Expand Up @@ -69,7 +72,7 @@ protected BaseGangZone(IOmpEntityProvider entityProvider, IGangZonesComponent ga
/// <returns>A lazy sequence of <see cref="Player" /> components.</returns>
public virtual IEnumerable<Player> GetShownFor()
{
foreach (var raw in _gangZone.GetShownFor())
foreach (var raw in Resource.GetShownFor())
{
var component = _entityProvider.GetComponent(raw);
if (component != null)
Expand All @@ -87,13 +90,13 @@ public virtual IEnumerable<Player> GetShownFor()
public virtual void SetPosition(Vector2 min, Vector2 max)
{
var pos = new GangZonePos(min, max);
_gangZone.SetPosition(ref pos);
Resource.SetPosition(ref pos);
}

/// <inheritdoc />
protected override void OnDestroyComponent()
{
if (!IsOmpEntityDestroyed)
if (!Resource.GetExtension<ComponentExtension>().IsOmpEntityDestroyed)
{
_gangZones.AsPool().Release(Id);
}
Expand All @@ -102,14 +105,18 @@ protected override void OnDestroyComponent()
/// <inheritdoc />
public override string ToString()
{
return $"(Id: {Id}, Color: {Color})";
if (!IsComponentAlive)
{
return "(Destroyed)";
}
return $"(Id: {Id}, Color: {Color}, Min: {Min}, Max: {Max})";
}

/// <summary>
/// Performs an implicit conversion from <see cref="BaseGangZone" /> to <see cref="IGangZone" />.
/// </summary>
public static implicit operator IGangZone(BaseGangZone? gangZone)
{
return gangZone?._gangZone ?? default;
return gangZone?.Resource ?? default;
}
}
33 changes: 20 additions & 13 deletions src/SampSharp.OpenMp.Entities/SAMP/Components/BasePickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace SampSharp.Entities.SAMP;
/// </summary>
public abstract class BasePickup : WorldEntity
{
private readonly IPickup _pickup;
private readonly IPickupsComponent _pickups;

/// <summary>
Expand All @@ -18,51 +17,55 @@ public abstract class BasePickup : WorldEntity
protected BasePickup(IPickupsComponent pickups, IPickup pickup) : base((IEntity)pickup)
{
_pickups = pickups;
_pickup = pickup;
Resource = pickup;
}

/// <summary>
/// Gets a value indicating whether the open.mp entity counterpart has been destroyed.
/// </summary>
protected bool IsOmpEntityDestroyed => _pickup.TryGetExtension<ComponentExtension>()?.IsOmpEntityDestroyed ?? true;
private IPickup Resource
{
get
{
ObjectDisposedException.ThrowIf(!IsComponentAlive, typeof(BasePickup));
return field;
}
}

/// <summary>
/// Gets the model of this pickup.
/// </summary>
public virtual int Model => _pickup.GetModel();
public virtual int Model => Resource.GetModel();

/// <summary>
/// Gets the type of this pickup.
/// </summary>
public virtual PickupType SpawnType => (PickupType)_pickup.GetPickupType();
public virtual PickupType SpawnType => (PickupType)Resource.GetPickupType();

/// <summary>Changes the type of this pickup.</summary>
/// <param name="type">The new <see cref="PickupType" />.</param>
/// <param name="update">Whether to update the pickup visually for streamed-in players.</param>
public virtual void SetType(PickupType type, bool update = true)
{
_pickup.SetType((byte)type, update);
Resource.SetType((byte)type, update);
}

/// <summary>Changes the model of this pickup.</summary>
/// <param name="model">The new model ID.</param>
/// <param name="update">Whether to update the pickup visually for streamed-in players.</param>
public virtual void SetModel(int model, bool update = true)
{
_pickup.SetModel(model, update);
Resource.SetModel(model, update);
}

/// <summary>Sets the position of this pickup without sending a visual update.</summary>
/// <param name="position">The new position.</param>
public virtual void SetPositionNoUpdate(Vector3 position)
{
_pickup.SetPositionNoUpdate(position);
Resource.SetPositionNoUpdate(position);
}

/// <inheritdoc />
protected override void OnDestroyComponent()
{
if (!IsOmpEntityDestroyed)
if (!Resource.GetExtension<ComponentExtension>().IsOmpEntityDestroyed)
{
_pickups.AsPool().Release(Id);
}
Expand All @@ -71,6 +74,10 @@ protected override void OnDestroyComponent()
/// <inheritdoc />
public override string ToString()
{
if (!IsComponentAlive)
{
return "(Destroyed)";
}
return $"(Id: {Id}, Model: {Model})";
}

Expand All @@ -79,6 +86,6 @@ public override string ToString()
/// </summary>
public static implicit operator IPickup(BasePickup? pickup)
{
return pickup?._pickup ?? default;
return pickup?.Resource ?? default;
}
}
29 changes: 18 additions & 11 deletions src/SampSharp.OpenMp.Entities/SAMP/Components/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace SampSharp.Entities.SAMP;
/// </summary>
public class Class : IdProvider
{
private readonly IClass _class;
private readonly IClassesComponent _classes;

/// <summary>
Expand All @@ -17,13 +16,17 @@ public class Class : IdProvider
protected Class(IClassesComponent classes, IClass playerClass) : base((IIDProvider)playerClass)
{
_classes = classes;
_class = playerClass;
Resource = playerClass;
}

/// <summary>
/// Gets a value indicating whether the open.mp entity counterpart has been destroyed.
/// </summary>
protected bool IsOmpEntityDestroyed => _class.TryGetExtension<ComponentExtension>()?.IsOmpEntityDestroyed ?? true;
private IClass Resource
{
get
{
ObjectDisposedException.ThrowIf(!IsComponentAlive, typeof(Class));
return field;
}
}

/// <summary>
/// Gets or sets the team ID for this player class.
Expand Down Expand Up @@ -59,7 +62,7 @@ public virtual void SetSpawnData(PlayerSpawnData data)
ArgumentNullException.ThrowIfNull(data);

var omp = data.ToOmpData();
_class.SetClass(ref omp);
Resource.SetClass(ref omp);
}

/// <summary>
Expand All @@ -68,22 +71,26 @@ public virtual void SetSpawnData(PlayerSpawnData data)
/// <returns>A <see cref="PlayerSpawnData"/> instance containing the player's spawn information.</returns>
public virtual PlayerSpawnData GetSpawnData()
{
ref var dat = ref _class.GetClass();
ref var dat = ref Resource.GetClass();
return PlayerSpawnData.FromOmpData(ref dat);
}

/// <inheritdoc />
protected override void OnDestroyComponent()
{
if (!IsOmpEntityDestroyed)
if (!Resource.GetExtension<ComponentExtension>().IsOmpEntityDestroyed)
{
_classes.AsPool().Release(Id);
_classes.AsPool().Release(Resource.GetID());
}
}

/// <inheritdoc />
public override string ToString()
{
if (!IsComponentAlive)
{
return "(Destroyed)";
}
return $"(Id: {Id})";
}

Expand All @@ -92,6 +99,6 @@ public override string ToString()
/// </summary>
public static implicit operator IClass(Class? playerClass)
{
return playerClass?._class ?? default;
return playerClass?.Resource ?? default;
}
}
Loading