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
|
|
@ -20,7 +20,7 @@ public partial class RuntimePlantData : Entity
|
||||||
}
|
}
|
||||||
public override void Kill()
|
public override void Kill()
|
||||||
{
|
{
|
||||||
PoolContainer.Instance.EntityField[Resource.Layer].Remove(GlobalPosition);
|
PoolContainer.Instance.RemoveEntity(GlobalPosition, Resource.Layer);
|
||||||
QueueFree();
|
QueueFree();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ public partial class ShovelButton : TextureButton
|
||||||
var checkedPosition = (position / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
var checkedPosition = (position / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
||||||
for (int i = FieldParams.LayersCount - 1; i >= 0; i--)
|
for (int i = FieldParams.LayersCount - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (PoolContainer.Instance.EntityField[i].TryGetValue(checkedPosition, out var entity) && entity is RuntimePlantData plantData)
|
if (PoolContainer.Instance.TryGetEntity(checkedPosition, out RuntimePlantData plantData,i))
|
||||||
{
|
{
|
||||||
return plantData;
|
return plantData;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,13 +40,13 @@ public partial class InitialPackedSceneSpawner : Node
|
||||||
PoolContainer.Instance.Plants.AddChild(plant);
|
PoolContainer.Instance.Plants.AddChild(plant);
|
||||||
node.GlobalPosition = position;
|
node.GlobalPosition = position;
|
||||||
plant.Resource = GameRegistry.GetEntityByName(plant.Resource.GetInternalID()) as PlantResource;
|
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
|
else
|
||||||
{
|
{
|
||||||
PoolContainer.Instance.Structures.AddChild(entity);
|
PoolContainer.Instance.Structures.AddChild(entity);
|
||||||
node.GlobalPosition = position;
|
node.GlobalPosition = position;
|
||||||
PoolContainer.Instance.EntityField[1].Add(entity.GlobalPosition, entity);
|
PoolContainer.Instance.TrySetEntity(entity.GlobalPosition,entity,1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public partial class PlantField : Node2D
|
||||||
|
|
||||||
bool canPlace = _resource != null
|
bool canPlace = _resource != null
|
||||||
&& inBoundary
|
&& inBoundary
|
||||||
&& PoolContainer.Instance.EntityField[_resource.Layer].ContainsKey(expected_pos) == false
|
&& PoolContainer.Instance.IsPositionVacant(expected_pos,_resource.Layer)
|
||||||
&& RuntimeLevelData.Instance.CheckSpendSun((int)_resource.Cost);
|
&& RuntimeLevelData.Instance.CheckSpendSun((int)_resource.Cost);
|
||||||
|
|
||||||
// Setting visuals
|
// 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.GlobalPosition = (_plantSetter.GlobalPosition / FieldParams.Tile).Ceil() * FieldParams.Tile - new Vector2(20, 14);
|
||||||
plant.Resource = (PlantResource)_resource;
|
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);
|
RuntimeLevelData.Instance.SpendSun((int)_resource.Cost);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,39 +24,39 @@ public partial class PoolContainer : Node2D
|
||||||
|
|
||||||
public static PoolContainer Instance { get; private set; }
|
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()
|
public override void _EnterTree()
|
||||||
{
|
{
|
||||||
Instance = this;
|
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))
|
var position = VectorToIndex(positionVector);
|
||||||
{
|
result = EntityField[layer, position] as T;
|
||||||
result = EntityField[layer][key];
|
return EntityField[layer,position] != null
|
||||||
}
|
|
||||||
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
|
|
||||||
&& result != 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)
|
public void SpawnParticles(PackedScene particles, Vector2 position, float rotation = 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ public class FieldParams
|
||||||
public const int TileWidth = 50;
|
public const int TileWidth = 50;
|
||||||
public const int TileHeight = 60;
|
public const int TileHeight = 60;
|
||||||
public const int LayersCount = 3;
|
public const int LayersCount = 3;
|
||||||
|
public const int ColumnsCount = 9;
|
||||||
|
public const int RowsCount = 5;
|
||||||
public static Vector2I LeftFieldBoundary = new(305, 76);
|
public static Vector2I LeftFieldBoundary = new(305, 76);
|
||||||
public static Vector2I RightFieldBoundary = new(755, 376);
|
public static Vector2I RightFieldBoundary = new(755, 376);
|
||||||
public static readonly Vector2 Tile = new(TileWidth, TileHeight);
|
public static readonly Vector2 Tile = new(TileWidth, TileHeight);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue