Arena Shooter tutorial

Step 4: Bullets

Bullets are created dynamically by pressing space, they fly (implement MovementDecorator) until they crash to the wall of Arena (implement CrashDecorator). For generating bullets, we use BulletFactory (a design pattern), which needs a single parameter: the gun which launched the bullet (it is the Rocket). The bullet copies the angle from the gun in the moment of launch, and then moves until crash to the wall.

There should be a dedicated object implementing Pool pattern which takes responsibility for bullets behavior in the game and also implementing Mediator pattern to inform other object that they were hit. But since the gun needs the bullet information (it can shoot only one per time) and no other object interact with the bullet, the gun itself can be the pool and mediator is not needed. Back to the game.

CrashDecorator = o => { o.hitBorderH = o.hitBorderV = d => (o.remove(),, o.crashed = 1); }; BulletFactory = gun => { var b = document.createElement("b"); b.innerHTML = "ı"; =; =; =; gun.parentNode.appendChild(b); RenderDecorator(b); MovementDecorator(b); CrashDecorator(b); b.angle = gun.angle; b.speed = 15; return b; }; GunDecorator = (o,F) => { o.bullet = null; Keys.reserved.add(F); o.register( _=> { if(o.bullet?.crashed) o.bullet = null; if(!o.bullet && Keys.down[F]) o.bullet = BulletFactory(o); }) }; GunDecorator(R, 32);