-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd1ebea
commit 688b3e2
Showing
5 changed files
with
75 additions
and
40 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace TinyEcs; | ||
|
||
public readonly struct Identity | ||
{ | ||
public readonly string Value; | ||
|
||
internal Identity(string value) => Value = value; | ||
} | ||
|
||
public static class UniqueEntityPlugin | ||
{ | ||
[ModuleInitializer] | ||
Check warning on line 14 in plugins/TinyEcs.Plugins/UniqueEntity.cs
|
||
internal static void ModuleInit() | ||
{ | ||
World.OnPluginInitialization += world => { | ||
// initialize the entity component | ||
world.Component<Identity>(); | ||
}; | ||
} | ||
|
||
public static EntityView Entity(this World world, string name) | ||
{ | ||
if (string.IsNullOrWhiteSpace(name)) | ||
return world.Entity(); | ||
|
||
var found = EntityView.Invalid; | ||
world.Query((EntityView entity, ref Identity identity) => { | ||
if (identity.Value?.Equals(name) ?? false) | ||
found = entity; | ||
}); | ||
|
||
if (found.ID != 0) | ||
return found; | ||
|
||
return world.Entity().Set(new Identity(name)); | ||
} | ||
|
||
public static string Name(this EntityView entity) | ||
{ | ||
if (!entity.Has<Identity>()) | ||
return $"{entity.ID}"; | ||
|
||
return entity.Get<Identity>().Value ?? string.Empty; | ||
} | ||
} |
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
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