some optimization and cleanup

main
Evan Hemsley 2020-07-14 16:07:58 -07:00
parent 83754fbe44
commit 72d99b36f3
3 changed files with 20 additions and 40 deletions

View File

@ -19,7 +19,6 @@ namespace PongFE.Engines
{
if (HasComponent<BounceResponseComponent>(message.Entity) && HasComponent<VelocityComponent>(message.Entity))
{
System.Console.WriteLine("hello bounce!");
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Entity);
Vector2 newVelocity;

View File

@ -27,7 +27,6 @@ namespace PongFE.Engines
{
if (HasComponent<CanBeBouncedComponent>(b))
{
System.Console.WriteLine("bounce");
SendMessage(new BounceMessage(b, hitOrientation));
}
}

View File

@ -8,85 +8,67 @@ using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(
typeof(PositionComponent),
typeof(CollisionComponent)
)]
[Receives(typeof(MotionMessage))]
[Sends(
typeof(UpdatePositionMessage),
typeof(CollisionMessage)
)]
[QueryWith(typeof(PositionComponent), typeof(CollisionComponent))]
public class MotionEngine : Engine
{
private readonly SpatialHash<Entity> _spatialHash = new SpatialHash<Entity>(32);
private readonly Dictionary<Entity, Vector2> _moveAmounts = new Dictionary<Entity, Vector2>();
private readonly Dictionary<Entity, Position2D> _finalPositions = new Dictionary<Entity, Position2D>();
public override void Update(double dt)
{
_spatialHash.Clear();
_moveAmounts.Clear();
_finalPositions.Clear();
foreach (ref readonly var entity in ReadEntities<CollisionComponent>())
foreach (var entity in TrackedEntities)
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
if (HasComponent<PositionComponent>(entity))
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_spatialHash.Insert(entity, collisionComponent.Rectangle, new Transform2D(positionComponent.Position));
}
_spatialHash.Insert(entity, collisionComponent.Rectangle, new Transform2D(positionComponent.Position));
}
foreach (ref readonly var entity in ReadEntities<PositionComponent>())
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_finalPositions[entity] = positionComponent.Position;
_moveAmounts[entity] = Vector2.Zero;
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
{
_moveAmounts[entity] += motionMessage.Movement;
}
}
foreach (var pair in _moveAmounts)
foreach (var entity in TrackedEntities)
{
var entity = pair.Key;
var moveAmount = pair.Value;
Vector2 moveAmount = _moveAmounts[entity];
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
var projectedPosition = positionComponent.Position + moveAmount;
if (!HasComponent<CollisionComponent>(entity))
{
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
}
else
{
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
var rectangle = collisionComponent.Rectangle;
var (xHit, yHit, newPosition, collisionEntity) = SolidCollisionPosition(rectangle, positionComponent.Position, projectedPosition);
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)
if (xHit || yHit)
{
projectedPosition = newPosition;
if (xHit)
{
projectedPosition = newPosition;
if (xHit)
{
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Horizontal));
}
else
{
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical));
}
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Horizontal));
}
else
{
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical));
}
}
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
}
}