Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EntityData.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.Generic;
5 using System.Linq;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Collections;
8 using SiliconStudio.Core.Serialization;
9 using SiliconStudio.Paradox.Games;
10 using SiliconStudio.Core.Extensions;
11 using SiliconStudio.Core.Serialization.Contents;
12 using SiliconStudio.Core.Serialization.Converters;
13 
14 namespace SiliconStudio.Paradox.EntityModel.Data
15 {
16  public partial class EntityComponentData
17  {
18  /// <summary>
19  /// Entity will get updated when added to EntityData.Components.
20  /// </summary>
21  [DataMemberIgnore]
23  }
24 
25  // Entities as they are stored in the git shared scene file
26  public partial class EntityData
27  {
28  [DataMember(10)]
29  public string Name;
30 
31  [DataMember(20)]
32  public TrackingDictionary<PropertyKey, EntityComponentData> Components = new TrackingDictionary<PropertyKey, EntityComponentData>();
33 
34  public EntityData()
35  {
36  Components.CollectionChanged += Components_CollectionChanged;
37  }
38 
39  void Components_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
40  {
41  // Remove entity owner from previous EntityComponentData.
43  {
44  ((EntityComponentData)e.OldItem).Entity = null;
45  }
46 
47  // Set entity owner to this new EntityComponentData.
48  if (e.Item is EntityComponentData)
49  {
50  ((EntityComponentData)e.Item).Entity = this;
51  }
52  }
53  }
54 
55  public partial class EntityDataConverter : DataConverter<EntityData, Entity>
56  {
57  public override bool CanConstruct
58  {
59  get { return true; }
60  }
61 
62  public override void ConstructFromData(ConverterContext converterContext, EntityData entityData, ref Entity entity)
63  {
64  entity = new Entity(entityData.Name);
65  foreach (var component in entityData.Components)
66  {
67  entity.Tags.SetObject(component.Key, converterContext.ConvertFromData<EntityComponent>(component.Value, ConvertFromDataFlags.Construct));
68  }
69  }
70 
71  public override void ConvertToData(ConverterContext converterContext, ref EntityData entityData, Entity entity)
72  {
73  entityData = new EntityData
74  {
75  Name = entity.Name,
76  };
77 
78  foreach (var component in entity.Tags.Where(x => x.Value is EntityComponent))
79  {
80  entityData.Components.Add(component.Key, converterContext.ConvertToData<EntityComponentData>(component.Value));
81  }
82  }
83 
84  public override void ConvertFromData(ConverterContext converterContext, EntityData entityData, ref Entity entity)
85  {
86  foreach (var component in entityData.Components)
87  {
88  var entityComponent = (EntityComponent)entity.Tags.Get(component.Key);
89  converterContext.ConvertFromData(component.Value, ref entityComponent, ConvertFromDataFlags.Convert);
90  entity.Tags.SetObject(component.Key, entityComponent);
91  }
92  }
93  }
94 }
EntityData Entity
Entity will get updated when added to EntityData.Components.
Definition: EntityData.cs:22
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Base class for converters to/from a data type.
override void ConvertToData(ConverterContext converterContext, ref EntityData entityData, Entity entity)
Definition: EntityData.cs:71
override void ConstructFromData(ConverterContext converterContext, EntityData entityData, ref Entity entity)
Definition: EntityData.cs:62
object OldItem
Gets the previous value. Only valid if Action is NotifyCollectionChangedAction.Add and ...
override void ConvertFromData(ConverterContext converterContext, EntityData entityData, ref Entity entity)
Definition: EntityData.cs:84
object Item
Gets the added or removed item (if dictionary, value only).
Data type for SiliconStudio.Paradox.EntityModel.Entity.
Definition: EngineData.cs:739
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
Data type for SiliconStudio.Paradox.EntityModel.EntityComponent.
Definition: EngineData.cs:342