Hi,
I’m new to the ECS pattern, and I kind of block on how I could achieve what I want.
I have a PlayerActionSystem that when an Input is register, will check if there is a tile that will collide over the next move, if not, the player do a move.
Looks like that:
public class PlayerActionSystem : ComponentSystem
{
[Inject] public PlayersGroupData PlayersGroup;
[Inject] public ObstaclesGroupData ObstaclesGroup;
protected override void OnUpdate()
{
if (PlayersGroup.Length == 0)
{
return;
}
for (var i = 0; i < PlayersGroup.Length; ++i)
{
var playerInput = PlayersGroup.Inputs[i];
var targetPosition = PlayersGroup.GridPositions[i];
if (playerInput.MoveX != 0 || playerInput.MoveY != 0)
{
targetPosition.Value.x += playerInput.MoveX;
targetPosition.Value.y += playerInput.MoveY;
//Check Collision
Entity obstacle = Entity.Null;
if (ObstaclesGroup.Length > 0)
{
for (int j = 0; j < ObstaclesGroup.Length; j++)
{
if (ObstaclesGroup.GridPositions[j].Value == targetPosition.Value)
{
obstacle = ObstaclesGroup.Entities[j];
break;
}
}
}
if (obstacle.Equals(Entity.Null))
{
//Move
PostUpdateCommands.AddComponent(PlayersGroup.Entities[i], new MoveToTarget(targetPosition.Value));
}
else
{
//Do Damage
if (EntityManager.HasComponent<Health>(obstacle))
{
PostUpdateCommands.AddComponent(obstacle, new DoDamage(1));
PostUpdateCommands.AddSharedComponent(PlayersGroup.Entities[i], new TriggerAnimation("playerChop"));
}
//Move
if (EntityManager.HasComponent<FoodTag>(obstacle))
{
PostUpdateCommands.AddComponent(PlayersGroup.Entities[i], new MoveToTarget(targetPosition.Value));
}
}
PostUpdateCommands.AddComponent(PlayersGroup.Entities[i], new HasPlayTurn());
}
}
}
public struct PlayersGroupData
{
public int Length;
public EntityArray Entities;
public ComponentDataArray<GridPosition> GridPositions;
[ReadOnly] public ComponentDataArray<PlayerInput> Inputs;
[ReadOnly] public SubtractiveComponent<MoveToTarget> MoveToTarget;
[ReadOnly] public ComponentDataArray<TurnBasedTag> TurnBased;
[ReadOnly] public ComponentDataArray<IsActiveTurn> IsActiveTurn;
[ReadOnly] public SubtractiveComponent<HasPlayTurn> HasPlayTurn;
}
public struct ObstaclesGroupData
{
public int Length;
public EntityArray Entities;
[ReadOnly]
public ComponentDataArray<GridPosition> GridPositions;
[ReadOnly]
public ComponentDataArray<BoardObstacle> BoardObstacle;
}
}
The thing is that, I also want my EnemyAISystem to check for collision before they could move.
Since I can access entities through Inject, I would have to inject and reproduce the same code in my EnemyAISystem which doesn’t seem very efficient.
How would you go to share “global” logic like that between systems ?
Thanks