Component.cs 589 字节
Newer Older
1 2 3
using MongoDB.Bson.Serialization.Attributes;

namespace Model
4
{
T
tanghai 已提交
5
	[BsonIgnoreExtraElements]
6
	public abstract partial class Component: Disposer
7
	{
8
		[BsonIgnore]
9
		public Entity Entity { get; set; }
10

11
		public T GetEntity<T>() where T : Entity
12
		{
13
			return this.Entity as T;
14 15 16 17
		}

		protected Component()
		{
18
			this.Id = 1;
19
		}
T
tanghai 已提交
20
		
21 22
		public T GetComponent<T>() where T : Component
		{
23
			return this.Entity.GetComponent<T>();
24 25 26 27 28 29 30 31 32 33 34
		}

		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
			}

			base.Dispose();

35
			this.Entity?.RemoveComponent(this.GetType());
36 37 38
		}
	}
}