UNPKG

@rpgjs/physic

Version:

A deterministic 2D top-down physics library for RPG, sandbox and MMO games

59 lines (58 loc) 1.17 kB
class ObjectPool { /** * Creates a new object pool * * @param createFn - Function to create new objects * @param maxSize - Maximum pool size (default: 100) * @param resetFn - Optional function to reset objects before reuse */ constructor(createFn, maxSize = 100, resetFn) { this.pool = []; this.createFn = createFn; this.resetFn = resetFn; this.maxSize = maxSize; } /** * Acquires an object from the pool * * @returns Object from pool or newly created */ acquire() { if (this.pool.length > 0) { const obj = this.pool.pop(); if (this.resetFn) { this.resetFn(obj); } return obj; } return this.createFn(); } /** * Releases an object back to the pool * * @param obj - Object to release */ release(obj) { if (this.pool.length < this.maxSize) { this.pool.push(obj); } } /** * Clears the pool */ clear() { this.pool.length = 0; } /** * Gets the current pool size * * @returns Number of objects in pool */ size() { return this.pool.length; } } export { ObjectPool }; //# sourceMappingURL=index42.js.map