npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fge

v1.0.4

Published

Simple functional-oriented game engine

Readme

FGE

FGE is a really simple functional-oriented pseudo game engine which acts like a reducer towards the game state.

Its objective is to bring functional programming into the world of game development.

A complete API documentation is available here.

Overview

FGE provides basic game engine components and tools to update state without mutating it.

A standard FGE usage would be :

type Vector = readonly [x: number, y: number];

type Input = "left" | "right" | "jump";

type State = {
  readonly groundY: number;
  readonly inputQueue: readonly Input[]
  readonly player: {
    readonly onGround: boolean;
    readonly position: Vector;
    readonly velocity: Vector;
  };
  readonly won: boolean;
};
import { Clock, update } from "fge";

const applyGroundCheck = update<State>((state) => {
  const onGround = state.player.position[1] <= state.groundY;

  return { player: { onGround } };
});

const getVelocityFromInput = (input: Input): Vector => {
  switch (input) {
    case "left":
      return [-1, 0];
    case "right":
      return [1, 0];
    case "jump":
      return [0, 1];
    default:
      return [0, 0];
  }
};

const applyInput = update<State>((state) => {
  const [input, ...inputQueue] = state.inputQueue;
  const velocity = input === "jump" && !state.player.onGround
    ? [0, 0]
    : getVelocityFromInput(input);

  return {
    inputQueue,
    player: {
      velocity: [state.player.velocity[0] + velocity[0], state.player.velocity[1] + velocity[1]],
    },
  };
});

const applyPseudoPhysics = update<State, Clock>((state, clock) => {
  const [x, y] = state.player.velocity;

  return {
    player: {
      position: [state.player.position[0] + x, state.player.position[1] + y],
      velocity: [(Math.abs(x) < 1e-8 ? 0 : x) - clock.delta * Math.sign(x), state.player.onGround ? 0 : y - clock.delta],
    },
  };
});

const applyWinCheck = update<State>((state) => ({
  won: state.player.position[0] >= 800,
}));
import { createClock, createVariableTimeStepRunner } from "fge";

let state: State = {
  groundY: 0,
  // Queue populated during a routine or by a platform-dependent API
  inputQueue: [],
  player: {
    onGround: false,
    position: [0, 0],
    velocity: [0, 0],
  },
  won: false,
};

let clock = createClock();

const runner = createVariableTimeStepRunner<State>(0, 1);
const routines = [
  applyGroundCheck,
  applyInput,
  applyPseudoPhysics,
  applyWinCheck,
];

while (!state.won) {
  [state, clock] = await runner(state, routines, clock);
}

Routine

Description

A routine is a pure async-able function taking the current game state and a runner's clock as parameters and computes a new state, it should return a new reference if the state has been modified.

For example :

type State = {
  playerPosition: {
    x: number;
    y: number;
  };
};

const applyPseudoGravity: Routine<State, Clock> = (state, clock) => {
  if (state.playerPosition.y > 0) {
    return {
      ...state,
      position: {
        ...state.playerPosition,
        y: Math.min(state.playerPosition.y - clock.delta, 0),
      },
    };
  }

  return state;
};

let state: State = {
  playerPosition: {
    x: 0,
    y: 0,
  },
};

state = await applyPseudoGravity(state, createClock());

As you can see, we define a routine updating the y component of the player position. If the position should be updated, new references to the objects are created using the spread operator (...object) and we modify only the y position.

Patch routines

In the previous example, we constructed the new state ourselves. However, this could be simplified by using the update helper wrapper and a patch routine :

...

const applyPseudoGravity: PatchRoutine<State, Clock> = (state, clock) => ({
  playerPosition: {
    y: state.playerPosition.y > 0
      ? Math.min(state.playerPosition.y - clock.delta, 0)
      : state.playerPosition.y,
  },
});

...

state = await update(applyPseudoGravity)(state, createClock());

The function now only returns the modified fields and the update wrapper will construct a routine that updates the state with the given values. It works with nested fields and creates a new reference only if the values are not the same as in the original state.

You can also define the patch routine this way :

...

const applyPseudoGravity = update<State, Clock>((state, clock) => ({
  playerPosition: {
    y: state.playerPosition.y > 0
      ? Math.min(state.playerPosition.y - clock.delta, 0)
      : state.playerPosition.y,
  },
}));

...

state = await applyPseudoGravity(state, createClock());

Apply multiple routines

TODO