Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/Unity-Bullet-Hell/Scripts/Core/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public struct Node
public bool Active;
}

private Node[] Nodes;
private Queue<int> Available;
protected Node[] Nodes;
protected Queue<int> Available;

public int ActiveNodes
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public Pool(int capacity)
}
}

public void Clear()
public virtual void Clear()
{
Available.Clear();
for (int i = 0; i < Nodes.Length; i++)
Expand All @@ -59,14 +59,14 @@ public Node GetActive(int index)
return Nodes[index];
}

public Node Get()
public virtual Node Get()
{
int index = Available.Dequeue();
Nodes[index].Active = true;
return Nodes[index];
}

public void Return(int index)
public virtual void Return(int index)
{
if (Nodes[index].Active)
{
Expand Down
91 changes: 91 additions & 0 deletions Assets/Unity-Bullet-Hell/Scripts/Core/PoolFallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using UnityEngine;

namespace BulletHell
{
public class PoolFallback<T> : Pool<T> where T : ProjectileData, new()
{
/// <summary>
/// `GameObject` to put fallback `GameObject`s (as children)
/// </summary>
private static GameObject manager;
public static GameObject Manager
{
get
{
manager ??= new GameObject("ProjectileManagerFallback");
return manager;
}
}

private static Texture2D GetTexture2DFromTexture(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = new RenderTexture(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
return texture2D;
}

private static Sprite GetSpriteFromTexture2D(Texture2D texture)
{
var texture2d = GetTexture2DFromTexture(texture);
return Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
}

/// <summary>
/// The fallback projectile prefab to display
/// </summary>
private ProjectilePrefab prefab;

public PoolFallback(int capacity, ProjectilePrefab prefab) : base(capacity)
{
// Make sure the `GameObject` instantiated later inactive at first, and not interfere with the original prefab
var active = prefab.gameObject.activeSelf;
prefab.gameObject.SetActive(false);
this.prefab = GameObject.Instantiate(prefab, Manager.transform);
prefab.gameObject.SetActive(active);

InitSprite();
}

private void InitSprite()
{
var renderer = prefab.gameObject.AddComponent<SpriteRenderer>();
if (renderer) // not attached to a `SpriteRenderer`
{
renderer.sprite = prefab.Texture
? GetSpriteFromTexture2D(GetTexture2DFromTexture(prefab.Texture)) // use specified sprite
: Resources.Load<Sprite>("Textures/circlewhite"); // use default sprite
}
renderer.sortingOrder = Mathf.CeilToInt(prefab.ZIndez); // `ZIndez` is floating point so it won't be the same but still try to keep consistency
}

public override void Clear()
{
for (int i = 0; i < Nodes.Length; i++)
{
if (Nodes[i].Item.FallBackObject) GameObject.Destroy(Nodes[i].Item.FallBackObject);
}
base.Clear();
}

public override Node Get()
{
var node = base.Get();
node.Item.FallBackObject = GameObject.Instantiate(prefab.gameObject, Manager.transform);
return node;
}

public override void Return(int index)
{
var fo = Nodes[index].Item.FallBackObject;
if (fo) GameObject.Destroy(fo);
Nodes[index].Item.FallBackObject = null;
base.Return(index);
}
}
}
Loading