-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finishes implementation of EntitySystem
- Loading branch information
1 parent
1f7dfa9
commit 99611b5
Showing
2 changed files
with
55 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,75 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using Microsoft.Xna.Framework.Input; | ||
|
||
namespace Aurora.Core.EC; | ||
|
||
public sealed class EntitySystem : ModSystem | ||
{ | ||
private static readonly List<Entity> Entities = []; | ||
private static readonly List<int> ActiveEntityIds = []; | ||
private static readonly List<int> InactiveEntityIds = []; | ||
|
||
private static readonly ConcurrentBag<int> FreeEntityIds = []; | ||
|
||
private static int NextEntityId; | ||
|
||
/// <summary> | ||
/// Creates a new instance of an entity. | ||
/// </summary> | ||
/// <param name="activate">Whether to activate the entity instance or not.</param> | ||
/// <returns>The created entity instance.</returns> | ||
public static Entity Create(bool activate = true) { | ||
var entity = new Entity(); | ||
public static Entity Create(bool activate) { | ||
int id; | ||
|
||
if (!FreeEntityIds.TryTake(out id)) { | ||
id = NextEntityId++; | ||
} | ||
|
||
if (activate) { | ||
Entities.Add(entity); | ||
ActiveEntityIds.Add(id); | ||
} | ||
|
||
return entity; | ||
return new Entity(id); | ||
} | ||
|
||
/// <summary> | ||
/// Removes an instance of an entity. | ||
/// Removes an instance of an entity from its unique identifier. | ||
/// </summary> | ||
/// <param name="entity">The entity to remove.</param> | ||
/// <param name="entityId">The identity of the entity to remove.</param> | ||
/// <returns><c>true</c> if the entity was successfully removed; otherwise, <c>false</c>.</returns> | ||
public static bool Remove(Entity entity) { | ||
return Entities.Remove(entity); | ||
public static bool Remove(int id) { | ||
if (id < 0) { | ||
return false; | ||
} | ||
|
||
ActiveEntityIds.Remove(id); | ||
InactiveEntityIds.Remove(id); | ||
|
||
FreeEntityIds.Add(id); | ||
|
||
return true; | ||
} | ||
|
||
internal static bool GetActive(int entityId) { | ||
if (entityId < 0) { | ||
return false; | ||
} | ||
|
||
return ActiveEntityIds.Contains(entityId); | ||
} | ||
|
||
internal static void SetActive(int entityId, bool value) { | ||
if (entityId < 0) { | ||
return; | ||
} | ||
|
||
if (value) { | ||
ActiveEntityIds.Add(entityId); | ||
InactiveEntityIds.Remove(entityId); | ||
return; | ||
} | ||
|
||
ActiveEntityIds.Remove(entityId); | ||
InactiveEntityIds.Add(entityId); | ||
} | ||
} |