some optimization and cleanup
parent
83754fbe44
commit
72d99b36f3
|
@ -19,7 +19,6 @@ namespace PongFE.Engines
|
||||||
{
|
{
|
||||||
if (HasComponent<BounceResponseComponent>(message.Entity) && HasComponent<VelocityComponent>(message.Entity))
|
if (HasComponent<BounceResponseComponent>(message.Entity) && HasComponent<VelocityComponent>(message.Entity))
|
||||||
{
|
{
|
||||||
System.Console.WriteLine("hello bounce!");
|
|
||||||
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Entity);
|
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Entity);
|
||||||
|
|
||||||
Vector2 newVelocity;
|
Vector2 newVelocity;
|
||||||
|
|
|
@ -27,7 +27,6 @@ namespace PongFE.Engines
|
||||||
{
|
{
|
||||||
if (HasComponent<CanBeBouncedComponent>(b))
|
if (HasComponent<CanBeBouncedComponent>(b))
|
||||||
{
|
{
|
||||||
System.Console.WriteLine("bounce");
|
|
||||||
SendMessage(new BounceMessage(b, hitOrientation));
|
SendMessage(new BounceMessage(b, hitOrientation));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,66 +8,48 @@ using PongFE.Messages;
|
||||||
|
|
||||||
namespace PongFE.Engines
|
namespace PongFE.Engines
|
||||||
{
|
{
|
||||||
[Reads(
|
|
||||||
typeof(PositionComponent),
|
|
||||||
typeof(CollisionComponent)
|
|
||||||
)]
|
|
||||||
[Receives(typeof(MotionMessage))]
|
[Receives(typeof(MotionMessage))]
|
||||||
[Sends(
|
[Sends(
|
||||||
typeof(UpdatePositionMessage),
|
typeof(UpdatePositionMessage),
|
||||||
typeof(CollisionMessage)
|
typeof(CollisionMessage)
|
||||||
)]
|
)]
|
||||||
|
[QueryWith(typeof(PositionComponent), typeof(CollisionComponent))]
|
||||||
public class MotionEngine : Engine
|
public class MotionEngine : Engine
|
||||||
{
|
{
|
||||||
private readonly SpatialHash<Entity> _spatialHash = new SpatialHash<Entity>(32);
|
private readonly SpatialHash<Entity> _spatialHash = new SpatialHash<Entity>(32);
|
||||||
private readonly Dictionary<Entity, Vector2> _moveAmounts = new Dictionary<Entity, Vector2>();
|
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)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
_spatialHash.Clear();
|
_spatialHash.Clear();
|
||||||
_moveAmounts.Clear();
|
_moveAmounts.Clear();
|
||||||
_finalPositions.Clear();
|
|
||||||
|
|
||||||
foreach (ref readonly var entity in ReadEntities<CollisionComponent>())
|
foreach (var entity in TrackedEntities)
|
||||||
{
|
|
||||||
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
|
|
||||||
|
|
||||||
if (HasComponent<PositionComponent>(entity))
|
|
||||||
{
|
{
|
||||||
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
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));
|
_spatialHash.Insert(entity, collisionComponent.Rectangle, new Transform2D(positionComponent.Position));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach (ref readonly var entity in ReadEntities<PositionComponent>())
|
foreach (ref readonly var entity in ReadEntities<PositionComponent>())
|
||||||
{
|
{
|
||||||
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
||||||
|
|
||||||
_finalPositions[entity] = positionComponent.Position;
|
|
||||||
_moveAmounts[entity] = Vector2.Zero;
|
_moveAmounts[entity] = Vector2.Zero;
|
||||||
|
|
||||||
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
|
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
|
||||||
{
|
{
|
||||||
_moveAmounts[entity] += motionMessage.Movement;
|
_moveAmounts[entity] += motionMessage.Movement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var pair in _moveAmounts)
|
foreach (var entity in TrackedEntities)
|
||||||
{
|
{
|
||||||
var entity = pair.Key;
|
Vector2 moveAmount = _moveAmounts[entity];
|
||||||
var moveAmount = pair.Value;
|
|
||||||
|
|
||||||
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
|
||||||
|
|
||||||
var projectedPosition = positionComponent.Position + moveAmount;
|
var projectedPosition = positionComponent.Position + moveAmount;
|
||||||
|
|
||||||
if (!HasComponent<CollisionComponent>(entity))
|
|
||||||
{
|
|
||||||
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
|
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
|
||||||
var rectangle = collisionComponent.Rectangle;
|
var rectangle = collisionComponent.Rectangle;
|
||||||
var (xHit, yHit, newPosition, collisionEntity) = SolidCollisionPosition(rectangle, positionComponent.Position, projectedPosition);
|
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 CollisionMessage(entity, collisionEntity, HitOrientation.Vertical));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
|
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue