add update method to code blocks for clarity
continuous-integration/drone/push Build is passing Details

main
Evan Hemsley 2020-07-27 14:12:47 -07:00
parent f84c7d1627
commit 57fb02daf2
1 changed files with 52 additions and 49 deletions

View File

@ -81,28 +81,28 @@ Next, in a separate block, let's consolidate our MotionMessages per Entity.
private readonly Dictionary<Entity, Vector2> _moveAmounts = new Dictionary<Entity, Vector2>(); private readonly Dictionary<Entity, Vector2> _moveAmounts = new Dictionary<Entity, Vector2>();
... public override void Update(double dt)
_spatialHash.Clear();
_moveAmounts.Clear();
foreach (var entity in TrackedEntities)
{ {
... _spatialHash.Clear();
} _moveAmounts.Clear();
foreach (ref readonly var entity in ReadEntities<PositionComponent>()) foreach (var entity in TrackedEntities)
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_moveAmounts[entity] = Vector2.Zero;
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
{ {
_moveAmounts[entity] += motionMessage.Movement; ...
} }
}
... foreach (ref readonly var entity in ReadEntities<PositionComponent>())
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_moveAmounts[entity] = Vector2.Zero;
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(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. 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. Now, in the final block of our **MotionEngine**, we can perform our collision tests and send out our messages.
```cs ```cs
... public override void Update(double dt)
foreach (var entity in TrackedEntities)
{ {
... ...
}
foreach (ref readonly var entity in ReadEntities<PositionComponent>()) foreach (var entity in TrackedEntities)
{
...
}
foreach (var entity in TrackedEntities)
{
Vector2 moveAmount = _moveAmounts[entity];
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
var projectedPosition = positionComponent.Position + moveAmount;
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(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));
}
} }
foreach (ref readonly var entity in ReadEntities<PositionComponent>())
{
...
}
SendMessage(new UpdatePositionMessage(entity, projectedPosition)); foreach (var entity in TrackedEntities)
{
Vector2 moveAmount = _moveAmounts[entity];
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
var projectedPosition = positionComponent.Position + moveAmount;
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(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));
}
} }
``` ```