47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
using Encompass;
|
|
using PongFE.Components;
|
|
using PongFE.Enums;
|
|
using PongFE.Messages;
|
|
|
|
namespace PongFE.Engines
|
|
{
|
|
[Reads(
|
|
typeof(ComputerControlComponent),
|
|
typeof(PositionComponent),
|
|
typeof(CanBeTrackedComponent)
|
|
)]
|
|
[Sends(typeof(PaddleMoveMessage))]
|
|
public class ComputerControlEngine : Engine
|
|
{
|
|
public override void Update(double dt)
|
|
{
|
|
foreach (ref readonly var entity in ReadEntities<ComputerControlComponent>())
|
|
{
|
|
if (HasComponent<PositionComponent>(entity))
|
|
{
|
|
ref readonly var trackerPositionComponent = ref GetComponent<PositionComponent>(entity);
|
|
|
|
if (SomeComponent<CanBeTrackedComponent>())
|
|
{
|
|
ref readonly var trackedEntity = ref ReadEntity<CanBeTrackedComponent>();
|
|
|
|
if (HasComponent<PositionComponent>(trackedEntity))
|
|
{
|
|
ref readonly var trackedPositionComponent = ref GetComponent<PositionComponent>(trackedEntity);
|
|
|
|
if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y > 20)
|
|
{
|
|
SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Up));
|
|
}
|
|
else if (trackerPositionComponent.Position.Y - trackedPositionComponent.Position.Y < -20)
|
|
{
|
|
SendMessage(new PaddleMoveMessage(entity, PaddleMoveDirection.Down));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|