entity field rework, now it is fuzzy and good 👍

This commit is contained in:
Rendo 2025-07-30 05:06:25 +05:00
commit e316fa3203
6 changed files with 35 additions and 33 deletions

View file

@ -24,39 +24,39 @@ public partial class PoolContainer : Node2D
public static PoolContainer Instance { get; private set; }
public Dictionary<Vector2, Entity>[] EntityField = { new(), new(), new() };
//public Dictionary<Vector2, Entity>[] EntityField = { new(), new(), new() };
private readonly Entity[,] EntityField = new Entity[3,FieldParams.ColumnsCount * FieldParams.RowsCount];
public override void _EnterTree()
{
Instance = this;
}
public bool TryGetEntity(Vector2 key, out Entity result, int layer = 1)
public bool TryGetEntity<T>(Vector2 positionVector, out T result, int layer = 1) where T : Entity
{
if (EntityField[layer].ContainsKey(key))
{
result = EntityField[layer][key];
}
else
{
result = null;
}
return EntityField[layer].ContainsKey(key)
&& EntityField[layer][key] != null;
}
public bool TryGetEntity<T>(Vector2 key, out T result, int layer = 1) where T : class
{
if (EntityField[layer].ContainsKey(key))
{
result = EntityField[layer][key] as T;
}
else
{
result = null;
}
return EntityField[layer].ContainsKey(key)
&& EntityField[layer][key] != null
var position = VectorToIndex(positionVector);
result = EntityField[layer, position] as T;
return EntityField[layer,position] != null
&& result != null;
}
public bool TrySetEntity(Vector2 positionVector, Entity whatToSet, int layer = 1)
{
var position = VectorToIndex(positionVector);
if (IsPositionVacant(positionVector, layer) == false) return false;
EntityField[layer, position] = whatToSet;
return true;
}
public bool IsPositionVacant(Vector2 positionVector, int layer = 1)
{
return EntityField[layer, VectorToIndex(positionVector)] == null;
}
public void RemoveEntity(Vector2 positionVector, int layer = 1)
{
EntityField[layer, VectorToIndex(positionVector)] = null;
}
public static int VectorToIndex(Vector2 vector)
{
var intedVec = (vector - FieldParams.LeftFieldBoundary) / FieldParams.Tile;
return (int)intedVec.X + (int)intedVec.Y * FieldParams.ColumnsCount;
}
public void SpawnParticles(PackedScene particles, Vector2 position, float rotation = 0)
{