Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Entity.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Diagnostics;
7 using System.Linq;
8 using System.Threading.Tasks;
9 
10 using SiliconStudio.Core.Mathematics;
11 using SiliconStudio.Core.Serialization.Converters;
12 using SiliconStudio.Paradox.Engine;
13 using SiliconStudio.Core;
14 using SiliconStudio.Core.Serialization.Contents;
15 using SiliconStudio.Core.Serialization.Serializers;
16 
17 namespace SiliconStudio.Paradox.EntityModel
18 {
19  /// <summary>
20  /// Game entity. It usually aggregates multiple EntityComponent
21  /// </summary>
22  //[ContentSerializer(typeof(EntityContentSerializer))]
23  //[ContentSerializer(typeof(DataContentSerializer<Entity>))]
24  [DebuggerTypeProxy(typeof(EntityDebugView))]
25  [DataConverter(AutoGenerate = false, ContentReference = true)]
26  [DataSerializer(typeof(EntitySerializer))]
27  [ContentSerializer(typeof(DataContentConverterSerializer<Entity>))]
29  {
31  internal List<Task> prepareTasks;
32 
33  static Entity()
34  {
35  PropertyContainer.AddAccessorProperty(typeof(Entity), TransformationComponent.Key);
36  }
37 
38  /// <summary>
39  /// Create a new <see cref="Entity"/> instance.
40  /// </summary>
41  public Entity()
42  : this(null)
43  {
44  }
45 
46  /// <summary>
47  /// Create a new <see cref="Entity"/> instance having the provided name.
48  /// </summary>
49  /// <param name="name">The name to give to the entity</param>
50  public Entity(string name)
51  : this(Vector3.Zero, name)
52  {
53  }
54 
55  /// <summary>
56  /// Create a new <see cref="Entity"/> instance having the provided name and initial position.
57  /// </summary>
58  /// <param name="position">The initial position of the entity</param>
59  /// <param name="name">The name to give to the entity</param>
60  public Entity(Vector3 position, string name = null)
61  : base(name)
62  {
63  Tags.PropertyUpdated += EntityPropertyUpdated;
64 
65  Transformation = new TransformationComponent();
66  transformation.Translation = position;
67  }
68 
69  /// <summary>
70  /// Gets or sets the <see cref="Transformation"/> associated to this entity.
71  /// Added for convenience over usual Get/Set method.
72  /// </summary>
73  public TransformationComponent Transformation
74  {
75  get { return transformation; }
76  set
77  {
78  var transformationOld = transformation;
79  transformation = value;
80  Tags.RaisePropertyContainerUpdated(TransformationComponent.Key, transformation, transformationOld);
81  }
82  }
83 
84  /// <summary>
85  /// Gets or create a component with the specified key.
86  /// </summary>
87  /// <typeparam name="T">Type of the entity component</typeparam>
88  /// <returns>A new or existing instance of {T}</returns>
89  public T GetOrCreate<T>() where T : EntityComponent, new()
90  {
91  var key = EntityComponent.GetDefaultKey<T>();
92  var component = Tags.Get(key);
93  if (component == null)
94  {
95  component = new T();
96  Tags.SetObject(key, component);
97  }
98 
99  return (T)component;
100  }
101 
102  /// <summary>
103  /// Gets or create a component with the specified key.
104  /// </summary>
105  /// <typeparam name="T">Type of the entity component</typeparam>
106  /// <param name="key">The key.</param>
107  /// <returns>A new or existing instance of {T}</returns>
108  public T GetOrCreate<T>(PropertyKey<T> key) where T : EntityComponent, new()
109  {
110  var component = Tags.Get(key);
111  if (component == null)
112  {
113  component = new T();
114  Tags.Set(key, component);
115  }
116 
117  return component;
118  }
119 
120  /// <summary>
121  /// Adds the specified component using the <see cref="EntityComponent.DefaultKey" />.
122  /// </summary>
123  /// <param name="component">The component.</param>
124  /// <exception cref="System.ArgumentNullException">component</exception>
125  public void Add(EntityComponent component)
126  {
127  if (component == null) throw new ArgumentNullException("component");
128  Tags.SetObject(component.DefaultKey, component);
129  }
130 
131  /// <summary>
132  /// Gets a component by the specified key.
133  /// </summary>
134  /// <typeparam name="T">Type of the component</typeparam>
135  /// <returns>The component or null if does no exist</returns>
136  /// <exception cref="System.ArgumentNullException">key</exception>
137  public T Get<T>() where T : EntityComponent, new()
138  {
139  return (T)Tags.Get(EntityComponent.GetDefaultKey<T>());
140  }
141 
142  /// <summary>
143  /// Gets a component by the specified key.
144  /// </summary>
145  /// <typeparam name="T">Type of the component</typeparam>
146  /// <param name="key">The key.</param>
147  /// <returns>The component or null if does no exist</returns>
148  /// <exception cref="System.ArgumentNullException">key</exception>
149  public T Get<T>(PropertyKey<T> key) where T : EntityComponent
150  {
151  if (key == null) throw new ArgumentNullException("key");
152  return Tags.Get(key);
153  }
154 
155  /// <summary>
156  /// Sets a component with the specified key.
157  /// </summary>
158  /// <typeparam name="T">Type of the component</typeparam>
159  /// <param name="key">The key.</param>
160  /// <param name="value">The value.</param>
161  /// <exception cref="System.ArgumentNullException">key</exception>
162  public void Add<T>(PropertyKey<T> key, T value) where T : EntityComponent
163  {
164  if (key == null) throw new ArgumentNullException("key");
165  Tags.SetObject(key, value);
166  }
167 
168  /// <summary>
169  /// Sets a component with the specified key.
170  /// </summary>
171  /// <typeparam name="T">Type of the component</typeparam>
172  /// <param name="key">The key.</param>
173  /// <param name="value">The value.</param>
174  /// <exception cref="System.ArgumentNullException">key</exception>
175  [Obsolete("Use Add() method instead")]
176  public void Set<T>(PropertyKey<T> key, T value) where T : EntityComponent
177  {
178  if (key == null) throw new ArgumentNullException("key");
179  Tags.SetObject(key, value);
180  }
181 
182  private void EntityPropertyUpdated(ref PropertyContainer propertyContainer, PropertyKey propertyKey, object newValue, object oldValue)
183  {
184  // Remove entity owner from previous EntityComponent.
185  if (oldValue is EntityComponent)
186  {
187  ((EntityComponent)oldValue).Entity = null;
188  }
189 
190  // Set entity owner to this new EntityComponent.
191  if (newValue is EntityComponent)
192  {
193  ((EntityComponent)newValue).Entity = this;
194  }
195  }
196 
197  internal class EntityDebugView
198  {
199  private readonly Entity entity;
200 
201  public EntityDebugView(Entity entity)
202  {
203  this.entity = entity;
204  }
205 
206  public string Name
207  {
208  get { return entity.Name; }
209  }
210 
211  public Entity[] Children
212  {
213  get
214  {
215  var transformationComponent = entity.Transformation;
216  if (transformationComponent == null)
217  return null;
218 
219  return transformationComponent.Children.Select(x => x.Entity).ToArray();
220  }
221  }
222 
223  public EntityComponent[] Components
224  {
225  get
226  {
227  return entity.Tags.Select(x => x.Value).OfType<EntityComponent>().ToArray();
228  }
229  }
230  }
231 
232  public override string ToString()
233  {
234  return string.Format("Entity {0}", Name);
235  }
236 
237  IEnumerator IEnumerable.GetEnumerator()
238  {
239  return Tags.Values.OfType<EntityComponent>().GetEnumerator();
240  }
241 
242  string IContentUrl.Url { get; set; }
243  }
244 }
Interface for serializable object having an url (so referenceable by other assets and saved into a si...
Definition: IContentUrl.cs:8
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Defines Position, Rotation and Scale of its Entity.
Entity(string name)
Create a new Entity instance having the provided name.
Definition: Entity.cs:50
Represents a container that can hold properties, lightweight to embed (lazy initialized).
Base class for converters to/from a data type.
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
Entity()
Create a new Entity instance.
Definition: Entity.cs:41
Base class for a framework component.
A class that represents a typed tag propety.
Definition: PropertyKey.cs:138
TransformationComponent transformation
Definition: Entity.cs:30
Entity(Vector3 position, string name=null)
Create a new Entity instance having the provided name and initial position.
Definition: Entity.cs:60
void Add(EntityComponent component)
Adds the specified component using the EntityComponent.DefaultKey.
Definition: Entity.cs:125
A class that represents a tag propety.
Definition: PropertyKey.cs:17