-
Notifications
You must be signed in to change notification settings - Fork 211
/
ObjectPool.ts
85 lines (67 loc) · 2.47 KB
/
ObjectPool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Component, instantiate, Node, Prefab } from "cc";
export class ObjectPool<T extends Component> {
private prefab: Prefab;
private parent: Node;
private pooledObjects: PooledObject<T>[] = [];
private componentName: string;
public constructor(prefab: Prefab, parent: Node, defaultPoolCount: number, componentName: string) {
this.prefab = prefab;
this.parent = parent;
this.componentName = componentName;
for (let i = 0; i < defaultPoolCount; i++) {
this.createNew();
}
}
public borrow(): T {
const objectToBorrow: PooledObject<T> | null = this.pooledObjects.find((o) => !o.IsBorrowed);
if (objectToBorrow != null) {
return objectToBorrow.borrow();
}
return this.createNew().borrow();
}
public return(object: T): void {
const objectToReturn: PooledObject<T> | null = this.pooledObjects.find((o) => o.Equals(object));
if (objectToReturn == null) {
throw new Error("Object " + this.prefab.name + " is not a member of the pool");
}
objectToReturn.return();
}
private createNew(): PooledObject<T> {
const newPooledObject: PooledObject<T> = new PooledObject(this.prefab, this.parent, this.componentName);
this.pooledObjects.push(newPooledObject);
return newPooledObject;
}
}
class PooledObject<T extends Component> {
private isBorrowed = false;
private defaultParent: Node;
private instancedNode: Node;
private instancedComponent: T;
public constructor(prefab: Prefab, defaultParent: Node, componentName: string) {
this.defaultParent = defaultParent;
this.instancedNode = instantiate(prefab);
this.instancedComponent = <T>this.instancedNode.getComponent(componentName);
if (this.instancedComponent == null) {
console.error("Object " + prefab.name + " does not have component " + componentName);
}
this.clear();
}
public get IsBorrowed(): boolean {
return this.isBorrowed;
}
public Equals(component: T): boolean {
return this.instancedComponent == component;
}
public borrow(): T {
this.isBorrowed = true;
return this.instancedComponent;
}
public return(): void {
this.clear();
}
private clear(): void {
this.instancedNode.active = false;
this.instancedNode.parent = this.defaultParent;
this.isBorrowed = false;
}
}