update formatting on concepts chapter
continuous-integration/drone/push Build is passing Details

main
Evan Hemsley 2020-07-12 12:09:27 -07:00
parent 3ee7328b7e
commit 84ecc93076
4 changed files with 20 additions and 27 deletions

View File

@ -13,7 +13,13 @@ using Encompass;
using System.Numerics; using System.Numerics;
public struct VelocityComponent : IComponent { public struct VelocityComponent : IComponent {
public Vector2 velocity;
public Vector2 Velocity { get; }
public VelocityComponent(Vector2 velocity)
{
Velocity = velocity;
}
} }
``` ```
@ -26,15 +32,15 @@ using Encompass;
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new VelocityComponent { velocity = Vector2.Zero }); worldBuilder.SetComponent(entity, new VelocityComponent(Vector2.One));
``` ```
**SetComponent** can also be used from within an **Engine**. We will talk more about this later. **SetComponent** can also be used from within an **Engine**. We will talk more about this later.
Components are always structs, meaning they follow value-type semantics. If you are used to working with classes you might find this confusing. Components are always structs, meaning they follow value-type semantics. If you are used to working with classes you might find this confusing.
One major point of difference is that value types are _copied_ rather that passed by reference by default. One major point of difference is that value types are _copied_ rather that passed by reference by default.
You can read more about value types here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types You can read more about value types here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types
If you use them idiomatically, you don't have to worry about them creating garbage collection pressure, so this is a big win for performance when working in C#. If you use them idiomatically, you don't have to worry about them creating garbage collection pressure, so this is a big win for performance when working in C#.

View File

@ -30,15 +30,12 @@ public class PauseEngine : Engine
{ {
foreach (ref readonly var pauseMessage in ReadMessages<PauseMessage>()) foreach (ref readonly var pauseMessage in ReadMessages<PauseMessage>())
{ {
SetComponent(pauseMessage.entity, new PauseComponent SetComponent(pauseMessage.entity, new PauseComponent(pauseMessage.time));
{
timer = pauseMessage.time
});
} }
foreach (ref readonly var entity in ReadEntities<PauseComponent>()) foreach (ref readonly var entity in ReadEntities<PauseComponent>())
{ {
ref readonly var pauseComponent = GetComponent<PauseComponent>(entity); ref readonly var pauseComponent = ref GetComponent<PauseComponent>(entity);
var timer = pauseComponent.timer; var timer = pauseComponent.timer;
timer -= dt; timer -= dt;
@ -49,7 +46,7 @@ public class PauseEngine : Engine
} }
else else
{ {
SetComponent(entity, new PauseComponent { timer = timer }); SetComponent(entity, new PauseComponent(timer));
} }
} }
} }
@ -57,6 +54,6 @@ public class PauseEngine : Engine
``` ```
This engine deals with a Component called a PauseComponent. When a PauseMessage is received, a new PauseComponent is attached to the Entity specified by the message. PauseComponent has a timer which counts down based on delta-time. When the timer ticks past zero, the PauseComponent is removed. This engine deals with a Component called a PauseComponent. When a PauseMessage is received, a new PauseComponent is attached to the Entity specified by the message. PauseComponent has a timer which counts down based on delta-time. When the timer ticks past zero, the PauseComponent is removed.
Notice that this Engine doesn't actually "do" the pausing, or even care if the Entity in question is capable of movement or not. In Engines that deals with movement, we can check if the Entities being moved have PauseComponents attached to them and modify how they are updated accordingly. This is the power of de-coupled logic. Notice that this Engine doesn't actually "do" the pausing, or even care if the Entity in question is capable of movement or not. In Engines that deals with movement, we can check if the Entities being moved have PauseComponents attached to them and modify how they are updated accordingly. This is the power of de-coupled logic.

View File

@ -14,7 +14,7 @@ To define a message, declare a struct which implements the IMessage interface.
using Encompass; using Encompass;
public struct MotionMessage : IMessage { public struct MotionMessage : IMessage {
public Vector2 motion; public Vector2 Motion { get; }
} }
``` ```

View File

@ -31,22 +31,12 @@ public class MyGame : Game
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
SetComponent(entity, new PositionComponent SetComponent(entity, new PositionComponent(0, 0));
{ SetComponent(entity, new VelocityComponent(20, 0));
x = 0,
y = 0
});
SetComponent(entity, new VelocityComponent SetComponent(entity, new TextureComponent(
{ TextureHelper.LoadTexture("Sprites/ball.png");
x = 20, );
y = 0
});
SetComponent(entity, new TextureComponent
{
texture = TextureHelper.LoadTexture("assets/sprites/ball.png")
});
world = worldBuilder.Build(); world = worldBuilder.Build();
} }