Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParticleProcessor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.Diagnostics;
5 using System.Linq;
6 
7 using SiliconStudio.Paradox;
8 using SiliconStudio.Paradox.Effects;
9 using SiliconStudio.Paradox.Effects.Modules;
10 using SiliconStudio.Paradox.Engine;
11 using SiliconStudio.Paradox.EntityModel;
12 using SiliconStudio.Paradox.Games;
13 using SiliconStudio.Core;
14 using SiliconStudio.Core.Collections;
15 using SiliconStudio.Core.Mathematics;
16 
17 namespace ScriptShader.Effects
18 {
19  public class ParticleUpdater
20  {
21  public Entity Entity;
22 
24 
26 
27  public MeshComponent Mesh;
28  }
29 
30  public class ParticleProcessor : EntityProcessor<ParticleUpdater>
31  {
32  public static readonly PropertyKey<ParticlePlugin> ParticlePluginKey = new PropertyKey<ParticlePlugin>("ParticlePlugin", typeof(ParticleProcessor));
33 
34  private ParticlePlugin particlePlugin;
35 
36  private List<ParticleUpdater> updaters;
37 
38  // TODO: Add support for optinal keys again, for MeshComponent.Key
39  public ParticleProcessor(ParticlePlugin particlePlugin)
40  : base(new PropertyKey[] { ParticleEmitterComponent.Key, TransformationComponent.Key })
41  {
42  this.particlePlugin = particlePlugin;
43  updaters = new List<ParticleUpdater>();
44  }
45 
46  protected override ParticleUpdater GenerateAssociatedData(Entity entity)
47  {
48  return new ParticleUpdater()
49  {
50  Entity = entity,
51  Emitter = entity.Get(ParticleEmitterComponent.Key),
52  Transform = entity.Transformation,
53  Mesh = entity.Get(MeshComponent.Key),
54  };
55  }
56 
57  protected override void OnEntityAdded(Entity entity, ParticleUpdater data)
58  {
59  data.Emitter.OnAddToSystem(Services);
60 
61  if (data.Mesh != null)
62  {
63  throw new NotImplementedException();
64  //var meshListener = data.Mesh.SubMeshes as ITrackingCollectionChanged;
65  //if (meshListener != null)
66  // meshListener.CollectionChanged += MeshListenerOnCollectionChanged;
67 
68  data.Emitter.OnMeshUpdate(data.Entity, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null, null));
69  }
70 
71  updaters.Add(data);
72  particlePlugin.Updaters.Add(data.Emitter);
73  }
74 
75  protected override void OnEntityRemoved(Entity entity, ParticleUpdater data)
76  {
77  if (data.Mesh != null)
78  {
79  throw new NotImplementedException();
80  //var meshListener = data.Mesh.SubMeshes as ITrackingCollectionChanged;
81  //if (meshListener != null)
82  // meshListener.CollectionChanged -= MeshListenerOnCollectionChanged;
83  }
84 
85  particlePlugin.Updaters.Remove(data.Emitter);
86  updaters.Remove(data);
87  }
88 
89  private void MeshListenerOnCollectionChanged(object sender, TrackingCollectionChangedEventArgs trackingCollectionChangedEventArgs)
90  {
91  throw new NotImplementedException();
92  //var updater = updaters.Where(particleUpdater => particleUpdater.Mesh != null).FirstOrDefault(particleUpdater => ReferenceEquals(sender, particleUpdater.Mesh.SubMeshes));
93  //if (updater != null)
94  // updater.Emitter.OnMeshUpdate(updater.Entity, trackingCollectionChangedEventArgs);
95  }
96 
97  public override void Update()
98  {
99  foreach (var matchingEntity in enabledEntities)
100  {
101  var entity = matchingEntity.Key;
102  var name = entity.Name;
103  var particleEmitterComponent = matchingEntity.Value.Emitter;
104  var transformationComponent = matchingEntity.Value.Transform;
105 
106  particleEmitterComponent.Parameters.Set(TransformationKeys.World, transformationComponent.WorldMatrix);
107 
108  // Call the OnUpdateSystem on the particle emitter
109  particleEmitterComponent.OnUpdateSystem();
110  }
111  }
112  }
113 }
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Defines Position, Rotation and Scale of its Entity.
override ParticleUpdater GenerateAssociatedData(Entity entity)
TransformationComponent Transformation
Gets or sets the Transformation associated to this entity. Added for convenience over usual Get/Set m...
Definition: Entity.cs:74
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
override void OnEntityAdded(Entity entity, ParticleUpdater data)
override void OnEntityRemoved(Entity entity, ParticleUpdater data)
ParticleProcessor(ParticlePlugin particlePlugin)
A class that represents a tag propety.
Definition: PropertyKey.cs:17