protected override void Draw() { GraphicsDeviceManager.GraphicsDevice.Clear(Color.FromNonPremultiplied(64, 64, 64, 255)); if (_effect == null) { return; } if (_batch == null) { _batch = new SpriteBatch(GraphicsDeviceManager.GraphicsDevice); } FarTransform transform; transform.Matrix = Matrix.Identity; transform.Translation.X = Width / 2; transform.Translation.Y = Height / 2; _renderer.RenderEffect(_effect, ref transform); }
public void OnDraw(Draw message) { if (!Enabled) { return; } // Get global transform. var transform = GetTransform(); var translation = GetTranslation(); // Get delta to keep update speed constant regardless of framerate. var delta = (message.ElapsedMilliseconds / 1000f) * _simulationSpeed(); // Get the interpolation system to get an interpolated position for the effect generator. var interpolation = (InterpolationSystem)Manager.GetSystem(InterpolationSystem.TypeId); // Handle particle effects attached to entities. foreach (var component in Components) { // Skip disabled components. if (!component.Enabled) { continue; } // Handle each effect per component. foreach (var effect in component.Effects) { // Get info for triggering and rendering. FarPosition position; float angle; interpolation.GetInterpolatedTransform(component.Entity, out position, out angle); // Load / initialize particle effects if they aren't yet. if (effect.Effect == null) { var content = ((ContentSystem)Manager.GetSystem(ContentSystem.TypeId)).Content; effect.Effect = content.Load <ParticleEffect>(effect.AssetName).DeepCopy(); effect.Effect.LoadContent(content); effect.Effect.Initialise(); } // Only do the triggering work if the effect is actually enabled. // ALWAYS RENDER, to allow already triggered effects to play out (and not // instantly disappear). if (effect.Enabled && effect.Scale * effect.Intensity > 0.1f) { // Check if it's in bounds, i.e. whether we have to trigger it at all. var localTranslation = Vector2.Transform((Vector2)(position + translation), transform); var bounds = _renderer.GraphicsDeviceService.GraphicsDevice.Viewport.Bounds; bounds.Inflate(256, 256); if (bounds.Contains((int)localTranslation.X, (int)localTranslation.Y)) { // Move the offset according to rotation. var cosRadians = (float)Math.Cos(angle); var sinRadians = (float)Math.Sin(angle); FarPosition offset; offset.X = effect.Offset.X * cosRadians - effect.Offset.Y * sinRadians; offset.Y = effect.Offset.X * sinRadians + effect.Offset.Y * cosRadians; // Adjust emitting rotation. angle += effect.Direction; // Trigger. effect.Effect.Trigger(offset, angle, effect.Scale * effect.Intensity); } } // Render at owning entity's position. FarTransform localTransform; localTransform.Matrix = transform; localTransform.Translation = FarUnitConversion.ToScreenUnits(translation + position); _renderer.RenderEffect(effect.Effect, ref localTransform); // Update after rendering. effect.Effect.Update(delta); } } // Render and update all known unbound effects (not attached to an entity). FarTransform globalTransform; globalTransform.Matrix = transform; globalTransform.Translation = FarUnitConversion.ToScreenUnits(translation); foreach (var effect in _effects.Values) { _renderer.RenderEffect(effect, ref globalTransform); effect.Update(delta); } }