-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fGameObjectPool, allows for re-use of GOs where we need a set of …
…similar objects (eg like a bunch of polylines)
- Loading branch information
Showing
2 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using g3; | ||
|
||
namespace f3 | ||
{ | ||
/// <summary> | ||
/// Pool of re-usable fGameObject instances, allocated by factory function you must provide to constructor. | ||
/// Unused GOs are automatically parented to per-instance hidden parent GO, to avoid cluttering scene. | ||
/// When a GO is freed, it is hidden. | ||
/// When a GO is allocated from pool, it is set to have no parent, visible, and transform is cleared. | ||
/// If ReinitializeF is set, this is also called on re-used GOs (*not* called on newly-allocated GOs as | ||
/// you can do that in your FactoryF) | ||
/// </summary> | ||
public class fGameObjectPool<T> where T : fGameObject | ||
{ | ||
public Func<T> AllocatorFactoryF; | ||
public Action<T> ReinitializeF; | ||
|
||
fGameObject pool_parent; | ||
|
||
List<T> Allocated; | ||
List<T> Free; | ||
|
||
public fGameObjectPool(Func<T> factoryF) | ||
{ | ||
Allocated = new List<T>(); | ||
Free = new List<T>(); | ||
|
||
pool_parent = GameObjectFactory.CreateParentGO("pool_parent"); | ||
|
||
AllocatorFactoryF = factoryF; | ||
} | ||
|
||
|
||
public void Destroy() | ||
{ | ||
foreach (var go in Allocated) | ||
go.Destroy(); | ||
Allocated.Clear(); | ||
Free.Clear(); | ||
pool_parent.Destroy(); | ||
} | ||
|
||
|
||
public T Allocate() | ||
{ | ||
if (Free.Count > 0) { | ||
T go = Free[Free.Count - 1]; | ||
Free.RemoveAt(Free.Count - 1); | ||
go.SetParent(null); | ||
go.SetVisible(true); | ||
go.SetLocalFrame(Frame3f.Identity); | ||
go.SetLocalScale(1.0f); | ||
if (ReinitializeF != null) | ||
ReinitializeF(go); | ||
return go; | ||
} else { | ||
T go = AllocatorFactoryF(); | ||
Allocated.Add(go); | ||
return go; | ||
} | ||
} | ||
|
||
|
||
public void FreeAll() | ||
{ | ||
foreach (T go in Allocated) { | ||
if (go.IsVisible()) { | ||
go.SetVisible(false); | ||
go.SetParent(pool_parent); | ||
} | ||
} | ||
Free.Clear(); | ||
Free.AddRange(Allocated); | ||
} | ||
|
||
} | ||
} |