4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
8 using System.Reflection;
10 using SiliconStudio.Core;
11 using SiliconStudio.Core.Collections;
12 using SiliconStudio.Core.Diagnostics;
13 using SiliconStudio.Core.ReferenceCounting;
14 using SiliconStudio.Paradox.Engine;
15 using SiliconStudio.Paradox.Games;
17 namespace SiliconStudio.
Paradox.EntityModel
27 private readonly TrackingDictionary<Entity, List<EntityProcessor>> entities;
30 private readonly TrackingHashSet<Entity> enabledEntities;
32 private readonly TrackingHashSet<EntityProcessor> processors;
41 entities =
new TrackingDictionary<Entity, List<EntityProcessor>>();
42 enabledEntities =
new TrackingHashSet<Entity>();
44 processors =
new TrackingHashSet<EntityProcessor>();
45 processors.CollectionChanged +=
new EventHandler<TrackingCollectionChangedEventArgs>(systems_CollectionChanged);
51 public ISet<EntityProcessor> Processors
53 get {
return processors; }
58 foreach (var processor
in processors)
60 if (processor.Enabled)
62 using (Profiler.Begin(processor.UpdateProfilingKey))
64 processor.Update(gameTime);
72 foreach (var processor
in processors)
74 if (processor.Enabled)
76 using (Profiler.Begin(processor.DrawProfilingKey))
78 processor.Draw(gameTime);
94 throw new ArgumentException(
"Entity shouldn't have a parent.",
"entity");
96 InternalAddEntity(entity);
105 foreach (var entity
in entitiesToAdd)
117 foreach (var entity
in entitiesToAdd)
131 List<EntityProcessor> entityProcessors;
132 if (!entities.TryGetValue(entity, out entityProcessors))
133 throw new InvalidOperationException(
"Entity is not part of this EntityManager.");
135 bool wasEnabled = enabledEntities.Contains(entity);
137 if (enabled != wasEnabled)
141 enabledEntities.Add(entity);
145 enabledEntities.Remove(entity);
148 foreach (var component
in entityProcessors)
150 component.SetEnabled(entity, enabled);
163 return enabledEntities.Contains(entity);
173 SetEnabled(entity,
true);
183 SetEnabled(entity,
false);
194 InternalRemoveEntity(entity,
true);
202 foreach (var entity
in entities.Keys.ToList())
204 InternalRemoveEntity(entity,
true);
215 foreach (var system
in processors)
228 internal void InternalAddEntity(
Entity entity)
231 if (entities.ContainsKey(entity))
234 var entityProcessors =
new List<EntityProcessor>();
235 entities.Add(entity, entityProcessors);
237 enabledEntities.Add(entity);
239 entity.AddReferenceInternal();
241 entity.Tags.PropertyUpdated += EntityPropertyUpdated;
244 foreach (var system
in processors)
246 system.EntityCheck(entity, entityProcessors);
255 internal void InternalRemoveEntity(Entity entity,
bool removeParent)
258 List<EntityProcessor> entityProcessors;
259 if (!entities.TryGetValue(entity, out entityProcessors))
262 entities.Remove(entity);
263 enabledEntities.Remove(entity);
268 entity.Transformation.Parent = null;
272 foreach (var system
in processors)
274 system.EntityCheck(entity, entityProcessors,
true);
277 entity.Tags.PropertyUpdated -= EntityPropertyUpdated;
279 entity.ReleaseInternal();
282 private void AddSystem(EntityProcessor processor)
284 processor.EntitySystem =
this;
285 processor.Services = Services;
286 processor.OnSystemAdd();
287 foreach (var entity
in entities)
289 processor.EntityCheck(entity.Key, entity.Value);
293 private void RemoveSystem(EntityProcessor processor)
295 processor.OnSystemRemove();
296 processor.Services = null;
297 processor.EntitySystem = null;
303 if (!typeof(EntityComponent).GetTypeInfo().IsAssignableFrom(propertyKey.
PropertyType.GetTypeInfo()))
307 if (oldValue == newValue)
310 var entity = (Entity)propertyContainer.Owner;
311 var entityProcessors = entities[entity];
312 foreach (EntityProcessor system in processors)
314 system.EntityCheck(entity, entityProcessors);
336 case NotifyCollectionChangedAction.Add:
337 AddSystem((EntityProcessor)e.
Item);
339 case NotifyCollectionChangedAction.Remove:
340 RemoveSystem((EntityProcessor)e.
Item);
352 return entities.ContainsKey(item);
357 return entities.Keys.GetEnumerator();
360 IEnumerator IEnumerable.GetEnumerator()
362 return GetEnumerator();
369 return entities.Count;
void Disable(Entity entity)
Disables the specified entity.
override void Update(GameTime gameTime)
This method is called when this game component is updated.
Game entity. It usually aggregates multiple EntityComponent
void Clear()
Removes all entities from the EntitySystem.
void SetEnabled(Entity entity, bool enabled=true)
Sets the enable state of this entity.
void Enable(Entity entity)
Enables the specified entity.
Represents a container that can hold properties, lightweight to embed (lazy initialized).
TransformationComponent Transformation
Gets or sets the Transformation associated to this entity. Added for convenience over usual Get/Set m...
A service registry is a IServiceProvider that provides methods to register and unregister services...
IEnumerator< Entity > GetEnumerator()
bool IsEnabled(Entity entity)
Determines whether the specified entity is enabled.
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
Base class for a GameSystemBase component.
Current timing used for variable-step (real time) or fixed-step (game time) games.
Type PropertyType
Gets the type of the property.
object Item
Gets the added or removed item (if dictionary, value only).
Manage a collection of entities.
void Remove(Entity entity)
Removes the entity from the EntitySystem. It works weither entity has a parent or not...
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
override void Draw(GameTime gameTime)
Draws this instance.
EntitySystem(IServiceRegistry registry)
void Add(Entity entity)
Adds the entity. If the Entity has a parent, its parent should be added (or TransformationComponent.Children) should be used.
A class that represents a tag propety.
void AddRange(IEnumerable< Entity > entitiesToAdd)
Adds a collection of entities to this system.
bool Contains(Entity item)
Determines whether this instance contains the specified entity.
void Add(params Entity[] entitiesToAdd)
Adds a collection of entities to this system.