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,4 +1,5 @@
using System;
using Cirno.Scripts.Controllers;
using Cirno.Scripts.Weapons;
using Godot;
using Godot.Collections;
@ -22,10 +23,9 @@ public partial class BulletSpawner : Node2D
for (int i = 0; i < bulletInfo.BulletCount; i++)
{
if (bulletInfo.IsLaser)
bullet = this.CreateChildOf<LaserBullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
else
bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
// bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
bullet = PoolingManager.Instance.SpawnBullet(bulletInfo.OriginalBulletResource);
bullet.GlobalPosition = bulletInfo.Position;
// var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);

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();
}
}

View file

@ -33,12 +33,11 @@ public partial class PlayerGrazingModule : PlayerArea2DModule
if (!Enabled) return;
if (area is Bullet bullet)
{
if (!bullet.Enabled) return;
if (bullet.IsGrazed) return;
if (!bullet.BulletInfo.Grazeable) return;
if (bullet.BulletOwner is BulletOwner.Player) return;
GD.Print("Grazed");
bullet.Graze();
//bullet.IsGrazed = true;
var baseGrazeValue = bullet.BulletInfo.GrazeValue;