From 57fb02daf2fda6ed400a150c9a25e02213a59576 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Mon, 27 Jul 2020 14:12:47 -0700 Subject: [PATCH] add update method to code blocks for clarity --- content/pong/ball/bouncing/motion_engine.md | 101 ++++++++++---------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/content/pong/ball/bouncing/motion_engine.md b/content/pong/ball/bouncing/motion_engine.md index 2a56404..4e0e205 100644 --- a/content/pong/ball/bouncing/motion_engine.md +++ b/content/pong/ball/bouncing/motion_engine.md @@ -81,28 +81,28 @@ Next, in a separate block, let's consolidate our MotionMessages per Entity. private readonly Dictionary _moveAmounts = new Dictionary(); - ... - - _spatialHash.Clear(); - _moveAmounts.Clear(); - - foreach (var entity in TrackedEntities) + public override void Update(double dt) { - ... - } + _spatialHash.Clear(); + _moveAmounts.Clear(); - foreach (ref readonly var entity in ReadEntities()) - { - ref readonly var positionComponent = ref GetComponent(entity); - - _moveAmounts[entity] = Vector2.Zero; - foreach (var motionMessage in ReadMessagesWithEntity(entity)) + foreach (var entity in TrackedEntities) { - _moveAmounts[entity] += motionMessage.Movement; + ... } - } - ... + foreach (ref readonly var entity in ReadEntities()) + { + ref readonly var positionComponent = ref GetComponent(entity); + + _moveAmounts[entity] = Vector2.Zero; + foreach (var motionMessage in ReadMessagesWithEntity(entity)) + { + _moveAmounts[entity] += motionMessage.Movement; + } + } + + ... ``` This is where our *IHasEntity* optimization comes in. It allows us to use the **ReadMessagesWithEntity** method. This method efficiently queries messages that refer to the given entity. @@ -236,45 +236,48 @@ namespace PongFE.Engines Now, in the final block of our **MotionEngine**, we can perform our collision tests and send out our messages. ```cs - ... - - foreach (var entity in TrackedEntities) + public override void Update(double dt) { ... - } - foreach (ref readonly var entity in ReadEntities()) - { - ... - } - - foreach (var entity in TrackedEntities) - { - Vector2 moveAmount = _moveAmounts[entity]; - - ref readonly var positionComponent = ref GetComponent(entity); - var projectedPosition = positionComponent.Position + moveAmount; - - ref readonly var collisionComponent = ref GetComponent(entity); - var rectangle = collisionComponent.Rectangle; - var (xHit, yHit, newPosition, collisionEntity) = SolidCollisionPosition(rectangle, positionComponent.Position, projectedPosition); - - if (xHit || yHit) + foreach (var entity in TrackedEntities) { - projectedPosition = newPosition; - - if (xHit) - { - SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Horizontal)); - } - else - { - SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical)); - } + ... } + foreach (ref readonly var entity in ReadEntities()) + { + ... + } - SendMessage(new UpdatePositionMessage(entity, projectedPosition)); + foreach (var entity in TrackedEntities) + { + Vector2 moveAmount = _moveAmounts[entity]; + + ref readonly var positionComponent = ref GetComponent(entity); + var projectedPosition = positionComponent.Position + moveAmount; + + ref readonly var collisionComponent = ref GetComponent(entity); + var rectangle = collisionComponent.Rectangle; + var (xHit, yHit, newPosition, collisionEntity) = SolidCollisionPosition(rectangle, positionComponent.Position, projectedPosition); + + if (xHit || yHit) + { + projectedPosition = newPosition; + + if (xHit) + { + SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Horizontal)); + } + else + { + SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical)); + } + } + + + SendMessage(new UpdatePositionMessage(entity, projectedPosition)); + } } ```