Hello, I am arraying entities in 2d grid layout. I have to find the Max Column and Min Column values so that I can find the maximum distance that the grid can travel left and right on the screen. But the code below is trying to find the max and min values, but the min and max values do not change even if all the columns are gone from the beginning or the end. I guess even though the entities are lost, the component data continues to live in the chunk. Is there a way to delete it completely or to check if a certain component data has an entity?
public partial class AlienMovement : SystemBase
{
public float time;
public int index;
public int maxRowValue;
public int maxColumnValue;
public int minColumnValue;
public bool forward;
protected override void OnCreate()
{
time = 1;
}
protected override void OnUpdate()
{
var soldierQuery = GetEntityQuery(typeof(Translation), ComponentType.ReadOnly<RowData>(), ComponentType.ReadOnly<ColumnData>());
Entities
.WithAll<SpaceInvaderTag>()
.ForEach((in RowData rowData) =>
{
int thisNum = rowData.Row;
if (thisNum > maxRowValue) { maxRowValue = thisNum; }
}).WithoutBurst().Run();
Entities
.WithAll<SpaceInvaderTag>()
.ForEach((in ColumnData columnData) =>
{
int minNumber = columnData.Column;
int maxNumber = columnData.Column;
if(minNumber < minColumnValue) { minColumnValue = minNumber; }
if(maxNumber > maxColumnValue) { maxColumnValue = maxNumber; }
}).WithoutBurst().Run();
Debug.Log("minColumData: " + minColumnValue);
Debug.Log("maxColumnData: " + maxColumnValue);
//Zaman 1 sn'ye artıkça index'de 1 artıyor.
time -= Time.DeltaTime;
if (time < 0)
{
index++;
time = 1;
}
//Row index'i maxValue ulaşınca sıfırlıyor
if (index >= maxRowValue + 1) { index = 0; }
//var denemeJob = new DenemeJob
//{
// TranslationTypeHandle = GetComponentTypeHandle<Translation>(false),
// RowTypeHandle = GetComponentTypeHandle<RowData>(true),
// DeltaTime = Time.DeltaTime,
// Row = index
//};
//Dependency = denemeJob.Schedule(soldierQuery, Dependency);
//RequireForUpdate(soldierQuery);
}
}
public partial struct DenemeJob : IJobEntityBatch
{
public ComponentTypeHandle<Translation> TranslationTypeHandle;
[ReadOnly] public ComponentTypeHandle<RowData> RowTypeHandle;
public float DeltaTime;
public int Row;
public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
{
var translationArray = batchInChunk.GetNativeArray(TranslationTypeHandle);
var rowDataArray = batchInChunk.GetNativeArray(RowTypeHandle);
for (int j = 0; j < batchInChunk.Count; j++)
{
if (Row == rowDataArray[j].Row)
{
var translation = translationArray[j];
translation.Value.x += 0.7f * DeltaTime;
translationArray[j] = translation;
}
}
}
}