Skip to main content

Space Shooter

A

Left

D

Right

Shoot

game.ts
export const createSpaceShooterGame = async (): Promise<Game> => {
const { game, world, renderContext } = createGame('demo-game');

const moveInput = new Axis2dAction('move', null, actionResetTypes.noReset);
const shootInput = new HoldAction('shoot');

const { inputsManager } = registerInputs(world, {
axis2dActions: [moveInput],
holdActions: [shootInput],
});

const keyboardInputSource = new KeyboardInputSource(inputsManager);

keyboardInputSource.axis2dBindings.add(
new KeyboardAxis2dBinding(
moveInput,
keyCodes.w,
keyCodes.s,
keyCodes.d,
keyCodes.a,
),
);

keyboardInputSource.holdBindings.add(
new KeyboardHoldBinding(shootInput, keyCodes.space),
);

const cameraEntity = registerCamera(world);

const playerSprite = await createImageSprite(
'space-shooter/ship_G.png',
renderContext,
cameraEntity,
);

playerSprite.tintColor = new Color(0.6, 1, 0.6);

const playerEntity = world.buildAndAddEntity([
new SpriteComponent(playerSprite),
new PositionComponent(0, 250),
new PlayerComponent(50, -300, 300, -100, 270),
]);

const renderLayerComponent = new RenderLayerComponent();
world.buildAndAddEntity([renderLayerComponent]);

const backgroundMaterial = createBackgroundMaterial(renderContext);

const backgroundSprite = createSprite(
backgroundMaterial,
renderContext,
cameraEntity,
renderContext.canvas.width,
renderContext.canvas.height,
);

const bgEntity = world.buildAndAddEntity([
new SpriteComponent(backgroundSprite),
new PositionComponent(0, 0),
new BackgroundComponent(),
]);

const smallStarSprite = await createImageSprite(
'space-shooter/star_small.png',
renderContext,
cameraEntity,
);

const emitter = new ParticleEmitter(smallStarSprite, renderLayerComponent, {
emitDurationSeconds: 10,
lifetimeSecondsRange: { min: 5, max: 10 },
});

const emitterComponent = new ParticleEmitterComponent(
new Map([['testEmitter', emitter]]),
);

world.buildAndAddEntity([emitterComponent]);

renderLayerComponent.addEntity(backgroundSprite.renderable, bgEntity);
renderLayerComponent.addEntity(playerSprite.renderable, playerEntity);

world.buildAndAddEntity([
new AudioComponent(
{
src: getAudioUrl('background-space-music.mp3'),
loop: true,
volume: 0.5,
},
true,
),
]);

world.addSystem(new RenderSystem(renderContext));
world.addSystem(new MovementSystem(moveInput, world.time));
world.addSystem(new BackgroundSystem(world.time));
world.addSystem(new ParticleEmitterSystem(world));
world.addSystem(new ParticlePositionSystem(world.time));
world.addSystem(new AudioSystem(world));

return game;
};

This demo showcases a complete space shooter game built using the Forge Game Engine. It features player-controlled movement, shooting mechanics, enemy spawning, and collision detection. The game demonstrates how to leverage the engine's capabilities to create an engaging and interactive experience. Players can navigate their spaceship, avoid obstacles, and shoot down enemies while enjoying smooth rendering and responsive controls.