Skip to content

Commit

Permalink
[ECS] Added Component.IsComponentAlive
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkentim committed Jan 25, 2020
1 parent 3689c48 commit b1aa74c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
42 changes: 42 additions & 0 deletions src/SampSharp.Entities/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public abstract class Component
/// </summary>
public EntityId Entity { get; internal set; }

/// <summary>
/// Gets a value indicating whether this component is alive (has not been destroyed).
/// </summary>
public bool IsComponentAlive { get; private set; } = true;

/// <summary>
/// Gets a component of the specified type <typeparamref name="T" /> attached to the entity.
/// </summary>
Expand Down Expand Up @@ -163,6 +168,43 @@ internal void InitializeComponent()
internal void DestroyComponent()
{
OnDestroyComponent();
IsComponentAlive = false;
}

/// <summary>
/// Implements the operator true. Returns <c>true</c> if the specified <paramref name="component"/> is alive.
/// </summary>
/// <param name="component">The component.</param>
/// <returns>
/// <c>true</c> if the specified <paramref name="component"/> is alive; <c>false</c> otherwise.
/// </returns>
public static bool operator true(Component component)
{
return component != null && component.IsComponentAlive;
}

/// <summary>
/// Implements the operator false. Returns <c>true</c> if the specified <paramref name="component"/> is not alive.
/// </summary>
/// <param name="component">The component.</param>
/// <returns>
/// <c>true</c> if the specified <paramref name="component"/> is not alive; <c>false</c> otherwise.
/// </returns>
public static bool operator false(Component component)
{
return component == null || !component.IsComponentAlive;
}

/// <summary>
/// Implements the operator !. Returns <c>true</c> if the specified <paramref name="component" /> is not alive.
/// </summary>
/// <param name="component">The component.</param>
/// <returns>
/// <c>true</c> if the specified <paramref name="component" /> is not alive; otherwise <c>false</c>.
/// </returns>
public static bool operator !(Component component)
{
return component == null || !component.IsComponentAlive;
}
}
}
6 changes: 0 additions & 6 deletions src/SampSharp.Entities/SAMP/Components/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,6 @@ public EntityId Menu
}
}

/// <summary>
/// Gets whether this player is connected to the server.
/// </summary>
public bool IsConnected => GetComponent<NativePlayer>().IsPlayerConnected();


/// <summary>
/// Gets or sets the rotation of this player.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/TestMode.Entities/Systems/TestSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ public void OnPlayerConnect(Player player, IVehicleRepository vehiclesRepository
Console.WriteLine("T2: " + t2);
Console.WriteLine("S2: " + s2);
player.SendClientMessage($"Hey there, {player.Name}");
KeepSayingHello(player);
}

private async void KeepSayingHello(Player player)
{
while (player)
{
player.SendClientMessage("HI!");
await Task.Delay(30000);
}
}

[Event]
Expand Down

0 comments on commit b1aa74c

Please sign in to comment.