some optimization and cleanup
parent
83754fbe44
commit
72d99b36f3
|
@ -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;
|
||||
|
|
|
@ -27,7 +27,6 @@ namespace PongFE.Engines
|
|||
{
|
||||
if (HasComponent<CanBeBouncedComponent>(b))
|
||||
{
|
||||
System.Console.WriteLine("bounce");
|
||||
SendMessage(new BounceMessage(b, hitOrientation));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,66 +8,48 @@ 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>())
|
||||
{
|
||||
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
|
||||
|
||||
if (HasComponent<PositionComponent>(entity))
|
||||
foreach (var entity in TrackedEntities)
|
||||
{
|
||||
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
||||
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
|
||||
|
||||
_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);
|
||||
|
@ -85,7 +67,7 @@ namespace PongFE.Engines
|
|||
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue