entity field rework, now it is fuzzy and good 👍
This commit is contained in:
parent
5a6eb151ae
commit
e316fa3203
6 changed files with 35 additions and 33 deletions
|
|
@ -33,20 +33,20 @@ public partial class InitialPackedSceneSpawner : Node
|
|||
if (entity is RuntimeZombieData)
|
||||
{
|
||||
PoolContainer.Instance.Zombies.AddChild(node);
|
||||
node.GlobalPosition = position + new Vector2(0,0.5f*FieldParams.TileHeight);
|
||||
node.GlobalPosition = position + new Vector2(0, 0.5f * FieldParams.TileHeight);
|
||||
}
|
||||
else if (entity is RuntimePlantData plant)
|
||||
{
|
||||
PoolContainer.Instance.Plants.AddChild(plant);
|
||||
node.GlobalPosition = position;
|
||||
plant.Resource = GameRegistry.GetEntityByName(plant.Resource.GetInternalID()) as PlantResource;
|
||||
PoolContainer.Instance.EntityField[plant.Resource.Layer].Add(plant.GlobalPosition, plant);
|
||||
PoolContainer.Instance.TrySetEntity(plant.GlobalPosition, plant, plant.Resource.Layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
PoolContainer.Instance.Structures.AddChild(entity);
|
||||
node.GlobalPosition = position;
|
||||
PoolContainer.Instance.EntityField[1].Add(entity.GlobalPosition, entity);
|
||||
PoolContainer.Instance.TrySetEntity(entity.GlobalPosition,entity,1);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ public partial class PlantField : Node2D
|
|||
|
||||
bool canPlace = _resource != null
|
||||
&& inBoundary
|
||||
&& PoolContainer.Instance.EntityField[_resource.Layer].ContainsKey(expected_pos) == false
|
||||
&& PoolContainer.Instance.IsPositionVacant(expected_pos,_resource.Layer)
|
||||
&& RuntimeLevelData.Instance.CheckSpendSun((int)_resource.Cost);
|
||||
|
||||
// Setting visuals
|
||||
|
|
@ -96,7 +96,7 @@ public partial class PlantField : Node2D
|
|||
plant.GlobalPosition = (_plantSetter.GlobalPosition / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
||||
plant.Resource = (PlantResource)_resource;
|
||||
|
||||
PoolContainer.Instance.EntityField[_resource.Layer].Add(plant.GlobalPosition, plant);
|
||||
PoolContainer.Instance.TrySetEntity(plant.GlobalPosition,plant,_resource.Layer);
|
||||
|
||||
RuntimeLevelData.Instance.SpendSun((int)_resource.Cost);
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue