This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
SpatialOSEntity.cs
60 lines (55 loc) · 2.51 KB
/
SpatialOSEntity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Improbable.Gdk.Core;
using Unity.Entities;
using Entity = Unity.Entities.Entity;
namespace Improbable.Gdk.GameObjectCreation
{
/// <summary>
/// Used to easily retrieve information about a SpatialOS Entity instance from a Unity ECS Entity instance.
/// </summary>
public struct SpatialOSEntity
{
public readonly EntityId SpatialOSEntityId;
private readonly EntityManager entityManager;
private readonly Entity entity;
internal SpatialOSEntity(Entity entity, EntityManager entityManager)
{
this.entity = entity;
this.entityManager = entityManager;
SpatialOSEntityId = entityManager.GetComponentData<SpatialEntityId>(entity).EntityId;
}
/// <summary>
/// Checks if this entity has a component of type <see cref="T"/>
/// </summary>
/// <typeparam name="T">The SpatialOS component.</typeparam>
/// <returns>True, if the entity has the component; false otherwise.</returns>
public bool HasComponent<T>() where T : struct, ISpatialComponentData, IComponentData
{
return entityManager.HasComponent<T>(entity);
}
/// <summary>
/// Gets a component of type <see cref="T"/> attached to this entity.
/// </summary>
/// <typeparam name="T">The SpatialOS component.</typeparam>
/// <returns>The component <see cref="T"/> attached to this entity.</returns>
/// <exception cref="System.ArgumentException">Thrown if the component <see cref="T"/> is not attached to this entity.</exception>
public T GetComponent<T>() where T : struct, ISpatialComponentData, IComponentData
{
return entityManager.GetComponentData<T>(entity);
}
/// <summary>
/// Attempts to get a component of type <see cref="T"/> attached to this entity.
/// </summary>
/// <param name="component">
/// When this method returns, this will be the component attached to this entity if it exists;
/// default constructed otherwise.
/// </param>
/// <typeparam name="T">The SpatialOS component.</typeparam>
/// <returns>True, if the entity has the component; false otherwise.</returns>
public bool TryGetComponent<T>(out T component) where T : struct, ISpatialComponentData, IComponentData
{
var has = HasComponent<T>();
component = has ? GetComponent<T>() : default(T);
return has;
}
}
}