Commit 8901d05c authored by Nathan H. Bean's avatar Nathan H. Bean

Added game componentization section to particle systems, and cleaned up Engine/System references

parent 571ac7b0
......@@ -4,6 +4,6 @@ pre: "1. "
weight: 1
date: 2020-03-20T10:53:05-05:00
---
Particle systems leverage thousands of very small sprites to create the illusion of fire, smoke, explosions, precipitation, waterfalls, and many other interesting effects. They are a staple in modern game engines both to create ambience (i.e. fires and smoke) and to enhance gameplay (glowing sparkles around objects that can be interacted with). In this section, we'll discuss the basics of how particle engines work, and iteratively build a particle system in MonoGame that can be used in your own games.
Particle systems leverage thousands of very small sprites to create the illusion of fire, smoke, explosions, precipitation, waterfalls, and many other interesting effects. They are a staple in modern game engines both to create ambience (i.e. fires and smoke) and to enhance gameplay (glowing sparkles around objects that can be interacted with). In this section, we'll discuss the basics of how particle systems work, and iteratively build a particle system in MonoGame that can be used in your own games.
I've prepared a starter project in which we will build this particle system. Feel free to clone it from GithHub, here: [https://github.com/ksu-cis/particle-system-starter](https://github.com/ksu-cis/particle-system-starter)
\ No newline at end of file
I've prepared a starter project in which we will build this particle system. Feel free to clone it from GithHub, here: [https://github.com/ksu-cis/particle-system-starter](https://github.com/ksu-cis/particle-system-starter).
\ No newline at end of file
---
title: "Refactoring as a Game Component"
pre: "6. "
weight: 6
date: 2020-03-22T10:53:05-05:00
---
Implementing delegate-based initialization and updating has made our particle system a bit less fragile, but we still have an opportunity to make it even more modular, by leveraging MonoGame's [Game Component System](https://gavsdevblog.wordpress.com/2016/09/04/monogame-components-and-services/).
## Refactoring the ParticleSystem Class
Game Components inherit from either the [GameComponent] or [DrawableGameComponent] base classes, depending on if they are drawn or not. They can be registered with the [Game] class's `Components` list, in which case the game will automatically call their `Update()` and `Draw()` methods; these no longer need to be invoked by you. Additionally, you can specify the `DrawOrder` property on `DrawableGameComponents` to enforce drawing order.
Since our particle systems are rendered, we'll want to refactor the `ParticleSystem` class to extend the `DrawableGameComponent` class:
```csharp
public class ParticleSystem : DrawableGameComponent
{
...
```
We'll also need to refactor our `Update()` and `Render()` methods to make them override the equivalent `DrawableGameComponent` methods (which are virutal):
```csharp
public override void Update(GameTime gameTime)
{
...
```
and
```csharp
public override void Draw(GameTime gameTime)
{
...
```
## Refactoring the Game1 Class
In the `Game1` class, we can remove the lines invoking `particleSystem.Update()` and `particleSystem.Draw()`, as this will now be handled by the component system. We'll also need to add the particle system to the components list, which we can do just after we construct it in the `Game1.LoadContent()` method:
```csharp
// Add the particle system to the list of game components
Components.Add(particleEngine);
```
As a game component, we can create the particle system in a fire-and-forget fashion. This works well if our game will always have the particle system running. In other cases, we'll need to remove it from the list when we're done with it. We can also still manually invoke its `Update()` and `Draw()` methods and _not_ add it to the `Components` list. Making it a `DrawableGameComponent` just gives us more flexibility.
\ No newline at end of file
---
title: "Standard Particle Systems"
pre: "6. "
weight: 6
date: 2020-03-21T10:53:05-05:00
---
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment