74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
using Godot;
|
|
using System.Collections.Generic;
|
|
using Newlon.Particles;
|
|
|
|
namespace Newlon.Components.Level;
|
|
|
|
//
|
|
// PoolContainer contains nodes that contain different elemnts that generate during runtime
|
|
// Is not pool in traditional sense, but named like that to prevent repetition
|
|
//
|
|
|
|
public partial class PoolContainer : Node2D
|
|
{
|
|
[Export]
|
|
public Node2D Zombies { get; private set; }
|
|
[Export]
|
|
public Node2D Plants { get; private set; }
|
|
[Export]
|
|
public Node2D Projectiles { get; private set; }
|
|
[Export]
|
|
public Node2D Structures { get; private set; }
|
|
[Export]
|
|
public Node2D Particles { get; private set; }
|
|
|
|
public static PoolContainer Instance { get; private set; }
|
|
|
|
//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<T>(Vector2 positionVector, out T result, int layer = 1) where T : Entity
|
|
{
|
|
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)
|
|
{
|
|
var emitter = particles.Instantiate<StandardParticles>();
|
|
Instance.Particles.AddChild(emitter);
|
|
emitter.GlobalPosition = position;
|
|
emitter.GlobalRotation = rotation;
|
|
}
|
|
public void SpawnParticles(PackedScene particles, Transform2D transform)
|
|
{
|
|
var emitter = particles.Instantiate<StandardParticles>();
|
|
Instance.Particles.AddChild(emitter);
|
|
emitter.GlobalTransform = transform;
|
|
}
|
|
}
|