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

@ -5,6 +5,7 @@ using System.Diagnostics;
using System.Linq;
using Cirno.Scripts;
using Cirno.Scripts.Components;
using Cirno.Scripts.Controllers;
using Cirno.Scripts.Resources;
public partial class Bullet : Area2D
@ -31,16 +32,21 @@ public partial class Bullet : Area2D
public bool IsGrazed { get; set; } = false;
public bool IsFrozen { get; private set; } = false;
public bool Enabled { get; private set; } = false;
[Signal] public delegate void OnDestroyEventHandler();
private AudioStreamPlayer2D _grazeSound;
private GpuParticles2D _grazeParticles;
private CollisionShape2D _collisionShape2D;
public override void _Ready()
{
_grazeSound = GetNodeOrNull<AudioStreamPlayer2D>("AudioStreamPlayer2D");
_grazeParticles = GetNodeOrNull<GpuParticles2D>("GrazeParticles");
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
@ -64,8 +70,43 @@ public partial class Bullet : Area2D
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
}
/// <summary>
/// Enables the bullet, shows the sprite and activates collisions
/// </summary>
public void Enable()
{
Enabled = true;
Show();
if (this._collisionShape2D is null)
{
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
_collisionShape2D.SetDeferred(CollisionShape2D.PropertyName.Disabled, false);
}
/// <summary>
/// Disables the bullet, hides the sprite and disables collisions
/// </summary>
public void Disable(bool hideSprite = true)
{
Enabled = false;
if (hideSprite)
{
Hide();
}
if (this._collisionShape2D is null)
{
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
_collisionShape2D.SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
}
public void Graze()
{
if (!Enabled) return;
_grazeSound?.Play();
if (_grazeParticles is not null)
{
@ -172,6 +213,7 @@ public partial class Bullet : Area2D
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (!Enabled) return;
_elapsedTime += delta;
if (_elapsedTime >= _bulletInfo.LifeTime)
@ -182,6 +224,7 @@ public partial class Bullet : Area2D
public override void _PhysicsProcess(double delta)
{
if (!Enabled) return;
if (_bulletInfo != null)
{
ApplyTimeModifiers(delta);
@ -197,6 +240,7 @@ public partial class Bullet : Area2D
private void ControlBullet(double delta)
{
if (!Enabled) return;
var axis = Input.GetAxis("left", "right");
if (axis != 0)
@ -208,6 +252,7 @@ public partial class Bullet : Area2D
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
if (!Enabled) return;
//Debug.WriteLine("Destroy bullet out of screen");
Destroy();
}
@ -259,6 +304,7 @@ public partial class Bullet : Area2D
public void RequestCollisionDestruction()
{
if (!Enabled) return;
if (_bulletInfo.DestroyOnCollision)
{
Destroy();
@ -274,14 +320,16 @@ public partial class Bullet : Area2D
//particle.Init();
}
EmitSignal(SignalName.OnDestroy);
QueueFree();
//QueueFree();
PoolingManager.Instance.DisableBullet(this);
}
public void Freeze()
{
IsFrozen = true;
EmitSignal(SignalName.OnDestroy);
QueueFree();
//QueueFree();
PoolingManager.Instance.DisableBullet(this);
}
}