newlon/scripts/level/PoolContainer.cs
2025-07-18 03:36:49 +05:00

72 lines
2.1 KiB
C#

using Godot;
using System.Collections.Generic;
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() };
public override void _EnterTree()
{
Instance = this;
}
public bool TryGetEntity(Vector2 key, out Entity result, int layer = 1)
{
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
&& result != null;
}
public void SpawnParticles(PackedScene particles, Vector2 position)
{
var emitter = particles.Instantiate<StandardParticles>();
Instance.Particles.AddChild(emitter);
emitter.GlobalPosition = position;
}
public void SpawnParticles(PackedScene particles, Transform2D transform)
{
var emitter = particles.Instantiate<StandardParticles>();
Instance.Particles.AddChild(emitter);
emitter.GlobalTransform = transform;
}
}