From 84ecc930761616617b9199a8e3847fde741de5f5 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sun, 12 Jul 2020 12:09:27 -0700 Subject: [PATCH] update formatting on concepts chapter --- content/concepts/component.md | 14 ++++++++++---- content/concepts/engine.md | 11 ++++------- content/concepts/message.md | 2 +- content/concepts/world_builder.md | 20 +++++--------------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/content/concepts/component.md b/content/concepts/component.md index f76b22e..8b610a7 100644 --- a/content/concepts/component.md +++ b/content/concepts/component.md @@ -13,7 +13,13 @@ using Encompass; using System.Numerics; 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 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. 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#. diff --git a/content/concepts/engine.md b/content/concepts/engine.md index 650960f..f395420 100644 --- a/content/concepts/engine.md +++ b/content/concepts/engine.md @@ -30,15 +30,12 @@ public class PauseEngine : Engine { foreach (ref readonly var pauseMessage in ReadMessages()) { - SetComponent(pauseMessage.entity, new PauseComponent - { - timer = pauseMessage.time - }); + SetComponent(pauseMessage.entity, new PauseComponent(pauseMessage.time)); } foreach (ref readonly var entity in ReadEntities()) { - ref readonly var pauseComponent = GetComponent(entity); + ref readonly var pauseComponent = ref GetComponent(entity); var timer = pauseComponent.timer; timer -= dt; @@ -49,7 +46,7 @@ public class PauseEngine : Engine } 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. diff --git a/content/concepts/message.md b/content/concepts/message.md index 46095c1..1b2deac 100644 --- a/content/concepts/message.md +++ b/content/concepts/message.md @@ -14,7 +14,7 @@ To define a message, declare a struct which implements the IMessage interface. using Encompass; public struct MotionMessage : IMessage { - public Vector2 motion; + public Vector2 Motion { get; } } ``` diff --git a/content/concepts/world_builder.md b/content/concepts/world_builder.md index cb0ac28..dba0250 100644 --- a/content/concepts/world_builder.md +++ b/content/concepts/world_builder.md @@ -31,22 +31,12 @@ public class MyGame : Game var entity = worldBuilder.CreateEntity(); - SetComponent(entity, new PositionComponent - { - x = 0, - y = 0 - }); + SetComponent(entity, new PositionComponent(0, 0)); + SetComponent(entity, new VelocityComponent(20, 0)); - SetComponent(entity, new VelocityComponent - { - x = 20, - y = 0 - }); - - SetComponent(entity, new TextureComponent - { - texture = TextureHelper.LoadTexture("assets/sprites/ball.png") - }); + SetComponent(entity, new TextureComponent( + TextureHelper.LoadTexture("Sprites/ball.png"); + ); world = worldBuilder.Build(); }