add update method to code blocks for clarity
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
f84c7d1627
commit
57fb02daf2
|
@ -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>();
|
||||
|
||||
...
|
||||
|
||||
_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<PositionComponent>())
|
||||
{
|
||||
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
||||
|
||||
_moveAmounts[entity] = Vector2.Zero;
|
||||
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
|
||||
foreach (var entity in TrackedEntities)
|
||||
{
|
||||
_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.
|
||||
|
@ -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<PositionComponent>())
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
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)
|
||||
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<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));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue