5 minutes
One enemy you can kill is a target, not a game. You need a lot of them. But there's a wrong way to make a lot of them. You could call new for a fresh enemy every time one spawns, then throw it away when it dies. That is how an arcade game stutters. The garbage collector wakes up in the middle of a fight and drops a frame. So the waves come from a pool instead. You build a fixed set of enemies once. Then you hand them out and take them back, over and over.
A pool to spawn from
Lesson 6 left a single robot in the arena. It was a full set of components you could see and shoot. To get many enemies without that stutter, you don't create them one at a time. You build a fixed set once, when the scene loads, and reuse it. That robot's whole component block becomes the template an EntityPool stamps onto each slot. Open src/scenes/TwinStickArena.json and add the pool to the World entity, next to the BulletPool and Bullet from Lesson 4:
size: 16 builds sixteen enemy slots the moment the scene loads. All sixteen wait offstage, and none are live yet. tags: ['enemy'] is the tag set that every reused slot resets back to. components is the set for each enemy. You write it just like a scene entity's components map. The MeshPrimitive sphere is the collision proxy the pool moves around. The Mesh and SkeletonAnimator ride on it as the GLB visual.
Continue reading
Unlock the Full Course
Every lesson, the runnable examples, and the finished build — yours to keep.