API Docs for: GoblinPhysics
Show:

File: src\classes\ObjectPool.js

  1. /**
  2. * Manages pools for various types of objects, provides methods for creating and freeing pooled objects
  3. *
  4. * @class ObjectPool
  5. * @static
  6. */
  7. Goblin.ObjectPool = {
  8. /**
  9. * key/value map of registered types
  10. *
  11. * @property types
  12. * @private
  13. */
  14. types: {},
  15.  
  16. /**
  17. * key/pool map of object type - to - object pool
  18. *
  19. * @property pools
  20. * @private
  21. */
  22. pools: {},
  23.  
  24. /**
  25. * registers a type of object to be available in pools
  26. *
  27. * @param key {String} key associated with the object to register
  28. * @param constructing_function {Function} function which will return a new object
  29. */
  30. registerType: function( key, constructing_function ) {
  31. this.types[ key ] = constructing_function;
  32. this.pools[ key ] = [];
  33. },
  34.  
  35. /**
  36. * retrieve a free object from the specified pool, or creates a new object if one is not available
  37. *
  38. * @param key {String} key of the object type to retrieve
  39. * @return {Mixed} object of the type asked for, when done release it with `ObjectPool.freeObject`
  40. */
  41. getObject: function( key ) {
  42. var pool = this.pools[ key ];
  43.  
  44. if ( pool.length !== 0 ) {
  45. return pool.pop();
  46. } else {
  47. return this.types[ key ]();
  48. }
  49. },
  50.  
  51. /**
  52. * adds on object to the object pool so it can be reused
  53. *
  54. * @param key {String} type of the object being freed, matching the key given to `registerType`
  55. * @param object {Mixed} object to release into the pool
  56. */
  57. freeObject: function( key, object ) {
  58. if ( object.removeAllListeners != null ) {
  59. object.removeAllListeners();
  60. }
  61. this.pools[ key ].push( object );
  62. }
  63. };
  64.  
  65. // register the objects used in Goblin
  66. Goblin.ObjectPool.registerType( 'ContactDetails', function() { return new Goblin.ContactDetails(); } );
  67. Goblin.ObjectPool.registerType( 'ContactManifold', function() { return new Goblin.ContactManifold(); } );
  68. Goblin.ObjectPool.registerType( 'GJK2SupportPoint', function() { return new Goblin.GjkEpa2.SupportPoint( new Goblin.Vector3(), new Goblin.Vector3(), new Goblin.Vector3() ); } );
  69. Goblin.ObjectPool.registerType( 'ConstraintRow', function() { return new Goblin.ConstraintRow(); } );
  70. Goblin.ObjectPool.registerType( 'ContactConstraint', function() { return new Goblin.ContactConstraint(); } );
  71. Goblin.ObjectPool.registerType( 'FrictionConstraint', function() { return new Goblin.FrictionConstraint(); } );
  72. Goblin.ObjectPool.registerType( 'RayIntersection', function() { return new Goblin.RayIntersection(); } );
  73. Goblin.ObjectPool.registerType( 'RigidBodyProxy', function() { return new Goblin.RigidBodyProxy(); } );