Bullets pooling

This commit is contained in:
Marco 2025-06-08 16:33:38 +02:00
commit fa3805ecfe
18 changed files with 280 additions and 69 deletions

View file

@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Controllers;
using Godot;
namespace Cirno.Scripts.Components.FSM.Player;
@ -13,18 +15,15 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
[Export] public double Cooldown { get; private set; } = 0.5f;
[Export] public double IceLife { get; private set; } = 4f;
[Export] public PackedScene IceScene { get; private set; }
[ExportGroup("Providers")]
[Export]
public ActorResourceProvider Shield { get; private set; }
[Export]
public InputProvider InputProvider { get; private set; }
[ExportGroup("Providers")] [Export] public ActorResourceProvider Shield { get; private set; }
[Export] public InputProvider InputProvider { get; private set; }
public bool Enabled { get; set; } = false;
private double _cooldownTimer = 0;
public override void EnterState(PlayerState state)
{
Enabled = true;
@ -37,7 +36,6 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{
}
public override void Process(double delta)
@ -68,31 +66,19 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
var ice = bullet.CreateSibling<Ice>(IceScene);
ice.Life = IceLife;
ice.FreezeModule = this;
}
}
private List<Bullet> GetNearbyBullets()
{
var nearbyBullets = new List<Bullet>();
foreach (var child in GameManager.Instance.BulletsContainer.GetChildren())
{
if (child is not Bullet bullet) continue;
if (bullet.BulletOwner is BulletOwner.Player)
{
continue;
}
if (bullet.IsFrozen) continue;
if (!bullet.BulletInfo.Freezable) continue;
var distance = GlobalPosition.DistanceTo(bullet.GlobalPosition);
if (distance <= FreezeRadius)
{
nearbyBullets.Add(bullet);
}
}
return nearbyBullets;
return (from child in PoolingManager.Instance.GetAllActiveBullets()
where child is not null
where child.Enabled // Could be redundant but better check in case of errors
where child.BulletOwner is not BulletOwner.Player
where !child.IsFrozen
where child.BulletInfo.Freezable
let distance = GlobalPosition.DistanceTo(child.GlobalPosition)
where distance <= FreezeRadius
select child).ToList();
}
}